Hello, I have problems with this exercise too.
I am using Thonny and according to the criteria it satisfies all the constraints and this is my code:
class Listadelistas:
newlis = []
def errores (problems):
num_prob = len(problems)
Listadelistas.newlis = []
if num_prob >4:
return "Error: Too many problems."
for prob in problems:
if "*" in prob or "/" in prob:
return "Error: Operator must be '+' or '-'."
while num_prob > 0:
for prob in problems:
Listadelistas.newlis.append(prob.split())
num_prob -= 1
for elemento in Listadelistas.newlis:
digito0 = elemento[0].isdigit()
digito2 = elemento[2].isdigit()
maslargo = max(len(elemento[0]),len(elemento[2]))
if digito0 != True or digito2 != True:
return "Error: Numbers must only contain digits."
elif maslargo > 4:
return "Error: Numbers cannot be more than four digits."
return "Sin errores"
def arithmetic_arranger(problems):
prueba_errores = errores(problems)
if prueba_errores != "Sin errores":
return prueba_errores
linea1 = str()
linea2 = str()
linea3 = str()
linea4 = str()
problemas = Listadelistas.newlis
for elemento in problemas:
maxnum= max(len(elemento[0]),len(elemento[2]))
if len(elemento[0]) == maxnum: # <--- Cuando el primer operando es mayor entonces el formato es el siguiente
dif = len(elemento[0]) - len(elemento[2]) + 1 # <--- Diferencia entre la longitud de ambos operandos, la uso luego para agregar los espacios entre el operador y el operando
lin1 = " " * 2 + elemento[0] + " " # <--- Formato de la primer linea: 2 espacios en blanco y luego el primer operando
linea1 += lin1
lin2 = elemento[1] + " " * dif + elemento[2] + " "
linea2 += lin2
formato3 = maxnum + 2
lin3 = "-" * formato3 + " "
linea3 += lin3
if elemento[1] == "+":
sol = int(elemento[0]) + int(elemento[2])
espacios = len(str(lin3)) - len(str(sol)) - 3 #<--- el 3 corresponde a los 3 espacios en blanco que hay a la derecha en la linea 3
lin4 = " " * espacios + str(sol) + " "
linea4 += lin4
else:
sol = int(elemento[0]) - int(elemento[2])
espacios = len(str(lin3)) - len(str(sol)) - 3 #<--- el 3 corresponde a los 3 espacios en blanco que hay a la derecha en la linea 3
lin4 = " " * espacios + str(sol) + " "
linea4 += lin4
else:
dif = len(elemento[2]) - len(elemento[0])
formato1 = dif + 2
lin1 = " " * formato1 + elemento[0] + " "
linea1 += lin1
lin2 = elemento[1] + " " + elemento[2] + " "
linea2 += lin2
formato3 = maxnum + 2
lin3 = "-" * formato3 + " "
linea3 += lin3
if elemento[1] == "+":
sol = int(elemento[0]) + int(elemento[2])
espacios = len(str(lin3)) - len(str(sol)) - 3 #<--- el 3 corresponde a los 3 espacios en blanco que hay a la derecha en la linea 3
lin4 = " " * espacios + str(sol) + " "
linea4 += lin4
else:
sol = int(elemento[0]) - int(elemento[2])
espacios = len(str(lin3)) - len(str(sol)) - 3 #<--- el 3 corresponde a los 3 espacios en blanco que hay a la derecha en la linea 3
lin4 = " " * espacios + str(sol) + " "
linea4 += lin4
arranged_problems = f"{linea1}\n{linea2}\n{linea3}\n{linea4}"
return arranged_problems
But when I enter the code in Repl.it, the evaluation returns the following result with errors:
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
F..E..
======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 37, in test_solutions
actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 10, in test_arrangement
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: ' 3 3801 45 123 \n+ 855 - 2 [79 chars]2 ' != ' 3 3801 45 123\n+ 855 - 2 [45 chars]----'
- 3 3801 45 123
? ---
+ 3 3801 45 123
? + + +
- + 855 - 2 + 43 + 49
? ---
+ + 855 - 2 + 43 + 49
? + + +
- ----- ------ ---- -----
? ----
+ ----- ------ ---- -----? + + +
- 858 3799 88 172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
----------------------------------------------------------------------
Ran 6 tests in 0.003s
FAILED (failures=1, errors=1)
Now I need some time to rest the neurons, then I will see the evaluation and try to solve it. But if anyone in the forum could help me, I would be very grateful.
p.d: I apologize for my English, but I am not good at writing in this language.