def solution(s): q = Queue(len(s)) total = 0 for token in s: if q.empty(): q.put(token) total += 1 else: if token > q.peek(): q.put(token) total += 1 else: while not q.empty() and token < q.peek(): q.get() if q.empty() or token != q.peek(): q.put(token) total += 1 return total
def bft(self, starting_vertex): """ Print each vertex in breadth-first order beginning from starting_vertex. """ # Validate the passed parameter: is the passed vertex valid? if starting_vertex not in self.vertices: # vertex not found, nothing to do print("vertex {vtx} not found, nothing to do".format( vtx=starting_vertex)) return False # Define a vertex search queue - "vertexes to traverse" vert_queue = Queue() # Define a vertex status map - "status of the vertex's traversal" vert_status = {} # Set the initial status for each vertex as "not_started" for vtx in self.vertices: vert_status[vtx] = "search_not_started" # Start the breadth search with the passed vertex vert_status[starting_vertex] = "search_started" # Place the start vertex in our queue vert_queue.enqueue(starting_vertex) # Process while there are vertices in the queue while vert_queue.size() != 0: tmp_vtx = vert_queue.peek( 0) # access the top of the queue (but don't dequeue) if tmp_vtx == None: print("error taking a peek at the queue") quit() # Iterate through the current vertex's neighbors # and initate the search process on those vertices for vrtx in self.get_neighbors(tmp_vtx): if vert_status[vrtx] == "search_not_started": vert_status[vrtx] = "search_started" vert_queue.enqueue(vrtx) # Dequeue the current vertex vert_queue.dequeue() print(tmp_vtx) vert_status[tmp_vtx] = "search_completed"
class User: """ User class serves as an abstraction for users. Users are only allowed to shop at only one store. User instances are treated as regular users. """ ##### Part 4.1 ##### user_counter = 0 def __init__(self, name, store): """ Constructor for User instances. Each instance is initialized with the user's username and the store they shop at. Instance attributes are: name, store, balance, their user ID, purchase history, and shopping cart. """ # YOUR CODE GOES HERE # self.name = name self.store = store self.balance = 0 self.id = User.user_counter User.user_counter += 1 self.purchase_history = Stack() self.cart = Queue() self.store.add_user(self) def __str__(self): """ Return the string representation as: "standard user: {name} - {balance}$" """ # YOUR CODE GOES HERE # return "standard user: {} - {}$".format(self.name, self.balance) def __repr__(self): """ Return the string representation as: "USER<{id}>" """ # YOUR CODE GOES HERE # return "USER<{}>".format(self.id) def set_name(self, new_name): """ Replaces a user's old name to new_name """ # YOUR CODE GOES HERE # self.name = new_name def get_name(self): """ Returns the user's current name """ # YOUR CODE GOES HERE # return self.name def set_balance(self, amount): """ Set the user's current balance to the given amount """ # YOUR CODE GOES HERE # self.balance = amount def get_balance(self): """ Returns the user's current balance """ # YOUR CODE GOES HERE # return self.balance def add_balance(self, amount): """ Add the given amount to the current balance """ # YOUR CODE GOES HERE # self.balance += amount def last_purchase(self): """ Return the user's most recent purchase """ # YOUR CODE GOES HERE # return self.purchase_history.peek() def view_history(self): """ Print the user's entire purchase history """ # YOUR CODE GOES HERE # print(self.purchase_history) def view_cart(self): """ Print all items in the user's cart """ # YOUR CODE GOES HERE # print(self.cart) def clear_cart(self): """ Remove all items from the user's cart """ # YOUR CODE GOES HERE # self.cart = Queue() ##### Part 5.2 ##### def add_cart(self, product_id): """ Add the product with the given product id to the user's cart """ # YOUR CODE GOES HERE # item = self.store.get_product(product_id) if item == None: pass else: self.cart.enqueue(item) def checkout(self): """ Purchase all items in the user's cart and return a list of all purchased products. As soon as a product cannot be purchased, purchasing is stopped. Anything that was purchased successfully is returned in a list. """ # YOUR CODE GOES HERE # purchases = [] while not(self.cart.is_empty()): product = self.cart.peek() purchase = self.store.order(self.id, product.id) if purchase: self.cart.dequeue() purchases.append(purchase.product) self.purchase_history.push(purchase) else: break return purchases ##### Part 5.3 ##### def undo_purchase(self): """ Undo the user's most recent purchase. If there is no shopping history, print "USER: no purchase history" and do nothing else. Else, attempt to undo the purchase. If the store allows the request, remove that purchase from the user's purchase history. """ # YOUR CODE GOES HERE # if self.purchase_history.is_empty(): print("USER: no purchase history") else: last_purchase = self.purchase_history.peek() user = last_purchase.user user_id = user.id product = last_purchase.product request = self.store.undo_order(user_id, product) if request: user.purchase_history.pop()