Added sum of three for GA2 and cleanup
This commit is contained in:
parent
414b0304be
commit
809a1166a7
7 changed files with 38 additions and 0 deletions
0
1st.py
Normal file → Executable file
0
1st.py
Normal file → Executable file
0
ga1_last.py → GA1/ga1_last.py
Normal file → Executable file
0
ga1_last.py → GA1/ga1_last.py
Normal file → Executable file
38
GA2/sum_of_three.py
Executable file
38
GA2/sum_of_three.py
Executable file
|
@ -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]))
|
||||||
|
|
0
heapsort.py
Normal file → Executable file
0
heapsort.py
Normal file → Executable file
0
mergesort.py
Normal file → Executable file
0
mergesort.py
Normal file → Executable file
0
pair_equal.py
Normal file → Executable file
0
pair_equal.py
Normal file → Executable file
0
partition.py
Normal file → Executable file
0
partition.py
Normal file → Executable file
Reference in a new issue