初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題433
from itertools import permutations
def all_combinations(lst):
return list(permutations(lst))
numbers = [1, 2, 3]
print(all_combinations(numbers)) # [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
問題434
def median(lst):
lst.sort()
n = len(lst)
if n % 2 == 1:
return lst[n // 2]
else:
return (lst[n // 2 – 1] + lst[n // 2]) / 2
numbers = [5, 1, 3, 8, 2]
print(median(numbers)) # 3
問題435
def all_even(lst):
return all(x % 2 == 0 for x in lst)
numbers = [2, 4, 6]
print(all_even(numbers)) # True
numbers = [2, 3, 6]
print(all_even(numbers)) # False



コメント