-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem01.py
More file actions
166 lines (118 loc) · 4.03 KB
/
Problem01.py
File metadata and controls
166 lines (118 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?'''
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(message)s")
# --------------------------------------------------
# 1. ONE-PASS HASH SET (Optimal)
# --------------------------------------------------
def has_pair_one_pass(nums, k):
seen = set()
for num in nums:
complement = k - num
logging.info(f"Checking num={num}, complement={complement}, seen={seen}")
if complement in seen:
logging.info(f"Pair found: ({num}, {complement})")
return True
seen.add(num)
return False
# --------------------------------------------------
# 2. TWO-PASS HASH MAP (Handles duplicates explicitly)
# --------------------------------------------------
def has_pair_two_pass(nums, k):
counts = {}
# First pass: count occurrences
for num in nums:
counts[num] = counts.get(num, 0) + 1
logging.info(f"Counts map: {counts}")
# Second pass: check pairs
for num in nums:
complement = k - num
if complement in counts:
# Case: same number twice (e.g., 5 + 5 = 10)
if complement == num:
if counts[num] > 1:
logging.info(f"Pair found (duplicate case): ({num}, {num})")
return True
else:
logging.info(f"Pair found: ({num}, {complement})")
return True
return False
# --------------------------------------------------
# 3. TWO POINTERS (SORTING)
# --------------------------------------------------
def has_pair_two_pointers(nums, k):
nums = sorted(nums)
left, right = 0, len(nums) - 1
logging.info(f"Sorted nums: {nums}")
while left < right:
s = nums[left] + nums[right]
logging.info(f"Checking: {nums[left]} + {nums[right]} = {s}")
if s == k:
logging.info(f"Pair found: ({nums[left]}, {nums[right]})")
return True
elif s < k:
left += 1
else:
right -= 1
return False
# --------------------------------------------------
# 4. BRUTE FORCE (All pairs)
# --------------------------------------------------
def has_pair_bruteforce(nums, k):
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
logging.info(f"Checking pair: ({nums[i]}, {nums[j]})")
if nums[i] + nums[j] == k:
logging.info(f"Pair found: ({nums[i]}, {nums[j]})")
return True
return False
# --------------------------------------------------
# EDGE CASE TESTS
# --------------------------------------------------
def run_tests():
test_cases = [
# Basic case
([10, 15, 3, 7], 17),
# No pair
([1, 2, 3, 4], 10),
# Duplicate numbers (valid case)
([5, 5], 10),
# Duplicate numbers (invalid case)
([5], 10),
# Negative numbers
([-1, -2, -3, -4, 5], 1),
# Mixed positive/negative
([3, -1, 9, 8, 2], 7),
# Zero handling
([0, 4, 3, 0], 0),
# Empty list
([], 5),
# Single element
([10], 10),
# Large numbers
([1000000, 500000, -500000], 500000),
]
methods = [
("One Pass", has_pair_one_pass),
("Two Pass", has_pair_two_pass),
("Two Pointers", has_pair_two_pointers),
("Brute Force", has_pair_bruteforce),
]
for nums, k in test_cases:
print("\n" + "=" * 50)
print(f"Input: nums={nums}, k={k}")
for name, method in methods:
logging.info(f"Running {name}")
result = method(nums, k)
print(f"{name}: {result}")
# --------------------------------------------------
# MAIN
# --------------------------------------------------
def main():
run_tests()
if __name__ == "__main__":
main()