初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題721
def filter_and_sort_words(lst):
return sorted([w for w in lst if len(w) ≥ 3], key=len)
# 呼び出し例
lst = [“hi”, “apple”, “on”, “banana”, “pie”]
print(filter_and_sort_words(lst)) # 出力: [‘pie’, ‘apple’, ‘banana’]
問題722
def min_difference(lst):
lst_sorted = sorted(lst)
return min(lst_sorted[i+1] – lst_sorted[i] for i in range(len(lst_sorted)-1))
# 呼び出し例
lst = [8, 1, 4, 10, 6]
print(min_difference(lst)) # 出力: 2
問題723
from collections import Counter
def most_common_char(s):
count = Counter(s)
return count.most_common(1)[0][0]
# 呼び出し例
s = “programming”
print(most_common_char(s)) # 出力: “g”



コメント