PythonプログラミングノックDay144 初心者向けLv.2 #プログラミング #python #初心者

初心者

初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。

問題430
from collections import Counter

def most_frequent(lst):
count = Counter(lst)
return count.most_common(1)[0][0]

numbers = [1, 2, 3, 3, 4, 4, 4, 5]
print(most_frequent(numbers)) # 4

問題431
def multiply_lists(list1, list2):
return [x * y for x, y in zip(list1, list2)]

list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(multiply_lists(list1, list2)) # [4, 10, 18]

問題432
def find_pairs(lst, target):
seen = set()
pairs = []
for num in lst:
complement = target – num
if complement in seen:
pairs.append((complement, num))
seen.add(num)
return pairs

numbers = [10, 20, 30, 40, 50, 60]
target = 70
print(find_pairs(numbers, target)) # [(20, 50), (30, 40)]

コメント

タイトルとURLをコピーしました