Skip to content

Instantly share code, notes, and snippets.

@berdfandrade
Created January 16, 2024 20:05
Show Gist options
  • Save berdfandrade/2867d4d91f806f0bc79595c1ff1b83b1 to your computer and use it in GitHub Desktop.
Save berdfandrade/2867d4d91f806f0bc79595c1ff1b83b1 to your computer and use it in GitHub Desktop.
def arithmetic_arranger(problems, solve=False):
first = "" #Definindo o primiero número
second = "" #Segundo...
third = ""
fourth = ""
arranged_problems = ""
for item in problems: # For loop para começar o programa.
#error 1
if len(problems) > 5: # Erro normal, exigido pelo teste.
return ('Error: Too many problems.') # Consertando isso. Estava restristo com o a igualdadde ==, então no meu tava faltando um ponto :( )
break
#splitting
itemz = item.split() # Criando uma variável nova que divide o item do for loop
#error2
if itemz[1] == "+":
op = itemz[1]
elif itemz[1] == "-":
op = itemz[1]
else:
return ("Error: Operator must be '+' or '-'.") # Erro esperado pelo teste.
break
#error 3
try:
n1 = int(itemz[0]) # Promisses em python, provalelmente.
n2 = int(itemz[2])
except:
return ("Error: Numbers must only contain digits.") # Erro exigido pelo teste.
break
#error 4
if len(itemz[0]) > 4 or len(itemz[2]) > 4:
return ("Error: Numbers cannot be more than four digits.") # Erro exigido pelo teste.
break
#Haus
if len(itemz[0]) + 2 > len(itemz[2]) + 2:
mn = len(itemz[0]) + 2
else:
mn = len(itemz[2]) + 2
#top, middle, bottom
top = str(n1).rjust(mn)
mid = str(n2).rjust(mn - 1)
strip = "-" * mn
#bottom
if op == "+":
result = n1 + n2
else:
result = n1 - n2
bot = str(result).rjust(mn)
#Primeiro, mid, bot
if item != problems[-1]: # Criando a formatação da linha.
first += top + " "
second += op + mid + " "
third += strip + " "
fourth += bot + " "
else:
first += top
second += op + mid
third += strip
fourth += bot
if solve:
arranged_problems = first + '\n' + second + '\n' + third + '\n' + fourth
else:
arranged_problems = first + '\n' + second + '\n' + third
return arranged_problems
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment