Python Exercises - TA Version

Bài Tập 1: Tính n Giai Thừa Bằng Vòng Lặp (Calculate n Factorial with Loop)

Đề bài: Nhập số n. Tính n! (n giai thừa) = 1 × 2 × 3 × ... × n.

Problem: Enter n. Calculate n! (n factorial) = 1 × 2 × 3 × ... × n.

Ví dụ:

Enter n: 5

Output:

Factorial: 120

Bài Tập 2: Đếm từ a đến b (Count from a to b)

Đề bài: Nhập hai số a và b. In các số từ a đến b.

Problem: Enter two numbers a and b. Print numbers from a to b.

Ví dụ:

Enter a: 3
Enter b: 7

Output:

3
4
5
6
7

Bài Tập 3: In Số Với Prefix (Print Numbers with Prefix)

Đề bài: Nhập số n. In các số từ 1 đến n với prefix 'Number: '.

Problem: Enter n. Print numbers from 1 to n with prefix 'Number: '.

Ví dụ:

Enter n: 5

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Bài Tập 4: In Số Lẻ từ 1 đến n (Print Odd Numbers from 1 to n)

Đề bài: Nhập một số n. In tất cả các số lẻ từ 1 đến n.

Problem: Enter a number n. Print all odd numbers from 1 to n.

Ví dụ:

Enter n: 10

Output:

1
3
5
7
9

Bài Tập 5: Tính Tích của n Số (Product of n Numbers)

Đề bài: Nhập n số nguyên. Tính và in tích của tất cả các số.

Problem: Enter n integers. Calculate and print the product of all numbers.

Ví dụ:

Enter count: 4
Numbers: 2, 3, 4, 5

Output:

Product: 120

Bài Tập 6: Đếm Số Âm và Số 0 (Count Negative and Zero)

Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số âm và bao nhiêu số 0.

Problem: Enter n integers. Count how many are negative and how many are zero.

Ví dụ:

Enter count: 6
Numbers: -3, 0, 7, -5, 0, 12

Output:

Negative: 2
Zero: 2

Bài Tập 7: Số Lớn Nhất và Nhỏ Nhất (Maximum and Minimum)

Đề bài: Nhập n số nguyên. Tìm và in cả số lớn nhất và số nhỏ nhất.

Problem: Enter n integers. Find and print both maximum and minimum.

Ví dụ:

Enter count: 5
Numbers: 15, 7, 42, 3, 23

Output:

Maximum: 42
Minimum: 3

Bài Tập 8: Đếm Số Chia Hết cho 2 HOẶC 3 (Count Numbers Divisible by 2 OR 3)

Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số chia hết cho 2 hoặc 3.

Problem: Enter n integers. Count how many are divisible by 2 or 3.

Ví dụ:

Enter count: 5
Numbers: 10, 13, 9, 7, 12

Output:

Count: 3

Bài Tập 9: Tìm Số Lớn Thứ Hai (Find Second Largest)

Đề bài: Nhập n số nguyên (n >= 2). Tìm số lớn thứ hai.

Problem: Enter n integers (n >= 2). Find the second largest number.

Ví dụ:

Enter count: 5
Numbers: 15, 42, 8, 42, 23

Output:

Second largest: 23

Bài Tập 10: Số Lớn Hơn 10 (Numbers Greater Than 10)

Đề bài: Nhập n số nguyên. Chỉ in ra các số lớn hơn 10.

Problem: Enter n integers. Print only numbers greater than 10.

Ví dụ:

Enter count: 5
Numbers: 5, 15, 8, 22, 3

Output:

15
22

Bài Tập 11: Xác Thực Mật Khẩu Đơn Giản (Simple Password Validation)

Đề bài: Người dùng có 3 lần thử để nhập đúng mật khẩu. Mật khẩu là 'python123'. In 'Access granted' hoặc 'Access denied'.

Problem: User has 3 attempts to enter correct password. Password is 'python123'. Print 'Access granted' or 'Access denied'.

Ví dụ:

Password: abc
Wrong! Try again
Password: xyz
Wrong! Try again
Password: python123
Access granted

Output:

Access granted

Bài Tập 12: Tính Tiền Điện (Electricity Bill Calculator)

Đề bài: Nhập n số kWh. Tính tiền: 0-50 kWh: 1000đ/kWh, 51-100: 1500đ/kWh, >100: 2000đ/kWh.

Problem: Enter n kWh values. Calculate bill: 0-50 kWh: 1000/kWh, 51-100: 1500/kWh, >100: 2000/kWh.

Ví dụ:

Enter count: 3
kWh: 30, 75, 120

Output:

30000
67500
170000

Bài Tập 13: Hình Chữ Nhật Rỗng (Hollow Rectangle)

Đề bài: Nhập chiều rộng w và chiều cao h. In hình chữ nhật rỗng bằng dấu sao.

Problem: Enter width w and height h. Print hollow rectangle with stars.

Ví dụ:

Width: 5
Height: 4

Output:

* * * * *
*       *
*       *
* * * * *

Bài Tập 14: Kiểm Tra Dãy Tăng (Check Increasing Sequence)

Đề bài: Nhập n số nguyên. Kiểm tra xem dãy có tăng dần không (mỗi số lớn hơn số trước).

Problem: Enter n integers. Check if sequence is increasing (each number greater than previous).

Ví dụ:

Enter count: 5
Numbers: 3, 7, 10, 15, 20

Output:

Increasing: Yes

Bài Tập 15: Đếm Số Theo Điều Kiện Phức Tạp (Count with Complex Condition)

Đề bài: Nhập n số nguyên. Đếm số lượng số chia hết cho 3 NHƯNG KHÔNG chia hết cho 6.

Problem: Enter n integers. Count numbers divisible by 3 BUT NOT divisible by 6.

Ví dụ:

Enter count: 6
Numbers: 3, 6, 9, 12, 15, 18

Output:

Count: 3

Bài Tập 16: Tổng Các Chữ Số (Sum of Digits)

Đề bài: Nhập một số nguyên dương. Tính tổng các chữ số của số đó.

Problem: Enter a positive integer. Calculate sum of its digits.

Ví dụ:

Enter number: 12345

Output:

Sum: 15

Bài Tập 17: Đảo Ngược Chữ Số (Reverse Digits)

Đề bài: Nhập một số nguyên dương. Đảo ngược các chữ số của số đó và in ra.

Problem: Enter a positive integer. Reverse its digits and print.

Ví dụ:

Enter number: 12345

Output:

54321

Bài Tập 18: Hình Kim Cương Số (Number Diamond)

Đề bài: Nhập n (lẻ). In hình kim cương bằng số: nửa trên tăng 1,3,5..., nửa dưới giảm.

Problem: Enter n (odd). Print diamond with numbers: upper half increases 1,3,5..., lower half decreases.

Ví dụ:

Enter n: 5

Output:

1
123
12345
123
1

Bài Tập 19: Số Mạnh (Strong Number) (Strong Number)

Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải Strong Number không (tổng giai thừa các chữ số = chính nó). VD: 145 = 1! + 4! + 5!

Problem: Enter a positive integer. Check if it's a Strong Number (sum of factorial of digits equals itself). Ex: 145 = 1! + 4! + 5!

Ví dụ:

Enter number: 145

Output:

Strong

Bài Tập 20: Số Chính Phương (Perfect Square)

Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số chính phương không (tồn tại số i mà i×i = số đó).

Problem: Enter a positive integer. Check if it's a perfect square (exists number i where i×i equals that number).

Ví dụ:

Enter number: 144

Output:

Perfect square

ĐÁP ÁN / SOLUTIONS

Solution 1: Tính n Giai Thừa Bằng Vòng Lặp

Giải thích: Nhân liên tiếp từ 1 đến n.

Explanation: Multiply consecutively from 1 to n.

Code:

python
n = int(input("Enter n: ")) result = 1 for i in range(1, n + 1): result *= i print(f"Factorial: {result}")

Solution 2: Đếm từ a đến b

Giải thích: range(a, b+1) tạo dãy từ a đến b.

Explanation: range(a, b+1) creates sequence from a to b.

Code:

python
a = int(input("Enter a: ")) b = int(input("Enter b: ")) for i in range(a, b + 1): print(i)

Solution 3: In Số Với Prefix

Giải thích: In với format string đơn giản.

Explanation: Print with simple format string.

Code:

python
n = int(input("Enter n: ")) for i in range(1, n + 1): print(f"Number: {i}")

Solution 4: In Số Lẻ từ 1 đến n

Giải thích: range(1, n+1, 2) tạo dãy số bắt đầu từ 1, tăng 2 mỗi lần (chỉ số lẻ).

Explanation: range(1, n+1, 2) creates sequence starting at 1, incrementing by 2 (odd numbers only).

Code:

python
n = int(input("Enter n: ")) for i in range(1, n + 1, 2): print(i)

Solution 5: Tính Tích của n Số

Giải thích: Khởi tạo tích = 1. Mỗi số nhân vào tích.

Explanation: Initialize product = 1. Multiply each number into product.

Code:

python
n = int(input("Enter count: ")) product = 1 for i in range(n): number = int(input("Enter number: ")) product *= number print(f"Product: {product}")

Solution 6: Đếm Số Âm và Số 0

Giải thích: Dùng if-elif để phân loại và đếm.

Explanation: Use if-elif to categorize and count.

Code:

python
n = int(input("Enter count: ")) negative = 0 zero = 0 for i in range(n): number = int(input("Enter number: ")) if number < 0: negative += 1 elif number == 0: zero += 1 print(f"Negative: {negative}") print(f"Zero: {zero}")

Solution 7: Số Lớn Nhất và Nhỏ Nhất

Giải thích: Theo dõi cả max và min đồng thời.

Explanation: Track both max and min simultaneously.

Code:

python
n = int(input("Enter count: ")) max_num = float('-inf') min_num = float('inf') for i in range(n): number = int(input("Enter number: ")) if number > max_num: max_num = number if number < min_num: min_num = number print(f"Maximum: {max_num}") print(f"Minimum: {min_num}")

Solution 8: Đếm Số Chia Hết cho 2 HOẶC 3

Giải thích: Dùng OR để kiểm tra một trong hai điều kiện.

Explanation: Use OR to check one of two conditions.

Code:

python
n = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0 or number % 3 == 0: count += 1 print(f"Count: {count}")

Solution 9: Tìm Số Lớn Thứ Hai

Giải thích: Theo dõi 2 số lớn nhất, cập nhật logic khi gặp số mới.

Explanation: Track 2 largest numbers, update logic when encountering new number.

Code:

python
n = int(input("Enter count: ")) max1 = float('-inf') max2 = float('-inf') for i in range(n): number = int(input("Enter number: ")) if number > max1: max2 = max1 max1 = number elif number > max2 and number != max1: max2 = number print(f"Second largest: {int(max2)}")

Solution 10: Số Lớn Hơn 10

Giải thích: Nhập từng số, kiểm tra điều kiện lớn hơn 10, in ngay nếu đúng.

Explanation: Input each number, check if greater than 10, print immediately if true.

Code:

python
n = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number > 10: print(number)

Solution 11: Xác Thực Mật Khẩu Đơn Giản

Giải thích: Vòng lặp 3 lần, break khi đúng, kiểm tra trạng thái cuối.

Explanation: Loop 3 times, break when correct, check final status.

Code:

python
correct_password = "python123" granted = False for i in range(3): password = input("Password: ") if password == correct_password: print("Access granted") granted = True break else: if i < 2: print("Wrong! Try again") if not granted: print("Access denied")

Solution 12: Tính Tiền Điện

Giải thích: Tính tiền theo bậc giá lũy tiến.

Explanation: Calculate bill using progressive pricing tiers.

Code:

python
n = int(input("Enter count: ")) for i in range(n): kwh = int(input("kWh: ")) bill = 0 if kwh <= 50: bill = kwh * 1000 elif kwh <= 100: bill = 50 * 1000 + (kwh - 50) * 1500 else: bill = 50 * 1000 + 50 * 1500 + (kwh - 100) * 2000 print(bill)

Solution 13: Hình Chữ Nhật Rỗng

Giải thích: Kiểm tra vị trí để in sao hoặc khoảng trắng.

Explanation: Check position to print star or space.

Code:

python
w = int(input("Width: ")) h = int(input("Height: ")) for i in range(h): for j in range(w): if i == 0 or i == h - 1 or j == 0 or j == w - 1: print("*", end=" ") else: print(" ", end=" ") print()

Solution 14: Kiểm Tra Dãy Tăng

Giải thích: So sánh mỗi số với số trước, đánh dấu nếu không tăng.

Explanation: Compare each number with previous, mark if not increasing.

Code:

python
n = int(input("Enter count: ")) is_increasing = True prev = float('-inf') for i in range(n): number = int(input("Enter number: ")) if number <= prev: is_increasing = False prev = number if is_increasing: print("Increasing: Yes") else: print("Increasing: No")

Solution 15: Đếm Số Theo Điều Kiện Phức Tạp

Giải thích: Kết hợp hai điều kiện bằng AND và NOT.

Explanation: Combine two conditions using AND and NOT.

Code:

python
n = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 3 == 0 and number % 6 != 0: count += 1 print(f"Count: {count}")

Solution 16: Tổng Các Chữ Số

Giải thích: Lấy từng chữ số và cộng vào tổng.

Explanation: Extract each digit and add to sum.

Code:

python
num = int(input("Enter number: ")) digit_sum = 0 while num > 0: digit = num % 10 digit_sum += digit num = num // 10 print(f"Sum: {digit_sum}")

Solution 17: Đảo Ngược Chữ Số

Giải thích: Lấy từng chữ số từ cuối, xây dựng số mới từ đầu.

Explanation: Extract digits from end, build new number from start.

Code:

python
num = int(input("Enter number: ")) reversed_num = 0 while num > 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num = num // 10 print(reversed_num)

Solution 18: Hình Kim Cương Số

Giải thích: In nửa trên tăng dần, nửa dưới giảm dần, mỗi dòng in số liên tiếp.

Explanation: Print upper half increasing, lower half decreasing, each row prints consecutive numbers.

Code:

python
n = int(input("Enter n (odd): ")) mid = (n + 1) // 2 # Upper half for i in range(1, mid + 1): for j in range(1, 2 * i): print(j, end="") print() # Lower half for i in range(mid - 1, 0, -1): for j in range(1, 2 * i): print(j, end="") print()

Solution 19: Số Mạnh (Strong Number)

Giải thích: Lấy từng chữ số, tính giai thừa, cộng dồn, so sánh với số gốc.

Explanation: Extract digits, calculate factorial, sum up, compare with original.

Code:

python
num = int(input("Enter number: ")) original = num sum_factorial = 0 while num > 0: digit = num % 10 # Calculate factorial of digit factorial = 1 for i in range(1, digit + 1): factorial *= i sum_factorial += factorial num = num // 10 if original == sum_factorial: print("Strong") else: print("Not strong")

Solution 20: Số Chính Phương

Giải thích: Vòng lặp kiểm tra xem có số nào bình phương bằng số cần kiểm tra không.

Explanation: Loop checks if any number squared equals the number to check.

Code:

python
number = int(input("Enter number: ")) is_perfect = False i = 1 while i * i <= number: if i * i == number: is_perfect = True break i += 1 if is_perfect: print("Perfect square") else: print("Not perfect square")