I’m not sure what’s wrong here. I’ve written code to handle the key rules for this challenge and all the tests look to be showing the correctly formatted answers, but they are not evaluating in the tests. My code is below:
def arithmetic_arranger(problems, torf=True):
# if optional argument is true, execute code
if torf:
#define variables used later
nums = ''
dens = ''
sums = ''
# error when too many problems
if len(problems) > 5:
return 'Error: Too many problems.'
# split problem into components
for k in problems:
l = (k.split(' '))
num = l[0]
op = l[1]
den = l[2]
# error when numbers are too long or non-numeric
if len(num) > 4 or len(den) > 4:
return 'Error: Numbers cannot be more than four digits.'
if not num.isnumeric() or not den.isnumeric():
return 'Error: Numbers must only contain digits.'
# execute code if operators are correct
if op == '+' or op == '-':
# set length of sum and top, bottom and line values
length = max(len(num), len(den)) + 2
top = str(num).rjust(length)
bottom = op + str(den).rjust(length - 1)
sum = ''
for s in range(length):
sum += '-'
# add to the overall string
nums += top + ' '
dens += bottom + ' '
sums += sum + ' '
else:
return "Error: Operator must be '+' or '-'."
# strip out spaces to the right of the string
nums.rstrip()
dens.rstrip()
sums.rstrip()
arranged_problems = nums + '\n' + dens + '\n' + sums
return arranged_problems
Below is the error message shown in the console.
3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/GratefulCuddlyDatasets/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: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [35 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 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_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/GratefulCuddlyDatasets/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: ' 3[23 chars] 123 \n- 698 - 3801 + 43 + 49 [35 chars] ' != ' 3[23 chars] 123\n- 698 - 3801 + 43 + 49\n-----[57 chars] 172'
- 32 1 45 123
? ----
+ 32 1 45 123
- - 698 - 3801 + 43 + 49
? ----
+ - 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.005s
FAILED (failures=2)
Link to my repl.it here
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: