Hello,
I’m trying to finish de Arithmetic Formatter but I can’t figure out what my mistake is.
Input:
import re
def arithmetic_arranger(problems, solve = False):
#chequear si el problema tiene menos de 5
if len(problems) > 5:
return "Error: Too many problems."
primero = ""
segundo = ""
lineas = ""
sumax = ""
string = ""
#Aplicamos las reglas
for problema in problems:
if re.search("[^\s0-9.+-]", problema):
if re.search("[/]", problema) or re.search("[*]", problema):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
#Separamos los numeros y operadores
PrimerNumero = problema.split(" ")[0]
operador = problema.split(" ")[1]
SegundoNumero = problema.split(" ")[2]
#Verificamos si tienen menos de 5 digitos
if len(PrimerNumero) >= 5 or len(SegundoNumero) >=5:
return "Error: Numbers cannot be more than four digits."
#Resolvemos el problema
suma = ""
if operador == "+":
suma = str(int(PrimerNumero) + int(SegundoNumero))
elif operador == "-":
suma = str(int(PrimerNumero) - int(SegundoNumero))
#Calculamos las lineas
length = max(len(PrimerNumero), len(SegundoNumero)) + 2
top = str(PrimerNumero).rjust(length)
bottom = operador + str(SegundoNumero).rjust(length - 1)
line = ""
res = str(suma).rjust(length)
for s in range(length):
line += "-"
#Formato de salida
if problema != problems[-1]:
primero += top + ' '
segundo += bottom + ' '
lineas += line + ' '
sumax += res + ' '
else:
primero += top
segundo += bottom
lineas += line
sumax += res
if solve:
string = primero + "\n" + segundo + "\n" + lineas + "\n" + sumax
else:
string = primero + "\n" + segundo + "\n" + lineas
return string
Output:
python main.py
32
+ 698
-----
============================================= test session starts ==============================================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items
test_module.py FFFF....FF [100%]
=================================================== FAILURES ===================================================
________________________________ test_template[test_two_problems_arrangement1] _________________________________
arguments = [['3801 - 2', '123 + 49']], expected_output = ' 3801 123\n- 2 + 49\n------ -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 \n- 2 \n------ ' == ' 3801 ...---- -----'
E - 3801 123
E - - 2 + 49
E - ------ -----
E + 3801
E + - 2
E + ------
test_module.py:77: AssertionError
________________________________ test_template[test_two_problems_arrangement2] _________________________________
arguments = [['1 + 2', '1 - 9380']], expected_output = ' 1 1\n+ 2 - 9380\n--- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E assert ' 1 \n+ 2 \n--- ' == ' 1 ...--- ------'
E - 1 1
E - + 2 - 9380
E - --- ------
E + 1
E + + 2
E + ---
test_module.py:77: AssertionError
________________________________ test_template[test_four_problems_arrangement] _________________________________
arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = ' 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E assert ' 3 \n+ 855 \n----- ' == ' 3 3...---- -----'
E - 3 3801 45 123
E - + 855 - 2 + 43 + 49
E - ----- ------ ---- -----
E + 3
E + + 855
E + -----
test_module.py:77: AssertionError
________________________________ test_template[test_five_problems_arrangement] _________________________________
arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = ' 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E assert ' 11 \n+ 4 \n---- ' == ' 11 38...--- ------'
E - 11 3801 1 123 1
E - + 4 - 2999 + 2 + 49 - 9380
E - ---- ------ --- ----- ------
E + 11
E + + 4
E + ----
test_module.py:77: AssertionError
_______________________________ test_template[test_two_problems_with_solutions] ________________________________
arguments = [['3 + 855', '988 + 40'], True]
expected_output = ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E assert ' 3 \n+ 8...-- \n 858 ' == ' 3 9... 858 1028'
E - 3 988
E - + 855 + 40
E - ----- -----
E - 858 1028
E + 3
E + + 855
E + ----- ...
E
E ...Full output truncated (2 lines hidden), use '-vv' to show
test_module.py:77: AssertionError
_______________________________ test_template[test_five_problems_with_solutions] _______________________________
The Replit link https://replit.com/@JonattanDa/boilerplate-arithmetic-formatter?v=1
I would really appreciate if you could give me a hand with this.