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: 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 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: Dãy Số Nhảy Bước (Skip Counting)

Đề bài: Nhập ba số: start, stop, step. In các số từ start đến stop với bước nhảy step.

Problem: Enter three numbers: start, stop, step. Print numbers from start to stop with step increment.

Ví dụ:

Start: 2
Stop: 20
Step: 3

Output:

2
5
8
11
14
17
20

Bài Tập 5: Đế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 6: 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 7: Đếm Số Chia Hết cho 5 (Count Numbers Divisible by 5)

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

Problem: Enter n integers. Count how many are divisible by 5.

Ví dụ:

Enter count: 5
Numbers: 10, 13, 25, 7, 30

Output:

Count: 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: 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 10: Trung Bình Số Dương (Average of Positive Numbers)

Đề bài: Nhập n số nguyên. Tính trung bình CHỈ các số dương. Nếu không có số dương, in 'No positive numbers'.

Problem: Enter n integers. Calculate average of ONLY positive numbers. If no positive numbers, print 'No positive numbers'.

Ví dụ:

Enter count: 5
Numbers: -3, 10, -5, 20, 30

Output:

Average: 20.0

Bài Tập 11: Đế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 12: 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 13: Số May Mắn (Lucky Numbers)

Đề bài: Nhập n số nguyên. In 'Lucky' nếu số chia hết cho 7 HOẶC có chữ số tận cùng là 7, ngược lại in 'Not lucky'.

Problem: Enter n integers. Print 'Lucky' if divisible by 7 OR ends with 7, otherwise print 'Not lucky'.

Ví dụ:

Enter count: 4
Numbers: 14, 17, 25, 7

Output:

Lucky
Lucky
Not lucky
Lucky

Bài Tập 14: Bậc Thang Số (Number Staircase)

Đề bài: Nhập số n. In bậc thang số: dòng 1 là '1', dòng 2 là '22', dòng 3 là '333', v.v.

Problem: Enter n. Print number staircase: row 1 is '1', row 2 is '22', row 3 is '333', etc.

Ví dụ:

Enter n: 5

Output:

1
22
333
4444
55555

Bài Tập 15: 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 16: 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 17: Số Hoàn Hảo (Perfect Number)

Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số hoàn hảo không (tổng các ước thực sự = chính nó). VD: 6 = 1+2+3.

Problem: Enter a positive integer. Check if it's a perfect number (sum of proper divisors equals itself). Ex: 6 = 1+2+3.

Ví dụ:

Enter number: 28

Output:

Perfect

Bài Tập 18: Đoán Số Có Giới Hạn (Number Guessing with Limit)

Đề bài: Chương trình nghĩ ra một số từ 1-50 (người dùng nhập số bí mật). Người chơi có tối đa 7 lần đoán. Mỗi lần đoán sai, cho gợi ý 'cao hơn' hoặc 'thấp hơn'. Nếu đoán đúng hoặc hết lượt, kết thúc game.

Problem: Program thinks of a number from 1-50 (user enters secret number). Player has max 7 guesses. For each wrong guess, give hint 'higher' or 'lower'. If guessed correctly or out of attempts, end game.

Ví dụ:

Secret number: 25
Guess: 30
Lower!
Guess: 20
Higher!
Guess: 25
Correct!

Output:

Lower!
Higher!
Correct! You won in 3 attempts.

Bài Tập 19: Kiểm Tra Số Nguyên Tố (Check Prime Number)

Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số nguyên tố không (chỉ chia hết cho 1 và chính nó).

Problem: Enter a positive integer. Check if it's a prime number (divisible only by 1 and itself).

Ví dụ:

Enter number: 17

Output:

Prime

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: 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 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: Dãy Số Nhảy Bước

Giải thích: range với 3 tham số cho phép tùy chỉnh bước nhảy.

Explanation: range with 3 parameters allows custom step increment.

Code:

python
start = int(input("Start: ")) stop = int(input("Stop: ")) step = int(input("Step: ")) for i in range(start, stop + 1, step): print(i)

Solution 5: Đế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 6: 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 7: Đếm Số Chia Hết cho 5

Giải thích: Kiểm tra chia hết cho 5 bằng modulo.

Explanation: Check divisibility by 5 using modulo.

Code:

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

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: 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 10: Trung Bình Số Dương

Giải thích: Tính tổng và đếm số dương, sau đó chia. Kiểm tra trường hợp đặc biệt.

Explanation: Sum and count positive numbers, then divide. Check edge case.

Code:

python
n = int(input("Enter count: ")) total = 0 count = 0 for i in range(n): number = int(input("Enter number: ")) if number > 0: total += number count += 1 if count > 0: average = total / count print(f"Average: {average:.1f}") else: print("No positive numbers")

Solution 11: Đế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 12: 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 13: Số May Mắn

Giải thích: Kiểm tra hai điều kiện bằng OR.

Explanation: Check two conditions using OR.

Code:

python
n = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number % 7 == 0 or number % 10 == 7: print("Lucky") else: print("Not lucky")

Solution 14: Bậc Thang Số

Giải thích: Vòng ngoài cho dòng, vòng trong in số của dòng đó nhiều lần.

Explanation: Outer loop for rows, inner loop prints row number multiple times.

Code:

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

Solution 15: 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 16: 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 17: Số Hoàn Hảo

Giải thích: Tính tổng các ước thực sự rồi so sánh với số gốc.

Explanation: Calculate sum of proper divisors then compare with original number.

Code:

python
number = int(input("Enter number: ")) sum_divisors = 0 for i in range(1, number): if number % i == 0: sum_divisors += i if sum_divisors == number: print("Perfect") else: print("Not perfect")

Solution 18: Đoán Số Có Giới Hạn

Giải thích: Vòng lặp tối đa 7 lần. Mỗi lần so sánh guess với secret, đưa gợi ý. Dùng break để thoát khi đoán đúng.

Explanation: Loop maximum 7 times. Each time compare guess with secret, give hint. Use break to exit when guessed correctly.

Code:

python
secret = int(input("Secret number (1-50): ")) max_attempts = 7 attempts = 0 for i in range(max_attempts): guess = int(input("Guess: ")) attempts += 1 if guess == secret: print(f"Correct! You won in {attempts} attempts.") break elif guess > secret: print("Lower!") else: print("Higher!") if attempts == max_attempts: print(f"Game over! The number was {secret}.")

Solution 19: Kiểm Tra Số Nguyên Tố

Giải thích: Vòng lặp kiểm tra chia hết từ 2 đến căn bậc 2 của số.

Explanation: Loop checks divisibility from 2 to square root of number.

Code:

python
number = int(input("Enter number: ")) if number <= 1: is_prime = False else: is_prime = True i = 2 while i * i <= number: if number % i == 0: is_prime = False break i += 1 if is_prime: print("Prime") else: print("Not prime")

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