初心者向けのPythonのプログラミング問題です。入門編としてチャレンジしてください。Pythonの正答例は以下になります。
問題730
def merge_two_sorted_lists(a, b):
result = []
i = j = 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(a[i]); i += 1
else:
result.append(b[j]); j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
# 呼び出し例
a = [1, 3, 5]
b = [2, 4, 6]
print(merge_two_sorted_lists(a, b)) # 出力: [1, 2, 3, 4, 5, 6]
問題731
def unique_sorted(lst):
return sorted(set(lst))
# 呼び出し例
lst = [4, 1, 2, 4, 3, 2]
print(unique_sorted(lst)) # 出力: [1, 2, 3, 4]
問題732
import heapq
def heap_sort(lst):
heapq.heapify(lst)
return [heapq.heappop(lst) for _ in range(len(lst))]
# 呼び出し例
lst = [9, 1, 4, 7, 3]
print(heap_sort(lst)) # 出力: [1, 3, 4, 7, 9]



コメント