Formatting Problems

Tell us what’s happening:
Describe your issue in detail here.
so far im always failing the arrangement tests, and have tried everything to fix the matter of spaces but so far im turning up blank although my Output is just the same as the one in the README given. i have no idea what to do tbh

Your code so far

import re


def arithmetic_arranger(problems, solve = False):

    if(len(problems) > 5):
      return "Error: Too many problems."

    first = ""
    second = ""
    lines = ""
    sumx = ""
    string = ""

    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."
      
      firstnumber = problem.split(" ")[0]
      operator = problem.split(" ")[1]
      secondnumber = problem.split(" ")[2]

      if (len(firstnumber) >= 5) or (len(secondnumber) >= 5):
        return "Error: Numbers cannot be more than four digits."

      sum = ""
      if (operator == "+"):
        sum = str(int(firstnumber) + int(secondnumber))
      elif (operator == "-"):
        sum = str(int(firstnumber) - int(secondnumber))

      lenght = max(len(firstnumber), len(secondnumber)) + 2 
      top = str(firstnumber).rjust(lenght)
      bottom = operator + str(secondnumber).rjust(lenght - 1)
      line = ""
      res = str(sum).rjust(lenght)
      for s in range(lenght):
        line += "-"

      if problem != problems[-1]:
        first += top + ' '
        second += bottom + ' '
        lines += line + ' '
        sumx += res + ' '
      else:
        first += top
        second += bottom
        lines += line
        sumx += res

    if solve:
      string = first + "\n" + second + "\n" + lines + "\n" + sumx + "\n"
    else:
      string = first + "\n" + second + "\n" + lines 
    return string

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Arithmetic Formatter

Link to the challenge:

What errors are you getting?

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

 python main.py
   32   3801   45   123
+ 698 -    2 + 43 +  49
----- ------ ---- -----
========================= test session starts =========================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter-1
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   123...n------ -----' == '  3801      ...----    -----'
E         -   3801      123
E         ?          ---
E         +   3801   123
E         - -    2    +  49
E         ?        ---
E         + -    2 +  49
E         - ------    -----...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

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...0\n--- ------' == '  1         ...---    ------'
E         -   1         1
E         ?    ---
E         +   1      1
E         - + 2    - 9380
E         ?     ---
E         + + 2 - 9380
E         - ---    ------...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

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...-- ---- -----' == '    3      3...----    -----'
E         -     3      3801      45      123
E         ?      ---          ---  ---
E         +     3   3801   45   123
E         - + 855    -    2    + 43    +  49
E         ?       ---       ---     ---
E         + + 855 -    2 + 43 +  49
E         - -----    ------    ----    -----...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

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 ... ----- ------' == '  11      38...---    ------'
E         -   11      3801      1      123         1
E         ?     ---          --- ---            ---
E         +   11   3801   1   123      1
E         - +  4    - 2999    + 2    +  49    - 9380
E         ?      ---       ---    ---      ---
E         + +  4 - 2999 + 2 +  49 - 9380
E         - ----    ------    ---    -----    ------...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

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   988\...  858  1028\n' == '    3      9... 858     1028'
E         -     3      988
E         ?      ---
E         +     3   988
E         - + 855    +  40
E         ?       ---
E         + + 855 +  40
E         - -----    -----...
E         
E         ...Full output truncated (7 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 '   32      1...  172  1028\n' == '   32       ... 172     1028'
E         -    32         1      45      123      988
E         ?            --- ---     ---      ---
E         +    32      1   45   123   988
E         - - 698    - 3801    + 43    +  49    +  40
E         ?       ---       ---     ---      ---
E         + - 698 - 3801 + 43 +  49 +  40
E         - -----    ------    ----    -----    -----...
E         
E         ...Full output truncated (7 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
======================= short test summary info =======================
FAILED test_module.py::test_template[test_two_problems_arrangement1]
FAILED test_module.py::test_template[test_two_problems_arrangement2]
FAILED test_module.py::test_template[test_four_problems_arrangement]
FAILED test_module.py::test_template[test_five_problems_arrangement]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]

Taking a look at the tests output it seems, the spacing between separate problems is not as expected.

yeah i dont understand where im making the mistake, im kind of stuck on how to fix the mistake

Which part of code is responsible for separating problems from each other?


this part

And to make it even more narrow - which lines are adding spaces? How can it be changed to add more space between each problem?

do i need to add more spaces or reduce the spaces?


although i resolved the spacing problem in the first 6 tests, now the only 2 tests im failing are the problems with solutions. So what im stuck here is that i dont understand what the “+” in the “?” line means

I figured it out now, no worries here, thanks alot for the help

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