Hi,
Why my code is not accepted and test is still failed?
This code is makeing every point from list.
def arithmetic_arranger(problems, show_answers):
def split(problems):
for problem in problems:
problems = str(problems)
problem = problems.split(' ')
return problem
problem = split(problems)
def clear_problem(problem):
cleared_problem = []
for i in problem:
clear_trans = str.maketrans({" ": "", "'": "","[":"","]":"",",":""})
clear_problem = i.translate(clear_trans)
cleared_problem.append(clear_problem)
return cleared_problem
problem = clear_problem(problem)
def clear_vales(problem):
values = []
for i in problem:
clear_trans = str.maketrans({"+": "", "-": "", "/": "", "*": ""})
clear_values = i.translate(clear_trans)
values.append(clear_values)
clear_values_list = list(filter(None, values))
return clear_values_list
clear_values_list = clear_vales(problem)
def clear_signs(problem):
values = []
for i in problem:
clear_trans = str.maketrans(
{' ': '', '0': '', '9': '', '8': '', '7': '', '6': '', '5': '', '4': '', '3': '', '2': '', '1': ''})
clear_values = i.translate(clear_trans)
values.append(clear_values)
clear_values_list = list(filter(None, values))
return clear_values_list
clear_signs_list = clear_signs(problem)
def class_dig(clear_values_list):
class_dig = []
for i in clear_values_list:
letters_trans = str.maketrans(
{'0': None, '9': None, '8': None, '7': None, '6': None, '5': None, '4': None, '3': None, '2': None, '1': None, '/':None, '+':None, '-':None})
letters = i.translate(letters_trans)
class_dig.append(letters)
return list(filter(None, class_dig))
letters = class_dig(clear_values_list)
first_values = clear_values_list[0::2]
second_values = clear_values_list[1::2]
res = []
for i in range(len(first_values)):
first = first_values[i]
second = second_values[i]
if show_answers == False:
res.append(' ')
elif clear_signs_list[i] == '+':
res.append(int(first) + int(second))
elif clear_signs_list[i] == '-':
res.append(int(first) - int(second))
else:
res.append(' ')
dots = []
for i in range(len(first_values)):
first = first_values[i]
second = second_values[i]
if len(first) > len(second):
dots.append(((len(first)) + 2) * '-')
elif len(first) < len(second):
dots.append(((len(second)) + 2) * '-')
else:
dots.append(((len(second)) + 2) * '-')
spaces_first = []
spaces_second = []
spaces_rest = []
for i in range(len(first_values)):
first = first_values[i]
second = second_values[i]
rest = str(res[i])
dot = dots[i]
if len(first) > len(second):
spaces_second.append((len(first) - len(second)) * ' ')
spaces_first.append(' ')
elif len(first) < len(second):
spaces_first.append(((len(second) - len(first)) + 2) * ' ')
spaces_second.append('')
else:
spaces_first.append(' ')
spaces_second.append('')
spaces_rest.append((len(dot) - len(rest)) * ' ')
leng = []
for i in problem:
leng.append(len(i))
for i in problem:
if max(leng) > 4:
print('Error: Numbers cannot be more than four digits.')
break
if len(problem) > 15:
print('Error: Too many problems.')
break
for i in clear_signs_list:
if i == '/':
return print("Error: Operator must be '+' or '-'.")
break
for i in letters:
if len(letters) > 0:
return print('Error: Numbers must only contain digits.')
break
if len(problem) == 3:
return print(f'{spaces_first[0]}{first_values[0]} \n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} \n{dots[0]} \n{spaces_rest[0]}{res[0]}')
if len(problem) == 6:
return print(f'{spaces_first[0]}{problem[0]} {spaces_first[1]}{first_values[1]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} {clear_signs_list[1]} {spaces_second[1]}{second_values[1]}\n{dots[0]} {dots[1]}\n{spaces_rest[0]}{res[0]} {spaces_rest[1]}{res[1]}')
if len(problem) == 9:
return print(f'{spaces_first[0]}{problem[0]} {spaces_first[1]}{first_values[1]} {spaces_first[2]}{first_values[2]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} {clear_signs_list[1]} {spaces_second[1]}{second_values[1]} {clear_signs_list[2]} {spaces_second[2]}{second_values[2]}\n{dots[0]} {dots[1]} {dots[2]}\n{spaces_rest[0]}{res[0]} {spaces_rest[1]}{res[1]} {spaces_rest[2]}{res[2]}')
if len(problem) == 12:
return print(f'{spaces_first[0]}{problem[0]} {spaces_first[1]}{first_values[1]} {spaces_first[2]}{first_values[2]} {spaces_first[3]}{first_values[3]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} {clear_signs_list[1]} {spaces_second[1]}{second_values[1]} {clear_signs_list[2]} {spaces_second[2]}{second_values[2]} {clear_signs_list[3]} {spaces_second[3]}{second_values[3]}\n{dots[0]} {dots[1]} {dots[2]} {dots[3]}\n{spaces_rest[0]}{res[0]} {spaces_rest[1]}{res[1]} {spaces_rest[2]}{res[2]} {spaces_rest[3]}{res[3]}')
if len(problem) == 15:
return print(f'{spaces_first[0]}{problem[0]} {spaces_first[1]}{first_values[1]} {spaces_first[2]}{first_values[2]} {spaces_first[3]}{first_values[3]} {spaces_first[4]}{first_values[4]}\n{clear_signs_list[0]} {spaces_second[0]}{second_values[0]} {clear_signs_list[1]} {spaces_second[1]}{second_values[1]} {clear_signs_list[2]} {spaces_second[2]}{second_values[2]} {clear_signs_list[3]} {spaces_second[3]}{second_values[3]} {clear_signs_list[4]} {spaces_second[4]}{second_values[4]}\n{dots[0]} {dots[1]} {dots[2]} {dots[3]} {dots[4]}\n{spaces_rest[0]}{res[0]} {spaces_rest[1]}{res[1]} {spaces_rest[2]}{res[2]} {spaces_rest[3]}{res[3]} {spaces_rest[4]}{res[4]}')
arithmetic_arranger(["3801 - 2", "123 + 49"],True)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project