From b4d338935d9c04f87b3fd670abc92ac328f2acdb Mon Sep 17 00:00:00 2001 From: alishachang2 Date: Thu, 15 Jan 2026 10:38:25 -0800 Subject: [PATCH 1/2] start sort, using insertion or comparison sort --- server.py | 18 +++++++++--------- static/scoreboard.js | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index 372a266c..57e0b7ec 100644 --- a/server.py +++ b/server.py @@ -3,6 +3,7 @@ from flask import Response, request, jsonify app = Flask(__name__) +#scoreboard is a json file with a hashmap that contains id, name and score scoreboard = [ { "id": 1, @@ -36,11 +37,11 @@ ] -@app.route('/') +@app.route('/') def show_scoreboard(): return render_template('scoreboard.html', scoreboard = scoreboard) -@app.route('/increase_score', methods=['GET', 'POST']) +@app.route('/increase_score', methods=['GET', 'POST']) #when get method is sent, post to update the value and update it on screen def increase_score(): global scoreboard @@ -50,13 +51,12 @@ def increase_score(): for team in scoreboard: if team["id"] == team_id: team["score"] += 1 - + + prev_team = scoreboard[0] + for team in scoreboard: + return jsonify(scoreboard=scoreboard) - +#implement sorting algorithm, change the display: use counting sort if __name__ == '__main__': - app.run(debug = True) - - - - + app.run(debug = True) \ No newline at end of file diff --git a/static/scoreboard.js b/static/scoreboard.js index 34ce2009..480f905b 100644 --- a/static/scoreboard.js +++ b/static/scoreboard.js @@ -23,7 +23,7 @@ function addTeamView(id, name, score){ $("#teams").append(team_template); } -function increase_score(id){ +function increase_score(id){ var team_id = {"id": id} $.ajax({ type: "POST", @@ -32,7 +32,7 @@ function increase_score(id){ contentType: "application/json; charset=utf-8", data : JSON.stringify(team_id), success: function(result){ - + display_scoreboard(result.scoreboard); }, error: function(request, status, error){ console.log("Error"); @@ -41,8 +41,9 @@ function increase_score(id){ console.log(error) } }); + } $(document).ready(function(){ display_scoreboard(scoreboard); -}) +}) \ No newline at end of file From 3ac97690522dc94a243162e0b992d9017b8553cc Mon Sep 17 00:00:00 2001 From: alishachang2 Date: Thu, 15 Jan 2026 10:46:45 -0800 Subject: [PATCH 2/2] finished implementing sort --- server.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 57e0b7ec..31963569 100644 --- a/server.py +++ b/server.py @@ -52,8 +52,14 @@ def increase_score(): if team["id"] == team_id: team["score"] += 1 - prev_team = scoreboard[0] - for team in scoreboard: + for i in range(1, len(scoreboard)): + key = scoreboard[i] + j = i - 1 + while j >= 0 and key["score"] > scoreboard[j]["score"]: + scoreboard[j + 1] = scoreboard[j] + j -= 1 + scoreboard[j + 1] = key + return jsonify(scoreboard=scoreboard)