# Need Help on Arithmetic Formatter

Hi I am stuck in this first test which is the Arithmetic Formatter

`````` import re

def arithmetic_arranger(problems, solve = False):
# this
# ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]

# into this
# "    32  3801     45    123\n+  698  -  2  +  43  +  49\n ------ ----  -----  -----"

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))

length = max(len(firstNumber), len(secondNumber)) + 2
top = str(firstNumber).rjust(length)
botton = operator + str(secondNumber).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range(length):
line += "-"

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

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

I am getting the following output

``````python main.py
32     3801     45     123
+ 698   -    2   + 43   +  49
-----   ------   ----   -----
F.FF.F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/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: '    3     3801     45     123   \n+ 855   -    2   [45 chars]-   ' != '    3      3801      45      123\n+ 855    -    2  [45 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_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/test_module.py", line 34, in test_only_digits
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: '  98     3801     45     123   \n+ 35   -[52 chars]-   ' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.-   98     3801     45     123
- + 35   -    2   + 43   +  49
- ----   ------   ----   -----    : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/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: '   32        1     45     123   \n- 698   - 3801   + 4[76 chars]2   ' != '   32         1      45      123\n- 698    - 3801    +[76 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`.

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-3/test_module.py", line 19, in test_too_many_problems
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: 'Error: Too many problems' != 'Error: Too many problems.'
- Error: Too many problems
+ Error: Too many problems.
?                         +
: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.080s

FAILED (failures=4)
îş§
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15`.

Challenge: Arithmetic Formatter

Also for a start:
I think your first RegEx is wrong, because you fail the test for non-numbers as inputs.
Read the error messages - the â€śto many problemsâ€ť error has a different of a single symbol

1 Like

@Jagaya Hi, Thank you so much for your time. Here is a link to my project so far https://repl.it/@lyndonmenezes/boilerplate-arithmetic-formatter-3

Yes I did think that the first regular expression is wrong. To be honest it is quite complicated as we have different files to execute different code. But nonetheless I will get my best on this project.

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 (â€™).

1 Like

@ieahleen Thank you for the tip. Will definitely use this in the future.

Personally I used try-except to test for numbers - I would need to check RegEx myself to see whatâ€™s wrong.

For the error message:
The symbols mark your output (-), expected output (+) and an indication for the difference (?)

If you look at your output and the expected, you should notice the difference.