Tell us what’s happening:
The tests where there are problems are passing, but the normal ones aren’t. I don’t understand what’s happening, i see everything perfect in my console. I tried to make the return string already a string, without being an array, but it doesn’t work
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if [s for s in problems if '/' in s] or [s for s in problems if '*' in s]:
return "Error: Operator must be '+' or '-'."
elif len(problems) > 5:
return 'Error: Too many problems.'
elif [s for s in problems if any(char.isalpha() for char in s)]:
return 'Error: Numbers must only contain digits.'
elif [s for s in problems if any(len(num) > 4 for num in s.split(' '))]:
return 'Error: Numbers cannot be more than four digits.'
else:
ops = [s.split(' ') for s in problems]
res = []
a = []
ma = lambda x: max(int(x[0]), int(x[2]))
mi = lambda x: min(int(x[0]), int(x[2]))
l = [ma(n) for n in ops]
c = [mi(n) for n in ops]
# Calculate results
for prob in ops:
pr = int(prob[0])
se = int(prob[2])
if prob[1] == '+':
res.append(pr + se)
else:
res.append(pr - se)
# Add to the return string
for i in range(4):
for prob in ops:
if i == 0:
a.append(2 * ' ')
if prob[0] == str(c[ops.index(prob)]):
a += (len(prob[2]) - len(prob[0])) * ' '
a.append(prob[0])
elif i == 1:
a.append(prob[1] + ' ')
if prob[2] == str(c[ops.index(prob)]):
a.append((len(prob[0]) - len(prob[2])) * ' ')
a.append(prob[2])
elif i == 2:
a.append((2 + len(str(l[ops.index(prob)]))) * '-')
elif i == 3 and show_answers:
a.append((2 + len(str(l[ops.index(prob)])) - len(str(res[ops.index(prob)]))) * ' ' + str(res[ops.index(prob)]))
if ops.index(prob) == len(ops) - 1:
a.append('\n')
else:
a.append(4 * ' ')
return ''.join(a)
print(f'{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project