Arithmetic Formatter problem

Hi everyone!

I am stuck with the arithmetic arranger/formatter, because although it works ok in jupyter or pycharm, it does not pass the too many digits test on repl.it…I am also stuck in arranging the operations…haven’t managed to find a way to print them inline. Some advice would be welcome! Repl.it link: https://repl.it/repls/ZestyLoosePrinter#arithmetic_arranger.py
Here’s the code:

def arithmetic_arranger(alist):
#----------------------assigning variables----------------------
  op = ["+", "-"]
  split_by_operators = []
  arranged_problems = []
  first = ""
  sign = ""
  second = ""
  sums = []
  line = []
    
#-------eliminate all spaces inside the elements-----------
  alist = [x.replace(' ', '') for x in alist]
    
#-------------------------max 5 math problems condition--------------------------
  if len(alist) > 5:
    arranged_problems = "Error: Too many problems."
  else:  
#----split the list of math problems into pieces of first operand, sign, second operand
      for i in range(len(alist)):
        for j in op:
          if j in alist[i]:
            split_by_operators.append(alist[i].split(j))
            split_by_operators[i].insert(1, j)
    
#-----------------only digits condition-------------------
      for i in split_by_operators:
        if not i[0].isdigit() or not i[2].isdigit():
          return 'Error: Numbers must only contain digits.'
            
#-----------------numbers length max 4 condition-------------------
      for i in split_by_operators:
        if len(i[0]) > 4 or len(i[2]) > 4:
          return "Error: Numbers cannot be larger than 4."
#-----------------compiling the operations----------------
      for i in split_by_operators:
        max_length = int(max(len(i[0]), len(i[2])))
        spc = " "
        sign = i[1]
        first = i[0]
        second = i[2]
        if sign in op:
          x = print(str(first.rjust(max_length+2) + "\n" + sign + "\n" + second.rjust(max_length+2) + "\n" + 2*"-" + max_length*"-" + "\n" + str(int(i[0]) + int(i[2])).rjust(max_length+2)))
          arranged_problems.append(x)
        else:
            print("Error: Operators can only be addition '+' or substraction '-'.")
  return arranged_problems

Thanx for taking the time to read it!

could you please post the link to your repl, so we can see how your code is evaluated by the tests?

It’s in the text now.

ok, let’s look at what the tests say

the first one:

ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/b5t5wjnedrt/test_module.py", line 22, in test_incorrect_operator
    actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
  File "/home/runner/b5t5wjnedrt/arithmetic_arranger.py", line 24, in arithmetic_arranger
    split_by_operators[i].insert(1, j)
IndexError: list index out of range

this one says IndexError: list index out of range, and it says where also (line 24)


ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/b5t5wjnedrt/test_module.py", line 37, in test_solutions
    actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

the function takes 1 positional argument,but 2 were given - you are asked to ahve a function with a second optional argument:

The function should optionally take a second argument. When the second argument is set to True , the answers should be displayed.

your function has only one argument


FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/b5t5wjnedrt/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: [None, None, None, None] != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

your function returns [None, None, None, None] instead of a string as required


FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/b5t5wjnedrt/test_module.py", line 29, in test_too_many_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
AssertionError: 'Error: Numbers cannot be larger than 4.' != 'Error: Numbers cannot be more than four digits.'
- Error: Numbers cannot be larger than 4.
?                          ^^ - -      ^
+ Error: Numbers cannot be more than four digits.
?                          ^^        ^^^^^^^^^^^
 : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

here if you look at the diff section:

- Error: Numbers cannot be larger than 4.
?                          ^^ - -      ^
+ Error: Numbers cannot be more than four digits.
?                          ^^        ^^^^^^^^^^^

your output is the line with -, the expected one the line starting with +, and the line with ? indicates the differences

Thank you a lot! I fixed most of the stuff that was wrong…and that mostly because of lack of focus. Now i am trying to find a way to print the results in line, still prints in cascade.

do not print, your function should return a string

also, if you want something like

A        An
Multi    Other
Lines    Thing
Thing

you need to have string with \n like

"A        An\nMulti    Other\nLines    Thing\nThing"

Thank you! I fixed the printing issue and managed to strip the space after the last math problem which was too an issue. And although the result looks fine, below it i get a weird string and the test_arrangement error comes up. Can u take a look please whenever u got some time? LInk: https://repl.it/repls/ZestyLoosePrinter#arithmetic_arranger.py

there is still an extra character at the end of your string

Yep, i noticed, but i can’t figure out where it comes from. I’ll keep on trying to test the output in different ways

FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/qx0nz2e2k48/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"]

in this case the function is called without the second argument, you should not have the solutions in the output

Ok. I added “True” in the main.py and the tests. Is that what i have to do? It still comes out the same, with that “F…” after the problems. I wrote the code in jupyter notebook and there is no extra string there in the output. Here’s a printscreen

when the function is called with only one argument, you should not have the solutions

The function should optionally take a second argument. When the second argument is set to True , the answers should be displayed.

also, you are getting these errors now:

TypeError: arithmetic_arranger() missing 1 required positional argument: 'cond'

Okay, i got it now! It works! Uhuuuu. Thank you a lot for helping out!

1 Like

awesome! congratulations!