defarithmetic_arranger(problems, show_answer=False):iflen(problems)>5:return'Error: Too many problems.'for prob in problems:ifnot(prob.split(' ')[1]=='+'or prob.split(' ')[1]=='-'):return"Error: Operator must be '+' or '-'."ifnot(prob.split(' ')[0].isdigit()and prob.split(' ')[2].isdigit()):return"Error: Numbers must only contain digits."ifint(prob.split(' ')[0])>=10000orint(prob.split(' ')[2])>=10000:return"Error: Numbers cannot be more than four digits."
str_a = str_b = str_c = str_d =""for prob in problems:
a = prob.split(' ')[0]
b = prob.split(' ')[1]
c = prob.split(' ')[2]
max_ =max(len(a),len(c))
str_a +=f"{' '*(2+max_-len(a))}{a}{' '*4}"
str_b +=f"{b}{' '*(1+max_-len(c))}{c}{' '*4}"
str_c +=f"{'-'*(max_+2)}{' '*4}"if b =='+':
d =int(a)+int(c)else:
d =int(a)-int(c)
str_d +=f"{' '*(max_+2-len(str(d)))}{d}{' '*4}"if show_answer:
arranged_problems =f"{str_a[:-4]}\n{str_b[:-4]}\n{str_c[:-4]}\n{str_d[:-4]}"else:
arranged_problems =f"{str_a[:-4]}\n{str_b[:-4]}\n{str_c[:-4]}"return arranged_problems