Skip to content

Commit f1d7ada

Browse files
authored
Create arithmetic-formatter.py
0 parents  commit f1d7ada

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

arithmetic-formatter.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
def arithmetic_arranger(problems, myans=False):
2+
# [ " sdf", "Dfsd"]
3+
mylen = []
4+
res = []
5+
mysign = []
6+
mystr = ""
7+
op = []
8+
if(len(problems)>5):
9+
return "Error: Too many problems."
10+
for i in problems:
11+
if i.find('*') !=-1 or i.find('/') !=-1 :
12+
return "Error: Operator must be '+' or '-'."
13+
14+
for i in problems:
15+
if i.find("+") != -1 or i.find("-") != -1:
16+
if i.find("+") != -1:
17+
sign = "+"
18+
else:
19+
sign = "-"
20+
ls = i.split(sign)
21+
if ((ls[0].strip().isnumeric() != True ) or (ls[1].strip().isnumeric() != True)):
22+
return "Error: Numbers must only contain digits."
23+
24+
if (len(ls[0].strip())>4 or len(ls[1].strip())>4):
25+
return "Error: Numbers cannot be more than four digits."
26+
27+
############################################################
28+
operand1 = int(ls[0].strip())
29+
operand2 = int(ls[1].strip())
30+
if(sign == "-"):
31+
res.append(operand1-operand2)
32+
else:
33+
res.append(operand1+operand2)
34+
if(len(ls[0].strip())>len(ls[1].strip())):
35+
mylen.append(len(ls[0].strip()))
36+
mysign.append(sign)
37+
else:
38+
mylen.append(len(ls[1].strip()))
39+
mysign.append(sign)
40+
op.append(operand1)
41+
op.append(operand2)
42+
j=0
43+
for i in range(0, len(op), 2):
44+
# mystr = mystr + " "
45+
mystr = mystr + " " + str(op[i]).rjust(mylen[j])
46+
# if i+1 != (len(op)):
47+
# mystr = mystr + " "
48+
mystr = mystr + " "
49+
# print(mysign[j], " ", f'{op[i]:>{mylen[j]}}', end=" ", sep="")
50+
j+=1
51+
mystr = mystr[0: len(mystr)-4]
52+
mystr = mystr + "\n"
53+
j=0
54+
for i in range(1, len(op), 2):
55+
mystr = mystr + mysign[j] + " " + str(op[i]).rjust(mylen[j])
56+
if i+1 != (len(op)):
57+
mystr = mystr + " "
58+
# mystr = mystr + " "
59+
# print(mysign[j], " ", f'{op[i]:>{mylen[j]}}', end=" ", sep="")
60+
j+=1
61+
# print(mystr)
62+
mystr = mystr + "\n"
63+
# print()
64+
for i in range(0, len(mysign)):
65+
for j in range(0, mylen[i]+2):
66+
# print("-",end="")
67+
mystr = mystr + "-"
68+
if i+1 != (len(mysign)):
69+
mystr = mystr + " "
70+
# print(" ", end="")
71+
# print(mystr)
72+
# print()
73+
# mystr = mystr[0: len(mystr)-4]
74+
# mystr = mystr + "\n"
75+
j=0
76+
if myans == True:
77+
mystr = mystr + "\n"
78+
for j in range(0, len(res)):
79+
mystr = mystr + str(res[j]).rjust(mylen[j]+2)
80+
# if j+1 != (len(res)):
81+
j+=1
82+
mystr = mystr + " "
83+
mystr = mystr[0: len(mystr)-4]
84+
# print(f'{res[j]:>{mylen[j]+2}}', end=" ", sep="")
85+
return mystr
86+
87+
# print(arithmetic_arranger(['3801 - 2', '123 + 49'], True))

0 commit comments

Comments
 (0)