Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

I’m failing the test cases with the second argument = True, but when I run the code locally the output looks right. I would be super duper appreciative if anyone can spot my error…

I’m pasting my entire ‘arithmetic_arranger.py’ file.

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.’

and

fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True.’

Your code so far

def arithmetic_arranger(problems, solve=False):

class classProblem:

def __init__(self, operand1: int, operand2: int, operator: str,
             solve: bool):
  self.solve = solve
  self.operand1 = operand1
  self.operand2 = operand2
  self.operator = operator
  if operator == '+':
    self.solution = operand1 + operand2
  elif operator == '-':
    self.solution = operand1 - operand2
  if not solve:
    self.maxLength = max(len(str(operand1)), len(str(operand2))) + 2
  elif solve:
    self.maxLength = len(str(self.solution)) + 2

if len(problems) > 5:
return “Error: Too many problems.”
objectProblems: list[classProblem] =

for problem in problems:
listProblem = problem.split()
if len(listProblem[0]) > 4 or len(listProblem[2]) > 4:
return “Error: Numbers cannot be more than four digits.”
if not listProblem[0].isnumeric() or not listProblem[2].isnumeric():
return “Error: Numbers must only contain digits.”
operand1 = int(listProblem[0])
operand2 = int(listProblem[2])
operator = listProblem[1]
if operator != ‘+’ and operator != ‘-’:
return “Error: Operator must be ‘+’ or ‘-’.”

objectProblem = classProblem(operand1, operand2, operator, solve)
objectProblems.append(objectProblem)

line1 = " ".join(
str(problem.operand1).rjust(problem.maxLength)
for problem in objectProblems)

line2 = " “.join(
f”{problem.operator} {str(problem.operand2).rjust(problem.maxLength - 2)}"
for problem in objectProblems)

line3 = " “.join(”-" * problem.maxLength for problem in objectProblems)

if solve:
line4 = " “.join(
str(problem.solution).rjust(problem.maxLength)
for problem in objectProblems)
arranged_problems = f”{line1}\n{line2}\n{line3}\n{line4}"
elif not solve:
arranged_problems = f"{line1}\n{line2}\n{line3}"

return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

I copy/pasted my code into my local IDE and called the following:
print(arithmetic_arranger([“32 + 698”, “2 - 3801”, “9999 + 9999”, “123 + 49”], True))
and got this output:
arithmetic_formatter test output

Could you share the complete output when running tests?

Sure thing! Here it is:

32 3801 45 123

  • 698 - 2 + 43 + 49

======================= test session starts =======================
platform linux – Python 3.10.11, pytest-6.2.5, py-1.11.0, pluggy-1.3.0 – /nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/bin/python3
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-3
collected 10 items

test_module.py::test_template[test_two_problems_arrangement1] PASSED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] PASSED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] PASSED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] PASSED [ 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_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 988\n+ 855 + 40\n----- ------\n 858 1028’ == ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
E - 3 988
E + 3 988
E ? +
E - + 855 + 40
E + + 855 + 40
E ? +
E - ----- -----
E + ----- ------
E ? +
E - 858 1028
E + 858 1028
E ? +

test_module.py:77: AssertionError
________ 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 (’ 32 1 45 123 988\n’\n ‘- 698 - 3801 + 43 + 49 + 40\n’\n ‘------ ------- ---- ----- ------\n’\n ’ -666 -3800 88 172 1028’) == (’ 32 1 45 123 988\n’\n ‘- 698 - 3801 + 43 + 49 + 40\n’\n ‘----- ------ ---- ----- -----\n’\n ’ -666 -3800 88 172 1028’)
E - 32 1 45 123 988
E + 32 1 45 123 988
E ? + + +
E - - 698 - 3801 + 43 + 49 + 40
E + - 698 - 3801 + 43 + 49 + 40
E ? + + +
E - ----- ------ ---- ----- -----
E + ------ ------- ---- ----- ------
E - -666 -3800 88 172 1028
E + -666 -3800 88 172 1028
E ? + + +

test_module.py:77: AssertionError
===================== short test summary info =====================
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
=================== 2 failed, 8 passed in 0.28s ===================

I figured it out! I was solving a problem that didn’t exist, creating an error in the process!
I implemented the following code to try and accommodate problems where the solution was longer than either operand. I wasn’t supposed to do that. The result was that the operator and second operator were separated by two spaces instead of one. It passes now!

  elif solve:
    self.maxLength = len(str(self.solution)) + 2

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.