初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題709
def sort_students_by_score(lst):
return sorted(lst, key=lambda x: x[1], reverse=True)
# 呼び出し例
students = [(“Alice”, 80), (“Bob”, 90), (“Charlie”, 85)]
print(sort_students_by_score(students))
# 出力: [(‘Bob’, 90), (‘Charlie’, 85), (‘Alice’, 80)]
問題710
from collections import Counter
def mode(lst):
count = Counter(lst)
return count.most_common(1)[0][0]
# 呼び出し例
lst = [1, 2, 2, 3, 3, 3, 4]
print(mode(lst)) # 出力: 3
問題711
import bisect
def exists_in_sorted(lst, value):
lst_sorted = sorted(lst)
index = bisect.bisect_left(lst_sorted, value)
return index < len(lst_sorted) and lst_sorted[index] == value
# 呼び出し例
lst = [8, 3, 6, 1, 4]
print(exists_in_sorted(lst, 6)) # 出力: True



コメント