Tell us what’s happening:
I can’not pass tests. I not sure why
my output looks to correct
Your code so far
import re
def precalculate(problems):
if len(problems) > 5:
return 'Error: Too many problems.'
delSpaceTrans = str.maketrans({" ":""})
problems = list(map( lambda x: x.translate(delSpaceTrans), problems))
sums = []
for problem in problems:
if not re.match(r"\d+(\s+)?[-+](\s+)?\d+", problem):
if re.match(r"(\w|\d)+(\s+)?[-+](\s+)?(\w|\d)+"):
return 'Error: Numbers must only contain digits.'
else:
return "Error: Operator must be '+' or '-'." # TODO
# endif not re
opperands = re.split(r"[\+\-]", problem)
operator = re.search(r'[\+\-]', problem).group()
if int(opperands[0]) > 9999 or int(opperands[1]) > 9999:
return 'Error: Numbers cannot be more than four digits.'
s = None
if operator == '+': s = int(opperands[0])+int(opperands[1])
elif operator == '-': s = int(opperands[0])-int(opperands[1])
sums.append(
{'op1': opperands[0], 'operator': operator, 'op2': opperands[1], 'sum': s}
)
return sums
def arithmetic_arranger(problems, show_answers=False):
#TODO
precalc = precalculate(problems)
#print(precalc)
operrands1 = [op['op1'] for op in precalc]
operrands2 = [op['op2'] for op in precalc]
operators = [op['operator'] for op in precalc]
sums = [op['sum'] for op in precalc]
#print(sums)
line1 = ""
for x in operrands1:
count_spaces = 4-len(str(x))
line1+=" "*count_spaces
line1+=x
line1+=" "
#line2 = " ".join(operrands2)
line2 = ""
for x in zip(operators, operrands2):
#print(x[0])
#print(x[1])
line2+=x[0]
count_spaces = 3-len(str(x[1]))
line2+=" "*count_spaces
line2+=""+x[1]
line2+=" "
line3 = ""
for _ in range(len(operrands2)):
line3+="-"*4+" "
text= line1
text += "\n"
text += line2
#text += "-"*max(len(line1),len(line2))
text += "\n"
text += line3
text += "\n"
line4=""
for res in sums:
count_spaces = 4-len(str(res))
line4+=" "*count_spaces
line4+=str(res)
line4+= " "
text += line4
return text
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
Example of my output
3801 123
- 2 + 49
---- ----
3799 172
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project