Level 1 - Cơ Bản / Basics

Bài Tập 1: 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

Bài Tập 2: Dòng Hồ Sơ (Profile Line)

Đề bài: Nhập tên, tuổi và điểm trung bình; in một dòng tóm tắt hồ sơ, trong đó điểm hiển thị với 1 chữ số thập phân.

Problem: Read a name, an age and a score; print one line summarising the student, with the score shown to 1 decimal place.

Ví dụ:

Name: Linh
Age: 14
Score: 80.6666

Output:

Linh is 14 and scored 80.7

Bài Tập 3: Xây Từ Điển Tuổi (Build an Age Dictionary)

Đề bài: Nhập 3 tên và tuổi tương ứng, lưu vào một dictionary, rồi nhập một tên để tra cứu tuổi.

Problem: Read 3 names with their ages, store them in a dictionary, then read one name and look up its age.

Ví dụ:

Name 1: Alice
Age 1: 14
Name 2: Bob
Age 2: 15
Name 3: Chi
Age 3: 13
Look up: Bob

Output:

Bob: 15

Bài Tập 4: Đà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 5: Tìm Từ Trong Câu (Find a Word in a Sentence)

Đề bài: Nhập một câu và một từ; in "Found" nếu từ đó xuất hiện trong câu, ngược lại in "Not found".

Problem: Read a sentence and a word; print "Found" if the word appears in the sentence, otherwise print "Not found".

Ví dụ:

Sentence: I love python
Word: love

Output:

Found

Level 2 - Trung Bình / Intermediate

Bài Tập 6: Môn Nào Dưới Trung Bình (Subjects Below Average)

Đề bài: Nhập 3 môn học và điểm tương ứng. Tính điểm trung bình của cả 3 môn, in mức trung bình đó (1 chữ số thập phân), rồi in những môn có điểm THẤP HƠN mức trung bình.

Problem: Read 3 subjects with their scores. Work out the average of all 3, print that average (1 decimal place), then print every subject scoring BELOW it.

Ví dụ:

Subject 1: math
Score 1: 8
Subject 2: english
Score 2: 5
Subject 3: science
Score 3: 9

Output:

Average: 7.3
Below: english (5)

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: 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 9: Học Sinh Điểm Cao Nhất (Best Student)

Đề bài: Nhập 3 học sinh và điểm của họ, rồi in ra ai đạt điểm cao nhất và điểm là bao nhiêu.

Problem: Read 3 students and their scores, then print who scored the highest and what that score was.

Ví dụ:

Student 1: Linh
Score 1: 7
Student 2: Nam
Score 2: 8
Student 3: Chi
Score 3: 9

Output:

Best: Chi (9)

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ừ Dài Nhất Trong Câu (Longest Word in a Sentence)

Đề bài: Nhập một câu và in ra từ dài nhất cùng độ dài của nó. Nếu có nhiều từ cùng độ dài, lấy từ đầu tiên.

Problem: Read a sentence and print its longest word with that word's length. If several words tie, keep the first one.

Ví dụ:

Enter a sentence: I love programming in python

Output:

Longest: programming (11)

Bài Tập 12: Sổ Điểm Nhiều Môn (Grade Book)

Đề bài: Nhập 2 học sinh, mỗi học sinh có 3 điểm nhập trên cùng một dòng cách nhau bởi khoảng trắng; lưu vào dictionary tên → list điểm và in điểm trung bình của từng người (1 chữ số thập phân).

Problem: Read 2 students, each with 3 scores typed on one line separated by spaces; store them in a dictionary of name → list of scores and print each student's average (1 decimal place).

Ví dụ:

Student 1: Linh
Scores: 8 9 10
Student 2: Nam
Scores: 7 7 8

Output:

Linh: 9.0
Nam: 7.3

Bài Tập 13: Danh Sách Từ Không Trùng (Unique Words in Order)

Đề bài: Nhập một câu có từ lặp lại; in các từ khác nhau theo đúng thứ tự xuất hiện lần đầu, ngăn cách bằng dấu phẩy và khoảng trắng.

Problem: Read a sentence with repeated words; print the distinct words in the order they first appear, separated by a comma and a space.

Ví dụ:

Enter a sentence: the cat the dog the bird

Output:

the, cat, dog, bird

ĐÁP ÁN / SOLUTIONS

Level 1 - Đáp Án

Solution 1: 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)}")

Solution 2: Dòng Hồ Sơ

Giải thích: Định dạng :.1f bên trong f-string in số thực với đúng 1 chữ số sau dấu phẩy.

Explanation: The :.1f format inside an f-string prints a float with exactly 1 digit after the decimal point.

Code:

python
name = input("Name: ") age = int(input("Age: ")) score = float(input("Score: ")) print(f"{name} is {age} and scored {score:.1f}")

Solution 3: Xây Từ Điển Tuổi

Giải thích: Gán vào một khóa mới sẽ thêm cặp mới; tra cứu bằng ages[khóa].

Explanation: Assigning to a new key adds a new pair; look a value up with ages[key].

Code:

python
ages = {} for i in range(1, 4): name = input(f"Name {i}: ") age = int(input(f"Age {i}: ")) ages[name] = age who = input("Look up: ") print(f"{who}: {ages[who]}")

Solution 4: Đà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 5: Tìm Từ Trong Câu

Giải thích: Với chuỗi, toán tử in kiểm tra chuỗi con — không cần vòng lặp thủ công.

Explanation: For strings, the in operator checks for a substring — no manual loop needed.

Code:

python
sentence = input("Sentence: ") word = input("Word: ") if word in sentence: print("Found") else: print("Not found")

Level 2 - Đáp Án

Solution 6: Môn Nào Dưới Trung Bình

Giải thích: Không thể vừa nhập vừa so sánh vì lúc đó chưa biết trung bình — phải lưu hết rồi mới duyệt lần hai, đó chính là lý do cần dictionary ở đây.

Explanation: Comparing while reading is impossible because the average is not known yet — everything must be stored first and walked a second time, which is exactly why the dictionary is needed here.

Code:

python
scores = {} for i in range(1, 4): subject = input(f"Subject {i}: ") score = int(input(f"Score {i}: ")) scores[subject] = score total = 0 for value in scores.values(): total = total + value average = total / len(scores) print(f"Average: {average:.1f}") for subject, score in scores.items(): if score < average: print(f"Below: {subject} ({score})")

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: 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 9: Học Sinh Điểm Cao Nhất

Giải thích: Mẫu tìm lớn nhất: giữ giá trị tốt nhất đã gặp và cập nhật khi thấy giá trị lớn hơn.

Explanation: The find-the-max pattern: keep the best value seen so far and update it whenever a bigger one appears.

Code:

python
scores = {} for i in range(1, 4): name = input(f"Student {i}: ") score = int(input(f"Score {i}: ")) scores[name] = score best_name = "" best_score = -1 for name, score in scores.items(): if score > best_score: best_score = score best_name = name print(f"Best: {best_name} ({best_score})")

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ừ Dài Nhất Trong Câu

Giải thích: Dùng > (không phải >=) nên khi hòa độ dài, từ gặp trước vẫn được giữ lại.

Explanation: Using > (not >=) means that on a tie the word seen first is kept.

Code:

python
sentence = input("Enter a sentence: ") words = sentence.split(" ") longest = "" for word in words: if len(word) > len(longest): longest = word print(f"Longest: {longest} ({len(longest)})")

Solution 12: Sổ Điểm Nhiều Môn

Giải thích: Giá trị trong dictionary không nhất thiết là số — ở đây mỗi khóa ánh xạ tới cả một list điểm.

Explanation: A dictionary value does not have to be a number — here each key maps to a whole list of scores.

Code:

python
grades = {} for i in range(1, 3): name = input(f"Student {i}: ") line = input("Scores: ") parts = line.split(" ") scores = [] for p in parts: scores.append(int(p)) grades[name] = scores for name, scores in grades.items(): total = 0 for s in scores: total = total + s print(f"{name}: {total / len(scores):.1f}")

Solution 13: Danh Sách Từ Không Trùng

Giải thích: Dictionary trả lời câu hỏi đã-gặp-chưa rất nhanh, còn list giữ đúng thứ tự xuất hiện.

Explanation: The dictionary answers have-I-seen-this quickly, while the list preserves the original order.

Code:

python
sentence = input("Enter a sentence: ") words = sentence.split(" ") seen = {} order = [] for word in words: if word not in seen: seen[word] = True order.append(word) print(", ".join(order))