-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (52 loc) · 2.27 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def arithmetic_arranger(problems, show_answers=False):
first_line = []
second_line = []
operators = []
result_line = []
if len(problems) > 5:
return 'Error: Too many problems.'
for problem in problems:
split_problem = problem.split(' ')
if split_problem[1] != '+' and split_problem[1] != '-':
return "Error: Operator must be '+' or '-'."
if not split_problem[0].isdigit() or not split_problem[2].isdigit():
return 'Error: Numbers must only contain digits.'
if not len(split_problem[0]) <= 4 or not len(split_problem[2]) <= 4:
return 'Error: Numbers cannot be more than four digits.'
first_line.append(split_problem[0])
second_line.append(split_problem[2])
operators.append(split_problem[1])
result = int(split_problem[0]) + int(split_problem[2]) if split_problem[1] == '+' else int(
split_problem[0]) - int(split_problem[2])
result_line.append(str(result))
first_line = [
' ' * (len(second_line[index]) - len(element)) + element
if len(element) < len(second_line[index])
else element
for index, element in enumerate(first_line)
]
second_line = [
operators[index] + ' ' + ' ' * (len(first_line[index]) - len(element)) + element
if len(element) < len(first_line[index])
else operators[index] + ' ' + element
for index, element in enumerate(second_line)
]
first_line = [
' ' * 2 + element
for element in first_line
]
result_line = [
' ' * (len(second_line[index]) - len(element)) + element
for index, element in enumerate(result_line)
]
dash_line = [
'-' * len(second_line[index])
for index, element in enumerate(second_line)
]
first_line = first_line[0] + ' ' + ' '.join(first_line[1:])
second_line = second_line[0] + ' ' + ' '.join(second_line[1:])
result_line = result_line[0] + ' ' + ' '.join(result_line[1:])
dash_line = dash_line[0] + ' ' + ' '.join(dash_line[1:])
problems = first_line + '\n' + second_line + '\n' + dash_line + ('\n' + result_line if show_answers else '')
return problems
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"], True)}')