Welcome to the Data Structures & Algorithms Edition of the Magic 8-Ball Simulator - a fun and interactive way to reinforce your Python skills while building a smart version of the classic toy.
This page contains a brief description and implementation instructions for the project.
Project Description
This project emulates a “Magic 8-Ball” toy. The user asks a yes/no question, and the program returns a random answer from a list of fortunes (positive, negative, or ambiguous).
In this edition, you will use multiple Python data structures and algorithms to manage responses, history, statistics, categories, and more.
Project Requirements
- Python 3.x (uses built-in
random
module). - List: Store all possible responses.
- Dictionary: Categorize responses (e.g., positive, negative, neutral).
- Set: Track unique questions asked.
- Queue: (
collections.deque
) Store last N question-answer pairs (rolling history). - Stack: (optional) Undo last question-answer entry.
- Tuple: Store immutable (question, answer) pairs in history.
- Heap: (
heapq
) Efficiently retrieve frequently asked questions. - Counter: Count response categories using
collections.Counter
. - Search Algorithm: (e.g., linear search) Find if a question has already been asked.
- Sorting Algorithm: (optional) Display questions or responses in order.
- Graph: (optional, via adjacency list) Relate similar questions/responses.
How to Run the Project
- Create a script (e.g.
magic8_ds.py
) that implements the required structures and logic. - Run it using:
python magic8_ds.py
. - When prompted “Ask the Magic 8-Ball a question (or type 'exit')”, enter a yes/no question.
- The program returns a random answer and updates relevant data structures.
- Commands you can type:
history
- View last N question/answer pairs.stats
- See number of unique questions, most frequent ones, and category distribution.undo
- Remove last question/answer (if implemented).search <your question>
- Check if a question was asked before.sort
- View sorted list of questions or responses.- Repeat interaction until you type
exit
.
Key Concepts Learned
- Lists: Store possible answers.
- Dictionaries: Categorize and manage answer types.
- Sets: Keep track of all unique questions.
- Queues: Maintain recent question/answer history.
- Stacks: Enable undo functionality (optional).
- Tuples: Ensure Q&A history is immutable.
- Heaps: Quickly access most frequently asked questions.
- Counters: Track response frequency by category.
- Graphs: (Optional) Represent related or similar questions.
- Search Algorithms: Identify whether a question has been asked.
- Sorting Algorithms: Order the stored questions/responses for display.
- Random Selection: Use
random.choice()
for selecting an answer. - Control Flow: Use loops and conditionals for managing interaction.
- Input/Output: Display responses and take user queries.
Possible Extensions
- Log all questions and answers to a file.
- Display live statistics using response history and counters.
- Enable user to add their own custom responses to categories.
- Improve UI with colored output and formatting.
- Build a graphical user interface (GUI).
- Visualize a graph of related questions/responses.
Troubleshooting & Debugging
See the full Troubleshooting & Debugging guide for help.