Arithmetic Formatter What is this failing for?

I am failing for output formatting however, the comparison of outputs seem the same to me. Any idea what is wrong with this formatting … code is below: Thanks

import re

def spaces(count, val):
    rtn = ""
    for x in range(count):
        rtn = rtn + val
    return rtn

def arithmetic_arranger(problems, *true):
    arranged_problems = ""

    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""
    cnt = 0
    for x in problems:
        cnt += 1

        if cnt > 5:
            return "Error: Too many problems."

        y = x.split()
        
        try:
            int(y[0])
            int(y[2])
        except:
            return "Error: Numbers must only contain digits."
        
        if y[1] == "+":
            sum = int(y[0]) + int(y[2])
        elif y[1] == "-":
            sum = int(y[0]) - int(y[2])
        else:
            return "Error: Operator must be \'+\' or \'-\'."
        sum = str(sum)

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

        spaceneeded = space1 - len(y[0]) + 2
        line1 = line1 + spaces(spaceneeded," ")+y[0] + "    "

        spaceneeded = space1 - len(y[2]) + 1
        line2 = line2 + "" + y[1] + spaces(spaceneeded," ") + y[2]+"    "

        spaceneeded = space1 + 2
        line3 = line3 + "" + spaces(spaceneeded, "-")+"    "

        if true :
            spaceneeded = space1 - len(str(sum)) + 2
            line4 = line4 + " " + spaces(spaceneeded, " ") + sum + "   "

    expected = "    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----"
    line1 = re.sub("    $","", line1)
    line2 = re.sub("    $", "", line2)
    line3 = re.sub("    $", "", line3)
    line4 = re.sub("    $", "", line4)

    expected = line1 + "\n" + line2 + "\n" + line3+ "\n"+line4

    return expected


print (arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print("    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----")

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

The first problem that jumps out to me is the fact that you are hardcoding one specific answer into your function.

Thanks for the help with the formatting.

This assignment line was there for debugging/comparing the outputs. It was overwritten a few lines later. That section of code has changed to the below but still fails:


    line1 = re.sub("    $","", line1)
    line2 = re.sub("    $", "", line2)
    line3 = re.sub("    $", "", line3)
    line4 = re.sub("    $", "", line4)

    arranged_problems = line1 + "\n" + line2 + "\n" + line3+ "\n"+line4

    return arranged_problems

I don’t understand what the regex part is doing for you.

What exactly do the failing tests say?

I had 4 trailing spaces in the individual line variables that I took out to match the expected output.

Here is the output of the test:

~/boilerplate-arithmetic-formatter-1$ python test_module.py 
F..F..
======================================================================
FAIL: test_arrangement (__main__.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "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: '    [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 (__main__.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_module.py", line 39, 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[72 chars] ------    ----    -----\n  -666     -3800      88      172   ' != '   3[72 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)
~/boilerplate-arithmetic-formatter-1$ 

Have you heard of trim? At least one of those looks like a whitespace at the end issue.

I looked for trim but didn’t find it, found strip() which also takes off leading white space. just found rstrip which looks like the same as trim… is there a direct trim function?

Oh, right, Python calls trim() strip() instead. You want strip().

Your other issue is the fact that you are adding an extra space here. Why add that " " here or the "" in the other lines?

Yea I saw I was failing the True test, wanted to get past the general one first. Looks like I am passing now. Thanks a lot for your help.

~/boilerplate-arithmetic-formatter-1$ python test_module.py 
......
----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK
~/boilerplate-arithmetic-formatter-1$ 

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