Hi I am stuck in this first test which is the Arithmetic Formatter
import re
def arithmetic_arranger(problems, solve = False):
# this
# ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
# into this
# " 32 3801 45 123\n+ 698 - 2 + 43 + 49\n ------ ---- ----- -----"
if (len(problems) > 5):
return "Error: Too many problems"
first = ""
second = ""
lines = ""
sumx = ""
string = ""
for problem in problems:
if(re.search("[^\s0-9.+-]", problem)):
if(re.search("[/]", problem) or re.search("(*)", problem)):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
firstNumber = problem.split(" ")[0]
operator = problem.split(" ")[1]
secondNumber = problem.split(" ")[2]
if(len(firstNumber) >= 5 or len(secondNumber) >= 5):
return "Error: Numbers cannot be more than four digits."
sum = ""
if(operator == "+"):
sum = str(int(firstNumber) + int(secondNumber))
elif(operator == "-"):
sum = str(int(firstNumber) - int(secondNumber))
length = max(len(firstNumber), len(secondNumber)) + 2
top = str(firstNumber).rjust(length)
botton = operator + str(secondNumber).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range(length):
line += "-"
if problem != problem[-1]:
first += top + ' '
second += botton + ' '
lines += line + ' '
sumx += res + ' '
else:
first += top
second += bottom
lines += line
sumx += res
if solve:
string = first + "\n" + second + "\n" + lines + "\n" + sumx
else:
string = first + "\n" + second + "\n" + lines
return string
I am getting the following output
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
F.FF.F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/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: ' 3 3801 45 123 \n+ 855 - 2 [45 chars]- ' != ' 3 3801 45 123\n+ 855 - 2 [45 chars]----'
- 3 3801 45 123
? ---
+ 3 3801 45 123
? + + +
- + 855 - 2 + 43 + 49
? ---
+ + 855 - 2 + 43 + 49
? + + +
- ----- ------ ---- ----- ? ---
+ ----- ------ ---- -----? + + +
: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/test_module.py", line 34, in test_only_digits
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: ' 98 3801 45 123 \n+ 35 -[52 chars]- ' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.- 98 3801 45 123
- + 35 - 2 + 43 + 49
- ---- ------ ---- ----- : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/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: ' 32 1 45 123 \n- 698 - 3801 + 4[76 chars]2 ' != ' 32 1 45 123\n- 698 - 3801 +[76 chars] 172'
- 32 1 45 123
? ---
+ 32 1 45 123
? + + +
- - 698 - 3801 + 43 + 49
? ---
+ - 698 - 3801 + 43 + 49
? + + +
- ----- ------ ---- -----
? ---
+ ----- ------ ---- -----
? + + +
- -666 -3800 88 172 ? ---
+ -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`.
======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/test_module.py", line 19, in test_too_many_problems
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: 'Error: Too many problems' != 'Error: Too many problems.'
- Error: Too many problems
+ Error: Too many problems.
? +
: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
----------------------------------------------------------------------
Ran 6 tests in 0.080s
FAILED (failures=4)

Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15
.
Challenge: Arithmetic Formatter
Link to the challenge: