Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions custom_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,44 @@

class Queue:
def __init__(self):
self.items = []
self.items = []

def enqueue(self, item):
# TODO: Add an item to the end of the queue
pass
# Add an item to the end of the queue
self.items.append(item)

def dequeue(self):
# TODO: Remove and return the item from the front of the queue
pass
# Remove and return the item from the front of the queue
if self.is_empty():
return None
return self.items.pop(0)


def peek(self):
# TODO: Return the item at the front of the queue without removing it
pass
# Return the item at the front of the queue without removing it
if self.is_empty():
return None
return self.items[0]

def is_empty(self):
# TODO: Return True if the queue is empty
pass
# Return True if the queue is empty
return len(self.items) == 0

def select_and_announce_winner(self):
"""
Randomly selects a winner from the queue.
Dequeues all items up to and including the winner.
Returns the name of the winning customer.
"""
# TODO: Implement winner selection and dequeue process
pass
if self.is_empty():
return None

# choose a random index
winner_index = random.randint(0, len(self.items) - 1)
winner = self.items[winner_index]

# remove everyone up to and including the winner
for _ in range(winner_index + 1):
self.dequeue()

return winner
29 changes: 28 additions & 1 deletion custom_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,31 @@ def is_valid_parentheses(s: str) -> bool:
Only (), {}, and [] are considered valid.
"""
# TODO: Implement stack logic to validate parentheses
pass
stack = [] # store opening brackets

# dictionary to match closing to opening brackets
matches = {
")": "(",
"}": "{",
"]": "["
}

# go through each character in the string
for char in s:
if char in "({[":
# push opening brackets onto the stack
stack.append(char)
elif char in ")}]":
# if there's no opening bracket to match
if len(stack) == 0:
return False

# pop the last opening bracket
top = stack.pop()

# check if it matches the current closing bracket
if top != matches[char]:
return False

# if stack is empty, all brackets were matched
return len(stack) == 0