Q.Write a menu driven python program using queue, to implement movement of shuttlecock in it's box.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →This program simulates the movement of shuttlecocks in a box using a menu-driven Python program. It implements a queue data structure, allowing you to add shuttlecocks (enqueue), remove them (dequeue), and display the current contents of the box, following a First-In, First-Out (FIFO) principle.
When managing items in a sequential order, where the first item added is always the first item to be processed or removed, a queue is the ideal data structure. Think of a line at a ticket counter: the person who arrives first is the first to be served. Similarly, in our "shuttlecock box," we want to add shuttlecocks to one end and remove them from the other, ensuring that the shuttlecock that was placed in the box earliest is the one taken out first. This is precisely the First-In, First-Out (FIFO) principle that queues embody.
In Python, we can easily implement a queue using a standard list.
- To add an item to the rear of the queue (enqueue), we use the
append()method. - To remove an item from the front of the queue (dequeue), we use the
pop(0)method. The0index specifies that the item at the beginning of the list should be removed.
The program will be menu-driven, meaning it will present a set of options to the user (like adding a shuttlecock, removing one, or viewing the box's contents) and perform actions based on their choice. This makes the program interactive and user-friendly.
# Initialize an empty list to represent our shuttlecock box (queue)
shuttlecock_box = []
def add_shuttlecock():
"""
Adds a shuttlecock to the rear of the box (enqueues an item).
"""
shuttlecock_id = input("Enter shuttlecock ID/name to add: ")
shuttlecock_box.append(shuttlecock_id)
print(f"'{shuttlecock_id}' added to the box.")
def remove_shuttlecock():
"""
Removes a shuttlecock from the front of the box (dequeues an item).
"""
if not shuttlecock_box: # Check if the queue is empty
print("The shuttlecock box is empty. No shuttlecock to remove.")
else:
removed_shuttlecock = shuttlecock_box.pop(0) # Remove the first item
print(f"'{removed_shuttlecock}' removed from the box.")
def display_shuttlecocks():
"""
Displays all shuttlecocks currently in the box, from front to rear.
"""
if not shuttlecock_box:
print("The shuttlecock box is empty.")
else:
print("\n--- Shuttlecocks in the Box (Front to Rear) ---")
for i, shuttlecock in enumerate(shuttlecock_box):
print(f"{i+1}. {shuttlecock}")
print("----------------------------------------------")
def main_menu():
"""
Presents the main menu and handles user choices.
"""
while True:
print("\n--- Shuttlecock Box Management ---")
print("1. Add Shuttlecock")
print("2. Remove Shuttlecock")
print("3. Display Shuttlecocks")
print("4. Exit")
print("----------------------------------")
choice = input("Enter your choice (1-4): ")
if choice == '1':
add_shuttlecock()
elif choice == '2':
remove_shuttlecock()
elif choice == '3':
display_shuttlecocks()
elif choice == '4':
print("Exiting program. Goodbye!")
break # Exit the while loop
else:
print("Invalid choice. Please enter a number between 1 and 4.")
# Run the main menu
if __name__ == "__main__":
main_menu()
Key Lines Explanation
shuttlecock_box = []: This line initializes an empty Python list, which will serve as our queue. In this context, the "box" is conceptually represented by this list.shuttlecock_box.append(shuttlecock_id): This is the enqueue operation. Theappend()method addsshuttlecock_idto the end (rear) of theshuttlecock_boxlist. This ensures that new shuttlecocks are always placed at the back of the line.removed_shuttlecock = shuttlecock_box.pop(0): This is the dequeue operation. Thepop(0)method removes and returns the item at index0(the front) of theshuttlecock_boxlist. This adheres to the FIFO principle, as the shuttlecock that was added first (and is therefore at the front) is the first to be removed.if not shuttlecock_box:: This condition is used to check if the queue is empty before attempting to remove an item or display its contents. Trying topop(0)from an empty list would result in anIndexError, so this check prevents such runtime errors.while True:: This creates an infinite loop for themain_menufunction, ensuring that the menu is repeatedly displayed until the user explicitly chooses to exit.break: When the user enters '4' to exit, this statement terminates thewhile Trueloop, ending the program.
For very large queues or performance-critical applications, Python's collections.deque (double-ended queue) is more efficient for queue operations than a standard list. deque provides append() for enqueue and popleft() for dequeue, both with time complexity, whereas list.pop(0) has complexity because all subsequent elements need to be shifted. For this problem, a list is perfectly acceptable and often taught first.
Expected Output
Here's a sample interaction with the program, demonstrating its functionality:
--- Shuttlecock Box Management ---
1. Add Shuttlecock
2. Remove Shuttlecock
3. Display Shuttlecocks
4. Exit
----------------------------------
Enter your choice (1-4): 1
Enter shuttlecock ID/name to add: Yonex AS-10 …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.