Skip to content

Instantly share code, notes, and snippets.

@Gasoid
Last active May 19, 2020 07:37
Show Gist options
  • Save Gasoid/d83262ed66d8d033a0a7ef3319c6f960 to your computer and use it in GitHub Desktop.
Save Gasoid/d83262ed66d8d033a0a7ef3319c6f960 to your computer and use it in GitHub Desktop.
def read_file_by_chunks(filename):
chunk_size = 9+1
with open(filename) as f:
read_bytes = 0
while True:
chunk = f.read(chunk_size)
chunk = chunk.strip()
if not chunk:
break
if '\n' in chunk:
index = chunk.index('\n')
elif ' ' in chunk:
index = chunk.index(' ')
else:
index = len(chunk)
yield int(chunk[:index])
read_bytes += index + 1
f.seek(read_bytes)
def two_sum(filename):
target_lst = []
source_lst = []
target = None
comp = {}
for num in read_file_by_chunks(filename):
if target is None:
target = num
else:
if num in comp:
return 1
comp[target - num] = True
return 0
if __name__ == '__main__':
result = two_sum("./input.txt")
with open("./output.txt", "w+") as f:
f.write(str(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment