Boilerplate-arithmetic-formatter testcase help

Students in primary school often arrange arithmetic problems vertically to make them easier to solve. For example, “235 + 52” becomes:

  235
+  52
-----

Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side. The function should optionally take a second argument. When the second argument is set to True , the answers should be displayed.

Example

Function Call:

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

I have written the code for this problem also the function arithmetic_formatter works perfectly however, when I run the test cases it fails 7 out of 10. But if I run the failed test cases individually on the function manually they work perfectly fine.

Your code so far

import re

def sum(n1,n2,op):

    if op=="+":

        sum=int(n1)+int(n2)

    if op=="-":

        sum=int(n1)-int(n2)

    return str(sum)

def arithmetic_arranger(problems, show_ans=False):

    top=''

    bottom=''

    dash=""

    n1=[]

    n2=[]

    ans=''

    string=""

    op=[]

    x=len(problems)

   

    if x>4:

        return "Error: Too many problems."

       

    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"

                   

        n1.append(problem.split(' ')[0])

        n2.append(problem.split(' ')[2])

        op.append(problem.split(' ')[1])

       

    for i in range(x):

        if (len(n1[i]) > 4) or (len(n2[i]) > 4 ):

            return "Error: Numbers cannot be more than four digits."

        length=max(len(n1[i]),len(n2[i]))

        if n1[i] != n1[-1] or n2[i] != n2[-1]:

            top=top+" "+n1[i].rjust(length+1)+'    '

            bottom=bottom+op[i]+" "+n2[i].rjust(length," ")+ '    '

            dash=dash+('-'*(length+2))+" "*4

            ans=ans+((sum(n1[i],n2[i],op[i])).rjust(length+2," "))+' '*4

        else:

            top=top+" "+n1[i].rjust(length+1)

            bottom=bottom+op[i]+" "+n2[i].rjust(length," ")

            dash=dash+('-'*(length+2))

            ans=ans+((sum(n1[i],n2[i],op[i])).rjust(length+2," "))

       

    if show_ans:

        string="```\n"+top+"\n"+bottom+"\n"+dash+"\n"+ans+"\n```"

    else:

        string="```\n"+top+"\n"+bottom+"\n"+dash+"\n```"

    return string

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

Which tests are failing? What is the full output?

It looks like you may have one extra newline character at the beginning of the output.

test_five_problem_arrangements
test_five_problem_arrangements_with_solution
test_four_problem_arrangement
test_only_digits
test_two_problem_arrangement1
test_two_problem_arrangement2
test_two_problem_arrangement_with_solution

=============================================================================================================== test session starts ===============================================================================================================
platform win32 – Python 3.9.10, pytest-7.1.1, pluggy-1.0.0
rootdir: D:\Devanshu\Python_programs\boilerplate-arithmetic-formatter
collected 10 items

test_module.py FFFF…FFF [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 ‘\n 3801 ... -----\n’ == ’ 3801 …---- -----’
E + E 3801 123 E - 2 + 49 E - ------ ----- 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 ‘\n 1 ... ------\n’ == ’ 1 …— ------’
E + E 1 1 E + 2 - 9380 E - --- ------ 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 ‘\n 3 ... -----\n’ == ’ 3 3…---- -----’
E + E 3 3801 45 123 E + 855 - 2 + 43 + 49 E - ----- ------ ---- ----- 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 ‘Error: Too many problems.’ == ’ 11 38…— ------’
E + Error: Too many problems.
E - 11 3801 1 123 1
E - + 4 - 2999 + 2 + 49 - 9380
E - ---- ------ — ----- ------

test_module.py:77: AssertionError
_________________________________________________________________________________________________________ test_template[test_only_digits] _________________________________________________________________________________________________________

arguments = [[‘98 + 3g5’, ‘3801 - 2’, ‘45 + 43’, ‘123 + 49’]], expected_output = ‘Error: Numbers must only contain digits.’
fail_message = ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’

@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 calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”
E assert ‘Error: Numbe…ntain digits’ == ‘Error: Numbe…ntain digits.’
E - Error: Numbers must only contain digits.
E ? -
E + Error: Numbers must only contain digits
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 ‘\n 3 ... 1028\n’ == ’ 3 9… 858 1028’
E + ```
E 3 988
E + 855 + 40
E ----- -----
E - 858 1028
E + 858 1028
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] _________________________________________________________________________________________________

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 ‘Error: Too many problems.’ == ’ 32 … 172 1028’
E + Error: Too many problems.
E - 32 1 45 123 988
E - - 698 - 3801 + 43 + 49 + 40
E - ----- ------ ---- ----- -----
E - -666 -3800 88 172 1028

test_module.py:77: AssertionError
============================================================================================================= short test summary info =============================================================================================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionError: Expected different output when calling “arithmetic_arranger()” with [“3801 - 2”, “123 + 49”]
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError: Expected different output when calling “arithmetic_arranger()” with [“1 + 2”, “1 - 9380”]
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError: Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]
FAILED test_module.py::test_template[test_only_digits] - AssertionError: Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”
FAILED test_module.py::test_template[test_two_problems_with_solutions] - AssertionError: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with [“3 + 855”, “988 + 40”] and a second argument of True.
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionError: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True.=========================================================================================================== 7 failed, 3 passed in 0.32s ===========================================================================================================

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Ah I’m so sorry this is my first post so I don’t really know my way around. I will keep this in mind going forwards

I have completed the problem. Just had some issues with printing the error messages.
Thanks for the help.

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