Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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: 7 additions & 5 deletions bit_manipulation/binary_count_trailing_zeros.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def binary_count_trailing_zeros(a: int) -> int:
>>> binary_count_trailing_zeros(4294967296)
32
>>> binary_count_trailing_zeros(0)
0
Traceback (most recent call last):
...
ValueError: Input value must be a positive integer
>>> binary_count_trailing_zeros(-10)
Traceback (most recent call last):
...
Expand All @@ -31,11 +33,11 @@ def binary_count_trailing_zeros(a: int) -> int:
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
if a < 0:
raise ValueError("Input value must be a positive integer")
elif isinstance(a, float):
if isinstance(a, float):
raise TypeError("Input value must be a 'int' type")
return 0 if (a == 0) else int(log2(a & -a))
if a < 0 or a == 0:
raise ValueError("Input value must be a positive integer")
return int(log2(a & -a))


if __name__ == "__main__":
Expand Down
6 changes: 6 additions & 0 deletions ciphers/a1z26.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ def encode(plain: str) -> list[int]:
"""
>>> encode("myname")
[13, 25, 14, 1, 13, 5]
>>> encode("ABCD")
Traceback (most recent call last):
...
ValueError: plain must contain only lowercase letters (a-z)
"""
if not plain.islower() or not plain.isalpha():
raise ValueError("plain must contain only lowercase letters (a-z)")
return [ord(elem) - 96 for elem in plain]


Expand Down
10 changes: 10 additions & 0 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
comparable items inside
:return: the same collection ordered by ascending

Complexity Analysis:
Time Complexity:
- Best Case: O(n) when the collection is already sorted
- Average Case: O(n^2)
- Worst Case: O(n^2) when the collection is sorted in reverse order

Space Complexity:
- O(1) because the algorithm sorts the collection in place and
uses only a constant amount of additional memory

Examples:
>>> insertion_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
Expand Down