Bài tập Recursion - Cơ bản
- Viết hàm đệ quy
countdown(n)đếm ngược từ n về 0 và in ra mỗi số.
def countdown(n): # Code của bạn ở đây pass
# Testcountdown(5)# Output:# 5# 4# 3# 2# 1# 0- Viết hàm đệ quy
count_up(n)đếm từ 1 đến n và in ra mỗi số.
def count_up(n): # Code của bạn ở đây pass
# Testcount_up(5)# Output:# 1# 2# 3# 4# 5- Viết hàm đệ quy
factorial(n)tính giai thừa của n.
def factorial(n): # n! = n * (n-1) * ... * 1 # 5! = 5 * 4 * 3 * 2 * 1 = 120 pass
# Testprint(factorial(5)) # 120print(factorial(0)) # 1print(factorial(3)) # 6💡 Base case: n = 0 hoặc n = 1, trả về 1
- Viết hàm đệ quy
sum_to_n(n)tính tổng các số từ 1 đến n.
def sum_to_n(n): # 1 + 2 + 3 + ... + n pass
# Testprint(sum_to_n(5)) # 15 (1+2+3+4+5)print(sum_to_n(10)) # 55- Viết hàm đệ quy
power(base, exp)tính base mũ exp.
def power(base, exp): # base^exp # 2^3 = 2 * 2 * 2 = 8 pass
# Testprint(power(2, 3)) # 8print(power(5, 2)) # 25print(power(3, 0)) # 1- Viết hàm đệ quy
fibonacci(n)tính số Fibonacci thứ n.
def fibonacci(n): # fib(0) = 0, fib(1) = 1 # fib(n) = fib(n-1) + fib(n-2) pass
# Testprint(fibonacci(0)) # 0print(fibonacci(1)) # 1print(fibonacci(6)) # 8print(fibonacci(10)) # 55- Viết hàm đệ quy
sum_digits(n)tính tổng các chữ số của một số nguyên.
def sum_digits(n): # 123 -> 1 + 2 + 3 = 6 pass
# Testprint(sum_digits(123)) # 6print(sum_digits(999)) # 27print(sum_digits(5)) # 5💡 Gợi ý: Dùng n % 10 để lấy chữ số cuối, n // 10 để bỏ chữ số cuối
- Viết hàm đệ quy
count_digits(n)đếm số lượng chữ số của một số nguyên.
def count_digits(n): # 123 -> 3 chữ số pass
# Testprint(count_digits(123)) # 3print(count_digits(99999)) # 5print(count_digits(5)) # 1- Viết hàm đệ quy
reverse_number(n)đảo ngược một số nguyên.
def reverse_number(n): # 123 -> 321 pass
# Testprint(reverse_number(123)) # 321print(reverse_number(5040)) # 405- Viết hàm đệ quy
sum_list(lst)tính tổng các phần tử trong list.
def sum_list(lst): # [1, 2, 3, 4, 5] -> 15 pass
# Testprint(sum_list([1, 2, 3, 4, 5])) # 15print(sum_list([10, 20, 30])) # 60print(sum_list([])) # 0💡 Base case: list rỗng trả về 0
- Viết hàm đệ quy
find_max(lst)tìm số lớn nhất trong list.
def find_max(lst): pass
# Testprint(find_max([3, 7, 2, 9, 1])) # 9print(find_max([5, 5, 5])) # 5print(find_max([100])) # 100- Viết hàm đệ quy
find_min(lst)tìm số nhỏ nhất trong list.
def find_min(lst): pass
# Testprint(find_min([3, 7, 2, 9, 1])) # 1print(find_min([5, 5, 5])) # 5print(find_min([100])) # 100- Viết hàm đệ quy
reverse_string(s)đảo ngược một chuỗi.
def reverse_string(s): pass
# Testprint(reverse_string("hello")) # "olleh"print(reverse_string("Python")) # "nohtyP"print(reverse_string("a")) # "a"- Viết hàm đệ quy
is_palindrome(s)kiểm tra chuỗi có phải palindrome không.
def is_palindrome(s): # Palindrome: đọc xuôi ngược như nhau pass
# Testprint(is_palindrome("radar")) # Trueprint(is_palindrome("hello")) # Falseprint(is_palindrome("level")) # Trueprint(is_palindrome("a")) # True- Viết hàm đệ quy
count_char(s, char)đếm số lần xuất hiện của ký tự trong chuỗi.
def count_char(s, char): pass
# Testprint(count_char("hello", "l")) # 2print(count_char("programming", "m")) # 2print(count_char("python", "z")) # 0- Viết hàm đệ quy
multiply(a, b)nhân hai số dương bằng cách cộng lặp đi lặp lại.
def multiply(a, b): # 3 * 4 = 3 + 3 + 3 + 3 = 12 # Sử dụng đệ quy, không dùng toán tử * pass
# Testprint(multiply(3, 4)) # 12print(multiply(5, 6)) # 30print(multiply(7, 0)) # 0- Viết hàm đệ quy
gcd(a, b)tính ước số chung lớn nhất của hai số (Euclidean algorithm).
def gcd(a, b): # Greatest Common Divisor # gcd(48, 18) = 6 pass
# Testprint(gcd(48, 18)) # 6print(gcd(100, 50)) # 50print(gcd(17, 13)) # 1💡 Gợi ý: gcd(a, b) = gcd(b, a % b), base case: b = 0
- Viết hàm đệ quy
list_length(lst)tính độ dài của list (không dùng hàm len()).
def list_length(lst): pass
# Testprint(list_length([1, 2, 3, 4, 5])) # 5print(list_length([])) # 0print(list_length(["a", "b"])) # 2- Viết hàm đệ quy
contains(lst, item)kiểm tra item có trong list không.
def contains(lst, item): pass
# Testprint(contains([1, 2, 3, 4], 3)) # Trueprint(contains([1, 2, 3, 4], 5)) # Falseprint(contains([], 1)) # False- Viết hàm đệ quy
print_list(lst)in từng phần tử của list (mỗi phần tử một dòng).
def print_list(lst): pass
# Testprint_list([1, 2, 3, 4, 5])# Output:# 1# 2# 3# 4# 5