Fixes on the code style based on the Code review and flake8.

And Refactored quick_sort method to improve code readability
This commit is contained in:
Wadie Botros 2024-11-20 12:15:50 +01:00
parent f3f32ae3ca
commit 851c740d21
5 changed files with 4523 additions and 32 deletions

View File

@ -5,8 +5,10 @@ from audio_filters.iir_filter import IIRFilter
"""
Create 2nd-order IIR filters with Butterworth design.
Code based on https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Alternatively you can use scipy.signal.butter, which should yield the same results.
Code based on
https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html
Alternatively you can use scipy.signal.butter,
which should yield the same results.
"""
@ -20,8 +22,8 @@ def make_lowpass(
>>> filter = make_lowpass(1000, 48000)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427, 0.004277569313094809,
0.008555138626189618, 0.004277569313094809]
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427,
0.004277569313094809,0.008555138626189618, 0.004277569313094809]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -50,8 +52,8 @@ def make_highpass(
>>> filter = make_highpass(1000, 48000)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427, 0.9957224306869052,
-1.9914448613738105, 0.9957224306869052]
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427,
0.9957224306869052,-1.9914448613738105, 0.9957224306869052]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -80,8 +82,8 @@ def make_bandpass(
>>> filter = make_bandpass(1000, 48000)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427, 0.06526309611002579,
0, -0.06526309611002579]
[1.0922959556412573, -1.9828897227476208,
0.9077040443587427, 0.06526309611002579,0, -0.06526309611002579]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -111,8 +113,8 @@ def make_allpass(
>>> filter = make_allpass(1000, 48000)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427, 0.9077040443587427,
-1.9828897227476208, 1.0922959556412573]
[1.0922959556412573, -1.9828897227476208, 0.9077040443587427,
0.9077040443587427, -1.9828897227476208, 1.0922959556412573]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -139,8 +141,8 @@ def make_peak(
>>> filter = make_peak(1000, 48000, 6)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[1.0653405327119334, -1.9828897227476208, 0.9346594672880666, 1.1303715025601122,
-1.9828897227476208, 0.8696284974398878]
[1.0653405327119334, -1.9828897227476208, 0.9346594672880666,
1.1303715025601122,-1.9828897227476208, 0.8696284974398878]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -171,8 +173,8 @@ def make_lowshelf(
>>> filter = make_lowshelf(1000, 48000, 6)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[3.0409336710888786, -5.608870992220748, 2.602157875636628, 3.139954022810743,
-5.591841778072785, 2.5201667380627257]
[3.0409336710888786, -5.608870992220748, 2.602157875636628,
3.139954022810743, -5.591841778072785, 2.5201667380627257]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)
@ -208,8 +210,8 @@ def make_highshelf(
>>> filter = make_highshelf(1000, 48000, 6)
>>> filter.a_coeffs + filter.b_coeffs # doctest: +NORMALIZE_WHITESPACE
[2.2229172136088806, -3.9587208137297303, 1.7841414181566304, 4.295432981120543,
-7.922740859457287, 3.6756456963725253]
[2.2229172136088806, -3.9587208137297303, 1.7841414181566304,
4.295432981120543, -7.922740859457287, 3.6756456963725253]
"""
w0 = tau * frequency / samplerate
_sin = sin(w0)

4471
flake8_output.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
def multiplication_table(number: int, number_of_terms: int) -> str:
"""
Prints the multiplication table of a given number till the given number of terms
Prints the multiplication table of a given number
till the given number of terms
>>> print(multiplication_table(3, 5))
3 * 1 = 3

View File

@ -1,4 +1,5 @@
"""Uses Pythagoras theorem to calculate the distance between two points in space."""
"""Uses Pythagoras theorem
to calculate the distance between two points in space."""
import math
@ -17,10 +18,12 @@ def distance(a: Point, b: Point) -> float:
"""
>>> point1 = Point(2, -1, 7)
>>> point2 = Point(1, -3, 5)
>>> print(f"Distance from {point1} to {point2} is {distance(point1, point2)}")
>>> print(f"Distance from {point1} to {point2}
is {distance(point1, point2)}")
Distance from Point(2, -1, 7) to Point(1, -3, 5) is 3.0
"""
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2))
return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2
+ (b.z - a.z) ** 2))
if __name__ == "__main__":

View File

@ -27,26 +27,40 @@ def quick_sort(collection: list) -> list:
>>> quick_sort([-2, 5, 0, -45])
[-45, -2, 0, 5]
"""
# Base case: if the collection has 0 or 1 elements, it is already sorted
if len(collection) < 2:
return collection
# Randomly select a pivot index and remove the pivot element from the collection
pivot_index = randrange(len(collection))
pivot = collection.pop(pivot_index)
# Partition the remaining elements into two groups: lesser or equal, and greater
lesser = [item for item in collection if item <= pivot]
greater = [item for item in collection if item > pivot]
lesser_partition, greater_partition = partition(collection, pivot)
# Recursively sort the lesser and greater groups, and combine with the pivot
return [*quick_sort(lesser), pivot, *quick_sort(greater)]
return [*quick_sort(lesser_partition), pivot,
*quick_sort(greater_partition)]
def partition(collection: list, pivot) -> tuple[list, list]:
"""Partition the collection into two lists: elements less
than or equal to the pivot, and elements greater than the pivot.
:param collection: a mutable collection of comparable items
:param pivot: the pivot element
:return: two lists representing the lesser and greater partitions
"""
lesser_partition = [item for item in collection if item <= pivot]
greater_partition = [item for item in collection if item > pivot]
return lesser_partition, greater_partition
def get_user_input() -> list:
"""Get user input and convert it into a list of integers.
:return: a list of integers provided by the user
"""
user_input = input("Enter numbers separated by a comma:\n").strip()
return [int(item) for item in user_input.split(",")]
if __name__ == "__main__":
# Get user input and convert it into a list of integers
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
# Print the result of sorting the user-provided list
unsorted = get_user_input()
print(quick_sort(unsorted))