Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ciphers/base64_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ def base64_decode(encoded_data: str) -> bytes:

# Check if the encoded string contains non base64 characters
if padding:
assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (
"Invalid base64 character(s) found."
)
assert all(
char in B64_CHARSET for char in encoded_data[:-padding]
), "Invalid base64 character(s) found."
else:
assert all(char in B64_CHARSET for char in encoded_data), (
"Invalid base64 character(s) found."
)
assert all(
char in B64_CHARSET for char in encoded_data
), "Invalid base64 character(s) found."

# Check the padding
assert len(encoded_data) % 4 == 0 and padding < 3, "Incorrect padding"
Expand Down
29 changes: 29 additions & 0 deletions data_structures/arrays/dutch_national_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def dutch_national_flag(arr):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: dutch_national_flag. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: arr

"""
Sorts an array containing only 0s, 1s and 2s

Args:
arr(list): The input array (containing only 0s, 1s and 2s)

Returns:
list: Sorted array
"""

low, mid, high = 0, 0, len(arr) - 1

while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
return arr


if __name__ == "__main__":
arr = [2, 0, 2, 1, 2, 0, 1]
print("Sorted array: ", dutch_national_flag(arr))
6 changes: 3 additions & 3 deletions data_structures/hashing/number_theory/prime_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def is_prime(number: int) -> bool:
"""

# precondition
assert isinstance(number, int) and (number >= 0), (
"'number' must been an int and positive"
)
assert isinstance(number, int) and (
number >= 0
), "'number' must been an int and positive"

if 1 < number < 4:
# 2 and 3 are primes
Expand Down
6 changes: 3 additions & 3 deletions data_structures/heap/min_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def is_empty(self):
return len(self.heap) == 0

def decrease_key(self, node, new_value):
assert self.heap[self.idx_of_element[node]].val > new_value, (
"newValue must be less that current value"
)
assert (
self.heap[self.idx_of_element[node]].val > new_value
), "newValue must be less that current value"
node.val = new_value
self.heap_dict[node.name] = new_value
self.sift_up(self.idx_of_element[node])
Expand Down
12 changes: 6 additions & 6 deletions data_structures/kd_tree/tests/test_kdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
assert kdtree is not None, "Expected a KDNode, got None"

# Check if root has correct dimensions
assert len(kdtree.point) == num_dimensions, (
f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
)
assert (
len(kdtree.point) == num_dimensions
), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"

# Check that the tree is balanced to some extent (simplistic check)
assert isinstance(kdtree, KDNode), (
f"Expected KDNode instance, got {type(kdtree)}"
)
assert isinstance(
kdtree, KDNode
), f"Expected KDNode instance, got {type(kdtree)}"


def test_nearest_neighbour_search():
Expand Down
24 changes: 12 additions & 12 deletions data_structures/suffix_tree/tests/test_suffix_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ def test_search_existing_patterns(self) -> None:
patterns = ["ana", "ban", "na"]
for pattern in patterns:
with self.subTest(pattern=pattern):
assert self.suffix_tree.search(pattern), (
f"Pattern '{pattern}' should be found."
)
assert self.suffix_tree.search(
pattern
), f"Pattern '{pattern}' should be found."

def test_search_non_existing_patterns(self) -> None:
"""Test searching for patterns that do not exist in the suffix tree."""
patterns = ["xyz", "apple", "cat"]
for pattern in patterns:
with self.subTest(pattern=pattern):
assert not self.suffix_tree.search(pattern), (
f"Pattern '{pattern}' should not be found."
)
assert not self.suffix_tree.search(
pattern
), f"Pattern '{pattern}' should not be found."

def test_search_empty_pattern(self) -> None:
"""Test searching for an empty pattern."""
assert self.suffix_tree.search(""), "An empty pattern should be found."

def test_search_full_text(self) -> None:
"""Test searching for the full text."""
assert self.suffix_tree.search(self.text), (
"The full text should be found in the suffix tree."
)
assert self.suffix_tree.search(
self.text
), "The full text should be found in the suffix tree."

def test_search_substrings(self) -> None:
"""Test searching for substrings of the full text."""
substrings = ["ban", "ana", "a", "na"]
for substring in substrings:
with self.subTest(substring=substring):
assert self.suffix_tree.search(substring), (
f"Substring '{substring}' should be found."
)
assert self.suffix_tree.search(
substring
), f"Substring '{substring}' should be found."


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions digital_image_processing/filters/gabor_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def gabor_filter_kernel(
_y = -sin_theta * px + cos_theta * py

# fill kernel
gabor[y, x] = np.exp(-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)) * np.cos(
2 * np.pi * _x / lambd + psi
)
gabor[y, x] = np.exp(
-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)
) * np.cos(2 * np.pi * _x / lambd + psi)

return gabor

Expand Down
6 changes: 3 additions & 3 deletions dynamic_programming/climbing_stairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def climb_stairs(number_of_steps: int) -> int:
...
AssertionError: number_of_steps needs to be positive integer, your input -7
"""
assert isinstance(number_of_steps, int) and number_of_steps > 0, (
f"number_of_steps needs to be positive integer, your input {number_of_steps}"
)
assert (
isinstance(number_of_steps, int) and number_of_steps > 0
), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
if number_of_steps == 1:
return 1
previous, current = 1, 1
Expand Down
6 changes: 3 additions & 3 deletions dynamic_programming/iterating_through_submasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def list_of_submasks(mask: int) -> list[int]:

"""

assert isinstance(mask, int) and mask > 0, (
f"mask needs to be positive integer, your input {mask}"
)
assert (
isinstance(mask, int) and mask > 0
), f"mask needs to be positive integer, your input {mask}"

"""
first submask iterated will be mask itself then operation will be performed
Expand Down
18 changes: 9 additions & 9 deletions financial/time_and_half_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def pay(hours_worked: float, pay_rate: float, hours: float = 40) -> float:
10.0
"""
# Check that all input parameters are float or integer
assert isinstance(hours_worked, (float, int)), (
"Parameter 'hours_worked' must be of type 'int' or 'float'"
)
assert isinstance(pay_rate, (float, int)), (
"Parameter 'pay_rate' must be of type 'int' or 'float'"
)
assert isinstance(hours, (float, int)), (
"Parameter 'hours' must be of type 'int' or 'float'"
)
assert isinstance(
hours_worked, (float, int)
), "Parameter 'hours_worked' must be of type 'int' or 'float'"
assert isinstance(
pay_rate, (float, int)
), "Parameter 'pay_rate' must be of type 'int' or 'float'"
assert isinstance(
hours, (float, int)
), "Parameter 'hours' must be of type 'int' or 'float'"

normal_pay = hours_worked * pay_rate
over_time = max(0, hours_worked - hours)
Expand Down
8 changes: 5 additions & 3 deletions greedy_methods/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
return (
0
if k == 0
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
else (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
)
)


Expand Down
4 changes: 3 additions & 1 deletion machine_learning/frequent_pattern_growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@
ascend_tree(leaf_node.parent, prefix_path)


def find_prefix_path(base_pat: frozenset, tree_node: TreeNode | None) -> dict: # noqa: ARG001
def find_prefix_path(
base_pat: frozenset, tree_node: TreeNode | None

Check failure on line 244 in machine_learning/frequent_pattern_growth.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ARG001)

machine_learning/frequent_pattern_growth.py:244:5: ARG001 Unused function argument: `base_pat`
) -> dict: # noqa: ARG001

Check failure on line 245 in machine_learning/frequent_pattern_growth.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF100)

machine_learning/frequent_pattern_growth.py:245:13: RUF100 Unused `noqa` directive (unused: `ARG001`)
"""
Find the conditional pattern base for a given base pattern.

Expand Down
12 changes: 6 additions & 6 deletions maths/numerical_analysis/integration_by_simpson_approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ def simpson_integration(function, a: float, b: float, precision: int = 4) -> flo
AssertionError: precision should be positive integer your input : -1

"""
assert callable(function), (
f"the function(object) passed should be callable your input : {function}"
)
assert callable(
function
), f"the function(object) passed should be callable your input : {function}"
assert isinstance(a, (float, int)), f"a should be float or integer your input : {a}"
assert isinstance(function(a), (float, int)), (
"the function should return integer or float return type of your function, "
f"{type(a)}"
)
assert isinstance(b, (float, int)), f"b should be float or integer your input : {b}"
assert isinstance(precision, int) and precision > 0, (
f"precision should be positive integer your input : {precision}"
)
assert (
isinstance(precision, int) and precision > 0
), f"precision should be positive integer your input : {precision}"

# just applying the formula of simpson for approximate integration written in
# mentioned article in first comment of this file and above this function
Expand Down
12 changes: 6 additions & 6 deletions maths/prime_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def test_primes(self):
def test_not_primes(self):
with pytest.raises(ValueError):
is_prime(-19)
assert not is_prime(0), (
"Zero doesn't have any positive factors, primes must have exactly two."
)
assert not is_prime(1), (
"One only has 1 positive factor, primes must have exactly two."
)
assert not is_prime(
0
), "Zero doesn't have any positive factors, primes must have exactly two."
assert not is_prime(
1
), "One only has 1 positive factor, primes must have exactly two."
assert not is_prime(2 * 2)
assert not is_prime(2 * 3)
assert not is_prime(3 * 3)
Expand Down
42 changes: 21 additions & 21 deletions maths/primelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def is_prime(number: int) -> bool:
"""

# precondition
assert isinstance(number, int) and (number >= 0), (
"'number' must been an int and positive"
)
assert isinstance(number, int) and (
number >= 0
), "'number' must been an int and positive"

status = True

Expand Down Expand Up @@ -254,9 +254,9 @@ def greatest_prime_factor(number):
"""

# precondition
assert isinstance(number, int) and (number >= 0), (
"'number' must been an int and >= 0"
)
assert isinstance(number, int) and (
number >= 0
), "'number' must been an int and >= 0"

ans = 0

Expand Down Expand Up @@ -296,9 +296,9 @@ def smallest_prime_factor(number):
"""

# precondition
assert isinstance(number, int) and (number >= 0), (
"'number' must been an int and >= 0"
)
assert isinstance(number, int) and (
number >= 0
), "'number' must been an int and >= 0"

ans = 0

Expand Down Expand Up @@ -399,9 +399,9 @@ def goldbach(number):
"""

# precondition
assert isinstance(number, int) and (number > 2) and is_even(number), (
"'number' must been an int, even and > 2"
)
assert (
isinstance(number, int) and (number > 2) and is_even(number)
), "'number' must been an int, even and > 2"

ans = [] # this list will returned

Expand Down Expand Up @@ -525,9 +525,9 @@ def kg_v(number1, number2):
done.append(n)

# precondition
assert isinstance(ans, int) and (ans >= 0), (
"'ans' must been from type int and positive"
)
assert isinstance(ans, int) and (
ans >= 0
), "'ans' must been from type int and positive"

return ans

Expand Down Expand Up @@ -574,9 +574,9 @@ def get_prime(n):
ans += 1

# precondition
assert isinstance(ans, int) and is_prime(ans), (
"'ans' must been a prime number and from type int"
)
assert isinstance(ans, int) and is_prime(
ans
), "'ans' must been a prime number and from type int"

return ans

Expand Down Expand Up @@ -705,9 +705,9 @@ def is_perfect_number(number):
"""

# precondition
assert isinstance(number, int) and (number > 1), (
"'number' must been an int and >= 1"
)
assert isinstance(number, int) and (
number > 1
), "'number' must been an int and >= 1"

divisors = get_divisors(number)

Expand Down
8 changes: 5 additions & 3 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
return Matrix(
[
[
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
(
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
)
for column in range(self.minors().num_columns)
]
for row in range(self.minors().num_rows)
Expand Down
6 changes: 3 additions & 3 deletions neural_network/input_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ def __init__(
self._num_examples = 10000
self.one_hot = one_hot
else:
assert images.shape[0] == labels.shape[0], (
f"images.shape: {images.shape} labels.shape: {labels.shape}"
)
assert (
images.shape[0] == labels.shape[0]
), f"images.shape: {images.shape} labels.shape: {labels.shape}"
self._num_examples = images.shape[0]

# Convert shape from [num examples, rows, columns, depth]
Expand Down
Loading
Loading