-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairs_given_sum.py
More file actions
47 lines (37 loc) · 1.13 KB
/
pairs_given_sum.py
File metadata and controls
47 lines (37 loc) · 1.13 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
# gets rid of any integers larger than "sum" we are looking for
def binary_search(num_list):
low = 0
high = len(num_list) - 1
while low <= high:
mid = low + ((high-low)//2)
if sum_num < num_list[mid]:
high = mid - 1
else:
low = mid + 1
if num_list[mid] < sum_num:
return num_list[:mid]
return None
# gets rid of large numbers if the sum of the smallest int > "sum" we are looking for
def shorten_numlist(nums):
while (nums[0] + nums[-1]) > sum_num:
nums.pop()
return nums
def find_sum_pairs(nums):
if len(nums) <= 1:
return None
nums = sorted(nums)
binary_search(nums)
shorten_numlist(nums)
nums_copy = nums[:]
counter_dict = {}
i = 0
for num in nums:
num_to_find = sum_num - (nums_copy[-1])
if num > num_to_find:
nums_copy.pop()
if num in nums_copy and nums_copy[-1] + num == 21:
counter_dict[(nums_copy[-1], num)] = 1
return len(counter_dict)
my_nums = [20, 1, 19, 2, 18, 3, 21, 0, 25, 3, 8]
sum_num = 21
print(find_sum_pairs(my_nums))