Level 1 - Cơ Bản / Basics

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

Output:

7 is Odd

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

Output:

Max: 12

Bà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: 98765

Output:

Digits: 5

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

Level 2 - Trung Bình / Intermediate

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

Output:

Sum: 18

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

Bài Tập 8: Giai Thừa Đệ Quy (Recursive Factorial)

Đề bài: Viết hàm factorial(n) tính n! bằng ĐỆ QUY (hàm tự gọi chính nó). Quy tắc: 0! = 1, n! = n × (n-1)!. Không dùng vòng lặp.

Problem: Write a function factorial(n) that calculates n! using RECURSION (function calls itself). Rule: 0! = 1, n! = n × (n-1)!. Do not use loops.

Ví dụ:

Enter a number: 5

Output:

5! = 120

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

Output:

Fibonacci(7) = 13

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

Output:

Discount: 10%
Total: 630

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: 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 90

Output:

Sorted: 11 12 22 25 34 64 90

Bà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: 95

Output:

=== RANKING ===
#1 Diana - 95
#2 Bob - 92
#3 Alice - 85
#4 Charlie - 78

ĐÁP ÁN / SOLUTIONS

Level 1 - Đáp Án

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

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

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

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

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

Solution 5: 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)}")

Level 2 - Đáp Án

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

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

Solution 8: Giai Thừa Đệ Quy

Giải thích: Đệ quy: hàm gọi chính nó với n nhỏ hơn. Dừng khi n = 0 (base case). Mỗi lần gọi nhân n với kết quả của (n-1)!.

Explanation: Recursion: function calls itself with smaller n. Stops when n = 0 (base case). Each call multiplies n with result of (n-1)!.

Code:

python
def factorial(n): if n == 0: return 1 return n * factorial(n - 1) # How it works: # factorial(4) # = 4 * factorial(3) # = 4 * 3 * factorial(2) # = 4 * 3 * 2 * factorial(1) # = 4 * 3 * 2 * 1 * factorial(0) # = 4 * 3 * 2 * 1 * 1 = 24 num = int(input("Enter a number: ")) print(f"{num}! = {factorial(num)}")

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

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

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

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

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

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