Question: I need to use Python programming and I think it needs to use heap. An online computer system for trading stocks needs to process orders
I need to use Python programming and I think it needs to use heap.


An online computer system for trading stocks needs to process orders of the form "buy 100 shares at $x each" or "sell 100 shares at $y each." A buy order for $x can only be processed if there is an existing sell order with price $y such that y S x. Likewise, a sell order for $y can only be processed if there is an existing buy order with price $x such that y s x. If a buy or sell order is entered but cannot be processed, it must wait for a future order that allows it to be processed. Write a class called Trading that will allow buy and sell orders to be entered in O(log n) time, independent of whether or not they can be immediately processed. You can assume that all orders are for 100 shares. Your class should have buy and sell methods and a method that will list all of the unexecuted orders. A sample execution follows. (Included the trader's name in the buy and sell orders.)In [2]: t = Trading() In [3]: t. sell(50, "s1") In [4]: t. sell(50, "s2") In [5]: t. buy (45, "b1") In [6]: t. sell(45,"53") Trade Executed. Seller: $3 45 Buy trade: (45, 'b1' ) In [7]: t. sell(40,"54") In [8]: t. buy (40, "b2") Trade Executed. Buyer: b2 40 Sell trade: (40, 's4' ) In [9]: t. buy (50, "b3") Trade Executed. Buyer: b3 50 Sell trade: (50, 's1' ) In [10]: t. buy (45, "b4") In [11] : t. close() Open Buy Orders (45, 'b4' ) Open Sell Orders (50, 's2' )
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
