Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I can’not pass tests. I not sure why
my output looks to correct

Your code so far

import re
def precalculate(problems):
    if len(problems) > 5:
        return 'Error: Too many problems.'
    delSpaceTrans = str.maketrans({" ":""})
    problems = list(map( lambda x: x.translate(delSpaceTrans), problems))
    sums = []

    for problem in problems:
        if not re.match(r"\d+(\s+)?[-+](\s+)?\d+", problem):
            if re.match(r"(\w|\d)+(\s+)?[-+](\s+)?(\w|\d)+"):
                return 'Error: Numbers must only contain digits.'
            else:
                return "Error: Operator must be '+' or '-'." # TODO
        # endif not re
        opperands = re.split(r"[\+\-]", problem)
        operator = re.search(r'[\+\-]', problem).group()
        if int(opperands[0]) > 9999 or int(opperands[1]) > 9999:
            return 'Error: Numbers cannot be more than four digits.'
        s = None
        if operator == '+': s = int(opperands[0])+int(opperands[1])
        elif operator == '-': s = int(opperands[0])-int(opperands[1])
        sums.append(
            {'op1': opperands[0], 'operator': operator, 'op2': opperands[1], 'sum': s}
        )
        
    return sums
def arithmetic_arranger(problems, show_answers=False):
        #TODO
    precalc = precalculate(problems)
    #print(precalc)
    operrands1 = [op['op1'] for op in precalc]
    operrands2 = [op['op2'] for op in precalc]
    operators = [op['operator'] for op in precalc]
    sums = [op['sum'] for op in precalc]
    #print(sums)
    line1 = ""
    for x in operrands1:
        count_spaces = 4-len(str(x))
        line1+=" "*count_spaces
        line1+=x
        line1+=" "
    #line2 = " ".join(operrands2)
    line2 = ""
    for x in zip(operators, operrands2):
        #print(x[0])
        #print(x[1])
        line2+=x[0]
        count_spaces = 3-len(str(x[1]))
        line2+=" "*count_spaces
        line2+=""+x[1]
        line2+=" "
    line3 = ""
    for _ in range(len(operrands2)):
        line3+="-"*4+" "
    text= line1
    text += "\n"
    text += line2
    #text += "-"*max(len(line1),len(line2))
    text += "\n"
    text += line3
    text += "\n"
    line4=""
    for res in sums:
        count_spaces = 4-len(str(res))
        line4+=" "*count_spaces
        line4+=str(res)
        line4+= " "
    text += line4
    return text
    
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')

Example of my output

3801  123 
-  2 + 49 
---- ---- 
3799  172 

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Open the browser console with F12 to see a more detailed output of the tests

The assertion error and diff gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

This is called a diff, and it shows you the differences between two files or blocks of code:

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

1 Like

Let’s look at the first one together:

AssertionError: '3801  123 \n-  2 + 49 \n---- ---- \n3799  172 ' != '  3801      123\n-    2    +  49\n------    -----'
+   3801      123
+ -    2    +  49
+ ------    -----
- 3801  123 
- -  2 + 49 
- ---- ---- 
- 3799  172 

you have a wrong alignment, not enough spaces between the problems, and you are including the result even if it’s not asked

1 Like

I tried, but I stuck. I would to skip for a while this and continue later. Thanks for your answers

import re
def precalculate(problems):
    if len(problems) > 5:
        return 'Error: Too many problems.'
    delSpaceTrans = str.maketrans({" ":""})
    problems = list(map( lambda x: x.translate(delSpaceTrans), problems))
    sums = []

    for problem in problems:
        if not re.match(r"\d+(\s+)?[-+](\s+)?\d+", problem):
            if re.match(r"(\w|\d)+(\s+)?[-+](\s+)?(\w|\d)+"):
                return 'Error: Numbers must only contain digits.'
            else:
                return "Error: Operator must be '+' or '-'." # TODO
        # endif not re
        opperands = re.split(r"[\+\-]", problem)
        operator = re.search(r'[\+\-]', problem).group()
        if int(opperands[0]) > 9999 or int(opperands[1]) > 9999:
            return 'Error: Numbers cannot be more than four digits.'
        s = None
        if operator == '+': s = int(opperands[0])+int(opperands[1])
        elif operator == '-': s = int(opperands[0])-int(opperands[1])
        sums.append(
            {'op1': opperands[0], 'operator': operator, 'op2': opperands[1], 'sum': s}
        )
        
    return sums
def arithmetic_arranger(problems, show_answers=False):
        #TODO
    precalc = precalculate(problems)
    #print(precalc)
    operrands1 = [op['op1'] for op in precalc]
    operrands2 = [op['op2'] for op in precalc]
    operators = [op['operator'] for op in precalc]
    sums = [op['sum'] for op in precalc]
    #print(sums)
    line1 = "  "
    for x in operrands1:
        count_spaces = 4-len(str(x))
        line1+="      "*count_spaces
        line1+=x
        line1+=""
    #line2 = " ".join(operrands2)
    line2 = ""
    line3 = ""
    line4=""
    x_ = []
    for i in range(len(operators)):
        x0 = operators[i]
        x1 = operrands2[i]
        line2 += x0
        count_spaces = 3 - len(str(x1))
        line2 += "  " * count_spaces + x1 
        x_.append(1+count_spaces+2+1)
        if i < len(operators) -1: 
            line2 += "    "    
    text= line1
    text += "\n"
    text += line2
    #text += "-"*max(len(line1),len(line2))
    text += "\n"
    for i in range(len(operators)):
        x0 = operators[i]
        x1 = operrands2[i]
        
        if i < len(operators) -1: 
            line2 += "    "
        line3+= "-" * (x_[i])
        if i < len(operators) - 1: line3+="   "
        
        if show_answers:
         res = sums[i]
         count_spaces = 4-len(str(x1))
         line4+=" "*count_spaces
         line4+=str(res)
         line4+= ""

    text += line3
    
    text += line4
    return text
t=arithmetic_arranger(["3801 - 2", "123 + 49"])
print(bytes(t,'utf-8'))
print(bytes("  3801      123\n-    2    +  49\n------    -----",'utf-8'))
print("~~~~")
print(t)

output:

b'  3801      123\n-    2    +  49\n------   -----'
b'  3801      123\n-    2    +  49\n------    -----'
~~~~
  3801      123
-    2    +  49
------   -----