mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Update find_pair_nums.py
This commit is contained in:
parent
e25dd1f601
commit
79872f08eb
@ -2,22 +2,19 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
# given a collection of numbers, find the pair
|
|
||||||
# of numbers that sum to a given number
|
|
||||||
|
|
||||||
def bs(array, desired_num):
|
def bs(array, item):
|
||||||
|
|
||||||
start = 0
|
start, end = 0, len(array)
|
||||||
end = len(array)
|
|
||||||
mid = (end - start) // 2
|
mid = (end - start) // 2
|
||||||
|
|
||||||
while len(array) > 0:
|
while len(array) > 0:
|
||||||
if array[mid] == desired_num:
|
if array[mid] == item:
|
||||||
return True
|
return True
|
||||||
elif array[mid] > desired_num:
|
elif array[mid] > item:
|
||||||
return bs(array[mid+1:], desired_num)
|
return bs(array[mid + 1:], item)
|
||||||
else:
|
else:
|
||||||
return bs(array[:mid], desired_num)
|
return bs(array[:mid], item)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -38,15 +35,17 @@ def find_pairs_max_sum(array, desired_sum):
|
|||||||
i, j = 0, len(array) - 1
|
i, j = 0, len(array) - 1
|
||||||
|
|
||||||
while i < j:
|
while i < j:
|
||||||
if array[i] + array[j] == desired_sum:
|
this_sum = array[i] + array[j]
|
||||||
|
if this_sum == desired_sum:
|
||||||
return array[i], array[j]
|
return array[i], array[j]
|
||||||
elif array[i] + array[j] > desired_sum:
|
elif this_sum > desired_sum:
|
||||||
j = j - 1
|
j -= 1
|
||||||
elif array[i] + array[j] < desired_sum:
|
else:
|
||||||
i = i + 1
|
i += 1
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def find_pairs_not_sorted(array, desired_sum):
|
def find_pairs_not_sorted(array, desired_sum):
|
||||||
|
|
||||||
lookup = {}
|
lookup = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user