Untitled
unknown
plain_text
a year ago
2.0 kB
1
Indexable
Never
class PriceLadder(): def __init__(self): self.buy_orders = [] # Store buy orders as tuples (price, quantity) self.sell_orders = [] # Store sell orders as tuples (price, quantity) def AddOrder(self, order): if order.quantity > 0: self.buy_orders.append((order.price, order.quantity)) else: self.sell_orders.append((-order.price, -order.quantity)) def DeleteOrder(self, delete): self.buy_orders = [(price, quantity) for price, quantity in self.buy_orders if price != delete.price] self.sell_orders = [(price, quantity) for price, quantity in self.sell_orders if price != delete.price] def DeletePriceLevel(self, deletePriceLevel): self.buy_orders = [(price, quantity) for price, quantity in self.buy_orders if price != deletePriceLevel.price] self.sell_orders = [(price, quantity) for price, quantity in self.sell_orders if price != deletePriceLevel.price] def GetPriceLevels(self, artist, numberOfPriceLevels): print(artist) # Get the top `numberOfPriceLevels` buy orders buy_price_levels = self.buy_orders[:numberOfPriceLevels] for price, quantity in buy_price_levels: print(f"0 {price} {quantity}") # Get the top `numberOfPriceLevels` sell orders sell_price_levels = self.sell_orders[:numberOfPriceLevels] for price, quantity in sell_price_levels: print(f"{quantity} {-price} 0") priceLadder = PriceLadder() for line in sys.stdin: tokens = line.split() if tokens[0] == "ADD": priceLadder.AddOrder(AddOrder(*tokens[1:])) elif tokens[0] == "DEL": priceLadder.DeleteOrder(DeleteOrder(*tokens[1:])) elif tokens[0] == "DEL_PRICE": priceLadder.DeletePriceLevel(DeletePriceLevel(*tokens[1:])) else: artist = tokens[0] numberOfPriceLevels = int(tokens[1]) priceLadder.GetPriceLevels(artist, numberOfPriceLevels)