I’ve created a solution to this problem which seems to always produce the correct output for me when ran in a seperate replit. However, whenever I run the tests on the replit you have provided it fails the tests. If anyone can explain why this would be appreciated.
Your code so far
edit: for some reason it won’t post my code with the indentations but I believe there are no indentation issues
def arithmetic_arranger(problems, show_answers=True):
if len(problems) > 5:
return "Error: Too many problems."
# create lines for solution
line1 = f''
line2 = f''
line3 = f''
line4 = f''
count = 0
# find where to split the string and value of the operator
for problem in problems:
count += 1
index = problem.find(' ')
operator = problem[index + 1]
first_number = problem[0:index]
second_number = problem[index + 3:]
# determine if solution requires addition or subtraction
if operator == "+":
sol = int(first_number) + int(second_number)
elif operator == "-":
sol = int(first_number) - int(second_number)
else:
return "Error: Operator must be '+' or '-'."
if first_number.isdigit():
pass
else:
return "Error: Numbers must only contain digits."
if second_number.isdigit():
pass
else:
return "Error: Numbers must only contain digits."
if len(first_number) > 4:
return "Error: Numbers cannot be more than four digits."
else:
pass
if len(second_number) > 4:
return "Error: Numbers cannot be more than four digits."
else:
pass
# create strings to be added to solution lines
x = ''
y = ''
z = ''
s = ''
# find how many spaces to add to each item to make soluution right aligned
if len(first_number) >= len(second_number):
length_of_solution = len(first_number) + 2
else:
length_of_solution = len(second_number) + 2
number_of_times_to_add_space1 = length_of_solution - len(first_number)
number_of_times_to_add_space2 = length_of_solution - len(second_number) -1
number_of_times_to_add_space3 = length_of_solution - len(str(sol))
# add the required spaces and then determine if its the last problem to decide if there needs to be another four spaces to seperate the problems
if count < len(problems):
for i in range(number_of_times_to_add_space1):
x = ' ' + x
x = x + first_number
for i in range(number_of_times_to_add_space2):
y = ' ' + y
y = operator + y + second_number
for i in range(number_of_times_to_add_space3):
s = ' ' + s
s = s + str(sol)
line1 = line1 + x + ' '
line2 = line2 + y + ' '
line4 = line4 + s + ' '
for i in range(length_of_solution):
z = '-' + z
line3 = line3 + z + ' '
else:
for i in range(number_of_times_to_add_space1):
x = ' ' + x
x = x + first_number
for i in range(number_of_times_to_add_space2):
y = ' ' + y
y = operator + y + second_number
for i in range(number_of_times_to_add_space3):
s = ' ' + s
s = s + str(sol)
line1 = line1 + x
line2 = line2 + y
line4 = line4 + s
# create dashed line
for i in range(length_of_solution):
z = '-' + z
line3 = line3 + z
# create final answer
arranged_problems = f'{line1}\n{line2}\n{line3}\n{line4}'
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Arithmetic Formatter