Finish assignments for course 2

This commit is contained in:
2020-12-12 10:53:27 -05:00
parent 99ef6d4d1c
commit 77828d0eb9
3 changed files with 54 additions and 4 deletions

25
misc/two_sums.py Normal file
View File

@@ -0,0 +1,25 @@
def two_sum(t, xs_list, xs_set):
for x in xs:
y = t - x
if x != y and y in xs_set:
return True
return False
if __name__ == "__main__":
with open("../data/c2a4.txt") as f:
xs = list(map(int, f.readlines()))
xs_min = [x for x in xs if x < 0]
xs_max_set = set([x for x in xs if x > 0])
limit = 10000
two_sums = 0
for t in range(-limit, limit + 1):
if t % 10 == 0:
print(t)
if two_sum(t, xs_min, xs_max_set):
two_sums += 1
print(two_sums)