A little help with arithmetic arranger

The following code passes all but two of the tests and returns the error shown below the code. When I run it in PyCharm, the output looks identical to what’s requested. Not sure where to go from here.

https://replit.com/@EdwardakaLarrya/boilerplate-arithmetic-formatter-2#arithmetic_arranger.py

def arithmetic_arranger(entered_problems, answers=False):
    lst = list()  # list of individual strings from every problem
    lst_arranged = list()  # list of probs arranged for horizontal output
    answers_list = list()  # list of answers to problems
    cw = list()  # column width for output
    sub_list = list()
    num_output_columns = len(entered_problems)
    # check for number of problems entered
    if num_output_columns > 5:
        return 'Error: Too many problems.'
    # check for + or - and non-allowed characters
    for problems in entered_problems:
        if problems.find('+') == -1 and problems.find('-') == -1:
            return "Error: Operator must be '+' or '-'."

        if problems.find('*') != -1 or problems.find('/') != -1:
            return "Error: Operator must be '+' or '-'."
        problems = problems.split()
        [lst.append(x) for x in problems]  # move entered problems into individual list = lst
    # ----check for operands with lengths > 4 ---------------
    for x in lst:
        if len(x) > 4:
            return "Error: Numbers cannot be more than four digits."
    for x in lst:
        if x.isdigit() is False and x != '+' and x != '-':
            return "Error: Numbers must only contain digits."

    #  place every third element in arranged_list for proper output
    lst_arranged = lst[::3]  # get all, start at 0, stepping 3 so 0,3,6,9......
    lst_arranged = lst_arranged + lst[1::3]
    lst_arranged = lst_arranged + lst[2::3]
    # determine column widths
    cw1 = len(max(lst[0:3], key=len)) + 2
    cw2 = len(max(lst[3:6], key=len)) + 2
    cw3 = len(max(lst[6:9], key=len)) + 2
    cw4 = len(max(lst[9:12], key=len)) + 2
    if num_output_columns == 5:
        cw5 = len(max(lst[12:], key=len)) + 2
    # --generate dashes to print out between problems and answers
    cw.append(cw1 * "-")
    cw.append(cw2 * "-")
    cw.append(cw3 * "-")
    cw.append(cw4 * "-")
    if num_output_columns == 5:
        cw.append(cw5 * "-")
    lst_arranged = lst_arranged + cw  # add dashes into proper place
    #  finds the width of the problem bottoms to calculate the space needed between the operator and operand
    between = []       # space needed between operators and problems
    bottoms = lst[2::3]
    x = -1
    while True:
        x += 1
        if x > len(bottoms) - 1:
            break
        space = len(cw[x]) - len(bottoms[x]) - 1
        between.append(space)
    between = [(int(x) * ' ') for x in between]
    # -------calculate answers and place in lst_arranged list----------
    if answers is True:
        if lst[1] == '+':
            answers_list.append(int(lst[0]) + int(lst[2]))
        else:
            answers_list.append(int(lst[0]) - int(lst[2]))
        if lst[4] == '+':
            answers_list.append(int(lst[3]) + int(lst[5]))
        else:
            answers_list.append(int(lst[3]) - int(lst[5]))
        if lst[7] == '+':
            answers_list.append(int(lst[6]) + int(lst[8]))
        else:
            answers_list.append(int(lst[6]) - int(lst[8]))
        if lst[10] == '+':
            answers_list.append(int(lst[9]) + int(lst[11]))
        else:
            answers_list.append(int(lst[9]) - int(lst[11]))
        if num_output_columns == 5:
            if lst[13] == '+':
                answers_list.append(int(lst[12]) + int(lst[14]))
            else:
                answers_list.append(int(lst[12]) - int(lst[14]))
    lst_arranged = lst_arranged + answers_list  # add answers to lst_arranged 
    # move '+' and '-' signs before each element in row two - then delete items
    if num_output_columns == 4:
        x = 0
        for i in range(4, 8):
            lst_arranged[i] = lst_arranged[i] + between[x] + lst_arranged[i + 4]
            x += 1
    else:
        x = 0
        for i in range(5, 10):
            lst_arranged[i] = lst_arranged[i] + between[x] + lst_arranged[i + 5]
            x += 1
    if num_output_columns == 4:
        del lst_arranged[8:12]
    else:
        del lst_arranged[10:15]
    # -------arrange lst_arranged into a list of three sublists for printing-----------------
    x = 0
    if num_output_columns == 4:
        while True:
            sub_list.append(lst_arranged[x:x + 4])
            x += 4
            if x + 4 > len(lst_arranged):
                break
    else:
        while True:
            sub_list.append(lst_arranged[x:x + 5])
            x += 5
            if x + 5 > len(lst_arranged):
                break
    lst_arranged = sub_list
    # print the problems formatted
    output = ""

    if num_output_columns == 4:
        for i in lst_arranged:
            output = output + str("{:>{cw1}} {:>{cw2}} {:>{cw3}} {:>{cw4}} {}".format(*i, "\n", cw1=cw1, cw2=cw2 + 3, cw3=cw3 + 3, cw4=cw4 + 3))
            
    else:
        for i in lst_arranged:
            output = output + str("{:>{cw1}} {:>{cw2}} {:>{cw3}} {:>{cw4}} {:>{cw5}} {}".format(*i, "\n", cw1=cw1, cw2=cw2 + 3, cw3=cw3 + 3, cw4=cw4 + 3, cw5=cw5 + 3))
    return output
python main.py
   32      3801      45      123 
+ 698    -    2    + 43    +  49 
-----    ------    ----    ----- 

F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 11, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [23 chars]  123 \n+ 855    -    2    + 43    +  49 \n---[28 chars]- \n' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 chars]----'
-     3      3801      45      123 
?                                 -
+     3      3801      45      123
- + 855    -    2    + 43    +  49 
?                                 -
+ + 855    -    2    + 43    +  49
- -----    ------    ----    ----- 
?                                 --
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 40, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123 \n- 698    - 3801    + 43    +  49 \n---[63 chars]2 \n' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 chars] 172'
-    32         1      45      123 
?                                 -
+    32         1      45      123
- - 698    - 3801    + 43    +  49 
?                                 -
+ - 698    - 3801    + 43    +  49
- -----    ------    ----    ----- 
?                                 -
+ -----    ------    ----    -----
-  -666     -3800      88      172 
?                                 --
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.013s

FAILED (failures=2)
 ^C
 ///

your output is almost identical, you have extra characters on the right of the lines

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

Thank you, ilenia for your help. I removed the spaces you referenced (i think). But I’m still receiving a similar error message:

 python main.py
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 11, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----\n' != '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----
?                                 -
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 40, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: '   3[70 chars]   ------    ----    -----\n -666     -3800      88      172\n' != '   3[70 chars]   ------    ----    -----\n -666     -3800      88      172'
     32         1      45      123
  - 698    - 3801    + 43    +  49
  -----    ------    ----    -----
-  -666     -3800      88      172
?                                 -
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=2)


Hi @eldmoz ,

From the error messages, it looks like you have an extra return character at the end ‘\n’ :

1 Like

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

looking at the assertion error and the diff, you can see there is a \n at the end

1 Like

Thanks for your pointer. I really appreciate it.

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