Hi,
I finished writing the code that I assume outputs the expected answer, however I only passed 4/10 tests on replit. I’ve spent quite a few hours on this problem but I am not good enough with my coding to recognize where my code went wrong. Does anyone know what causes the tests to fail?
Any suggestions would be helpful!
My code:
def arithmetic_arranger(a, whole_argument=False):
if len(a)>5:
return "Error: Too many problems." #(works)
number_1 = []
operator = []
number_2 = []
answer = []
arranger = ["","",""]
arranger2 = ["","","",""]
for i in a: #outputting different error messages(working)
a = i.split()
if len(a[0])>4:
return 'Error: Numbers cannot be more than four digits.'
try:
a[0] = int(a[0])
except:
return 'Error: Numbers must only contain digits.'
number_1.append(a[0]) #putting all the first row numbers into number_1[] list
if len(a[2])>4: #
return 'Error: Numbers cannot be more than four digits.'
try:
a[2] = int(a[2])
except:
return 'Error: Numbers must only contain digits.'
number_2.append(a[2]) #putting all the second row numbers into number_2[] list
if a[1] != '+' and a[1] != '-':
return 'Error: Operator must be \'+\' or \'-\'.'
answer.append(a[2] + a[0]) #putting the total of row 1+2 numbers into answer[] list
operator.append(a[1]) #putting all operators into operator[] list
if whole_argument: #arranging all the elements from all the lists into the expected output with answers
for i in range(len(number_1)):
num1 = str(number_1[i])
op = str(operator[i])
num2 = str(number_2[i])
ans = str(answer[i])
width_within_problem = max(len(number_1),len(number_2)) + 2
arranger2[0] += num1.rjust(width_within_problem)
arranger2[1] += op + num2.rjust(width_within_problem - 1)
arranger2[2] += "-" * width_within_problem
arranger2[3] += ans.rjust(width_within_problem)
if i != len(number_1) - 1:
arranger2[0] += " " * 4
arranger2[1] += " " * 4
arranger2[2] += " " * 4
arranger2[3] += " " * 4
final_answer = print("\n".join(arranger2))
else: #arranging all the elements from all the lists into the expected output without answers
for i in range(len(number_1)):
num1 = str(number_1[i])
op = str(operator[i])
num2 = str(number_2[i])
width_within_problem = max(len(number_1),len(number_2)) + 2
arranger[0] += num1.rjust(width_within_problem)
arranger[1] += op + num2.rjust(width_within_problem - 1)
arranger[2] += "-" * width_within_problem
if i != len(number_1) - 1:
arranger[0] += " " * 4
arranger[1] += " " * 4
arranger[2] += " " * 4
final_answer = ('\n'.join(arranger))
return final_answer
Replit tests’ output:
32 3801 45 123 123
+ 98 - 2 + 43 + 49 + 29
------- ------- ------- ------- -------
130 3803 88 172 152
32 3801 45 123
+ 698 - 2 + 43 + 49
------ ------ ------ ------
================================= test session starts =================================
platform linux -- Python 3.10.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter-6/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-6
collected 10 items
test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [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 123\n- 2 + 49\n---- ----' == ' 3801 123\n- 2 + 49\n------ -----'
E - 3801 123
E ? -- -
E + 3801 123
E - - 2 + 49
E ? -- -
E + - 2 + 49
E - ------ -----
E ? -- -
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 1\n+ 2 -9380\n---- ----' == ' 1 1\n+ 2 - 9380\n--- ------'
E - 1 1
E ? --
E + 1 1
E ? +
E - + 2 - 9380
E ? -
E + + 2 -9380
E ? +
E - --- ------
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 3801 45 123\n'\n '+ 855 - 2 + 43 + 49\n'\n '------ ------ ------ ------') == (' 3 3801 45 123\n'\n '+ 855 - 2 + 43 + 49\n'\n '----- ------ ---- -----')
E - 3 3801 45 123
E + 3 3801 45 123
E ? + ++ +
E - + 855 - 2 + 43 + 49
E + + 855 - 2 + 43 + 49
E ? + ++ +
E - ----- ------ ---- -----
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 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '------- ------- ------- ------- -------') == (' 11 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '---- ------ --- ----- ------')
E - 11 3801 1 123 1
E + 11 3801 1 123 1
E ? +++ + ++++ ++ +
E - + 4 - 2999 + 2 + 49 - 9380
E + + 4 - 2999 + 2 + 49 - 9380
E ? +++ + ++++ ++ +
E - ---- ------ --- ----- ------
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 None == ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
E +None
E -' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
test_module.py:77: AssertionError
-------------------------------- Captured stdout call ---------------------------------
3 988
+855 + 40
---- ----
858 1028
__________________ test_template[test_five_problems_with_solutions] ___________________
arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = ' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems 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 five arithmetic problems and a second argument of `True`.
E assert None == (' 32 1 45 123 988\n'\n '- 698 - 3801 + 43 + 49 + 40\n'\n '----- ------ ---- ----- -----\n'\n ' -666 -3800 88 172 1028')
E +None
E -' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
test_module.py:77: AssertionError
-------------------------------- Captured stdout call ---------------------------------
32 1 45 123 988
- 698 - 3801 + 43 + 49 + 40
------- ------- ------- ------- -------
730 3802 88 172 1028
=============================== short test summary info ===============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionErro...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionErro...
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionErro...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionErro...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - AssertionEr...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionE...
============================= 6 failed, 4 passed in 0.19s =============================