Welcome to Kickoffs - Coding - Oct 06 2023 - Python - Trading Platform
Problem Description:-
first input line represnts number of events "N"
subsequent line of i < N represent individuals inputs of events.
<Event> <Stock_name> <Quantity>
| Events | Portfolio | Net Profit |
|---|---|---|
| But Stock1 | 789 | |
| Change Stock1 | -666 | |
| Buy Stock5 | 662 | |
| Change Stock5 | -130 | |
| Buy Stock15 | 414 | |
| Change Stock15 | -565 | |
| Buy Stock35 | 732 | |
| Sell Stock15 | 345 | |
| Buy Stock17 | 784 | |
| Query | -845444 |
def getNetProfit(events):
profit =0
d= {}
temp = []
for i in events:
print(i)
if "BUY" in i:
stock = i.split()[1]
quantity = i.split()[2]
d[stock] = quantity
# return profit
if "CHANGE" in i:
stock = i.split()[1]
quantity = i.split()[2]
st_value =d[stock]
temp.append(int(st_value)*int(quantity))
if "QUERY" in i:
profit = sum(temp)
if "SELL" in i:
try:
stock = i.split()[1]
quantity = i.split()[2]
st_value =d[stock]
new_value = int(st_value) - int(quantity)
d[stock] = new_value
except KeyError:
raise KeyError("Stock Not Found")
return profit
events = [
'BUY stock57 789',
'CHANGE stock57 -666',
'BUY stock196 662',
'CHANGE stock196 -130',
'BUY stock860 414',
'CHANGE stock860 -565',
'BUY stock61 732',
'SELL stock860 69',
'BUY stock269 794',
'QUERY']
Net_profit = getNetProfit(events)
print(Net_profit)
Q2. User-Friendly Password System
Problem Description:-
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'authEvents' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts 2D_STRING_ARRAY events as parameter.
#
# def authEvents(events):
# # Write your code here
def authEvents(events):
print(events)
import string
h = list(string.ascii_letters)+ [str(d) for d in range(10)] # + [""]
# Write your code here
ans = []
for i in range(len(events)):
def f(x):
z = ord("%s" %x)
return z
def hash_val(lst, ln):
count =1
w=[]
for ij in lst:
t= f(ij) * (131** (ln-count))
w.append(t)
count= count+1
return sum(w) % (10**9+7)
if events[i][0] == 'setPassword':
lst= list(events[i][1])
ln = len(lst)
res =[]
res.append(hash_val(lst, ln))
for k in h:
lst_add= list(events[i][1])
lst_add.append(k)
ln_add = len(lst_add)
res.append(hash_val(lst_add, ln_add))
print(res)
if events[i][0] == "authorize":
if int(events[i][1]) in res:
ans.append(1)
else:
ans.append(0)
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
events_rows = int(input().strip())
events_columns = int(input().strip())
events = []
for _ in range(events_rows):
events.append(input().rstrip().split())
result = authEvents(events)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
