-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_arranger1.py
62 lines (46 loc) · 1.64 KB
/
arithmetic_arranger1.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
def arithmetic_arranger(problems, k=False):
if len(problems) > 5:
return "Error: Too many problems."
operators = ['+', '-']
row_1 = ''
row_2 = ''
row_3 = ''
row_4 = ''
for e, problem in enumerate(problems):
num1, operator, num2 = problem.split()
num1_len = len(num1)
num2_len = len(num2)
# handling 3 ERRORS
# 1. checking operators
if operator not in operators:
return "Error: Operator must be '+' or '-'."
# 2. checking digits
if num1.isdigit() == False or num2.isdigit() == False:
return 'Error: Numbers must only contain digits.'
# 3. checking digits lenght (>4)
if num1_len > 4 or num2_len > 4:
return 'Error: Numbers cannot be more than four digits.'
# calculations
if operator == '+':
result = int(num1) + int(num2)
else:
result = int(num1) - int(num2)
# space
space = max(num1_len,num2_len) + 2
extra_space = ' '
row_1 += num1.rjust(space)
row_2 += operator + num2.rjust(space-1)
#row_3 = row_3 + ''.rjust(space, '-')
row_3 += '-' * space
row_4 += str(result).rjust(space)
if e < len(problems) - 1:
row_1 += extra_space
row_2 += extra_space
row_3 += extra_space
row_4 += extra_space
if k:
solution = row_1 + '\n' + row_2 + '\n' + row_3 + '\n' + row_4
else:
solution = row_1 + '\n' + row_2 + '\n' + row_3
return solution
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"],True))