-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_arranger.py
49 lines (49 loc) · 1.67 KB
/
arithmetic_arranger.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
def arithmetic_arranger(problems, printing=False):
one = ''
two = ''
divider = ''
summary = ''
if len(problems) >= 6:
return 'Error: Too many problems.'
for i in problems:
x = i.split()
first = x[0]
second = x[2]
operation = x[1]
if len(first) > 4 or len(second) > 4:
return 'Error: Numbers cannot be more than four digits.'
if not first.isnumeric() or not second.isnumeric():
return 'Error: Numbers must only contain digits.'
if operation == '+' or operation == '-':
if operation == '+':
sum = str(int(first) + int(second))
else:
sum = str(int(first) - int(second))
length = max(len(first), len(second)) + 2
top = str(first).rjust(length)
bottom = operation + str(second).rjust(length - 1)
line = ''
result = str(sum).rjust(length)
for y in range(length):
line += '-'
if i != problems[-1]:
one += top + ' '
two += bottom + ' '
divider += line + ' '
summary += result + ' '
else:
one += top
two += bottom
divider += line
summary += result
else:
return "Error: Operator must be '+' or '-'."
one.rstrip()
two.rstrip()
divider.rstrip()
if printing:
summary.rstrip()
arranged_problems = one + '\n' + two + '\n' + divider + '\n' + summary
else:
arranged_problems = one + '\n' + two + '\n' + divider
return arranged_problems