-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmetic_Formatter.py
More file actions
45 lines (31 loc) · 1.28 KB
/
Arithmetic_Formatter.py
File metadata and controls
45 lines (31 loc) · 1.28 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
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
left, operator, right = problem.split()
if operator not in ('+', '-'):
return "Error: Operator must be '+' or '-'."
if not left.isdigit() or not right.isdigit():
return "Error: Numbers must only contain digits."
if len(left) > 4 or len(right) > 4:
return "Error: Numbers cannot be more than four digits."
first_line = []
second_line = []
hash_line = []
result_line = []
for problem in problems:
left, operator, right = problem.split()
width = max(len(left), len(right)) + 2
first_line.append(left.rjust(width))
second_line.append(operator + right.rjust(width - 1))
hash_line.append('-' * width)
if show_answers:
answer = str(eval(left + operator + right))
result_line.append(answer.rjust(width))
arranged = ' '.join(first_line) + '\n' + \
' '.join(second_line) + '\n' + \
' '.join(hash_line)
if show_answers:
arranged += '\n' + ' '.join(result_line)
return arranged
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')