Arithmetic Formatter test arrangement fails when result desplayed

The code fails the arrangement test only when display results is True. I checked everything and I am not able to find the problem. Hope someone can help me here.

The code:

import re

def arithmetic_arranger(problems,solve = True):
    first = ""
    second = ""
    third = ""
    result = ""
    if len(problems) > 5:
        return "Error: Too many problems."
    for s in problems:
        first_number = s.split(" ")[0]
        operator = s.split(" ")[1]
        second_numer = s.split(" ")[2]
        match_oper = re.search(r'[\-+]', operator)
        if match_oper == None:
            return "Error: Operator must be '+' or '-'." 
        try: 
            if operator == "-":
                sum = int(first_number) - int(second_numer)
            elif operator == "+":
                sum = int(first_number) + int(second_numer)
        except:
            return "Error: Numbers must only contain digits."
        if len(first_number) > 4 or len(second_numer) > 4:
            return "Error: Numbers cannot be more than four digits."
    
        length = max(len(first_number), len(second_numer))
        line = ""
        space = "    "
        str_sum = str(sum)
        
        if s != problems[-1]:
            first += first_number.rjust(length+2) + space
            second += operator+second_numer.rjust(length+1) + space
            third += line.rjust(length+2,"-") + space
            result += str_sum.rjust(length+2) + space
        else:
            first += first_number.rjust(length+2)
            second += operator+second_numer.rjust(length+1)
            third += line.rjust(length+2,"-")
            result += str_sum.rjust(length+2)
    
    if solve:
        return first+"\n"+second+"\n"+third+"\n"+result
    else:
        return first+"\n"+second+"\n"+third

Tests output without displaying the solution:

 python main.py
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
......
----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK
 

Tests output with the solution enabled:

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

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

FAILED (failures=1)

it seems you are showing the solutions even when you shouldn’t
in this case the function is arithmetic_arranger( ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]) so the solutions should not be there

I think you are right. Thank you!

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