An order book holds resting limit orders: buyers' bids (the most they'll pay) and sellers' asks (the least they'll accept). The best bid is the highest bid, the best ask the lowest ask: together, the "top of book".
Implement top_of_book(orders). Each order is (side, price, qty) with side "buy" or "sell" and positive integer price and qty. Return (best_bid, best_ask), each a tuple (price, total_qty) summing every order at that price on that side, or None for an empty side.
orders = [
("buy", 99, 5),
("buy", 100, 2),
("sell", 103, 4),
("buy", 100, 3),
("sell", 101, 1),
]
top_of_book(orders) # ((100, 5), (101, 1))
top_of_book([("sell", 50, 7)]) # (None, (50, 7))
top_of_book([]) # (None, None)
Don't assume bids sit below asks: a bid and an ask may share a price and still belong to separate sides.
Show hint
best bid is a max over buy prices, best ask a min over sell prices; sum quantities per (side, price) in a dict.