In [230]:
def arithmetic_arranger(x, solve=False) :
mathlist = list()
numlist = list()
onlynumbers = list()
mathlist.append(x)
first = ''
second = ''
third = ''
fourth = ''
for problems in mathlist :
numproblems = len(problems)
#Diagnostic Phase
if numproblems > 5 :
return ('Error: Too many problems.')
for problem in problems :
if problem.find('*') > -1 or problem.find('/') > -1:
return ("Error: Operator must be '+' or '-'.")
numlist.append(problem[:problem.find(' ')])
numlist.append(problem[problem.find(' ') + 1:problem.find(' ') + 2])
numlist.append(problem[problem.find(' ') + 3:])
onlynumbers.append(problem[:problem.find(' ')])
onlynumbers.append(problem[problem.find(' ') + 3:])
for item in onlynumbers:
if len(item) > 4 :
return ('Error: Numbers cannot be more than four digits.')
for item in onlynumbers :
if item.isdigit() is False :
return ('Error: Numbers must only contain digits.')
#Formatting Phase
for problem in x :
first_operand = problem.split(' ')[0]
operator = problem.split(' ')[1]
second_operand = problem.split(' ')[2]
solution = eval(first_operand + operator + second_operand)
length = max(len(first_operand), len(second_operand)) + 2
toprow = str(first_operand).rjust(length)
midrow = operator + str(second_operand).rjust(length -1)
line = ''
bottomrow = str(solution).rjust(length)
for z in range(length) :
line += '-'
first += toprow + ' '
second += midrow + ' '
third += line + ' '
fourth += bottomrow + ' '
first = first[:len(first) -4]
second = second[:len(second) -4]
third = third[:len(third) -4]
fourth = fourth[:len(fourth) -4]
if solve is True :
string = '\n'.join((first, second, third, fourth))
else :
string = '\n'.join((first, second, third))
return print(string)
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)
#print ("\n")
#arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
#print ("\n")
#arithmetic_arranger(['3801 - 2', '123 + 49'])
#print ("\n")
#arithmetic_arranger(['1 + 2', '1 - 9380'])
#print ("\n")
#arithmetic_arranger(['3 + 855', '3801 - 2', '45 + 43', '123 + 49'])
#print ("\n")
#arithmetic_arranger(['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49'])
#print ("\n")
#arithmetic_arranger(['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380'])
#print ("\n")
#arithmetic_arranger(['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87'])
#print ("\n")
#arithmetic_arranger(['3 / 855', '3801 - 2', '45 + 43', '123 + 49'])
#print ("\n")
#arithmetic_arranger(['24 + 85215', '3801 - 2', '45 + 43', '123 + 49'])
32 3801 45 123 + 698 - 2 + 43 + 49 ----- ------ ---- ----- 730 3799 88 172
In [ ]:
Note: for pytest, final line should only read: return string
pytest
==================================== test session starts ====================================
platform linux — Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /home/runner/FreeCodeCamp-Project-1-Arithmetic-Formatter
collected 10 items
test_module.py ………. [100%]
==================================== 10 passed in 0.10s =====================================