Level 1 - Cơ Bản / Basics

Bài Tập 1: Kiểm Tra Khóa Tồn Tại (Check if a Key Exists)

Đề bài: Cho sẵn ages = {"Alice": 14, "Bob": 15}. Nhập một tên; nếu có trong từ điển thì in tuổi, ngược lại in "Not found".

Problem: Given ages = {"Alice": 14, "Bob": 15}, read a name. If it is in the dictionary print the age, otherwise print "Not found".

Ví dụ:

Enter a name: Dan

Output:

Not found

Bài Tập 2: Đếm Nguyên Âm (Count Vowels)

Đề bài: Nhập một câu và đếm số nguyên âm (a, e, i, o, u) trong đó.

Problem: Read a sentence and count how many vowels (a, e, i, o, u) it contains.

Ví dụ:

Enter a sentence: programming is fun

Output:

Vowels: 5

Bài Tập 3: Đàn Thỏ Sinh Sôi (Rabbit Pairs)

Đề bài: Một trang trại có 1 cặp thỏ ở tháng 1 và 1 cặp ở tháng 2. Từ tháng 3 trở đi, số cặp thỏ mỗi tháng bằng tổng số cặp của hai tháng liền trước. Nhập N và in số cặp thỏ ở tháng N.

Problem: A farm has 1 pair of rabbits in month 1 and 1 pair in month 2. From month 3 on, the number of pairs each month is the two previous months added together. Read N and print how many pairs there are in month N.

Ví dụ:

Month: 6

Output:

Pairs: 8

Bài Tập 4: Điểm Giao Hàng Gần Nhất (Nearest Delivery Stop)

Đề bài: Cửa hàng ở vị trí (0, 0). Nhập 3 điểm giao hàng, mỗi điểm gồm hai số x và y. Khoảng cách tính bằng số ô phải đi: |x| + |y|. In ra điểm gần cửa hàng nhất.

Problem: The shop is at (0, 0). Read 3 delivery stops, each given as two numbers x and y. The distance is the number of blocks walked: |x| + |y|. Print the stop closest to the shop.

Ví dụ:

Stop 1 x: 4
Stop 1 y: 2
Stop 2 x: -1
Stop 2 y: 2
Stop 3 x: 5
Stop 3 y: 5

Output:

Closest: (-1, 2)

Bài Tập 5: Ký Tự Đầu, Cuối Và Độ Dài (First, Last and Length)

Đề bài: Nhập một từ và in ký tự đầu, ký tự cuối và độ dài của nó.

Problem: Read a word and print its first character, its last character, and its length.

Ví dụ:

Enter a word: banana

Output:

First: b
Last: a
Length: 6

Level 2 - Trung Bình / Intermediate

Bài Tập 6: Dòng Hóa Đơn (Receipt Line)

Đề bài: Nhập tên món và giá tiền; in một dòng hóa đơn với giá có đúng 2 chữ số thập phân.

Problem: Read an item name and a price; print one receipt line showing the price with exactly 2 decimal places.

Ví dụ:

Item: Apple
Price: 3.5

Output:

Apple .... $3.50

Bài Tập 7: Đếm Số Lần Xuất Hiện Của Từ (Word Count)

Đề bài: Nhập một câu và đếm số lần mỗi từ xuất hiện, in ra từng cặp từ và số lần.

Problem: Read a sentence and count how many times each word appears, printing each word with its count.

Ví dụ:

Enter a sentence: the cat sat on the mat

Output:

the: 2
cat: 1
sat: 1
on: 1
mat: 1

Bài Tập 8: Gắn Thẻ Hashtag (Make Hashtags)

Đề bài: Nhập một câu. Với mỗi từ có từ 4 chữ cái trở lên, tạo một hashtag bằng cách thêm dấu # ở đầu. In tất cả hashtag trên một dòng, cách nhau đúng một khoảng trắng.

Problem: Read a sentence. Turn every word with 4 or more letters into a hashtag by putting a # in front. Print all the hashtags on one line, separated by exactly one space.

Ví dụ:

Enter a sentence: I want to learn python today

Output:

#want #learn #python #today

Bài Tập 9: Các Từ Bắt Đầu Bằng Một Chữ Cái (Words Starting With a Letter)

Đề bài: Nhập một câu và một chữ cái; in các từ bắt đầu bằng chữ cái đó và tổng số từ tìm được.

Problem: Read a sentence and a letter; print every word that starts with that letter and the total count found.

Ví dụ:

Sentence: banana bread and butter
Letter: b

Output:

banana
bread
butter
Count: 3

Bài Tập 10: Viết Hoa Chữ Cái Đầu Mỗi Từ (Capitalize Each Word)

Đề bài: Nhập họ tên viết thường; in lại với chữ cái đầu của mỗi từ viết hoa.

Problem: Read a full name in lowercase; print it back with the first letter of each word capitalized.

Ví dụ:

Enter full name: linh nguyen van

Output:

Linh Nguyen Van

Level 3 - Nâng Cao / Advanced

Bài Tập 11: Tính Tiền Giỏ Hàng (Shopping Cart Total)

Đề bài: Cho bảng giá prices = {"apple": 5000, "bread": 12000, "milk": 20000}. Nhập số món cần mua, rồi với mỗi món nhập tên và số lượng; in thành tiền từng dòng và tổng cộng.

Problem: Given prices = {"apple": 5000, "bread": 12000, "milk": 20000}, read how many items are being bought, then for each read a name and a quantity; print each line total and the grand total.

Ví dụ:

How many items? 2
Item: apple
Quantity: 3
Item: bread
Quantity: 1

Output:

apple x3 = 15000
bread x1 = 12000
Total: 27000

Bài Tập 12: Sổ Danh Bạ (Contact Book)

Đề bài: Xây một sổ danh bạ dùng dictionary tên → số điện thoại, với các lệnh add, find, list và quit lặp trong vòng while.

Problem: Build a contact book using a dictionary of name → phone, with add, find, list and quit commands looping in a while.

Ví dụ:

add / find / list / quit: add
Name: Minh
Phone: 0987654321
add / find / list / quit: find
Name: Nam
add / find / list / quit: list
add / find / list / quit: quit

Output:

Not found
Minh: 0987654321

Bài Tập 13: Từ Xuất Hiện Nhiều Nhất (Most Frequent Word)

Đề bài: Nhập một câu và in ra từ xuất hiện nhiều nhất cùng số lần xuất hiện.

Problem: Read a sentence and print the word that appears most often, together with its count.

Ví dụ:

Enter a sentence: the cat sat on the mat the end

Output:

Most frequent: the (3)

ĐÁP ÁN / SOLUTIONS

Level 1 - Đáp Án

Solution 1: Kiểm Tra Khóa Tồn Tại

Giải thích: Với dictionary, toán tử in kiểm tra khóa (không phải giá trị) — luôn kiểm tra trước khi truy cập.

Explanation: For a dictionary, the in operator checks keys (not values) — always check before accessing.

Code:

python
ages = {"Alice": 14, "Bob": 15} name = input("Enter a name: ") if name in ages: print(f"{name}: {ages[name]}") else: print("Not found")

Solution 2: Đếm Nguyên Âm

Giải thích: Vòng lặp duyệt từng ký tự, và letter in "aeiou" kiểm tra ký tự đó có phải nguyên âm.

Explanation: The loop visits each character, and letter in "aeiou" tests whether it is a vowel.

Code:

python
text = input("Enter a sentence: ").lower() count = 0 for letter in text: if letter in "aeiou": count = count + 1 print(f"Vowels: {count}")

Solution 3: Đàn Thỏ Sinh Sôi

Giải thích: Cả vế phải của a, b = b, a + b được tính XONG rồi mới gán, nên a + b vẫn dùng giá trị a cũ; gán từng dòng thì a đã đổi và kết quả sai.

Explanation: The whole right side of a, b = b, a + b is computed BEFORE anything is assigned, so a + b still uses the old a; assigning line by line changes a first and gives the wrong answer.

Code:

python
n = int(input("Month: ")) a = 1 b = 1 for i in range(n - 2): a, b = b, a + b print(f"Pairs: {b}")

Solution 4: Điểm Giao Hàng Gần Nhất

Giải thích: Một điểm là MỘT giá trị gồm hai phần, nên tuple giữ được cả x và y trong một biến khi so sánh qua từng vòng lặp.

Explanation: A point is ONE value with two parts, so a tuple keeps x and y together in a single variable while the loop compares candidates.

Code:

python
best = () best_distance = -1 for i in range(1, 4): x = int(input(f"Stop {i} x: ")) y = int(input(f"Stop {i} y: ")) distance = abs(x) + abs(y) if best_distance == -1 or distance < best_distance: best_distance = distance best = (x, y) bx, by = best print(f"Closest: ({bx}, {by})")

Solution 5: Ký Tự Đầu, Cuối Và Độ Dài

Giải thích: Chuỗi là một dãy nên truy cập bằng chỉ số như list; chỉ số âm đếm từ cuối.

Explanation: A string is a sequence, so it is indexed like a list; negative indexes count from the end.

Code:

python
word = input("Enter a word: ") print(f"First: {word[0]}") print(f"Last: {word[-1]}") print(f"Length: {len(word)}")

Level 2 - Đáp Án

Solution 6: Dòng Hóa Đơn

Giải thích: Định dạng :.2f luôn in đủ 2 chữ số thập phân, nên 3.5 hiện thành 3.50.

Explanation: The :.2f format always prints 2 decimal digits, so 3.5 shows up as 3.50.

Code:

python
item = input("Item: ") price = float(input("Price: ")) print(f"{item} .... ${price:.2f}")

Solution 7: Đếm Số Lần Xuất Hiện Của Từ

Giải thích: Đây là mẫu đếm kinh điển: khóa là từ, giá trị là số đếm; .items() duyệt cả cặp.

Explanation: This is the classic counting pattern: the key is the word, the value is the count; .items() loops over both.

Code:

python
sentence = input("Enter a sentence: ") words = sentence.split(" ") counts = {} for word in words: if word in counts: counts[word] = counts[word] + 1 else: counts[word] = 1 for word, n in counts.items(): print(f"{word}: {n}")

Solution 8: Gắn Thẻ Hashtag

Giải thích: Số hashtag không biết trước, nên phải gom vào list rồi ghép — join đặt khoảng trắng vào GIỮA các phần tử chứ không thừa ở hai đầu.

Explanation: The number of hashtags is not known in advance, so they are collected in a list and joined — join puts a space only BETWEEN the items, never at the ends.

Code:

python
sentence = input("Enter a sentence: ") words = sentence.split(" ") tags = [] for word in words: if len(word) >= 4: tags.append("#" + word) print(" ".join(tags))

Solution 9: Các Từ Bắt Đầu Bằng Một Chữ Cái

Giải thích: startswith trả về True/False, dùng trực tiếp trong if mà không cần so sánh thêm.

Explanation: startswith returns True/False, so it can be used directly in an if with no extra comparison.

Code:

python
sentence = input("Sentence: ") letter = input("Letter: ") words = sentence.split(" ") count = 0 for word in words: if word.startswith(letter): print(word) count = count + 1 print(f"Count: {count}")

Solution 10: Viết Hoa Chữ Cái Đầu Mỗi Từ

Giải thích: Slicing tách ký tự đầu khỏi phần đuôi, xử lý riêng rồi nối lại bằng dấu cộng.

Explanation: Slicing splits the first character from the rest, each is handled separately, then joined back with +.

Code:

python
full = input("Enter full name: ").strip() words = full.split(" ") result = [] for w in words: result.append(w[0].upper() + w[1:].lower()) print(" ".join(result))

Level 3 - Đáp Án

Solution 11: Tính Tiền Giỏ Hàng

Giải thích: Dictionary đóng vai bảng tra giá; vòng lặp vừa in từng dòng vừa cộng dồn tổng.

Explanation: The dictionary acts as a price lookup table; the loop prints each line while accumulating the total.

Code:

python
prices = {"apple": 5000, "bread": 12000, "milk": 20000} n = int(input("How many items? ")) total = 0 for i in range(n): item = input("Item: ") qty = int(input("Quantity: ")) line = prices[item] * qty total = total + line print(f"{item} x{qty} = {line}") print(f"Total: {total}")

Solution 12: Sổ Danh Bạ

Giải thích: Vòng while giữ chương trình chạy; dictionary lưu dữ liệu giữa các lệnh cho tới khi quit.

Explanation: The while loop keeps the program running; the dictionary holds the data between commands until quit.

Code:

python
contacts = {} while True: action = input("add / find / list / quit: ") if action == "add": name = input("Name: ") phone = input("Phone: ") contacts[name] = phone elif action == "find": name = input("Name: ") if name in contacts: print(f"{name}: {contacts[name]}") else: print("Not found") elif action == "list": for name, phone in contacts.items(): print(f"{name}: {phone}") elif action == "quit": break

Solution 13: Từ Xuất Hiện Nhiều Nhất

Giải thích: Bài này ghép hai mẫu đã học: đếm bằng dictionary, rồi tìm giá trị lớn nhất.

Explanation: This combines two known patterns: counting with a dictionary, then finding the maximum.

Code:

python
sentence = input("Enter a sentence: ") words = sentence.split(" ") counts = {} for word in words: if word in counts: counts[word] = counts[word] + 1 else: counts[word] = 1 best_word = "" best_count = 0 for word, n in counts.items(): if n > best_count: best_count = n best_word = word print(f"Most frequent: {best_word} ({best_count})")