Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python3
- import sys
- import os
- buying_history = [];
- buying_history_pointer = 0;
- def getFromTable(shares):
- print("-----------------------------")
- print("Asked for",shares,"shares")
- wanted = shares
- temp_total = 0.0;
- global buying_history_pointer;
- while(wanted > 0):
- print("Wanted", wanted, "more")
- print("Available ",buying_history)
- if wanted < buying_history[buying_history_pointer][0]:
- temp_total = temp_total + wanted * buying_history[buying_history_pointer][1]
- buying_history[buying_history_pointer][0] = buying_history[buying_history_pointer][0] - wanted
- print("Finished with",buying_history)
- wanted = 0
- elif wanted >= buying_history[buying_history_pointer][0]:
- temp_total = buying_history[buying_history_pointer][0] * buying_history[buying_history_pointer][1]
- wanted = wanted - buying_history[buying_history_pointer][0];
- buying_history[buying_history_pointer][0] = 0
- buying_history_pointer+=1
- return temp_total;
- # Complete the function below.
- def calculateTax(trades):
- processed_trades = [];
- total_tax = float(0);
- print(trades)
- # Create a 2D list
- for i in range(0, len(trades)):
- processed_trades.append(trades[i].split(","))
- # Create a history of buying
- for i in range(0, len(processed_trades)):
- #Whenever there is a buy order
- if processed_trades[i][2] == "B":
- temp = [int(processed_trades[i][3]),float(processed_trades[i][4])]
- buying_history.append(temp)
- for i in range(0,len(processed_trades)):
- #Check if is buy or sell
- if processed_trades[i][2] == "B":
- print("Buy order")
- else:
- print("Sell order")
- cost_price = float(getFromTable(int(processed_trades[i][3])))
- selling_price = float(processed_trades[i][3]) * float(processed_trades[i][4])
- margin = selling_price - cost_price
- print("C.P",cost_price)
- print("S.P", selling_price)
- print("Margin", margin)
- if margin < 0:
- #Loss
- print("Loss")
- else:
- print("Profit")
- #Calculate the tax
- tax = (25/100) * margin
- total_tax= total_tax + tax
- print("Total tax so far :",total_tax)
- return 0;
- if __name__ == "__main__":
- trades = []
- trades.append("2015-01-03,AAPL,B,50,80.0")
- trades.append("2015-01-05,AAPL,B,60,100.0")
- trades.append("2015-02-05,AAPL,S,70,130.0")
- trades.append("2015-02-08,AAPL,S,10,90.0")
- trades.append("2015-03-10,AAPL,S,80,120.0")
- trades.append("2015-03-12,AAPL,B,10,70.0")
- trades.append("2015-04-08,AAPL,B,70,160.0")
- res = calculateTax(trades);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement