Level 1 - Cơ Bản / Basics
Bài Tập 1: Luỹ Thừa (Power Function)
Đề bài: Viết hàm power(base, exp) trả về base mũ exp (KHÔNG dùng ** hay pow()). Dùng vòng lặp nhân.
Problem: Write a function power(base, exp) that returns base raised to exp (DO NOT use ** or pow()). Use loop multiplication.
Ví dụ:
Enter base: 2
Enter exponent: 5Output:
Result: 32Bài Tập 2: Đếm Số Chữ Số (Count Digits)
Đề bài: Viết hàm count_digits(n) trả về số chữ số của một số nguyên dương n. Ví dụ: 1234 có 4 chữ số. Dùng vòng lặp chia cho 10.
Problem: Write a function count_digits(n) that returns the number of digits in a positive integer n. Example: 1234 has 4 digits. Use loop dividing by 10.
Ví dụ:
Enter a number: 98765Output:
Digits: 5Bài Tập 3: Chuyển Đổi Nhiệt Độ (Temperature Converter)
Đề bài: Viết hàm to_fahrenheit(celsius) trả về nhiệt độ Fahrenheit theo công thức F = C × 9/5 + 32. Nhập nhiệt độ Celsius và in kết quả.
Problem: Write a function to_fahrenheit(celsius) that returns Fahrenheit using formula F = C × 9/5 + 32. Read Celsius from input and print result.
Ví dụ:
Enter Celsius: 100Output:
Fahrenheit: 212.0Bài Tập 4: Tổng Các Chữ Số (Sum of Digits)
Đề bài: Viết hàm sum_digits(n) trả về tổng các chữ số của n. Ví dụ: 123 → 1+2+3 = 6.
Problem: Write a function sum_digits(n) that returns sum of digits of n. Example: 123 → 1+2+3 = 6.
Ví dụ:
Enter a number: 456Output:
Sum of digits: 15Bài Tập 5: Kiểm Tra Chẵn Lẻ (Check Even or Odd)
Đề bài: Viết hàm check_even_odd(n) trả về "Even" nếu n chẵn, "Odd" nếu n lẻ. Nhập số và in kết quả.
Problem: Write a function check_even_odd(n) that returns "Even" if n is even, "Odd" if n is odd. Read number and print result.
Ví dụ:
Enter a number: 7Output:
7 is OddLevel 2 - Trung Bình / Intermediate
Bài Tập 6: Lọc Số Chẵn Trong Danh Sách (Filter Even Numbers from List)
Đề bài: Viết hàm filter_even(numbers) nhận một danh sách số và trả về danh sách mới chỉ chứa các số chẵn.
Problem: Write a function filter_even(numbers) that takes a list and returns a new list containing only even numbers.
Ví dụ:
Enter numbers: 3 8 5 12 7 4 9 2Output:
Even numbers: 8 12 4 2Bài Tập 7: Tìm Min và Max Trong Danh Sách (Find Min and Max in List)
Đề bài: Viết hàm find_min(numbers) và find_max(numbers) trả về giá trị nhỏ nhất và lớn nhất trong danh sách (KHÔNG dùng min(), max() có sẵn). In cả hai.
Problem: Write functions find_min(numbers) and find_max(numbers) returning smallest and largest in list (DO NOT use built-in min(), max()). Print both.
Ví dụ:
Enter numbers: 5 2 9 1 7 3Output:
Min: 1
Max: 9Bài Tập 8: Tổng Danh Sách Bằng Đệ Quy (Recursive Sum of List)
Đề bài: Viết hàm recursive_sum(numbers) tính tổng danh sách bằng ĐỆ QUY. Quy tắc: tổng danh sách = phần tử đầu + tổng phần còn lại. Danh sách rỗng có tổng = 0. Không dùng vòng lặp.
Problem: Write a function recursive_sum(numbers) that calculates list sum using RECURSION. Rule: sum of list = first element + sum of rest. Empty list sum = 0. Do not use loops.
Ví dụ:
Enter numbers: 3 7 2 5 1Output:
Sum: 18Bài Tập 9: Tìm Trung Bình Danh Sách (Average of a List)
Đề bài: Viết hàm average(numbers) nhận danh sách số và trả về trung bình cộng. Nhập dãy số từ bàn phím.
Problem: Write a function average(numbers) that takes a list and returns the average. Read numbers from input.
Ví dụ:
Enter numbers: 10 20 30 40 50Output:
Average: 30.0Bài Tập 10: Kiểm Tra Số Nguyên Tố (Check Prime Number)
Đề bài: Viết hàm is_prime(n) trả về True nếu n là số nguyên tố, False nếu không. In "Prime" hoặc "Not prime".
Problem: Write a function is_prime(n) that returns True if n is prime, False otherwise. Print "Prime" or "Not prime".
Ví dụ:
Enter a number: 17Output:
17 is PrimeLevel 3 - Nâng Cao / Advanced
Bài Tập 11: Sắp Xếp Nổi Bọt (Bubble Sort)
Đề bài: Viết hàm bubble_sort(numbers) sắp xếp danh sách tăng dần bằng thuật toán nổi bọt: so sánh 2 phần tử liền kề, đổi chỗ nếu sai thứ tự, lặp cho đến khi sắp xếp xong. KHÔNG dùng sort() hay sorted().
Problem: Write a function bubble_sort(numbers) that sorts list in ascending order using bubble sort: compare adjacent pairs, swap if wrong order, repeat until sorted. DO NOT use sort() or sorted().
Ví dụ:
Enter numbers: 64 34 25 12 22 11 90Output:
Sorted: 11 12 22 25 34 64 90Bài Tập 12: Giỏ Hàng Mua Sắm (Shopping Cart Calculator)
Đề bài: Viết chương trình giỏ hàng: hàm add_item(cart, name, price, qty) thêm sản phẩm vào giỏ, hàm calculate_total(cart) tính tổng tiền, hàm print_receipt(cart) in hoá đơn. Nhập sản phẩm cho đến khi gõ 'done'.
Problem: Write shopping cart program: add_item(cart, name, price, qty) adds product, calculate_total(cart) calculates total, print_receipt(cart) prints receipt. Input items until 'done'.
Ví dụ:
Item (or 'done'): Apple
Price: 2
Quantity: 3
Item (or 'done'): Milk
Price: 5
Quantity: 1
Item (or 'done'): doneOutput:
=== RECEIPT ===
Apple x3 = 6
Milk x1 = 5
Total: 11Bài Tập 13: Ghép Hai Danh Sách Đã Sắp Xếp (Merge Two Sorted Lists)
Đề bài: Nhập 2 danh sách đã sắp xếp tăng dần. Viết hàm merge_sorted(list1, list2) ghép chúng thành 1 danh sách tăng dần (KHÔNG dùng sort()). Dùng 2 chỉ mục duyệt đồng thời.
Problem: Input 2 sorted (ascending) lists. Write function merge_sorted(list1, list2) that merges them into one sorted list (DO NOT use sort()). Use 2 indices to traverse simultaneously.
Ví dụ:
Enter list 1: 1 3 5 7 9
Enter list 2: 2 4 6 8 10Output:
Merged: 1 2 3 4 5 6 7 8 9 10ĐÁP ÁN / SOLUTIONS
Level 1 - Đáp Án
Solution 1: Luỹ Thừa
Giải thích: Nhân base liên tục exp lần để tính luỹ thừa.
Explanation: Multiply base repeatedly exp times to calculate power.
Code:
pythondef power(base, exp): result = 1 for i in range(exp): result *= base return result b = int(input("Enter base: ")) e = int(input("Enter exponent: ")) print(f"Result: {power(b, e)}")
Solution 2: Đếm Số Chữ Số
Giải thích: Liên tục chia cho 10 và đếm cho đến khi hết chữ số.
Explanation: Repeatedly divide by 10 and count until no digits remain.
Code:
pythondef count_digits(n): if n == 0: return 1 count = 0 while n > 0: count += 1 n = n // 10 return count num = int(input("Enter a number: ")) print(f"Digits: {count_digits(num)}")
Solution 3: Chuyển Đổi Nhiệt Độ
Giải thích: Áp dụng công thức chuyển đổi và return kết quả.
Explanation: Apply conversion formula and return the result.
Code:
pythondef to_fahrenheit(celsius): return celsius * 9/5 + 32 c = int(input("Enter Celsius: ")) print(f"Fahrenheit: {to_fahrenheit(c)}")
Solution 4: Tổng Các Chữ Số
Giải thích: Tách từng chữ số bằng % 10, cộng dồn, rồi bỏ chữ số cuối bằng // 10.
Explanation: Extract each digit with % 10, accumulate, then remove last digit with // 10.
Code:
pythondef sum_digits(n): total = 0 while n > 0: total += n % 10 n = n // 10 return total num = int(input("Enter a number: ")) print(f"Sum of digits: {sum_digits(num)}")
Solution 5: Kiểm Tra Chẵn Lẻ
Giải thích: Dùng phép chia dư để xác định chẵn lẻ, return chuỗi tương ứng.
Explanation: Use modulo to determine even/odd, return corresponding string.
Code:
pythondef check_even_odd(n): if n % 2 == 0: return "Even" else: return "Odd" num = int(input("Enter a number: ")) print(f"{num} is {check_even_odd(num)}")
Level 2 - Đáp Án
Solution 6: Lọc Số Chẵn Trong Danh Sách
Giải thích: Duyệt danh sách, kiểm tra chẵn, thêm vào danh sách mới. Hàm trả về danh sách.
Explanation: Iterate list, check even, append to new list. Function returns a list.
Code:
pythondef filter_even(numbers): result = [] for num in numbers: if num % 2 == 0: result.append(num) return result nums = list(map(int, input("Enter numbers: ").split())) evens = filter_even(nums) print("Even numbers: " + " ".join(map(str, evens)))
Solution 7: Tìm Min và Max Trong Danh Sách
Giải thích: Hai hàm cùng mẫu: duyệt danh sách, so sánh và cập nhật giá trị.
Explanation: Two functions with same pattern: iterate list, compare and update value.
Code:
pythondef find_min(numbers): min_val = numbers[0] for num in numbers: if num < min_val: min_val = num return min_val def find_max(numbers): max_val = numbers[0] for num in numbers: if num > max_val: max_val = num return max_val nums = list(map(int, input("Enter numbers: ").split())) print(f"Min: {find_min(nums)}") print(f"Max: {find_max(nums)}")
Solution 8: Tổng Danh Sách Bằng Đệ Quy
Giải thích: Đệ quy trên danh sách: lấy phần tử đầu + tổng phần còn lại. Dừng khi danh sách rỗng.
Explanation: Recursion on list: take first element + sum of rest. Stops when list is empty.
Code:
pythondef recursive_sum(numbers): if len(numbers) == 0: return 0 return numbers[0] + recursive_sum(numbers[1:]) # How it works: # recursive_sum([3, 7, 2]) # = 3 + recursive_sum([7, 2]) # = 3 + 7 + recursive_sum([2]) # = 3 + 7 + 2 + recursive_sum([]) # = 3 + 7 + 2 + 0 = 12 nums = list(map(int, input("Enter numbers: ").split())) print(f"Sum: {recursive_sum(nums)}")
Solution 9: Tìm Trung Bình Danh Sách
Giải thích: Tính tổng danh sách rồi chia cho số phần tử.
Explanation: Sum the list then divide by number of elements.
Code:
pythondef average(numbers): total = 0 for num in numbers: total += num return total / len(numbers) nums = list(map(int, input("Enter numbers: ").split())) print(f"Average: {average(nums)}")
Solution 10: Kiểm Tra Số Nguyên Tố
Giải thích: Kiểm tra tất cả ước từ 2 đến n-1. Nếu có ước → không phải nguyên tố.
Explanation: Check all divisors from 2 to n-1. If any divides evenly → not prime.
Code:
pythondef is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True num = int(input("Enter a number: ")) if is_prime(num): print(f"{num} is Prime") else: print(f"{num} is Not prime")
Level 3 - Đáp Án
Solution 11: Sắp Xếp Nổi Bọt
Giải thích: So sánh từng cặp liền kề và đổi chỗ nếu sai thứ tự. Lặp đến khi không cần đổi nữa.
Explanation: Compare each adjacent pair and swap if in wrong order. Repeat until no swaps needed.
Code:
pythondef bubble_sort(numbers): n = len(numbers) for i in range(n - 1): for j in range(n - 1 - i): if numbers[j] > numbers[j + 1]: temp = numbers[j] numbers[j] = numbers[j + 1] numbers[j + 1] = temp return numbers nums = list(map(int, input("Enter numbers: ").split())) result = bubble_sort(nums) print("Sorted: " + " ".join(map(str, result)))
Solution 12: Giỏ Hàng Mua Sắm
Giải thích: Nhiều hàm phối hợp: thêm sản phẩm, tính tổng, in hoá đơn. Danh sách lồng nhau [name, price, qty].
Explanation: Multiple functions collaborate: add items, calculate total, print receipt. Nested list [name, price, qty].
Code:
pythondef add_item(cart, name, price, qty): cart.append([name, price, qty]) def calculate_total(cart): total = 0 for item in cart: total += item[1] * item[2] return total def print_receipt(cart): print("=== RECEIPT ===") for item in cart: line_total = item[1] * item[2] print(f"{item[0]} x{item[2]} = {line_total}") print(f"Total: {calculate_total(cart)}") cart = [] while True: name = input("Item (or 'done'): ") if name == "done": break price = int(input("Price: ")) qty = int(input("Quantity: ")) add_item(cart, name, price, qty) print_receipt(cart)
Solution 13: Ghép Hai Danh Sách Đã Sắp Xếp
Giải thích: Duyệt đồng thời 2 danh sách bằng 2 chỉ mục. Luôn lấy phần tử nhỏ hơn. Thêm phần còn lại khi 1 danh sách hết.
Explanation: Traverse both lists simultaneously with 2 indices. Always pick smaller element. Append remainder when one list is exhausted.
Code:
pythondef merge_sorted(list1, list2): result = [] i = 0 j = 0 while i < len(list1) and j < len(list2): if list1[i] <= list2[j]: result.append(list1[i]) i += 1 else: result.append(list2[j]) j += 1 while i < len(list1): result.append(list1[i]) i += 1 while j < len(list2): result.append(list2[j]) j += 1 return result list1 = list(map(int, input("Enter list 1: ").split())) list2 = list(map(int, input("Enter list 2: ").split())) merged = merge_sorted(list1, list2) print("Merged: " + " ".join(map(str, merged)))