Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
Re Hello to all
had a problem with indentation
I went from 3 to 8

Old 2:
Re Hello to all,
I found several errors by looking at others on the forum, I went from 0 to 3 tests OK

Honestly, I don’t understand why the rest don’t pass, could you help me?

Old :

Hello to all,
I have a difficulty to validate the unit tests. Indeed, on my side my code works

I did some tests of the instructions by hand and it works.
But if I launch with unit tests nothing is valid anymore, I don’t understand why

Could you look at my code and tell me or I made a mistake?

Thanks in advance
Arnaud

Link of code

arithmetic_arranger.py


def arithmetic_arranger(problems, solv=False):
    #check_problems
    if len(problems) > 5:
        return "Error: Too many problems"

    for problem in problems:
        # check 1 per 1 problem
        check_prob = problem.split(" ")

        if check_prob[1] in "*/":
            return "Error: Operator must be '+' or '-'."

       
        if not (check_prob[0].isdigit() and check_prob[2].isdigit()):
            return "Error: Numbers must only contain digits."

        
        if len(check_prob[0]) > 4 or len(check_prob[2]) > 4:
            return "Error: Numbers cannot be more than four digits."


    # Formating the all problem in line per line
    lines = ["", "", ""]
    if solv:
        lines.append("")

    for problem in problems:
        check_prob = problem.split(" ")
        num_one = check_prob[0]
        num_two = check_prob[2]
        operator = check_prob[1]

        list_of_nb_char = [len(num_one), len(num_two)]
        if solv:
            result = str(eval(problem))
            list_of_nb_char.append(len(result))

        nb_char_max = max(list_of_nb_char) + 2

        lines[0] += ' ' * (nb_char_max - len(num_one)) + num_one + ' ' * 4
        lines[1] += operator + ' ' * (nb_char_max - len(num_two) - 1) + num_two + ' ' * 4
        lines[2] += '-' * nb_char_max + ' ' * 4
        if solv:
            lines[3] += ' ' * (nb_char_max - len(result)) + result + ' ' * 4

    # concatenate all line in one 
    arranged_problems = ""
    for line in lines:
        arranged_problems += line[:-4] + "\n"

    return arranged_problems[:-1]

Result run

 python main.py
   32      3801      45      123      12
+ 698    -    2    + 43    -  49    +  4
-----    ------    ----    -----    ----
================= test session starts ==================
platform linux -- Python 3.8.13, pytest-7.1.1, pluggy-1.0.0 -- /nix/store/06c55y0c5yzx5gx4l6k0pp6071zf1y5i-python3-3.8.13/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/Arnaud-arithmetic-formatter
collected 10 items                                     

test_module.py::test_template[test_two_problems_arrangement1] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 40%]
test_module.py::test_template[test_too_many_problems] <- ../boilerplate-arithmetic-formatter/test_module.py FAILED [ 50%]
test_module.py::test_template[test_incorrect_operator] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 70%]
test_module.py::test_template[test_only_digits] <- ../boilerplate-arithmetic-formatter/test_module.py PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] <- ../boilerplate-arithmetic-formatter/test_module.py FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] <- ../boilerplate-arithmetic-formatter/test_module.py FAILED [100%]

======================= FAILURES =======================
________ test_template[test_too_many_problems] _________

arguments = [['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87']]
expected_output = 'Error: Too many problems.'
fail_message = 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."'

>   ???
E   AssertionError: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
E   assert 'Error: Too many problems' == 'Error: Too many problems.'
E     - Error: Too many problems.
E     ?                         -
E     + Error: Too many problems

/home/runner/boilerplate-arithmetic-formatter/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`.'

>   ???
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     ?      +

/home/runner/boilerplate-arithmetic-formatter/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`.'

>   ???
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     ? +     +                           +

/home/runner/boilerplate-arithmetic-formatter/test_module.py:77: AssertionError
=============== short test summary info ================
FAILED test_module.py::test_template[test_too_many_problems]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
============= 3 failed, 7 passed in 0.21s ==============
 

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

The errors are telling you where the problems are. For instance:

E   AssertionError: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
E   assert 'Error: Too many problems' == 'Error: Too many problems.'
E     - Error: Too many problems.
E     ?                         -
E     + Error: Too many problems

In this case the - line is the expected line, the + line is your output and the ? line is indicating the difference. Note the - under the . Likewise:

E     -   858     1028
E     +   858      1028
E     ?      +

Lines like these are indicating spacing issues.

1 Like

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