-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic-formatter.py
87 lines (83 loc) · 2.73 KB
/
arithmetic-formatter.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def arithmetic_arranger(problems, myans=False):
# [ " sdf", "Dfsd"]
mylen = []
res = []
mysign = []
mystr = ""
op = []
if(len(problems)>5):
return "Error: Too many problems."
for i in problems:
if i.find('*') !=-1 or i.find('/') !=-1 :
return "Error: Operator must be '+' or '-'."
for i in problems:
if i.find("+") != -1 or i.find("-") != -1:
if i.find("+") != -1:
sign = "+"
else:
sign = "-"
ls = i.split(sign)
if ((ls[0].strip().isnumeric() != True ) or (ls[1].strip().isnumeric() != True)):
return "Error: Numbers must only contain digits."
if (len(ls[0].strip())>4 or len(ls[1].strip())>4):
return "Error: Numbers cannot be more than four digits."
############################################################
operand1 = int(ls[0].strip())
operand2 = int(ls[1].strip())
if(sign == "-"):
res.append(operand1-operand2)
else:
res.append(operand1+operand2)
if(len(ls[0].strip())>len(ls[1].strip())):
mylen.append(len(ls[0].strip()))
mysign.append(sign)
else:
mylen.append(len(ls[1].strip()))
mysign.append(sign)
op.append(operand1)
op.append(operand2)
j=0
for i in range(0, len(op), 2):
# mystr = mystr + " "
mystr = mystr + " " + str(op[i]).rjust(mylen[j])
# if i+1 != (len(op)):
# mystr = mystr + " "
mystr = mystr + " "
# print(mysign[j], " ", f'{op[i]:>{mylen[j]}}', end=" ", sep="")
j+=1
mystr = mystr[0: len(mystr)-4]
mystr = mystr + "\n"
j=0
for i in range(1, len(op), 2):
mystr = mystr + mysign[j] + " " + str(op[i]).rjust(mylen[j])
if i+1 != (len(op)):
mystr = mystr + " "
# mystr = mystr + " "
# print(mysign[j], " ", f'{op[i]:>{mylen[j]}}', end=" ", sep="")
j+=1
# print(mystr)
mystr = mystr + "\n"
# print()
for i in range(0, len(mysign)):
for j in range(0, mylen[i]+2):
# print("-",end="")
mystr = mystr + "-"
if i+1 != (len(mysign)):
mystr = mystr + " "
# print(" ", end="")
# print(mystr)
# print()
# mystr = mystr[0: len(mystr)-4]
# mystr = mystr + "\n"
j=0
if myans == True:
mystr = mystr + "\n"
for j in range(0, len(res)):
mystr = mystr + str(res[j]).rjust(mylen[j]+2)
# if j+1 != (len(res)):
j+=1
mystr = mystr + " "
mystr = mystr[0: len(mystr)-4]
# print(f'{res[j]:>{mylen[j]+2}}', end=" ", sep="")
return mystr
# print(arithmetic_arranger(['3801 - 2', '123 + 49'], True))