Hi everyone!
I am stuck with the arithmetic arranger/formatter, because although it works ok in jupyter or pycharm, it does not pass the too many digits test on repl.it…I am also stuck in arranging the operations…haven’t managed to find a way to print them inline. Some advice would be welcome! Repl.it link: https://repl.it/repls/ZestyLoosePrinter#arithmetic_arranger.py
Here’s the code:
def arithmetic_arranger(alist):
#----------------------assigning variables----------------------
op = ["+", "-"]
split_by_operators = []
arranged_problems = []
first = ""
sign = ""
second = ""
sums = []
line = []
#-------eliminate all spaces inside the elements-----------
alist = [x.replace(' ', '') for x in alist]
#-------------------------max 5 math problems condition--------------------------
if len(alist) > 5:
arranged_problems = "Error: Too many problems."
else:
#----split the list of math problems into pieces of first operand, sign, second operand
for i in range(len(alist)):
for j in op:
if j in alist[i]:
split_by_operators.append(alist[i].split(j))
split_by_operators[i].insert(1, j)
#-----------------only digits condition-------------------
for i in split_by_operators:
if not i[0].isdigit() or not i[2].isdigit():
return 'Error: Numbers must only contain digits.'
#-----------------numbers length max 4 condition-------------------
for i in split_by_operators:
if len(i[0]) > 4 or len(i[2]) > 4:
return "Error: Numbers cannot be larger than 4."
#-----------------compiling the operations----------------
for i in split_by_operators:
max_length = int(max(len(i[0]), len(i[2])))
spc = " "
sign = i[1]
first = i[0]
second = i[2]
if sign in op:
x = print(str(first.rjust(max_length+2) + "\n" + sign + "\n" + second.rjust(max_length+2) + "\n" + 2*"-" + max_length*"-" + "\n" + str(int(i[0]) + int(i[2])).rjust(max_length+2)))
arranged_problems.append(x)
else:
print("Error: Operators can only be addition '+' or substraction '-'.")
return arranged_problems
Thanx for taking the time to read it!