top of page

Python Practice Set (Part 12): Fundamental Data Structures (Stacks & Queues)

ree

Organizing Your Data: An Introduction to Stacks and Queues in Python


Welcome to Part 12 of our Python coding series! As your programs become more complex, the way you organize your data becomes critically important. Data Structures are specialized formats for organizing, processing, and storing data. In this session, we'll build two of the most fundamental data structures from scratch using Python lists: the Stack (Last-In, First-Out) and the Queue (First-In, First-Out). Understanding these concepts is essential for solving a wide range of programming puzzles and real-world problems.


1. The Simple Stack (LIFO)


A stack follows the "Last-In, First-Out" (LIFO) principle, like a stack of plates. The last plate you add is the first one you take off.

  • Create a list called my_stack.

  • Push (add) three numbers onto the stack: 10, 20, and 30.

  • Pop (remove) an item from the stack and print it. Repeat two more times.

  • Observe the order in which the items are removed.

Hint: A list's .append() method is perfect for a push, and .pop() is perfect for a pop.


2. Simulating a Browser's Back Button (Stack Application)


A web browser's history is a perfect example of a stack. Create a browser_history list.

  1. Simulate visiting a few pages by "pushing" their URLs onto the stack (e.g., 'https://www.google.com/search?q=google.com', 'wikipedia.org', 'python.org').

  2. Now, simulate hitting the "back" button by "popping" the last visited page from the stack and printing "Going back to [URL]".

  3. Do this until the history is empty.


3. The Simple Queue (FIFO)


A queue follows the "First-In, First-Out" (FIFO) principle, just like a line of people waiting for a bus. The first person in line is the first person to get on.

  • Create a list called my_queue.

  • Enqueue (add) three names to the queue: "Alice", "Bob", and "Charlie".

  • Dequeue (remove) an item from the front of the queue and print it. Repeat two more times.

  • Observe that the items are removed in the same order they were added.

Hint: You'll need to .pop(0) to remove from the front of the list.


4. Managing a Printer Queue (Queue Application)


Simulate a printer that processes jobs in the order they are received.

  • Create a print_queue list.

  • Add three documents to the queue (e.g., "report.docx", "presentation.pptx", "datasheet.xlsx").

  • Write a loop that continues as long as the queue is not empty. In each iteration, "process" a job by dequeuing the item from the front and printing "Printing [document name]".


5. Balanced Parentheses Checker (Advanced Stack Application)


This is a classic coding interview question. Write a function is_balanced that takes a string containing different types of brackets: (), [], and {}. The function should return True if the brackets are balanced and correctly nested, and False otherwise.

  • Example 1: is_balanced("()[]{}") should return True.

  • Example 2: is_balanced("([{}])") should return True.

  • Example 3: is_balanced("([)]") should return False.

Hint: When you see an opening bracket, push it onto a stack. When you see a closing bracket, pop from the stack and see if it's the matching opening bracket.




Comments


bottom of page