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: 5Output:
Factorial: 120Bài Tập 2: In 'Hello' n Lần (Print 'Hello' n Times)
Đề bài: Nhập số n. In chữ 'Hello' n lần, mỗi lần trên một dòng.
Problem: Enter number n. Print 'Hello' n times, one per line.
Ví dụ:
Enter n: 4Output:
Hello
Hello
Hello
HelloBài Tập 3: Nhân Đôi Mỗi Số (Double Each Number)
Đề bài: Nhập n số nguyên. In ra số gấp đôi của mỗi số ngay sau khi nhập.
Problem: Enter n integers. Print double of each number immediately after input.
Ví dụ:
Enter count: 3
Numbers: 5, 10, 7Output:
10
20
14Bài Tập 4: Đế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: 7Output:
3
4
5
6
7Bài Tập 5: Tính Lũy Thừa Bằng Vòng Lặp (Calculate Power with Loop)
Đề bài: Nhập hai số a và n. Tính a^n (a mũ n) bằng vòng lặp (không dùng **).
Problem: Enter two numbers a and n. Calculate a^n (a to power n) using loop (no **).
Ví dụ:
Enter a: 2
Enter n: 5Output:
Result: 32Bài Tập 6: 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, 3Output:
15
22Bà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, 30Output:
Count: 3Bài Tập 8: Tổng Số Chẵn (Sum of Even Numbers)
Đề bài: Nhập n số nguyên. Tính tổng CHỈ các số chẵn.
Problem: Enter n integers. Calculate sum of ONLY even numbers.
Ví dụ:
Enter count: 5
Numbers: 10, 15, 22, 7, 8Output:
Sum: 40Bài Tập 9: 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, 23Output:
Maximum: 42
Minimum: 3Bà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, 30Output:
Average: 20.0Bài Tập 11: 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, 7Output:
Lucky
Lucky
Not lucky
LuckyBài Tập 12: Tìm Khoảng Cách Lớn Nhất (Find Maximum Gap)
Đề bài: Nhập n số nguyên liên tiếp. Tìm khoảng cách lớn nhất giữa hai số liên tiếp.
Problem: Enter n consecutive integers. Find the maximum gap between two consecutive numbers.
Ví dụ:
Enter count: 5
Numbers: 3, 7, 10, 12, 20Output:
Max gap: 8Bài Tập 13: Chuyển Đổi Giờ-Phút-Giây (Convert Hours-Minutes-Seconds)
Đề bài: Nhập n số giây. Chuyển mỗi số thành định dạng 'H giờ M phút S giây'.
Problem: Enter n seconds. Convert each to format 'H hours M minutes S seconds'.
Ví dụ:
Enter count: 2
Seconds: 3665, 7200Output:
1 hours 1 minutes 5 seconds
2 hours 0 minutes 0 secondsBài Tập 14: 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: 4Output:
* * * * *
* *
* *
* * * * *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, 18Output:
Count: 3Bài Tập 16: 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: 17Output:
PrimeBài Tập 17: Đếm Chữ Số (Count Digits)
Đề bài: Nhập một số nguyên. Đếm số lượng chữ số của số đó (bao gồm cả số âm).
Problem: Enter an integer. Count the number of digits (including negatives).
Ví dụ:
Enter number: -4567Output:
Digits: 4Bài Tập 18: 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: 145Output:
StrongBài Tập 19: Dãy Fibonacci (Fibonacci Sequence)
Đề bài: Nhập số n (n > 2). In n số Fibonacci đầu tiên. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).
Problem: Enter n (n > 2). Print first n Fibonacci numbers. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).
Ví dụ:
Enter n: 8Output:
0 1 1 2 3 5 8 13Bài Tập 20: Đ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.ĐÁ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:
pythonn = int(input("Enter n: ")) result = 1 for i in range(1, n + 1): result *= i print(f"Factorial: {result}")
Solution 2: In 'Hello' n Lần
Giải thích: Vòng lặp đơn giản in cùng một chuỗi n lần.
Explanation: Simple loop prints same string n times.
Code:
pythonn = int(input("Enter n: ")) for i in range(n): print("Hello")
Solution 3: Nhân Đôi Mỗi Số
Giải thích: Nhập số, nhân 2 và in ngay.
Explanation: Input number, multiply by 2 and print immediately.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) print(number * 2)
Solution 4: Đế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:
pythona = int(input("Enter a: ")) b = int(input("Enter b: ")) for i in range(a, b + 1): print(i)
Solution 5: Tính Lũy Thừa Bằng Vòng Lặp
Giải thích: Nhân a với chính nó n lần.
Explanation: Multiply a by itself n times.
Code:
pythona = int(input("Enter a: ")) n = int(input("Enter n: ")) result = 1 for i in range(n): result *= a print(f"Result: {result}")
Solution 6: 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:
pythonn = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number > 10: print(number)
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:
pythonn = 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: Tổng Số Chẵn
Giải thích: Chỉ cộng số chẵn vào tổng.
Explanation: Only add even numbers to sum.
Code:
pythonn = int(input("Enter count: ")) total = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0: total += number print(f"Sum: {total}")
Solution 9: 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:
pythonn = 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 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:
pythonn = 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: 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:
pythonn = 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 12: Tìm Khoảng Cách Lớn Nhất
Giải thích: So sánh khoảng cách giữa các số liên tiếp.
Explanation: Compare gaps between consecutive numbers.
Code:
pythonn = int(input("Enter count: ")) max_gap = 0 prev = int(input("Enter number: ")) for i in range(n - 1): current = int(input("Enter number: ")) gap = current - prev if gap > max_gap: max_gap = gap prev = current print(f"Max gap: {max_gap}")
Solution 13: Chuyển Đổi Giờ-Phút-Giây
Giải thích: Dùng phép chia và modulo để tách thành giờ, phút, giây.
Explanation: Use division and modulo to separate into hours, minutes, seconds.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): total_seconds = int(input("Seconds: ")) hours = total_seconds // 3600 remaining = total_seconds % 3600 minutes = remaining // 60 seconds = remaining % 60 print(f"{hours} hours {minutes} minutes {seconds} seconds")
Solution 14: 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:
pythonw = 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 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:
pythonn = 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: 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:
pythonnumber = 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 17: Đếm Chữ Số
Giải thích: Lấy giá trị tuyệt đối, chia 10 cho đến hết và đếm.
Explanation: Take absolute value, divide by 10 until zero and count.
Code:
pythonnum = int(input("Enter number: ")) num = abs(num) # Take absolute value if num == 0: count = 1 else: count = 0 while num > 0: count += 1 num = num // 10 print(f"Digits: {count}")
Solution 18: 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:
pythonnum = 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 19: Dãy Fibonacci
Giải thích: Dùng 2 biến để tính số tiếp theo từ 2 số trước.
Explanation: Use 2 variables to calculate next number from previous 2.
Code:
pythonn = int(input("Enter n: ")) a, b = 0, 1 print(a, end=" ") print(b, end=" ") for i in range(2, n): c = a + b print(c, end=" ") a, b = b, c print()
Solution 20: Đ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:
pythonsecret = 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}.")