forked from mareksuscak/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
99 lines (80 loc) Β· 2.69 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import cs50
import re
from flask import Flask, abort, redirect, render_template, request
from html import escape
from werkzeug.exceptions import default_exceptions, HTTPException
from helpers import distances, Operation
# Web app
app = Flask(__name__)
@app.after_request
def after_request(response):
"""Disable caching"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def index():
"""Handle requests for / via GET (and POST)"""
return render_template("index.html")
@app.route("/score", methods=["POST"])
def score():
"""Handle requests for /score via POST"""
# Read files
s1 = request.form.get("string1")
s2 = request.form.get("string2")
if not s1 or not s2:
abort(400, "missing strings")
# Score files
matrix = distances(s1, s2)
# Extract operations from table
operations = []
i, j = len(s1), len(s2)
while True:
_, operation = matrix[i][j]
if not operation:
break
if operation == Operation.INSERTED:
j -= 1
elif operation == Operation.DELETED:
i -= 1
else:
i -= 1
j -= 1
operations.append(operation)
operations.reverse()
# Maintain list of intermediate strings, operation, and descriptions
transitions = [(s1, None, None)]
i = 0
# Apply each operation
prev = s1
for operation in operations:
# Update string and description of operation
if operation == Operation.INSERTED:
s = (prev[:i], s2[i], prev[i:])
description = f"inserted '{s2[i]}'"
prev = prev[:i] + s2[i] + prev[i:]
i += 1
elif operation == Operation.DELETED:
s = (prev[:i], prev[i], prev[i + 1:])
description = f"deleted '{prev[i]}'"
prev = prev[:i] + prev[i + 1:]
elif prev[i] != s2[i]:
s = (prev[:i], s2[i], prev[i + 1:])
description = f"substituted '{prev[i]}' with '{s2[i]}'"
prev = prev[:i] + s2[i] + prev[i + 1:]
i += 1
else:
i += 1
continue
transitions.append((s, str(operation), description))
transitions.append((s2, None, None))
# Output comparison
return render_template("score.html", matrix=matrix, s1=s1, s2=s2, operations=transitions)
@app.errorhandler(HTTPException)
def errorhandler(error):
"""Handle errors"""
return render_template("error.html", error=error), error.code
# https://github.com/pallets/flask/pull/2314
for code in default_exceptions:
app.errorhandler(code)(errorhandler)