Added sum of three for GA2 and cleanup
This commit is contained in:
parent
414b0304be
commit
809a1166a7
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def search_two(A, sum2, i_skip):
|
||||||
|
i = 0
|
||||||
|
j = len(A) - 1
|
||||||
|
|
||||||
|
while i < j:
|
||||||
|
if i == i_skip:
|
||||||
|
i = i + 1
|
||||||
|
elif j == i_skip:
|
||||||
|
j = j - 1
|
||||||
|
else:
|
||||||
|
cs = A[i] + A[j]
|
||||||
|
if cs == sum2:
|
||||||
|
return True
|
||||||
|
elif cs > sum2:
|
||||||
|
j = j - 1
|
||||||
|
else:
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def sum_of_three(A, sum3):
|
||||||
|
A.sort() # assume using mergesort for worst case of O(n*log(n))
|
||||||
|
l = len(A)
|
||||||
|
|
||||||
|
for i in range(l):
|
||||||
|
if search_two(A, sum3 - A[i], i):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
args = [int(x) for x in sys.argv[1:]]
|
||||||
|
print(sum_of_three(args[1:], args[0]))
|
||||||
|
|
Reference in New Issue