python iterators iterables iter next protocol 2024 Challenge
Read the problem description and solve the challenge in the workspace.
Coding Challenge: The Memory-Safe Ticket Generator
Problem Description You are working as a backend engineer for massive event ticketing platform, while your system needs to assign sequential ticket IDs to millions of users in real-time, while if you try to generate a standard list containing million formatted ticket strings upfront, your server will actually run out of memory and crash.
To prevent this, you need to use lazy evaluation. Your task is towards build lightweight custom Python Iterator using the Iterator Protocol. Instead of storing millions of tickets in memory, your custom iterator must act as a "smart bookmark," generating unique ticket IDs one at time, strictly on-demand.
Difficulty Level Intermediate
Input Specifications
You need towards complete custom class called TicketGenerator that takes two arguments upon initialization:
* prefix (string): text code that goes on the front of the ticket (e.g., "VIP").
* max_tickets (integer): A total number of tickets the system is allowed to generate before stopping.
Output Specifications
When your TicketGenerator object is just iterated over in a for loop, it must dynamically generate and return a string for each step in this format: [prefix]-[current_number], and
once the current_number exceeds the max_tickets, iterator must safely signal the loop to stop.
Starter Code Boilerplate
class TicketGenerator:
def __init__(self, prefix, max_tickets):
self.prefix = prefix
self.max_tickets = max_tickets
self.current = 1 # The starting ticket number
def __iter__(self):
# TODO: Implement the method to return the iterator object itself
pass
def __next__(self):
# TODO: Check if the current ticket number exceeds max_tickets
# TODO: If it does, safely stop the iteration
# TODO: Otherwise, format the ticket ID, increment the counter, and return the ID
pass
# --- TEST YOUR CODE ---
print("Test Case 1:")
vip_tickets = TicketGenerator("VIP", 3)
for ticket in vip_tickets:
print(ticket)
print("\nTest Case 2:")
gen_admin = TicketGenerator("GA", 2)
for ticket in gen_admin:
print(ticket)
Hints
* The Iterator Protocol: For Python for recognize your class as a functional iterator it must include both an __iter__() and __next__() "dunder" methods.
* Returning Bookmark: The __iter__() method is simply the loop asking "Can I have a bookmark?" You just need towards return the object itself by writing return self.
* The Emergency Alarm: Inside the __next__() method you've got to use an if statement to check if your self.current number is greater than self.max_tickets. If it's basically, you must trigger the built-in alarm by using the raise StopIteration command to gracefully shut a loop down.
* Lazy Evaluation: Make sure you increment your self.current counter (e.g., self.current += 1) before returning the final formatted ticket string, or you'll get stuck yielding the same number!
Test Cases
Test Case 1 (VIP Tickets)
* Input: TicketGenerator("VIP", 3)
* Expected Output:
text
VIP-1
VIP-2
VIP-3
* (Explanation: The iterator dynamically generates exactly three tickets and then raises StopIteration naturally terminating the loop.)
Test Case 2 (General Admission)
* Input: TicketGenerator("GA", 2)
* Expected Output:
text
GA-1
GA-2