-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic_arranger.py
More file actions
62 lines (49 loc) · 2.26 KB
/
Copy patharithmetic_arranger.py
File metadata and controls
62 lines (49 loc) · 2.26 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def arithmetic_arranger(problems, result=False):
"""
Receives a list of strings that are arithmetic problems and
returns the problems arranged vertically and side-by-side.
If result is set to True, the answers are displayed as well.
Possible errors:
`Error: Too many problems.`: More than 5 problems supplied to the function.
`Error: Operator must be '+' or '-'.`: The appropriate operators the function accepts are addition (+) and subtraction (-).
Multiplication and division will return an error.
Other operators not mentioned in this bullet point are not included.
`Error: Numbers must only contain digits.`: Each number (operand) should only contain digits.
`Error: Numbers cannot be more than four digits.`: Each operand (aka number on each side of the operator) has a max of four digits in width.
"""
if len(problems) > 5:
return "Error: Too many problems."
ops = {
'+': lambda pair: str(pair[0] + pair[1]),
'-': lambda pair: str(pair[0] - pair[1]),
}
arranged_problems = []
top = []
bottom = []
lines = []
results = []
for problem in problems:
chunks = problem.split()
max_len = len(max(chunks, key=len))
if not all([i.isnumeric() for i in chunks[::2]]):
return "Error: Numbers must only contain digits."
elif chunks[1] not in ops.keys():
return "Error: Operator must be '+' or '-'."
elif max_len > 4:
return "Error: Numbers cannot be more than four digits."
line_len = max_len + 2
line = '-' * line_len
first_num = chunks[0].rjust(line_len, ' ')
second_num = f"{chunks[1]}{' ' * (line_len - len(chunks[2]) - 1)}{chunks[2]}"
top.append(first_num)
bottom.append(second_num)
lines.append(line)
if result:
res = ops[chunks[1]]([int(i) for i in chunks[::2]])
results.append(f"{res.rjust(line_len, ' ')}")
arranged_problems = '\n'.join([' '.join(i)
for i in (top, bottom, lines)])
if results:
arranged_problems += '\n' + ' '.join(results)
return arranged_problems
print(arithmetic_arranger(["1 + 2", "1 - 9380"], True))