Untitled
unknown
python
2 years ago
2.1 kB
14
Indexable
# HW3_List_processing_Function (ไม่ลบหรือแก้ไขบรรทัดนี้ หรือเพิ่มอะไรก่อนบรรทัดนี้ โดยเด็ดขาด) # - เขียนในเซลล์นี้เท่านั้น # - ถ้าต้องการเขียนฟังก์ชันเพิ่ม ก็เขียนในเซลล์นี้ def grade_to_score(x): if (x=="A"): return 4 elif (x=="B"): return 3 elif (x=="C"): return 2 elif (x=="D"): return 1 elif (x=="F"): return 0 def get_weighted_score(weights, grades): sum = 0 for i in range(len(grades)): sum += grade_to_score(grades[i])*weights[i] return sum # --------------------------------------------------- def student_sorting(stu_weight_scores, n): max = [] out = [] for i in range (1,len(stu_weight_scores),2): max.append(stu_weight_scores[i]) while(len(max)>n): max.remove(min(max)) for i in range(1,len(stu_weight_scores),2): if stu_weight_scores[i] in max: out.append(stu_weight_scores[i-1]) return sorted(out) # --------------------------------------------------- def test(): # ตัวตรวจจะไม่สนใจการทำงานใดๆใน ฟังก์ชันนี้ นิสิตสามารถเพิ่มโค้ดในนี้เพื่อทดสอบฟังก์ชัน # ที่เขียนมาข้างบนได้ # ตัวอย่างการเขียนโค้ดทดสอบฟังก์ชัน get_weighted_score my_weighted_score = get_weighted_score([1,1,9,1,1,1,1], 'AABAAAA') print(my_weighted_score) # ตัวอย่างการเขียนโค้ดทดสอบฟังก์ชัน student_sorting S = ["stu04",48,"stu10",52,"stu22",40,"stu19",35,"stu23",44,"stu07",59,"stu40",57,"stu05",36] n_stu = 5 selected_students = student_sorting(S, n_stu) print(selected_students) test()
Editor is loading...