Skip to content

Commit a37511d

Browse files
Normalize string quoting and fix formatting inconsistencies.
Switched all single quotes to double quotes for consistency across the codebase. Added or removed blank lines where appropriate for better readability. Reformatted multi-line strings, list definitions, and function arguments to align with PEP 8 standards.
1 parent 12b3307 commit a37511d

18 files changed

+121
-71
lines changed

Binary Search/On Solution Space/koko_eating_bananas.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import unittest
55
from typing import List
66

7+
78
def min_eating_speed(piles: List[int], hours: int) -> int:
89
piles.sort()
910

10-
left = 1
11+
left = 1
1112
right = max(piles)
1213

1314
result = right
@@ -27,6 +28,7 @@ def min_eating_speed(piles: List[int], hours: int) -> int:
2728

2829
return result
2930

31+
3032
class Test(unittest.TestCase):
3133
def test_min_eating_speed(self):
3234
self.assertEqual(min_eating_speed([1, 4, 3, 2], 9), 2)

Binary Search/Rotated Array/find_minimum_in_rotated_sorted_array.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import unittest
44
from typing import List
55

6+
67
def find_min(nums: List[int]) -> int:
78
n = len(nums)
89

910
left = 0
1011
right = n - 1
11-
min_element = float('inf')
12+
min_element = float("inf")
1213

1314
while left < right:
1415
pivot = (left + right) // 2
@@ -21,6 +22,7 @@ def find_min(nums: List[int]) -> int:
2122

2223
return min(min_element, nums[left])
2324

25+
2426
class TestFindMin(unittest.TestCase):
2527
def test_sorted_array(self):
2628
# Case: Sorted array without rotation

Binary Search/Rotated Array/search_in_rotated_sorted_array.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def search(nums: List[int], target: int) -> int:
78
n = len(nums)
89

@@ -28,6 +29,7 @@ def search(nums: List[int], target: int) -> int:
2829

2930
return -1
3031

32+
3133
class TestSearchRotatedSortedArray(unittest.TestCase):
3234
def test_target_in_rotated_array(self):
3335
nums = [4, 5, 6, 7, 0, 1, 2]

Binary Search/Rotated Array/search_in_rotated_sorted_array_II.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def find_min(nums: List[int]) -> int:
78
left, right = 0, len(nums) - 1
89

@@ -23,6 +24,7 @@ def find_min(nums: List[int]) -> int:
2324

2425
return nums[left]
2526

27+
2628
class TestFindMin(unittest.TestCase):
2729
def test_example_cases(self):
2830
# Example cases given in the prompt

Binary Search/Search In Array/binary_search.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# # If target is not present, return -1
2121
# return -1
2222

23+
2324
def binary_search(nums: List[int], target: int) -> int:
2425
n = len(nums)
2526

@@ -39,6 +40,7 @@ def binary_search(nums: List[int], target: int) -> int:
3940

4041
return -1
4142

43+
4244
class Test(unittest.TestCase):
4345
def test_binary_search(self):
4446
self.assertEqual(binary_search([-1, 0, 2, 4, 6, 8], 4), 3)

Binary Search/Search In Array/count_negative_numbers_in_a_sorted_matrix.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def count_negatives(grid: List[List[int]]) -> int:
78
def count_negatives_in_row(row: List[int]) -> int:
89
n = len(row)
@@ -27,8 +28,8 @@ def count_negatives_in_row(row: List[int]) -> int:
2728

2829
return result
2930

30-
class TestCountNegatives(unittest.TestCase):
3131

32+
class TestCountNegatives(unittest.TestCase):
3233
def test_empty_grid(self):
3334
# Test case with an empty grid
3435
grid = []
@@ -61,11 +62,13 @@ def test_multiple_rows_with_negatives(self):
6162

6263
def test_large_matrix(self):
6364
# Test case with a large matrix for performance validation
64-
grid = [[5, 4, 3, 2, -1],
65-
[4, 3, 2, -1, -2],
66-
[3, 2, -1, -2, -3],
67-
[2, -1, -2, -3, -4],
68-
[-1, -2, -3, -4, -5]]
65+
grid = [
66+
[5, 4, 3, 2, -1],
67+
[4, 3, 2, -1, -2],
68+
[3, 2, -1, -2, -3],
69+
[2, -1, -2, -3, -4],
70+
[-1, -2, -3, -4, -5],
71+
]
6972
self.assertEqual(count_negatives(grid), 15)
7073

7174
def test_single_column(self):

Binary Search/Search In Array/find_right_interval.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List, Tuple
55

6+
67
def find_right_interval(intervals: List[List[int]]) -> List[int]:
78
if not intervals:
89
return []
@@ -46,6 +47,7 @@ def search_left(start_intervals: List[Tuple[int, int]], end: int) -> int:
4647

4748
return result
4849

50+
4951
class TestFindRightInterval(unittest.TestCase):
5052
def test_no_intervals(self):
5153
self.assertEqual(find_right_interval([]), [])
@@ -57,22 +59,15 @@ def test_single_interval_is_its_own_right(self):
5759
self.assertEqual(find_right_interval([[1, 1]]), [0])
5860

5961
def test_multiple_intervals_no_overlap(self):
60-
self.assertEqual(
61-
find_right_interval([[1, 2], [3, 4], [5, 6]]), [1, 2, -1]
62-
)
62+
self.assertEqual(find_right_interval([[1, 2], [3, 4], [5, 6]]), [1, 2, -1])
6363

6464
def test_multiple_intervals_with_overlap(self):
65-
self.assertEqual(
66-
find_right_interval([[1, 4], [2, 3], [3, 4]]), [-1, 2, -1]
67-
)
65+
self.assertEqual(find_right_interval([[1, 4], [2, 3], [3, 4]]), [-1, 2, -1])
6866

6967
def test_multiple_intervals_with_same_start(self):
70-
self.assertEqual(
71-
find_right_interval([[1, 2], [1, 3], [3, 4]]), [2, 2, -1]
72-
)
68+
self.assertEqual(find_right_interval([[1, 2], [1, 3], [3, 4]]), [2, 2, -1])
7369

7470
def test_large_intervals(self):
7571
self.assertEqual(
76-
find_right_interval([[1, 1000], [500, 1500], [1001, 2000]]),
77-
[2, -1, -1]
72+
find_right_interval([[1, 1000], [500, 1500], [1001, 2000]]), [2, -1, -1]
7873
)

Binary Search/Search In Array/find_smallest_letter_greater_than_target.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def next_letter_greater(letters: List[str], target: str) -> str:
78
if not letters:
89
raise ValueError("The input list 'letters' cannot be empty.")
@@ -22,35 +23,36 @@ def next_letter_greater(letters: List[str], target: str) -> str:
2223

2324
return letters[left % n]
2425

26+
2527
class TestNextLetterGreater(unittest.TestCase):
2628
def test_single_element(self):
27-
self.assertEqual(next_letter_greater(['c'], 'a'), 'c')
28-
self.assertEqual(next_letter_greater(['c'], 'c'), 'c')
29+
self.assertEqual(next_letter_greater(["c"], "a"), "c")
30+
self.assertEqual(next_letter_greater(["c"], "c"), "c")
2931

3032
def test_basic_cases(self):
31-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'a'), 'c')
32-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'c'), 'f')
33-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'd'), 'f')
34-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'g'), 'j')
35-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'j'), 'c')
33+
self.assertEqual(next_letter_greater(["c", "f", "j"], "a"), "c")
34+
self.assertEqual(next_letter_greater(["c", "f", "j"], "c"), "f")
35+
self.assertEqual(next_letter_greater(["c", "f", "j"], "d"), "f")
36+
self.assertEqual(next_letter_greater(["c", "f", "j"], "g"), "j")
37+
self.assertEqual(next_letter_greater(["c", "f", "j"], "j"), "c")
3638

3739
def test_wrap_around(self):
38-
self.assertEqual(next_letter_greater(['c', 'f', 'j'], 'k'), 'c')
40+
self.assertEqual(next_letter_greater(["c", "f", "j"], "k"), "c")
3941

4042
def test_duplicates(self):
41-
self.assertEqual(next_letter_greater(['c', 'c', 'f', 'f', 'j', 'j'], 'a'), 'c')
42-
self.assertEqual(next_letter_greater(['c', 'c', 'f', 'f', 'j', 'j'], 'c'), 'f')
43-
self.assertEqual(next_letter_greater(['c', 'c', 'f', 'f', 'j', 'j'], 'f'), 'j')
44-
self.assertEqual(next_letter_greater(['c', 'c', 'f', 'f', 'j', 'j'], 'j'), 'c')
43+
self.assertEqual(next_letter_greater(["c", "c", "f", "f", "j", "j"], "a"), "c")
44+
self.assertEqual(next_letter_greater(["c", "c", "f", "f", "j", "j"], "c"), "f")
45+
self.assertEqual(next_letter_greater(["c", "c", "f", "f", "j", "j"], "f"), "j")
46+
self.assertEqual(next_letter_greater(["c", "c", "f", "f", "j", "j"], "j"), "c")
4547

4648
def test_large_list(self):
47-
letters = ['a', 'b', 'c', 'd', 'e']
48-
self.assertEqual(next_letter_greater(letters, 'c'), 'd')
49-
self.assertEqual(next_letter_greater(letters, 'e'), 'a')
49+
letters = ["a", "b", "c", "d", "e"]
50+
self.assertEqual(next_letter_greater(letters, "c"), "d")
51+
self.assertEqual(next_letter_greater(letters, "e"), "a")
5052

5153
def test_edge_cases(self):
5254
with self.assertRaises(ValueError):
53-
next_letter_greater([], 'a')
55+
next_letter_greater([], "a")
5456

5557
def test_nonexistent_target(self):
56-
self.assertEqual(next_letter_greater(['a', 'b', 'd', 'e'], 'c'), 'd')
58+
self.assertEqual(next_letter_greater(["a", "b", "d", "e"], "c"), "d")

Binary Search/Search In Array/first_position_and_last_position_of_element_in_sorted_array.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def search_range(nums: List[int], target: int) -> List[int]:
78
n = len(nums)
89

@@ -39,14 +40,15 @@ def find_right_index(nums: List[int], target: int) -> int:
3940

4041
# Make sure that the target is in range
4142
if (
42-
left_index <= right_index < n and
43-
nums[left_index] == target and
44-
nums[right_index] == target
43+
left_index <= right_index < n
44+
and nums[left_index] == target
45+
and nums[right_index] == target
4546
):
4647
return [left_index, right_index]
4748

4849
return [-1, -1]
4950

51+
5052
class TestSearchRange(unittest.TestCase):
5153
def test_multiple_occurrences(self):
5254
nums = [5, 7, 7, 8, 8, 10]

Binary Search/Search In Array/search_insert_position.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44
from typing import List
55

6+
67
def search_left(nums: List[int], target: int) -> int:
78
n = len(nums)
89

@@ -22,6 +23,7 @@ def search_left(nums: List[int], target: int) -> int:
2223

2324
return left
2425

26+
2527
class TestSearchLeft(unittest.TestCase):
2628
def test_target_found(self):
2729
self.assertEqual(search_left([1, 3, 5, 6], 5), 2)

Binary Search/Search In Array/snapshot_array.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import unittest
44
from collections import defaultdict
55

6-
class SnapshotArray:
76

7+
class SnapshotArray:
88
def __init__(self, _: int):
99
self.snap_id = 0
10-
self.history = defaultdict(lambda: [(0, 0)]) # Default history with initial (snap_id, value)
10+
self.history = defaultdict(
11+
lambda: [(0, 0)]
12+
) # Default history with initial (snap_id, value)
1113

1214
def set(self, index: int, value: int) -> None:
1315
# Fetch the last recorded snap_id for the given index
@@ -40,13 +42,18 @@ def get(self, index: int, snap_id: int) -> int:
4042
else:
4143
right = mid - 1
4244

43-
return snaps[right][1] # The value corresponding to the largest snap_id ≤ the requested snap_id
45+
return snaps[right][
46+
1
47+
] # The value corresponding to the largest snap_id ≤ the requested snap_id
48+
4449

4550
class TestSnapshotArray(unittest.TestCase):
4651
def test_initialization(self):
4752
# Test that the SnapshotArray initializes without errors and the default history is correct.
4853
obj = SnapshotArray(5)
49-
for i in range(5): # Check that every index initializes with a default value of 0
54+
for i in range(
55+
5
56+
): # Check that every index initializes with a default value of 0
5057
self.assertEqual(obj.get(i, 0), 0)
5158

5259
def test_set_and_get_before_snap(self):
@@ -128,4 +135,6 @@ def test_large_snapshots(self):
128135
for i in range(1000):
129136
obj.set(0, i)
130137
snap_id = obj.snap()
131-
self.assertEqual(obj.get(0, snap_id), i) # Value should match at each snapshot
138+
self.assertEqual(
139+
obj.get(0, snap_id), i
140+
) # Value should match at each snapshot

Binary Search/Search In Array/time_based_key_value_store.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class TimeMap:
9-
109
def __init__(self):
1110
self.store = defaultdict(list)
1211

@@ -87,7 +86,9 @@ def test_multiple_keys(self):
8786

8887
self.assertEqual(self.time_map.get("foo", 1), "bar")
8988
self.assertEqual(self.time_map.get("baz", 2), "qux")
90-
self.assertEqual(self.time_map.get("baz", 1), "") # baz doesn't have a value at or before 1
89+
self.assertEqual(
90+
self.time_map.get("baz", 1), ""
91+
) # baz doesn't have a value at or before 1
9192

9293
def test_non_chronological_timestamps(self):
9394
"""Test if the function works regardless of insertion order."""
@@ -104,7 +105,15 @@ def test_early_and_large_timestamps(self):
104105
self.time_map.set("foo", "late", 100000)
105106

106107
self.assertEqual(self.time_map.get("foo", 0), "") # no value at 0
107-
self.assertEqual(self.time_map.get("foo", 1), "early") # exact match at earliest
108-
self.assertEqual(self.time_map.get("foo", 99999), "early") # closest before 100000
109-
self.assertEqual(self.time_map.get("foo", 100000), "late") # exact match at 100000
110-
self.assertEqual(self.time_map.get("foo", 100001), "late") # closest after 100000
108+
self.assertEqual(
109+
self.time_map.get("foo", 1), "early"
110+
) # exact match at earliest
111+
self.assertEqual(
112+
self.time_map.get("foo", 99999), "early"
113+
) # closest before 100000
114+
self.assertEqual(
115+
self.time_map.get("foo", 100000), "late"
116+
) # exact match at 100000
117+
self.assertEqual(
118+
self.time_map.get("foo", 100001), "late"
119+
) # closest after 100000

0 commit comments

Comments
 (0)