Level 1 - Cơ Bản / Basics
Bài Tập 1: In Ngược Danh Sách (Print List in Reverse)
Đề bài: Nhập một dãy số nguyên. In danh sách theo thứ tự ngược lại, trên cùng một dòng cách nhau bởi dấu cách.
Problem: Enter a list of integers. Print the list in reverse order, on one line separated by spaces.
Ví dụ:
Enter numbers: 1 2 3 4 5Output:
5 4 3 2 1Bài Tập 2: Tổng Các Phần Tử (Sum of Elements)
Đề bài: Nhập một dãy số nguyên trên một dòng (cách nhau bởi dấu cách). Tính và in tổng tất cả các phần tử.
Problem: Enter a list of integers on one line (separated by spaces). Calculate and print the sum of all elements.
Ví dụ:
Enter numbers: 3 7 2 5 1Output:
Sum: 18Bài Tập 3: Tìm Số Nhỏ Nhất (Find Minimum)
Đề bài: Nhập một dãy số nguyên. Tìm và in số nhỏ nhất trong danh sách.
Problem: Enter a list of integers. Find and print the smallest number in the list.
Ví dụ:
Enter numbers: 8 3 9 1 5Output:
Minimum: 1Bài Tập 4: Tổng Số Lẻ (Sum of Odd Numbers)
Đề bài: Nhập một dãy số nguyên. Tính tổng CHỈ các số lẻ trong danh sách.
Problem: Enter a list of integers. Calculate sum of ONLY odd numbers in the list.
Ví dụ:
Enter numbers: 2 7 4 3 8 5Output:
Sum of odd: 15Bài Tập 5: Đếm Phần Tử Bằng Giá Trị (Count Elements Equal to Value)
Đề bài: Nhập một dãy số nguyên, sau đó nhập một số x. Đếm số lần x xuất hiện trong danh sách.
Problem: Enter a list of integers, then enter a number x. Count how many times x appears in the list.
Ví dụ:
Enter numbers: 3 5 3 7 3 9
Enter x: 3Output:
Count: 3Level 2 - Trung Bình / Intermediate
Bài Tập 6: Nén Phần Tử Liên Tiếp (Compress Consecutive Duplicates)
Đề bài: Nhập một dãy số nguyên. Nén các phần tử liên tiếp giống nhau thành (giá_trị, số_lần). In mỗi nhóm trên một dòng.
Problem: Enter a list of integers. Compress consecutive identical elements into (value, count). Print each group on a line.
Ví dụ:
Enter numbers: 1 1 1 2 2 3 3 3 3 1Output:
1 x3
2 x2
3 x4
1 x1Bài Tập 7: Tìm Phần Tử Xuất Hiện Nhiều Nhất (Find Most Frequent Element)
Đề bài: Nhập một dãy số nguyên. Tìm phần tử xuất hiện nhiều lần nhất. Nếu có nhiều phần tử cùng số lần, in phần tử đầu tiên.
Problem: Enter a list of integers. Find the most frequent element. If there is a tie, print the first one encountered.
Ví dụ:
Enter numbers: 3 5 3 7 5 3 7Output:
Most frequent: 3 (3 times)Bài Tập 8: Loại Bỏ Trùng Lặp (Remove Duplicates)
Đề bài: Nhập một dãy số nguyên. In danh sách sau khi loại bỏ các phần tử trùng lặp (giữ thứ tự xuất hiện đầu tiên).
Problem: Enter a list of integers. Print list after removing duplicates (keep first occurrence order).
Ví dụ:
Enter numbers: 3 5 3 7 5 9 3Output:
3 5 7 9Bài Tập 9: Trung Bình và Đếm Trên Trung Bình (Average and Count Above Average)
Đề bài: Nhập một dãy số nguyên. Tính trung bình, sau đó đếm có bao nhiêu phần tử lớn hơn trung bình.
Problem: Enter a list of integers. Calculate average, then count how many elements are above average.
Ví dụ:
Enter numbers: 2 8 4 6 10Output:
Average: 6.0
Above average: 2Bài Tập 10: Kiểm Tra Danh Sách Tăng Dần (Check if List is Sorted)
Đề bài: Nhập một dãy số nguyên. Kiểm tra xem danh sách có được sắp xếp tăng dần không. In 'Yes' hoặc 'No'.
Problem: Enter a list of integers. Check if the list is sorted in ascending order. Print 'Yes' or 'No'.
Ví dụ:
Enter numbers: 1 3 5 7 9Output:
Sorted: YesĐÁP ÁN / SOLUTIONS
Level 1 - Đáp Án
Solution 1: In Ngược Danh Sách
Giải thích: Duyệt từ cuối về đầu bằng range ngược.
Explanation: Iterate from end to start using reverse range.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) result = [] for i in range(len(numbers) - 1, -1, -1): result.append(numbers[i]) print(" ".join(map(str, result)))
Solution 2: Tổng Các Phần Tử
Giải thích: Nhập dãy số bằng split(), duyệt qua từng phần tử và cộng dồn.
Explanation: Input numbers using split(), iterate through each element and accumulate sum.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) total = 0 for num in numbers: total += num print(f"Sum: {total}")
Solution 3: Tìm Số Nhỏ Nhất
Giải thích: Tương tự tìm max nhưng so sánh nhỏ hơn.
Explanation: Similar to finding max but compare for smaller.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) min_val = numbers[0] for num in numbers: if num < min_val: min_val = num print(f"Minimum: {min_val}")
Solution 4: Tổng Số Lẻ
Giải thích: Duyệt danh sách, chỉ cộng các số lẻ vào tổng.
Explanation: Iterate list, only add odd numbers to sum.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) total = 0 for num in numbers: if num % 2 != 0: total += num print(f"Sum of odd: {total}")
Solution 5: Đếm Phần Tử Bằng Giá Trị
Giải thích: Duyệt danh sách và đếm số lần gặp giá trị x.
Explanation: Iterate list and count occurrences of value x.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) x = int(input("Enter x: ")) count = 0 for num in numbers: if num == x: count += 1 print(f"Count: {count}")
Level 2 - Đáp Án
Solution 6: Nén Phần Tử Liên Tiếp
Giải thích: Theo dõi giá trị hiện tại và đếm. Khi giá trị thay đổi, in kết quả và reset.
Explanation: Track current value and count. When value changes, print result and reset.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) current = numbers[0] count = 1 for i in range(1, len(numbers)): if numbers[i] == current: count += 1 else: print(f"{current} x{count}") current = numbers[i] count = 1 print(f"{current} x{count}")
Solution 7: Tìm Phần Tử Xuất Hiện Nhiều Nhất
Giải thích: Đếm từng phần tử bằng vòng lặp lồng, theo dõi max.
Explanation: Count each element using nested loop, track maximum.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) max_count = 0 max_num = numbers[0] for num in numbers: count = 0 for n in numbers: if n == num: count += 1 if count > max_count: max_count = count max_num = num print(f"Most frequent: {max_num} ({max_count} times)")
Solution 8: Loại Bỏ Trùng Lặp
Giải thích: Duyệt danh sách, chỉ thêm phần tử nếu chưa có trong danh sách kết quả.
Explanation: Iterate list, only append element if not already in result list.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) result = [] for num in numbers: if num not in result: result.append(num) print(" ".join(map(str, result)))
Solution 9: Trung Bình và Đếm Trên Trung Bình
Giải thích: Tính trung bình trước, sau đó duyệt lại đếm phần tử vượt trung bình.
Explanation: Calculate average first, then iterate again to count elements above average.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) total = 0 for num in numbers: total += num average = total / len(numbers) count = 0 for num in numbers: if num > average: count += 1 print(f"Average: {average}") print(f"Above average: {count}")
Solution 10: Kiểm Tra Danh Sách Tăng Dần
Giải thích: Duyệt từ phần tử thứ 2, so sánh với phần tử trước. Nếu vi phạm, đánh dấu False.
Explanation: Iterate from 2nd element, compare with previous. If violated, mark False.
Code:
pythonnumbers = list(map(int, input("Enter numbers: ").split())) is_sorted = True for i in range(1, len(numbers)): if numbers[i] < numbers[i - 1]: is_sorted = False break if is_sorted: print("Sorted: Yes") else: print("Sorted: No")