-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4sum.py
More file actions
36 lines (29 loc) · 991 Bytes
/
Copy path4sum.py
File metadata and controls
36 lines (29 loc) · 991 Bytes
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
# this is 3 sum with another loop
# w, x, y, z = target
def search_quads(arr, target):
result = []
arr.sort()
for i in range(len(arr) - 1):
# avoid duplicate values
if i > 0 and arr[i] == arr[i - 1]:
continue
for j in range(i + 1, len(arr) - 1):
# avoid duplicate values
if j > i + i and arr[j] == arr[j - 1]:
continue
left = j + 1
right = len(arr) - 1
while left < right:
w, x, y, z = arr[i], arr[j], arr[left], arr[right]
sum = w + x + y + z
print(f'{w} + {x} + {y} + {z} = {sum}')
if sum == target:
result.append([w, x, y, z])
left += 1
right -= 1
elif sum > target:
right -= 1
else: # sum < target
left += 1
return result
print(search_quads([0,0,0,0], 0))