Hello Folks,
I´ve been working on the freecode camp Arithmetic Formatter lately. This is the first time Im doing a project this tediuos and long. I´ve been having all type of issues which I have solved. However, I cannot figure this out. I searched on other topics, but since the code is different I cannot see what the write way would be without changing the whole code. I feel like I either fail when arranging the spaces or when I say “If show_results == True: …”
I would appreciate any hint on this!
Thank you so much
import operator
import re
def arithmetic_arranger(problems, show_results = False):
### Diferent lines to print
arranged_problems = ""
line1 = list()
line2 = list()
line3 =list()
line4 = list()
# Limit of 5 problems solution
if len(problems) > 5:
return "Error: Too many problems."
# Looking for appropiate operators
for items in problems:
if "/" in items:
return "Error: Operator must be '+' or '-'."
if "x" in items:
return "Error: Operator must be '+' or '-'."
else:
continue
# Looking for letters
for item in problems:
letter = re.findall("[A-Za-z]+", item)
if len(letter) > 0 :
return "Error: Numbers must only contain digits."
# Max 4 digits
for items in problems:
item = items.split()
if len(item[0]) >4 or len(item[2]) >4:
return "Error: Numbers cannot be more than four digits."
# Turning strings into integers and oinf the calculations
results = []
operators = {"+" : operator.add, "-" : operator.sub}
for items in problems:
item = items.split()
i1, i2 = int(item[0]), int(item[2])
oper = item[1]
results.append((operators[oper](i1,i2)))
#### Line 3
for items in problems:
item = items.split()
if len(item[0]) > len(item[2]):
line3.append("_" * (len(item[0])+2))
elif len(item[2]) > len(item[0]):
line3.append("_" * (len(item[2])+2))
elif len(item[0]) == len(item[2]):
line3.append("_" * (len(item[2])+2))
### Linea 4
for dashes, numbers in zip(line3, results):
line4.append( (" " * (len(dashes) - len(str(numbers))) + f"{numbers}"))
### Line 1
for items, dashes in zip(problems, line3):
item = items.split()
line1.append ( " " * ( len(dashes) - len(item[0])) + item[0])
#Line 2
for items, dashes in zip(problems, line3):
item = items.split()
simbol = ""
if "+" in item:
simbol = "+"
if "-" in item:
simbol = "-"
line2.append( simbol + " " * ( len(dashes) - len(item[2])-1) + item[2])
# Turning lists into strings
string_1 = " ".join([str(element) for element in line1])
string_2 = " ".join([str(element) for element in line2])
string_3 = " ".join([str(element) for element in line3])
string_4 = " ".join([str(element) for element in line4])
string_1.rstrip()
string_2.rstrip()
string_3.rstrip()
string_4.rstrip()
if show_results == True:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip() + "\n" + string_4.rstrip()
return arranged_problems
else:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip()
return arranged_problems
Whenever I run it I got the following errors:
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
_____ ______ ____ _____
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 10, in test_arrangement
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: ' [34 chars]5 - 2 + 43 + 49\n_____ ______ ____ _____' != ' [34 chars]5 - 2 + 43 + 49\n----- ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- _____ ______ ____ _____+ ----- ------ ---- ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: ' 3[59 chars] 49\n_____ ______ ____ _____\n -666 [21 chars] 172' != ' 3[59 chars] 49\n----- ------ ---- -----\n -666 [21 chars] 172'
32 1 45 123
- 698 - 3801 + 43 + 49
- _____ ______ ____ _____
+ ----- ------ ---- -----
-666 -3800 88 172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.003s
FAILED (failures=2)
The link to the problem is: [Arithmetic Formatter - Free Code Camp] https://replit.com/@alfonsosmdc/boilerplate-arithmetic-formatter#arithmetic_arranger.py