From 698342588355f795dcbbc273ca353bff8220356e Mon Sep 17 00:00:00 2001 From: 16519111 Date: Wed, 28 Jul 2021 06:40:35 +0700 Subject: [PATCH] Final commit --- app.py | 190 ++++++++++++++++++++++ html/options.html | 38 +++++ models/__pycache__/sorts.cpython-39.pyc | Bin 0 -> 1530 bytes models/sorts.py | 38 +++++ test/data.csv | 206 ++++++++++++++++++++++++ 5 files changed, 472 insertions(+) create mode 100644 app.py create mode 100644 html/options.html create mode 100644 models/__pycache__/sorts.cpython-39.pyc create mode 100644 models/sorts.py create mode 100644 test/data.csv diff --git a/app.py b/app.py new file mode 100644 index 0000000..c1d1af2 --- /dev/null +++ b/app.py @@ -0,0 +1,190 @@ +from models.sorts import database +from flask import Flask, request, render_template +import io +import csv +import time + +app = Flask(__name__) + + +@app.route("/") +def main(): + return "Tugas Seleksi IRK 2021" + +@app.route("/sort/selection", methods=["POST"]) +def selectionSort(): + file = request.files["file"] + column = request.form["column"] + orientation = request.form["orientation"] + stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None) + csv_input = csv.reader(stream) + final_table = [row for row in csv_input] + column_data = [row[int(column)] for row in final_table if len(row) > int(column)] + + # Preprocess + numCount = 0 + strCount = 0 + for data in column_data: + if data.isnumeric(): + numCount = numCount + 1 + else: + strCount = strCount + 1 + + if (numCount >= strCount): + column_data = [data for data in column_data if data.isnumeric()] + else: + column_data = [data for data in column_data if not data.isnumeric()] + + # Selection Sort + result = [] + start_time = time.time() + if (orientation == "ASC"): + while (len(column_data) != 0): + minimum = column_data[0] + for i in range(1,len(column_data)): + if (column_data[i] < minimum): + minimum = column_data[i] + result.append(minimum) + column_data.remove(minimum) + else: + while (len(column_data) != 0): + maximum = column_data[0] + for i in range(1,len(column_data)): + if (column_data[i] > maximum): + maximum = column_data[i] + result.append(maximum) + column_data.remove(maximum) + exec_time = time.time() - start_time + + print(result) + + params = { + "algorithm": "selection", + "result": ",".join(result), + "exec_time": str(exec_time) + } + + mysqldb.postSort(**params) + + for i in range(len(final_table)): + if(len(final_table[i]) > int(column) and len(result) > i): + final_table[i][int(column)] = result[i] + if(len(result) <= i and len(final_table[i]) > int(column)): + final_table[i][int(column)] = None + + htmlResult = """""" + for row in final_table: + htmlResult = htmlResult + """""" + for col in row: + if (col is not None): + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """
""" + htmlResult = htmlResult + str(col) + htmlResult = htmlResult + """
""" + + return htmlResult + +@app.route("/sort/insertion", methods=["POST"]) +def insertionSort(): + file = request.files["file"] + column = request.form["column"] + orientation = request.form["orientation"] + stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None) + csv_input = csv.reader(stream) + final_table = [row for row in csv_input] + column_data = [row[int(column)] for row in final_table if len(row) > int(column)] + + # Preprocess + numCount = 0 + strCount = 0 + for data in column_data: + if data.isnumeric(): + numCount = numCount + 1 + else: + strCount = strCount + 1 + + if (numCount >= strCount): + column_data = [data for data in column_data if data.isnumeric()] + else: + column_data = [data for data in column_data if not data.isnumeric()] + + # Insertion Sort + result = column_data + start_time = time.time() + if (orientation == "ASC"): + for i in range(1, len(result)): + pivot = result[i] + j = i - 1 + while j >= 0 and pivot < result[j] : + result[j + 1] = result[j] + j -= 1 + result[j + 1] = pivot + else: + for i in range(1, len(result)): + pivot = result[i] + j = i - 1 + while j >= 0 and pivot > result[j] : + result[j + 1] = result[j] + j -= 1 + result[j + 1] = pivot + exec_time = time.time() - start_time + + params = { + "algorithm": "insertion", + "result": ",".join(result), + "exec_time": str(exec_time) + } + + print(result) + + mysqldb.postSort(**params) + + for i in range(len(final_table)): + if(len(final_table[i]) > int(column) and len(result) > i): + final_table[i][int(column)] = result[i] + if(len(result) <= i and len(final_table[i]) > int(column)): + final_table[i][int(column)] = None + + print(final_table) + + htmlResult = """""" + for row in final_table: + htmlResult = htmlResult + """""" + for col in row: + if (col is not None): + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """""" + htmlResult = htmlResult + """
""" + htmlResult = htmlResult + str(col) + htmlResult = htmlResult + """
""" + + return htmlResult + + +@app.route("/sort/result", methods=["GET"]) +def getResult(): + if "id" in request.args.keys(): + sort_id = request.args["id"] + response = mysqldb.getSort(sort_id) + else: + response = mysqldb.getSort() + + response_dict = { + "id": response[0], + "date_time": response[1], + "algorithm": response[2], + "result": response[3], + "exec_time": response[4] + } + return response_dict + +if __name__ == "__main__": + mysqldb = database() + if mysqldb.db.is_connected(): + print('Connected to MySQL database') + + app.run(debug=True) + + if mysqldb.db is not None and mysqldb.db.is_connected(): + mysqldb.db.close() \ No newline at end of file diff --git a/html/options.html b/html/options.html new file mode 100644 index 0000000..ec5588e --- /dev/null +++ b/html/options.html @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
365
443
237
1
2
3
4
5
+ + + \ No newline at end of file diff --git a/models/__pycache__/sorts.cpython-39.pyc b/models/__pycache__/sorts.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..599ed1df92e825fc669b0079a6ac96d740d75531 GIT binary patch literal 1530 zcmZvcTW{Mo6vs)4l4Z|H+pN2E0|o*EmTHKDbm&vN4Qtn7GlCggT_BhcKvP5}(WZ+_ z(rfM5eraC!9pcA*CB5!xUtz$|okPWj3siy*Z-><3|98k_YpX-x*;%~)^}a{Q-`LoE zTxfg)pZ)}lAcE$kpb@3ydm|bD)l~e+!@f0nGG$1My*0 zf(h?WHX|`TBIO<<#+f{(WDZeJZyR?;UqS7N`~v%#(*-R%O?f~Lpa=fw52AKnrAdBX zX%qRnQbwHAOjVVM0+VDqDNPcxh~c=xJ=JWirHX=4qVt zYj32o(u7XUw9HRyD*qzt9q{}2=O+)2pY(rxH01qfkB^5YE0W`(%;oPothU7?~f*R5XV`WnK)+f1AP+=p$=v6!TVpAZPV$k zm9G0M-!}w?bg%*+;sTdorcVu-Q98h zohvqTm#bOck{|%Y3vm3&0^l$Uhs@Xp9JXK<4wM^k>UNT!RVp**#q=;ML3qPK){5J` z^SkTOJ+7o4=Vp(~mokk(RT=U>l6)*Rzq@;JFyGzdyBGJcxcFio)_&t&x7Y5|?S8ZV zaGa)6>#(H)5JLH2YCjz-_+{;%R;ox$-NI>OBO^bpiXt*USx|)}cOqgW+n4;lKj){aDy|v95s`|YOgLnqv|zZ9I^}r=y77Atr=PxU){Aqg zB+mr@iUYid00VbtB~?3EdlyU;oJy0PSEaQ2TVA2xKyWhA0Pq^^Z{Vc20cf2`tN+K~ zpl^dg{W~pPy^@kPbp0PhOc6GLGv`9K*33=UDH?06fW?5rtsYduTpD zgO|XD!6dNv3f?UmHyO~N9cu9o<;PF52YNmrWR7