Arithmetic Arranger Failing Tests

Hey everyone,

I have been stuck trying to pass the Arithmetic Arranger project for the past little while. I am failing 2 tests out of the 6 and I cannot seem to figure out what is missing in my code. I copied the code into IDLE and the formatting looked fine for all the tests I did myself but there must be something that I am not seeing. Would someone be able to take a look at my code and give me some guidance in where I went wrong?

def arithmetic_arranger(problems, printOutput=False):
    formattedStrings = ['', '', '', '']
    if len(problems) < 5:
        for i in range(len(problems)):
            components = problems[i].split(' ')
            if components[1] == '+' or components[1] == '-':
                if components[0].isnumeric(
                ) == True and components[2].isnumeric() == True:
                    if len(components[0]) < 5 and len(components[2]) < 5:
                        #Create the formatted question strings
                        for i in range(4):
                            if (formattedStrings[i] == ''):
                                formattedStrings[i] += ''
                            else:
                                formattedStrings[i] += '    '

                        #In this part of the code, the equations are formatted with a right align and added to a list (to be printed later)

                        #If second number is shorter (or numbers both same length)
                        if (len(components[0]) >= len(components[2])):
                            formattedStrings[1] += components[1] + ' ' * (
                                len(components[0]) - len(components[2]) + 1)
                            formattedStrings[1] += components[2]
                            formattedStrings[0] += ' ' * 2 + components[0]

                        #If the first number is shorter
                        else:
                            formattedStrings[0] += ' ' * (
                                len(components[2]) - len(components[1]) + 2
                            )  # Extra 2 spaces are to compensate for operator and space between digits
                            formattedStrings[0] += components[0]
                            formattedStrings[
                                1] += components[1] + ' ' + components[2]

                        #In this part of the code, the solution is created and added (if specified) and the other lines are finished

                        maxLength = max(len(components[0]), len(components[2]))
                        formattedStrings[2] += '-' * (maxLength + 2)

                        if (printOutput == True):
                            sol = ''
                            if (components[1] == '+'):
                                sol = str(
                                    int(components[0]) + int(components[2]))
                            else:
                                sol = str(
                                    int(components[0]) - int(components[2]))

                            formattedStrings[3] += ' ' * (
                                maxLength - len(sol) + 2)
                            formattedStrings[3] += sol

                    else:
                        return (
                            'Error: Numbers cannot be more than four digits.')

                else:
                    return ('Error: Numbers must only contain digits.')
            else:
                return ("Error: Operator must be '+' or '-'.")

        arrangedProblems = ''

        for i in range(len(formattedStrings)):
            if (i != len(formattedStrings) - 1):
                arrangedProblems += formattedStrings[i] + '\n'
            else:
                arrangedProblems += formattedStrings[i]

        return arrangedProblems
    else:
        return ("Error: Too many problems.")

Thanks

Michael

make sure the top row has 5 spaces starting from the left. i think you are over a space that’s why it’s not aligning with the bottom spacing. have a good day!

Hi dajo, thanks for your reply!

I tried playing around with the spacing a bit but the test still remained. Would you be able to give me an example of one of the tests that had messed up spacing? I have been having troubles understanding the output of the test code and seeing an example would help me diagnose where there is a flaw in my logic. :slight_smile:

Looking at the test outputs seems there’s issue when, in top line number is just a single digit. In such case function for some reason outputs one space not enough before that number and the further numbers on the right get misaligned as well later. You can copy lines below to the main.py file to see it in the full outputs form the function.

print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True))

Thank you! That helped me notice the problem, I had a typo and was calculating the number of spaces based off how many characters the operator had.

Also side note, I found my second bug, I had set my max amount of questions as 4 instead of 5. All is fixed!