diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index f401c4ab9266..751f36720fa2 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -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): ... @@ -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__": diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py index a1377ea6d397..102cef4e424e 100644 --- a/ciphers/a1z26.py +++ b/ciphers/a1z26.py @@ -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] diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 2e39be255df7..24f53b654439 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -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]