-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_arranger.py
62 lines (47 loc) · 1.78 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
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
def arithmetic_arranger(problems,value = False):
if len(problems) > 5:
return "Error: Too many problems."
line1 , line2 ,line3 ,line4 = [] , [] , [] , []
for problem in problems:
operator = re.findall("\s([+|-])\s",problem)
if (len(operator) == 0):
return "Error: Operator must be '+' or '-'."
num1 = problem.split(" ")[0]
num2 = problem.split(" ")[2]
try:
num3 = int(num1) + int(num2) if (operator[0] == "+") else int(num1) - int(num2)
except:
return "Error: Numbers must only contain digits."
str(num1),str(num2)
if (len(num1) > 4) or (len(num2) > 4):
return "Error: Numbers cannot be more than four digits."
length = max(len(num1),len(num2)) + 2
temp = ""
for i in range(length - len(num1)):
temp += " "
temp += num1
line1.append(temp)
temp = "" + operator[0]
for i in range(length - len(num2) -1):
temp += " "
temp += num2
line2.append(temp)
temp = ""
for i in range(length):
temp += "-"
line3.append(temp)
temp = ""
num3 = str(num3)
for i in range(length - len(num3)):
temp += " "
temp += num3
line4.append(temp)
line1str = " ".join(line1)
line2str = " ".join(line2)
line3str = " ".join(line3)
line4str = " ".join(line4)
if value == True:
return line1str + "\n" + line2str + "\n" + line3str + "\n" + line4str
return line1str + "\n" + line2str + "\n" + line3str
print(arithmetic_arranger(['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True))