Level 1 - Cơ Bản / Basics

Bài Tập 1: 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: 100

Output:

Fahrenheit: 212.0

Bài Tập 2: 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: Alice

Output:

Hello, Alice! Welcome back.

Bài Tập 3: 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: 456

Output:

Sum of digits: 15

Bài Tập 4: Giá Trị Tuyệt Đối (Absolute Value)

Đề bài: Viết hàm my_abs(n) trả về giá trị tuyệt đối của n (KHÔNG dùng hàm abs() có sẵn). Nhập số và in kết quả.

Problem: Write a function my_abs(n) that returns absolute value of n (DO NOT use built-in abs()). Read number and print result.

Ví dụ:

Enter a number: -15

Output:

Absolute value: 15

Bài Tập 5: Đế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: 98765

Output:

Digits: 5

Level 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: 17

Output:

17 is Prime

Bà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 3

Output:

Min: 1
Max: 9

Bài Tập 8: 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 2

Output:

Even numbers: 8 12 4 2

Bài Tập 9: Tìm Tất Cả Số Nguyên Tố Đến N (Find All Primes Up To N)

Đề bài: Viết hàm is_prime(n) kiểm tra nguyên tố. Viết hàm find_primes(limit) dùng is_prime() để tìm tất cả số nguyên tố từ 2 đến limit.

Problem: Write a function is_prime(n) to check prime. Write find_primes(limit) that uses is_prime() to find all primes from 2 to limit.

Ví dụ:

Enter limit: 20

Output:

Primes: 2 3 5 7 11 13 17 19

Bài Tập 10: 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: 5

Output:

5! = 120

Level 3 - Nâng Cao / Advanced

Bài Tập 11: 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 10

Output:

Merged: 1 2 3 4 5 6 7 8 9 10

Bà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'): done

Output:

=== RECEIPT ===
Apple x3 = 6
Milk x1 = 5
Total: 11

Bài Tập 13: 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): 4

Output:

Deposited 1000. Balance: 1000
Withdrew 300. Balance: 700
Current balance: 700
Goodbye!

ĐÁP ÁN / SOLUTIONS

Level 1 - Đáp Án

Solution 1: 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:

python
def to_fahrenheit(celsius): return celsius * 9/5 + 32 c = int(input("Enter Celsius: ")) print(f"Fahrenheit: {to_fahrenheit(c)}")

Solution 2: 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:

python
def greet(name): print(f"Hello, {name}! Welcome back.") name = input("Enter your name: ") greet(name)

Solution 3: 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:

python
def 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 4: Giá Trị Tuyệt Đối

Giải thích: Nếu số âm, đổi dấu bằng -n. Nếu không âm, giữ nguyên.

Explanation: If negative, flip sign with -n. If non-negative, keep as is.

Code:

python
def my_abs(n): if n < 0: return -n else: return n num = int(input("Enter a number: ")) print(f"Absolute value: {my_abs(num)}")

Solution 5: Đế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:

python
def 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)}")

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:

python
def 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ì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:

python
def 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: 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:

python
def 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 9: Tìm Tất Cả Số Nguyên Tố Đến N

Giải thích: Hàm find_primes() gọi is_prime() cho từng số — ví dụ về hàm gọi hàm.

Explanation: find_primes() calls is_prime() for each number — example of function calling function.

Code:

python
def is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True def find_primes(limit): primes = [] for num in range(2, limit + 1): if is_prime(num): primes.append(num) return primes limit = int(input("Enter limit: ")) result = find_primes(limit) print("Primes: " + " ".join(map(str, result)))

Solution 10: 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:

python
def 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)}")

Level 3 - Đáp Án

Solution 11: 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:

python
def 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)))

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:

python
def 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: 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:

python
def 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