Finding some of two with/without doubles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 | |
5 100 200 30 56 456 4 37 44 38 5 9999 2 44 89 100 5435 324 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import getsizeof | |
def two_sum(string): | |
seen = set() | |
target, nums = string.split('\n') | |
target = int(target) | |
check_doubles = False if target % 2 != 0 else True | |
for num in (int(n) for n in nums.split()): | |
if target > num and num not in seen: | |
seen.add(num) | |
if num in seen and check_doubles: | |
if target == num * 2: | |
print(f"{num, num} = {target}") | |
return 1 | |
if len(seen) >= 2: | |
if target - num in seen: | |
print(f"{target - num, num} = {target}") | |
return 1 | |
return 0 | |
if __name__ == '__main__': | |
INPUT: str = """67 | |
5 100 200 30 56 456 4 37 44 38 5 9999 2 44 89 100 5435 324""" | |
t, num_str = INPUT.split('\n') | |
nums_gen = (int(n) for n in num_str.split()) | |
nums_list = list(nums_gen) | |
print(f"Size of num string: {getsizeof(num_str)}") | |
print(f"Size of generator with nums: {getsizeof(nums_gen)}") | |
print(f"Size of list with nums: {getsizeof(nums_list)}") | |
print(f"Size of tuple with nums: {getsizeof(tuple(nums_list))}") | |
with open('input.txt', 'r') as input_file: | |
result = two_sum(input_file.read()) | |
if result: | |
print('Yay!') | |
with open('output.txt', 'w') as output_file: | |
output_file.write(str(result)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TwoSum: | |
CHUNK_SIZE = 1024 | |
target = 0 | |
seen = set() | |
def read_and_write(self, input_f, output_f): | |
with open(input_f, 'r') as input_file: | |
self.target = int(input_file.readline()) | |
result = 0 | |
while True: | |
chunk = input_file.read(self.CHUNK_SIZE) | |
if chunk: | |
result = self.compute(chunk) | |
if result: | |
break | |
else: | |
print('EOF') # Result will be 0 | |
break | |
with open(output_f, 'w') as output_file: | |
output_file.write(str(result)) | |
def compute(self, nums): | |
check_doubles = False if self.target % 2 != 0 else True | |
for num in (int(n) for n in nums.split()): | |
if self.target > num and num not in self.seen: | |
self.seen.add(num) | |
if num in self.seen and check_doubles: | |
if self.target == num * 2: | |
print(f"{num, num} = {self.target}") | |
return 1 | |
if len(self.seen) >= 2: | |
if self.target - num in self.seen: | |
print(f"{self.target - num, num} = {self.target}") | |
return 1 | |
return 0 | |
if __name__ == '__main__': | |
INPUT = 'input.txt' | |
OUTPUT = 'output.txt' | |
c = TwoSum() | |
c.read_and_write(INPUT,OUTPUT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment