Hi @jeremy.a.gray, thanks for the reply. I tried with above solutions, but my code is still not passing test case. It’s throwing ValueError. Please help.
def arithmetic_arranger(problems, option=False):
if len(problems) > 5:
return "Error: Too many problems."
line_1 = line_2 = line_3 = line_4 = ' '
for i, problem in enumerate(problems):
# operators = {'+', '-'}
n1, operator, n2 = problem.split()
number_1, number_2 = len(n1), len(n2)
max_space = max(number_1, number_2)
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
if not (n1.isdigit()) and not (n2.isdigit()):
return "Error: Numbers must only contain digits."
if number_1 > 4 or number_2 > 4:
return "Error: Numbers cannot be more than four digits."
result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
line_1 = line_1 + f"{n1:>2}"
line_2 = line_2 + operator + f"{n2:>1}"
line_3 = line_3 + ''.rjust(max_space + 1, '-')
line_4 = line_4 + str(result).rjust(max_space)
if i < len(problems) - 1:
line_1 += ' '
line_2 += ' '
line_3 += ' '
line_4 += ' '
if option:
arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3 + '\n' + line_4
else:
arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3
return arranged_problems
Error:
=======
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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 \n +855 \n ---- ' != ' 3 3801 45 123\n+ 855 [53 chars]----'
- 3
- +855
- ---- + 3 3801 45 123
python main.py
32
+698
----
F.EF..
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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 \n +855 \n ---- ' != ' 3 3801 45 123\n+ 855 [53 chars]----'
- 3
- +855
- ---- + 3 3801 45 123
python main.py
32
+698
----
F.EF..
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 18, in arithmetic_arranger
result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)
ValueError: invalid literal for int() with base 10: '3g5'
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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 \n +855 \n ---- ' != ' 3 3801 45 123\n+ 855 [53 chars]----'
- 3
- +855
- ---- + 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 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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 arithemetic problems and a second argument of `True`.')
AssertionError: ' 32 \n -698 \n ---- \n -666 ' != ' 32 1 45 123\n- 698 [88 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 arithemetic problems and a second argument of `True`.
----------------------------------------------------------------------
Ran 6 tests in 0.003s
FAILED (failures=2, errors=1)