算法设计历史版本

main
Zhuang 1 month ago
parent 7da6212489
commit 038dcbc2b5

@ -0,0 +1,53 @@
import pandas as pd
import random
# 读取学生名单 Excel 文件
def read_student_list(file_path):
df = pd.read_excel(file_path)
df['积分'] = 0 # 初始化每个学生的积分为0
return df
# 学生名单保存在 students.xlsx 文件中
students = read_student_list('students.xlsx')
print(students)
# 点名规则:按积分计算选中的概率
def weighted_random_choice(df):
total_weight = sum([1 / (1 + score) for score in df['积分']])
r = random.uniform(0, total_weight)
upto = 0
for i, row in df.iterrows():
weight = 1 / (1 + row['积分'])
if upto + weight >= r:
return i # 返回被点名的学生索引
upto += weight
# 更新积分
def update_score(df, index, answer_correctness):
df.loc[index, 'score'] += 1 # 点到一次+1
# 根据回答情况调整积分
if answer_correctness == 'wrong':
df.loc[index, 'score'] -= 1
elif answer_correctness == 'partial':
df.loc[index, 'score'] += 1
elif answer_correctness == 'correct':
df.loc[index, 'score'] += 3
# 示例:更新一个被选中学生的积分
selected_index = weighted_random_choice(students)
update_score(students, selected_index, 'correct')
# 保存更新后的名单
def save_to_excel(df):
df.to_excel("updated_students.xlsx", index=False)
# 示例
save_to_excel(students)
# 主循环,随机点名并更新积分
def classroom_roll_call(file_path, num_sessions):
for _ in range(num_sessions):
selected_index = weighted_random_choice(students)
# 模拟回答情况
answer = random.choice(['wrong', 'partial', 'correct'])
update_score(students, selected_index, answer)
# 示例进行10次点名
classroom_roll_call('students.xlsx', 10)

@ -0,0 +1,70 @@
import pandas as pd
import random
# 加载学生名单从Excel文件导入
def load_students(file_path):
df = pd.read_excel(file_path)
# Excel文件有两列'姓名' 和 '学号'
students = df.to_dict('records')
for student in students:
student['积分'] = 0 # 初始化每位学生的积分为0
return students
# 保存学生名单更新积分后导出到Excel
def save_students(students, file_path):
df = pd.DataFrame(students)
df.to_excel(file_path, index=False)
# 根据积分设置每个学生的被选概率,积分越高,权重越低
def get_student_weights(students):
max_score = max([s['积分'] for s in students])
weights = [(max_score - s['积分'] + 1) for s in students] # 权重为 max_score - 当前积分 + 1
return weights
# 随机点名
def pick_student(students):
weights = get_student_weights(students)
picked_student = random.choices(students, weights=weights, k=1)[0]
print(f"点到学生: {picked_student['姓名']} (学号: {picked_student['学号']})")
return picked_student
# 增加到达课堂的积分
def update_attendance_score(student):
student['积分'] += 1
print(f"{student['姓名']} 到达课堂,积分 +1总积分: {student['积分']}")
# 处理回答问题的积分
def update_answer_score(student, repeated_question_correctly, answer_score):
if repeated_question_correctly:
student['积分'] += 0.5
print(f"{student['姓名']} 正确重复问题,积分 +0.5")
else:
student['积分'] -= 1
print(f"{student['姓名']} 未能正确重复问题,积分 -1")
student['积分'] += answer_score
print(f"{student['姓名']} 回答问题,加 {answer_score} 分,总积分: {student['积分']}")
def main():
# Excel 文件路径
file_path = 'students.xlsx'
# 加载学生数据
students = load_students(file_path)
# 随机点名
picked_student = pick_student(students)
# 假设学生到达课堂 客户端反馈是否调用函数
update_attendance_score(picked_student)
# 假设学生回答问题 客户端反馈具体答题情况再调用函数
repeated_correctly = True # 假设学生正确重复了问题
answer_score = 2.0 # 假设学生回答问题的得分是2分
update_answer_score(picked_student, repeated_correctly, answer_score)
# 保存更新后的数据回到 Excel 文件
save_students(students, file_path)
if __name__ == '__main__':
main()

@ -0,0 +1,100 @@
from flask import Flask, request, jsonify
import pandas as pd
import random
app = Flask(__name__)
# 加载学生名单从Excel文件导入
def load_students(file_path):
df = pd.read_excel(file_path)
students = df.to_dict('records')
return students
# 保存学生名单更新积分后导出到Excel
def save_students(students, file_path):
df = pd.DataFrame(students)
df.to_excel(file_path, index=False)
# 根据积分设置每个学生的被选概率,积分越高,权重越低
def get_student_weights(students):
max_score = max([s['积分'] for s in students])
weights = [(max_score - s['积分'] + 1) for s in students]
return weights
# 随机点名
def pick_student(students):
weights = get_student_weights(students)
picked_student = random.choices(students, weights=weights, k=1)[0]
return picked_student
# 增加到达课堂的积分
def update_attendance_score(student):
student['积分'] += 1
return student['积分']
# 处理回答问题的积分
def update_answer_score(student, repeated_question_correctly, answer_score):
if repeated_question_correctly:
student['积分'] += 0.5
else:
student['积分'] -= 1
student['积分'] += answer_score
return student['积分']
# 文件路径
file_path = 'students.xlsx'
@app.route('/get_random_student', methods=['GET'])
def get_random_student():
students = load_students(file_path)
picked_student = pick_student(students)
return jsonify({'姓名':picked_student['姓名'], '学号':picked_student['学号']})
@app.route('/update_attendance_score_route', methods=['POST'])
def update_attendance_score_route():
data = request.json #确定学生到达课堂后 前端传递学号回来
student_id = data['学号']
students = load_students(file_path)
for student in students:
if student['学号'] == student_id:
updated_score = update_attendance_score(student)
break
save_students(students, file_path)
return jsonify({"学号": student_id, "积分": updated_score})
@app.route('/update_answer_score_route', methods=['POST'])
def update_answer_score_route():
data = request.json
student_id = data['学号']
repeated_correctly = data['repeated_correctly']
answer_score = data['answer_score']
students = load_students(file_path)
for student in students:
if student['学号'] == student_id:
updated_score = update_answer_score(student, repeated_correctly, answer_score)
break
save_students(students, file_path)
return jsonify({"学号": student_id, "积分": updated_score})
if __name__ == '__main__':
app.run(debug=True)
Loading…
Cancel
Save