Arithmetic arranger help

Hey everyone. Can someone help me figure out why my arithmetic arranger is failing? I’ve tested it with multiple variations myself and it seems to work fine, but when I run it in main, it fails a bunch of tests. I’m primarily having a hard time decoding the error messages to know what to fix. Any help would be greatly appreciated.

Here is the code:

import re

def right_justified(numbers):
    '''Function that takes a string representation of a math problem and returns
    it as a string for long-form arithmetic.

    numbers: str
    return: tuple
    '''
    parts = numbers.split()
    length = ''
    for part in parts:
        if len(part) > len(length):
            length = part
    
    top = parts[0].rjust(len(length) + 2)
    bottom = parts[1] + ' ' + parts[2].rjust(len(length))
    dashes = '-' * (len(length) + 2)
    answer = str(int(parts[0]) + int(parts[2])).rjust(len(length) + 2)

    return (top, bottom, dashes, answer)

def checks(numbers):
    '''Checks if list of string represented math problems pass specified parameters

    numbers: list
    return: bool or str
    '''
    check_digits = []
    correct_format = []

    num_problems = len(numbers) <= 5
    if not num_problems:
        return 'Error: Too many problems.'

    for n in numbers:
        check_digits = n.split()
        correct_format.extend(re.findall(r'[\d]+ [+-] [\d]+', n))

        if not check_digits[0].isdigit() or not check_digits[-1].isdigit():
            return 'Error: Numbers must only contain digits'

        elif len(check_digits[0]) > 4 or len(check_digits[-1]) > 4:
            return 'Error: Numbers cannot be more than four digits.'

    if len(correct_format) < (len(numbers)):
        return 'Error: Operator must be "+" or "-".'

    return True

def arithmetic_arranger(numbers, show_answer=False):
    '''Take a list of string represented math problems and returns them for long-form math

    numbers: list
    show_answer: bool
    return: str
    '''
    tops = ''
    bottoms = ''
    dashes = ''
    answers = ''
    new_line = '\n'

    res_checks = checks(numbers)

    if res_checks != True:
        print(res_checks)
        return checks(numbers)

    else:
        for i in numbers:
            tops += right_justified(i)[0] + '    '
            bottoms += right_justified(i)[1] + '    '
            dashes += right_justified(i)[2] + '    '
            answers += right_justified(i)[3] + '    '

        tops = tops.rstrip()
        bottoms = bottoms.rstrip()
        dashes = dashes.rstrip()
        answers = answers.rstrip()

        show = (
                f'{tops}{new_line}{bottoms}{new_line}'
                f'{dashes}{new_line}{answers}'
            )

        hide = (
            f'{tops}{new_line}{bottoms}{new_line}'
            f'{dashes}{new_line}'
        )

        print(show)
        print(hide)

        if show_answer:
            return (
                f'{tops}{new_line}{bottoms}{new_line}'
                f'{dashes}{new_line}{answers}'
            )

        return (
            f'{tops}{new_line}{bottoms}{new_line}'
            f'{dashes}{new_line}'
        )

Sorry, I should specify: I’m having a hard time decoding the error messages from the test_module in Replit; I do not have any error codes when I run it locally.

And here is the Replit link: boilerplate-arithmetic-formatter-1 - Replit

I finally figured it out. The test_module is very particular. I had double quotes around + and - in one of the Error messages and it wanted single quotes. I also forgot a period in one of my Error messages. Lastly, my code did not account for arithmetic problems where the answer would turn out negative. Once I fixed these problems, I passed all of the test.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.