Finish assignments for course 2
This commit is contained in:
25
misc/two_sums.py
Normal file
25
misc/two_sums.py
Normal 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)
|
||||
Reference in New Issue
Block a user