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}'
)