Level 1 - Cơ Bản / Basics
Bài Tập 1: Chào Hỏi Theo Tên (Personalized Greeting)
Đề bài: Viết hàm greet(name) in ra "Hello, <name>! Welcome back." Nhập tên từ bàn phím và gọi hàm.
Problem: Write a function greet(name) that prints "Hello, <name>! Welcome back." Read name from input and call the function.
Ví dụ:
Enter your name: AliceOutput:
Hello, Alice! Welcome back.Bài Tập 2: Số Lớn Hơn Trong Hai Số (Max of Two Numbers)
Đề bài: Viết hàm max_of_two(a, b) trả về số lớn hơn trong hai số. Nhập 2 số và in số lớn hơn.
Problem: Write a function max_of_two(a, b) that returns the larger of two numbers. Read 2 numbers and print the larger one.
Ví dụ:
Enter first number: 12
Enter second number: 8Output:
Max: 12Bài Tập 3: 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 4: Đế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 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: 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 PrimeBài Tập 7: 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 8: Giai Thừa (Factorial)
Đề bài: Viết hàm factorial(n) trả về n! (n giai thừa). Ví dụ: 5! = 5×4×3×2×1 = 120.
Problem: Write a function factorial(n) that returns n! (n factorial). Example: 5! = 5×4×3×2×1 = 120.
Ví dụ:
Enter a number: 5Output:
5! = 120Bài Tập 9: Tính Giảm Giá (Discount Calculator)
Đề bài: Viết hàm calculate_total(price, quantity) tính tổng tiền sau giảm giá: mua ≥10 giảm 20%, mua ≥5 giảm 10%, còn lại không giảm. In mức giảm và tổng tiền.
Problem: Write a function calculate_total(price, quantity) that calculates total after discount: buy ≥10 → 20% off, buy ≥5 → 10% off, else no discount. Print discount and total.
Ví dụ:
Enter price per item: 100
Enter quantity: 7Output:
Discount: 10%
Total: 630Bài Tập 10: 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.0Level 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: Hệ Thống Ngân Hàng Mini (Mini Banking System)
Đề bài: Viết chương trình ngân hàng: hàm deposit(balance, amount) nạp tiền và trả về số dư mới, hàm withdraw(balance, amount) rút tiền (kiểm tra đủ số dư), hàm show_balance(balance) hiển thị. Chạy menu cho đến khi chọn thoát.
Problem: Write banking program: deposit(balance, amount) returns new balance, withdraw(balance, amount) with check, show_balance(balance) displays. Run menu loop until exit.
Ví dụ:
Choose (1/2/3/4): 1
Amount: 1000
Choose (1/2/3/4): 2
Amount: 300
Choose (1/2/3/4): 3
Choose (1/2/3/4): 4Output:
Deposited 1000. Balance: 1000
Withdrew 300. Balance: 700
Current balance: 700
Goodbye!Bà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: Chào Hỏi Theo Tên
Giải thích: Hàm nhận tên và in câu chào. Đây là hàm void (không return).
Explanation: Function takes a name and prints a greeting. This is a void function (no return).
Code:
pythondef greet(name): print(f"Hello, {name}! Welcome back.") name = input("Enter your name: ") greet(name)
Solution 2: Số Lớn Hơn Trong Hai Số
Giải thích: So sánh hai số và trả về số lớn hơn.
Explanation: Compare two numbers and return the larger one.
Code:
pythondef max_of_two(a, b): if a > b: return a else: return b a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print(f"Max: {max_of_two(a, b)}")
Solution 3: 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 4: Đế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 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: 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")
Solution 7: 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 8: Giai Thừa
Giải thích: Nhân dồn từ 1 đến n. Xử lý trường hợp 0! = 1.
Explanation: Accumulate product from 1 to n. Handle 0! = 1 case.
Code:
pythondef factorial(n): result = 1 for i in range(1, n + 1): result *= i return result num = int(input("Enter a number: ")) print(f"{num}! = {factorial(num)}")
Solution 9: Tính Giảm Giá
Giải thích: Xác định mức giảm giá theo số lượng, áp dụng vào tổng tiền.
Explanation: Determine discount rate by quantity, apply to total price.
Code:
pythondef calculate_total(price, quantity): total = price * quantity if quantity >= 10: discount = 20 elif quantity >= 5: discount = 10 else: discount = 0 final = total - total * discount // 100 print(f"Discount: {discount}%") print(f"Total: {final}") p = int(input("Enter price per item: ")) q = int(input("Enter quantity: ")) calculate_total(p, q)
Solution 10: 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)}")
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: Hệ Thống Ngân Hàng Mini
Giải thích: Hàm trả về balance mới — minh hoạ return để cập nhật trạng thái. Menu loop thực tế.
Explanation: Functions return new balance — demonstrates using return to update state. Practical menu loop.
Code:
pythondef deposit(balance, amount): balance += amount print(f"Deposited {amount}. Balance: {balance}") return balance def withdraw(balance, amount): if amount > balance: print(f"Not enough money! Balance: {balance}") return balance balance -= amount print(f"Withdrew {amount}. Balance: {balance}") return balance def show_balance(balance): print(f"Current balance: {balance}") balance = 0 while True: print("1-Deposit 2-Withdraw 3-Balance 4-Exit") choice = input("Choose (1/2/3/4): ") if choice == "1": amount = int(input("Amount: ")) balance = deposit(balance, amount) elif choice == "2": amount = int(input("Amount: ")) balance = withdraw(balance, amount) elif choice == "3": show_balance(balance) elif choice == "4": print("Goodbye!") break
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)))