samedi 22 mai 2021

Arithmetic Formatter Help Output

So I've almost finished the arithmetic formatter that I am required to do in www.freeCodeCamp.org. The calculations must appear inline vertically and if we add a True statement then we would be able to see the result of the calculations. It would go like this:

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output: Inline calculations without results

(now with calculations)

arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)

Output: Inline calculations with results

So I've finished my code but I still have 5 errors and 1 failure.

Here is my code:

def arithmetic_arranger(problems, see = True):

  first_operand = list()
  operator = list()
  second_operand = list()
  max_length = list()
  dashes = list()
  operation = list()
  upper_part = ""
  lower_part = ""
  dashesline = ""
  results = ""
  index = 0

  # Get the length of the list, if it is higher than 5, print an error 
  if len(problems) > 5:
    print(" Error: Too many problems")

  # Divide the list into sub lists
  for increment in problems:
    new_item = increment.split()

    # Assign first, second operand and operator
    first_operand.append(new_item[0])
    operator.append(new_item[1])
    second_operand.append(new_item[2])

  # Checking if the operands are only digits (must be separated into 
  # two different for loops if we do not want to see the error written 4 times)
  for go_over in first_operand:
    if not go_over.isnumeric():
      print("Error: Numbers must only contain digits")
  for go_over in second_operand:
    if not go_over.isnumeric():
      print("Error: Numbers must only contain digits")

  # Checking if numbers are not above 4 digits (must be separated into 
  # two different for loops if we do not want to see the error written 4 times)
  for go_over in first_operand:
    if len(go_over) > 4:
      print("Error: Numbers cannot be more than four digits")
  for go_over in second_operand:
    if len(go_over) > 4:
      print("Error: Numbers cannot be more than four digits")
        
  # Checking if the operator is a '+' or a '-'
  for go_over in operator:
    index += 1
    if go_over == "*" or go_over == "/":
      print("Error: Operator must be '+' or '-'")
    # Doing the calculation for '+' and '-'
    elif go_over == "+":
      operation.append(str(int(first_operand[index-1]) + int(second_operand[index-1])))
    elif go_over == "-":
      operation.append(str(int(first_operand[index-1]) - int(second_operand[index-1])))

  for go_over in range(len(first_operand)):
    max_length.append(max(len(first_operand[go_over]),len(second_operand[go_over])))
    dashes.append("-"*(2+max_length[go_over]))
  
  for go_over in range(len(first_operand)):
    if see == True:
      if go_over == 0:
        upper_part += first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
        dashesline += dashes[go_over]
        results += operation[go_over].rjust(2+max_length[go_over])
      else:
        upper_part += "    " + first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += "    " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])
        dashesline += "    " + dashes[go_over]
        results += "    " + operation[go_over].rjust(2+max_length[go_over])
        
      arranged_problems = upper_part + "\n" + lower_part + "\n" + dashesline + "\n" + results

    else:
      if go_over == 0:
        upper_part += first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
      else:
        upper_part += "    " + first_operand[go_over].rjust(2+max_length[go_over])
        lower_part += "    " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])
        
      arranged_problems = upper_part + "\n" + lower_part
    
    return arranged_problems

Here is the response from the console:

python main.py
   32
+ 698
-----
  730
EEEFEE
======================================================================
ERROR: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 8, in test_arrangement
    actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 22, in test_incorrect_operator
    actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 32, in test_only_digits
    actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 27, in test_too_many_digits
    actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
ERROR: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 17, in test_too_many_problems
    actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-13/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\n- 698\n-----\n -666' != '   32         1      45      123\n- 698    - 3[84 chars] 172'
-    32
- - 698
- -----
-  -666+    32         1      45      123
+ - 698    - 3801    + 43    +  49
+ -----    ------    ----    -----
+  -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=1, errors=5)

Aucun commentaire:

Enregistrer un commentaire