diff --git a/flask/testapp/views.py b/flask/testapp/views.py index acb5f5c..c7a08da 100644 --- a/flask/testapp/views.py +++ b/flask/testapp/views.py @@ -48,8 +48,9 @@ print("Received choice:", choices) if len(choices) >= user_number: print("集計...") + most_common_option = calculate_majority(choices) # 追記 choices.clear() - return "一番多い選択肢" + return most_common_option # 追記 # ゲーム開始時にユーザー数を出しておき,それぞれに識別番号を振る if request.form.get("originalname"): @@ -70,7 +71,7 @@ # スコアを取得 score = request.form.get("score") data[user_number] = score - + calculate_order(data) print( "Received number:", user_number, @@ -91,3 +92,21 @@ # 必要な処理を行う(例:データベースへの書き込みなど) return "Data received successfully" + + +# 多数派の計算(追記) +def calculate_majority(get_data): + counter = Counter(get_data) + # 最も多く出現する要素を取得する + get_most_common_option = counter.most_common(1)[0][0] + return get_most_common_option + + +# ランキング上位の計算(追記) +def calculate_order(get_data): + sorted_numbers_with_indices = sorted(enumerate(get_data), key=lambda x: x[1], reverse=True) + sorted_indices = [index for index, _ in sorted_numbers_with_indices] + sorted_numbers = [get_data[index] for index in sorted_indices] + + +# 追記(5)