-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
47 lines (40 loc) · 1.65 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import click
@click.command()
@click.argument("input", type=click.File("r"), default='input.txt')
def app(input):
"""This CLI finds all pair summing a certain number
reading the input from a file.
"""
# Read the file assuming each line could be a different input.
for line in input.readlines():
numbers, target = line.split()
try:
# parsing strings in file into integers
target = int(target)
numbers = [int(char) for char in numbers.split(",")]
print(f'The array of numbers: {numbers}')
print(f'The target sum: {target}')
except Exception as error:
print(f'Error reading line: {line}')
print(f'Please check the input is in the expected format')
else:
# If no parsing error, find pairs with the required sum.
pairs = find_pairs_with_sum(numbers, target)
print(f'All pairs suming {target}: {pairs}')
def find_pairs_with_sum(numbers, target):
"""Find all pairs in numbers summing target
Args:
numbers (array of integers): array to look up for pairs.
target (integer): The target sum a given pair should match.
"""
complement_search = dict()
pairs = []
for number in numbers:
complement_seen = complement_search.get(number)
if number in complement_search :
# This case means the other number making the pair was seen before.
pairs.append((complement_seen, number))
else:
# save the complement in a dict, in order to find it later quickly.
complement_search[target - number] = number
return pairs