BookFinder:用幾個字母尋找下一本經典讀物
Finding your next classic read with a few typed letters
BookFinder 是一個命令列推薦程式,讀者輸入類別名稱的前綴,即可尋找經典冒險、推理和科幻小説推薦。程式以 trie 配合字典儲存和搜尋資料;相比逐一掃描類別名稱,trie 的前綴查找主要取決於輸入字元數,但需要較多設定和記憶體。
BookFinder is a small command-line recommendation program for readers who know the kind of story they want but do not have a title in mind. It suggests classic adventure, mystery, and science-fiction books from a category prefix.
How it works
The recommendations live in a dictionary: each category maps to a short list of titles, authors, and descriptions. For autocomplete, the program uses a trie. Each node represents a character, so BookFinder follows the characters the reader typed and then collects the category names below that node. This avoids scanning every full category name just to locate the matching prefix.
After the reader chooses a category, the program prints its recommendations. Entering sci, for example, finds the science-fiction category. An unknown prefix gets a helpful retry message, and q exits the program.
What the project covers
This project brings together dictionaries, nested data, tree-shaped structures, prefix searching, sorting, and command-line input. It also demonstrates the trade-off between a simple linear scan and a trie: the trie takes more setup and memory, but prefix lookup depends mainly on the number of characters entered.
Try it
Run python3 recommend.py, enter a prefix such as mys or sci, choose a result, and explore the list. The current dataset is intentionally small, so new categories and books can be added directly to BOOKS_BY_CATEGORY.
Disclosure: The project and this write-up were prepared with AI assistance as part of a Codecademy course.
來源:DEV Community · dev.to