Tell us what’s happening:
Hi everyone!
My code is done but somehow the unit test complains with the following error:
AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 ... ----- ' == ' 3801 ...---- -----'
E 3801 123
E - 2 + 49
E - ------ -----
E + ------ -----
E ? ++++
Which is really weird because I am not printing anywhere plus signs or question marks, and I’m sure not printing 2 rows of dashes (as you will later see in the code).
Even weirder is that when I pass the final string with raw \n, and i compare the output with the expected_output, they are identical!! I even checked it using an online diff tool.
So I would really appreciate any advice here.
Thanks in advance.
Your code so far
def arithmetic_arranger(problems, show=False):
##############
#problems validation
valid_chars = ["1","2","3","4","5","6","7","8","9","0","+","-"," "]
#first case
if len(problems) > 5:
return "Error: Too many problems."
#second case
for problem in problems:
if "/" in problem or "*" in problem:
return "Error: Operator must be '+' or '-'."
#third case
for problem in problems:
for char in problem:
if char not in valid_chars:
return "Error: Numbers must only contain digits."
#4th case
for problem in problems:
elements = problem.split()
if len(elements[0]) > 4 or len(elements[-1]) > 4:
return "Error: Numbers cannot be more than four digits."
##############
space = " "
processed_problems = []
arranged_problems = ""
first_column = ""
second_column = ""
third_column = ""
fourth_column = ""
for problem in problems:
processed_problems.append(problem.split())
for problem in processed_problems:
int_problem = []
int_problem.append(int(problem[0]))
int_problem.append(int(problem[-1]))
l = len(str(max(int_problem))) + 2
first_column += ( problem[0].rjust(l) + space )
second_column += ( problem[1] + problem[2].rjust(l - 1) + space )
third_column += ( "-" * l + space )
if show:
if problem[1] == "+":
result = str(int(problem[0]) + int(problem[-1]))
else:
result = str(int(problem[0]) - int(problem[-1]))
fourth_column += ( result.rjust(l) + space)
first_column = first_column.rstrip() + "\n"
second_column = second_column.rstrip() + "\n"
if show:
third_column = third_column.rstrip() + "\n"
fourth_column = fourth_column.rstrip()
arranged_problems = first_column + second_column + third_column + fourth_column
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0
Challenge: Arithmetic Formatter
Link to the challenge: