Skip to content

Instantly share code, notes, and snippets.

View jhidajat's full-sized avatar
🧠

Jeremy Hidajat jhidajat

🧠
View GitHub Profile
from mns.account import Account
class CloudQueue:
def __init__(self, size):
MNS_ACCOUNT = Account(MNS_ENDPOINT, MNS_ACCESS_KEY_ID, MNS_ACCESS_KEY_SECRET, MNS_SECURITY_TOKEN)
self.queue = MNS_ACCOUNT.get_queue('work-items')
self.long_polling_duration = 3
"""
A naive approach is converting the tree to its (in,pre,post)order forms, leaving empty children as NULL.
However, since the nature of the tree might not be a complete binary tree, this approach will result in
the serialization size to always be 2^h regardless of the tree structure. In order to mitigate this,
construct a serialization where each node keeps track of its parent, and side of which it belongs to.
Thus, it will be convenient to label each node with an ID. As such, time complexity and space complexity
will be linear O(N) for N is the number of nodes in the given BT.
This program utilize BFS to serialize the binary tree. The serialization will be in the form:
VAR=0.01;
WAVE=zeros(10,381); % create matrix of zeroes of dimension 10 x 381
d=-20:1:360; % 3D vector [-20,1,360]
i=0
for i=1:10 % iterate i from 1 to 10
A=normrnd(1,0.08); % random number from normal distribution with mu=1, sigma=0.08
f=normrnd(1,0.01); % random number from normal distribution with mu=1, sigma=0.08
phi=normrnd(10*(pi/180),0.1) % random number from normal distribution with mu=10*(pi/180), sigma=0.1
"""
Order Book
Take in a stream of orders (as lists of limit price, quantity, and side, like ["155", "3", "buy"]) and return the total number of executed shares.
Rules
- An incoming buy is compatible with a standing sell if the buy's price is >= the sell's price. Similarly, an incoming sell is compatible with a standing buy if the sell's price <= the buy's price.
- An incoming order will execute as many shares as possible against the best compatible order, if there is one (by "best" we mean most favorable to the incoming order).
- Any remaining shares will continue executing against the best compatible order, if there is one. If there are shares of the incoming order remaining and no compatible standing orders, the incoming order becomes a standing order.
@jhidajat
jhidajat / basic_calc.py
Created December 1, 2019 01:23
Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or multiply sign *, non-negative integers and empty spaces. There can only be at most two operands per expression.
from operator import add, mul
from functools import reduce
def calc(s):
def _calc(s,i):
operands, operator = [], lambda x : x
while i < len(s):
if s[i] =='(':
evaluated, i = _calc(s,i+1)
operands.append(evaluated)
elif s[i] == ')':
@jhidajat
jhidajat / naive_recursion.py
Created November 30, 2019 04:35
Basic Calculator with only + and *
from functools import reduce
from operator import add, mul
def calc(s):
if not s:
return 0
n = len(s)
operands = []
operator = None
def spiral(matrix):
n, m = len(matrix), len(matrix[0])
DIR = [(0,1),(1,0),(0,-1),(-1,0)]
_next = lambda x, d: (x[0]+d[0],x[1]+d[1])
_dir = 0
rc_count = [m, n-1]
count_type = 0
count = n*m
curr = [0,-1]
"""
A trade is defined as a string containing the 4 properties given below separated by commas:
- Symbol (4 alphabetical characters, left-padded by spaces)
- Side (either "B" or "S" for buy or sell)
- Quantity (4 digits, left-padded by zeros)
- ID (6 alphanumeric characters)
e.g. "AAPL,B,0100,ABC123" represents a trade of a buy of 100 shares of AAPL with ID "ABC123"
Given two lists of trades - called "house" and "street" trades, write code to create groups
of matches between trades and return a list of unmatched house and street trades sorted
alphabetically. Without any matching, the output list would contain all elements of both house
"""
---------------
P R O B L E M
---------------
You have N securities available to buy that each has a price Pi.
Your friend predicts for each security the stock price will be Si at some future date.
But based on volatility of each share, you only want to buy up to Ai shares of each security i.
Given M dollars to spend, calculate the maximum value (revenue not profit) you could potentially
achieve based on the predicted prices Si (and including any cash you have remaining).
"""
Given a series of stocks:
[
(buy, 10 shares, google, $1200),
(sell, 8 shares, google, $1300),
(buy, 5 shares, google, $1500),
(sell, 4 shares, google, $1100),
]
Determine the profit/loss of the trades.