I have been coding this project under scientific computing with python and my return values are correct in terms of display but it did not pass the test cases.
def arithmetic_arranger(problems, show_answers=False):
'''
Takes a list of strings containing arithemtic equations of addition and subtraction and converts and returns them in an elementary illustrated working of the calculation.
'''
#Check if problems are more than 5.
if len(problems) > 5:
raise ValueError('Error: Too many problems.')
#Will accumulate the values for each row i.e., the first number, the second number with operator, the dashed line and the answer when required.
row1 = []
row2 = []
row3 = []
row4 = []
#Verifies further if input is correct and then converts input if correct.
for element in problems:
element = element.split(' ')
#print(element)
#Checks if only addition or subtraction operator present.
if element[1] == '+' or element[1] == '-':
#Check if numbers are numerical.
if element[0].isnumeric() and element [2].isnumeric():
#Check if numbers are not longer than 4 digits.
if len(element[0]) <=4 and len(element[2])<=4:
#Assigns variables from element list.
num1, num2, operator = element[0], element[2], element[1]
ans = 0 #Default value for answer.
#print(num1, num2, operator )
#Check if default parameter was set to True
if show_answers == True:
#Adds values when operator is '+'
if operator == '+':
ans = int(num1) + int(num2)
#Subtracts balues when operator is '-'
elif operator == '-':
ans = int(num1) - int(num2)
#Calculates total length of arithmetic working
total_length = max(len(num1), len(num2)) + 2 # Adds 2 which is to account for the operator and the space between the operator and numbers.
#Calculate right alignment
space = ' '
#Check difference between total_length and length of number to see how many spaces to push number to the right.
num1_indenter = total_length-len(num1)
num2_indenter = total_length-len(num2)-1
# Spacing between each arithmetic calculation
separator = ' '
#Check if ans is stil default or has an actual value.
if ans == 0:
ans_indenter = 0 #Right alignment set to 0.
row4.append(''+separator) #Appends nothing.
else:
ans_indenter = total_length-len(str(ans)) #Calculate right alignment
row4.append(space*ans_indenter+str(ans)+separator) #Appends right alignment, answer and additional 4 spaces to separate itself from other arithmetic calculations.
#print(row4)
#For making dashed line
line = '-'
#The following appends right alignment, the respective number/operator and four spaces to separate itself from other arithmetic calculations.
row1.append(space*num1_indenter+num1+separator)
#print(row1)
row2.append(operator+space*num2_indenter+num2+separator)
#print(row2)
row3.append(line*total_length+separator)
#print(row3)
else:
return ValueError('Error: Numbers cannot be more than four digits.')
else:
return ValueError("Error: Numbers must only contain digits.")
else:
return ValueError("Error: Operator must be '+' or '-'.")
#Holds final display of arithmetic working.
final_display = f"{separator.join(row1)}\n{separator.join(row2)}\n{separator.join(row3)}\n{separator.join(row4)}"
return final_display
print(f'{arithmetic_arranger(["32 + 8", "1 - 3081", "9999 + 9999", "523 - 49"], True)}')
I’m not entirely sure why it’s not passing the test cases but it could be due to the spacing sensitivity?