FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-FORKED/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"]
I have no idea what’s wrong with the strings. I’ve tested it on my IDE, and it shows that they both are not the same string.
actual = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
expected = " 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------"
if actual != expected:
print(expected)
print(actual)
print("Error")
#####################
11 3801 1 123 1
+ 4 - 2999 + 2 + 49 - 9380
---- ------ --- ----- ------
11 3801 1 123 1
+ 4 - 2999 + 2 + 49 - 9380
---- ------ --- ----- ------
Error
To me at least, they look identical. I don’t understand why it is not recognized as the same string.
Here is the code to my arithmetic_arranger:
def arithmetic_arranger(problems, check=False):
if len(problems) >5:
return 'Error: Too many problems.'
arranged_problems = ''
row_one = ''
row_two = ''
row_three = ''
row_four = ''
#Error Checks and construction of rows
for eq in problems:
equation = eq.split()
eq = ''.join(eq.split(' '))
if eq.find('+') == -1 and eq.find('-') == -1:
return 'Error: Operator must be \'+\' or \'-\'.'
regex_eq = re.split(pattern= r"[+\-]", string=eq)
for i in regex_eq:
if not i.isdigit():
return 'Error: Numbers must only contain digits.'
if len(i) > 4:
return 'Error: Numbers cannot be more than four digits.'
regex_eq = [int(i) for i in regex_eq]
# For the white-spaces I just subtract the length of the highest value with the first number in an equation and then add 2
row_one += ' '*(len(str(max(regex_eq))) - len(equation[0]) + 2) + '{}'.format(equation.pop(0)) + ' '
row_two += '{}'.format(equation.pop(0)) + ' '*(len(str(max(regex_eq))) - len(equation[0]) + 1) + '{}'.format(equation.pop())+ ' '
row_three += '{}'.format('-'*(len(str(max(regex_eq)))+2)) + ' '
if check:
row_four += ' '*(len(str(max(regex_eq))) - len(str(eval(eq))) + 2) + '{}'.format(str(eval(eq))) + ' '
arranged_problems += row_one + '\n' + row_two + '\n' + row_three
if check:
arranged_problems += '\n' + row_four
return arranged_problems