Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I am unsure as to why it won’t pass the test, The code functions as it should but I am not exactly clear on why the code won’t pass the test?

I would appreciate the help.

Thank You!

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    # Limit the number of problems to 5
    if len(problems) > 5:
        return "Error: Too many problems."
    
    # Lists to store the components of the problems
    first_numbers = []
    operators = []
    second_numbers = []
    answers = []

    for problem in problems:
        # Split problems into components
        parts = problem.split()
        if len(parts) != 3:
            return "Error: Invalid problem format."
        
        num1, operator, num2 = parts

        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."  # Only '+' or '-' allowed

        if not num1.isdigit() or not num2.isdigit():
            return "Error: Numbers must only contain digits."
        
        if len(num1) > 4 or len(num2) > 4:
            return "Error: Numbers cannot be more than four digits."

        # Append the components to the corresponding lists
        first_numbers.append(num1)
        operators.append(operator)
        second_numbers.append(num2)

        if show_answers:
            # Calculate the answer if requested
            if operator == "+":
                answers.append(str(int(num1) + int(num2)))
            else:  # operator == "-"
                answers.append(str(int(num1) - int(num2)))

    # Calculate the necessary widths for formatting
    max_width = max(len(num) for num in first_numbers + second_numbers) + 2

    # Prepare each line for the result
    first_line = ""
    second_line = ""
    third_line = ""
    answer_line = ""

    for i in range(len(problems)):
        num1 = first_numbers[i].rjust(max_width)
        num2 = second_numbers[i].rjust(max_width - 1)
        operator = operators[i]
        answer = answers[i] if show_answers else ""

        first_line += num1 + "    "
        second_line += operator + "" + num2 + "    "
        third_line += "-" * max_width + "    "

        if show_answers:
            answer_line += answer.rjust(max_width) + "    "

    # Strip the trailing spaces
    first_line = first_line.rstrip()
    second_line = second_line.rstrip()
    third_line = third_line.rstrip()
    answer_line = answer_line.rstrip()

    # Return the final arranged output with answers if requested
    if show_answers:
        return f"{first_line}\n{second_line}\n{third_line}\n{answer_line}"
    else:
        return f"{first_line}\n{second_line}\n{third_line}"

# Test cases
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)}')
print(f'\n{arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)}')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I have edited your post to add the three backticks to end the code block


you could open the browser console for a more detailed output from the tests, you can also use the repr function like this: print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))

that is the function for the first test that is failing:

arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ ----- .

using the repr function you can compare the output. It prints the following, then the second line is the expected output, I have added the quotes to align them.

'  3801       123\n-    2    +   49\n------    ------'
'  3801      123\n-    2    +  49\n------    -----'

There are some differences, you will need to work on removing those differences