Bài tập Recursion - Nâng cao
Bài tập về Đệ quy (Recursion) - Nâng cao
def flatten(lst):
# [1, [2, [3, 4], 5], 6] -> [1, 2, 3, 4, 5, 6]
pass
# Test
nested = [1, [2, [3, 4], 5], 6, [7, [8, 9]]]
print(flatten(nested))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]def sum_nested(lst):
pass
# Test
nested = [1, [2, 3, [4, 5]], 6, [7, [8, 9]]]
print(sum_nested(nested)) # 45def binary_search(lst, target, left=0, right=None):
# Trả về index của target, hoặc -1 nếu không tìm thấy
pass
# Test
numbers = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(binary_search(numbers, 7)) # 3
print(binary_search(numbers, 11)) # 5
print(binary_search(numbers, 20)) # -1Last updated