Skip to content

Commit 76927ec

Browse files
author
deepshekhardas
committed
fix: apply PRs #14939, #14938, #14936
1 parent c0db072 commit 76927ec

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

bit_manipulation/binary_count_trailing_zeros.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def binary_count_trailing_zeros(a: int) -> int:
1717
>>> binary_count_trailing_zeros(4294967296)
1818
32
1919
>>> binary_count_trailing_zeros(0)
20-
0
20+
Traceback (most recent call last):
21+
...
22+
ValueError: Input value must be a positive integer
2123
>>> binary_count_trailing_zeros(-10)
2224
Traceback (most recent call last):
2325
...
@@ -31,11 +33,11 @@ def binary_count_trailing_zeros(a: int) -> int:
3133
...
3234
TypeError: '<' not supported between instances of 'str' and 'int'
3335
"""
34-
if a < 0:
35-
raise ValueError("Input value must be a positive integer")
36-
elif isinstance(a, float):
36+
if isinstance(a, float):
3737
raise TypeError("Input value must be a 'int' type")
38-
return 0 if (a == 0) else int(log2(a & -a))
38+
if a < 0 or a == 0:
39+
raise ValueError("Input value must be a positive integer")
40+
return int(log2(a & -a))
3941

4042

4143
if __name__ == "__main__":

ciphers/a1z26.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ def encode(plain: str) -> list[int]:
1313
"""
1414
>>> encode("myname")
1515
[13, 25, 14, 1, 13, 5]
16+
>>> encode("ABCD")
17+
Traceback (most recent call last):
18+
...
19+
ValueError: plain must contain only lowercase letters (a-z)
1620
"""
21+
if not plain.islower() or not plain.isalpha():
22+
raise ValueError("plain must contain only lowercase letters (a-z)")
1723
return [ord(elem) - 96 for elem in plain]
1824

1925

sorts/insertion_sort.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
3131
comparable items inside
3232
:return: the same collection ordered by ascending
3333
34+
Complexity Analysis:
35+
Time Complexity:
36+
- Best Case: O(n) when the collection is already sorted
37+
- Average Case: O(n^2)
38+
- Worst Case: O(n^2) when the collection is sorted in reverse order
39+
40+
Space Complexity:
41+
- O(1) because the algorithm sorts the collection in place and
42+
uses only a constant amount of additional memory
43+
3444
Examples:
3545
>>> insertion_sort([0, 5, 3, 2, 2])
3646
[0, 2, 2, 3, 5]

0 commit comments

Comments
 (0)