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: Đế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: Diện Tích Hình Chữ Nhật (Rectangle Area)
Đề bài: Viết hàm area(width, height) trả về diện tích hình chữ nhật. Nhập chiều rộng và chiều cao, in diện tích.
Problem: Write a function area(width, height) that returns rectangle area. Read width and height, print area.
Ví dụ:
Enter width: 5
Enter height: 8Output:
Area: 40Bài Tập 4: 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 5: 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: 15Level 2 - Trung Bình / Intermediate
Bài Tập 6: Số Fibonacci Thứ N (Nth Fibonacci Number)
Đề bài: Viết hàm fibonacci(n) trả về số Fibonacci thứ n. Dãy bắt đầu: 0, 1, 1, 2, 3, 5, 8, 13, ...
Problem: Write a function fibonacci(n) that returns the nth Fibonacci number. Sequence starts: 0, 1, 1, 2, 3, 5, 8, 13, ...
Ví dụ:
Enter n: 7Output:
Fibonacci(7) = 13Bài Tập 7: 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 8: 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 9: 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 10: 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: 20Output:
Primes: 2 3 5 7 11 13 17 19Level 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: Thống Kê Danh Sách Số (List Statistics)
Đề bài: Viết các hàm: find_min(nums) tìm min, find_max(nums) tìm max, find_average(nums) tính trung bình, count_above_average(nums, avg) đếm phần tử trên trung bình. In báo cáo thống kê đầy đủ.
Problem: Write functions: find_min(nums) for min, find_max(nums) for max, find_average(nums) for average, count_above_average(nums, avg) counts elements above average. Print full statistics report.
Ví dụ:
Enter numbers: 4 8 2 9 1 7 5 3Output:
=== Statistics ===
Count: 8
Min: 1
Max: 9
Average: 4.9
Above average: 4Bài Tập 13: Xếp Hạng Học Sinh (Student Ranking System)
Đề bài: Nhập n học sinh (tên và điểm). Viết các hàm: add_student(students, name, score) thêm học sinh, sort_by_score(students) sắp xếp giảm dần theo điểm (dùng bubble sort), print_ranking(students) in bảng xếp hạng với thứ hạng 1, 2, 3...
Problem: Input n students (name and score). Write functions: add_student(students, name, score) adds student, sort_by_score(students) sorts descending by score (use bubble sort), print_ranking(students) prints ranking table with rank 1, 2, 3...
Ví dụ:
How many students? 4
Name: Alice
Score: 85
Name: Bob
Score: 92
Name: Charlie
Score: 78
Name: Diana
Score: 95Output:
=== RANKING ===
#1 Diana - 95
#2 Bob - 92
#3 Alice - 85
#4 Charlie - 78ĐÁ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: Đế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: Diện Tích Hình Chữ Nhật
Giải thích: Hàm nhiều tham số: nhận chiều rộng và cao, trả về tích.
Explanation: Multiple parameter function: takes width and height, returns product.
Code:
pythondef area(width, height): return width * height w = int(input("Enter width: ")) h = int(input("Enter height: ")) print(f"Area: {area(w, h)}")
Solution 4: 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 5: 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)}")
Level 2 - Đáp Án
Solution 6: Số Fibonacci Thứ N
Giải thích: Dùng 2 biến a, b theo dõi 2 số liên tiếp, cập nhật n lần.
Explanation: Use 2 variables a, b to track consecutive numbers, update n times.
Code:
pythondef fibonacci(n): a = 0 b = 1 for i in range(n): temp = a + b a = b b = temp return a n = int(input("Enter n: ")) print(f"Fibonacci({n}) = {fibonacci(n)}")
Solution 7: 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 8: 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 9: 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 10: 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:
pythondef 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)))
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: Thống Kê Danh Sách Số
Giải thích: 4 hàm phối hợp tạo báo cáo thống kê. Kết quả hàm này truyền vào hàm kia.
Explanation: 4 functions collaborate to build statistics report. Output of one feeds into another.
Code:
pythondef find_min(nums): min_val = nums[0] for num in nums: if num < min_val: min_val = num return min_val def find_max(nums): max_val = nums[0] for num in nums: if num > max_val: max_val = num return max_val def find_average(nums): total = 0 for num in nums: total += num return total / len(nums) def count_above_average(nums, avg): count = 0 for num in nums: if num > avg: count += 1 return count nums = list(map(int, input("Enter numbers: ").split())) avg = find_average(nums) print("=== Statistics ===") print(f"Count: {len(nums)}") print(f"Min: {find_min(nums)}") print(f"Max: {find_max(nums)}") print(f"Average: {avg:.1f}") print(f"Above average: {count_above_average(nums, avg)}")
Solution 13: Xếp Hạng Học Sinh
Giải thích: Kết hợp nhiều kỹ năng: danh sách lồng, bubble sort trên trường cụ thể, và function composition. Ứng dụng thực tế.
Explanation: Combines multiple skills: nested lists, bubble sort on specific field, and function composition. Real-world application.
Code:
pythondef add_student(students, name, score): students.append([name, score]) def sort_by_score(students): n = len(students) for i in range(n - 1): for j in range(n - 1 - i): if students[j][1] < students[j + 1][1]: temp = students[j] students[j] = students[j + 1] students[j + 1] = temp def print_ranking(students): sort_by_score(students) print("=== RANKING ===") for i in range(len(students)): rank = i + 1 name = students[i][0] score = students[i][1] print(f"#{rank} {name} - {score}") students = [] n = int(input("How many students? ")) for i in range(n): name = input("Name: ") score = int(input("Score: ")) add_student(students, name, score) print_ranking(students)