Я работаю над проблемой гарвардского курса CS50.Я написал код, который не выполняет последний шаг и дает мне код ошибки выше.Я сравнил его с рабочим кодом, который нашел на этой платформе.Может кто-нибудь сказать мне, в чем разница и почему мой код не работает?
Мой код:
from enum import Enum
class Operation(Enum):
DELETED = 1
INSERTED = 2
SUBSTITUTED = 3
def __str__(self):
return str(self.name.lower())
def distances(a, b):
rows = len(a) + 1
columns = len(b) + 1
matrix = []
for i in range(rows):
matrix.append([])
for i in range(rows):
for j in range(columns):
matrix[i].append(j)
if i == 0:
matrix[i][j] = (j, Operation.DELETED)
elif j == 0:
matrix[i][j] = (i, Operation.INSERTED)
elif a[i-1] == b[j-1]:
matrix[i][j] = (matrix[i - 1][j - 1][0], Operation.SUBSTITUTED)
else:
# Operations
ins_cost = matrix[i-1][j][0] + 1
del_cost = matrix[i][j-1][0] + 1
sub_cost = matrix[i-1][j-1][0] + 1
# Get easiest method
cost = min(ins_cost, sub_cost, del_cost)
if cost == sub_cost:
op = Operation.SUBSTITUTED
elif cost == ins_cost:
op = Operation.INSERTED
elif cost == del_cost:
op = Operation.DELETED
# Set cell value
matrix[i][j] = (cost, op)
return matrix
Рабочий код от кого-то другого:
from enum import Enum
class Operation(Enum):
DELETED = 1
INSERTED = 2
SUBSTITUTED = 3
def __str__(self):
return str(self.name.lower())
def distances(a, b):
rows = len(a) + 1
columns = len(b) + 1
# List comprehension for 2D matrix
matrix = [[None for _col in range(columns)] for _row in range(rows)]
# Set base case empty string to empty string
matrix[0][0] = (0, None)
for i in range(rows):
for j in range(columns):
if i == 1:
matrix[i][j] = (j, Operation.DELETED)
elif j == 1:
matrix[i][j] = (i, Operation.INSERTED)
elif a[i-1] == b[j-1]:
matrix[i][j] = (matrix[i - 1][j - 1][0], Operation.SUBSTITUTED)
else:
# Operations
ins_cost = matrix[i-1][j][0] + 1
del_cost = matrix[i][j-1][0] + 1
sub_cost = matrix[i-1][j-1][0] + 1
# Get easiest method
cost = min(ins_cost, sub_cost, del_cost)
if cost == sub_cost:
op = Operation.SUBSTITUTED
elif cost == ins_cost:
op = Operation.INSERTED
elif cost == del_cost:
op = Operation.DELETED
# Set cell value
matrix[i][j] = (cost, op)
return matrix
полная трассировка:
Ошибка по запросу:
Traceback (most recent call last):
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
execute(self.server.app)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
application_iter = app(environ, start_response)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1748, in handle_exception
return self.finalize_request(handler(e), from_error_handler=True)
File "/home/ubuntu/workspace/pset6/hello/similarities/application.py", line 94, in errorhandler
return render_template("error.html", error=error), error.code
AttributeError: 'TypeError' object has no attribute 'code'
* Detected change in '/home/ubuntu/workspace/pset6/hello/similarities/helpers.py', reloading
* Restarting with stat