Skip to content

Instantly share code, notes, and snippets.

View h2rashee's full-sized avatar

Harris Rasheed h2rashee

View GitHub Profile
@h2rashee
h2rashee / Makefile
Last active May 7, 2020 22:20
Datadog Take Home Assignment
run:
python3 main.py $(input_file)
test:
python3 main_test.py -v -b
format:
/Users/harrisrasheed/Library/Python/2.7/lib/python/site-packages/pycodestyle.py *.py --max-line-length=100
@h2rashee
h2rashee / shortest_path.py
Created April 22, 2020 23:42
Find the shortest path in a graph (distance and nodes)
# Shortest Path
# G = [][]
# G[i][j] is the distance from i to j, and if G[i][j] < 0 means they are not connected.
# node ids ranging from 0 to len(G)-1
# src, dst, return the shortest distance.
# revise it so that we can get one shortest path [src, node1, node2.... dst]
G=[
[0, 2, -1 , 1, 7, -1],
[-1, 0, 3, 5, -1, -1],
@h2rashee
h2rashee / concurrent.py
Last active April 25, 2020 21:31
Given a list of values, a function and the amount of concurrency to work with, apply the function so that it processes the function at the same time instead of blocking
from multiprocessing import Pool
def fn(x):
return x+1
def solution(vals, concurrency=1):
if concurrency < 1:
raise Exception('Concurrency value invalid')
with Pool(concurrency) as p:
@h2rashee
h2rashee / tic_tac_toe.py
Last active May 21, 2020 07:13
Tic Tac Toe
import random
ROWS = 3
COLS = 3
EMPTY = '-'
USER_MOVE = 'X'
AI_MOVE = 'O'
@h2rashee
h2rashee / decipher.py
Last active April 20, 2020 16:26
Given an encrypted message string, determine the possible valid strings could match the message.
import requests
GARBLED_CHAR = '?'
class Trie:
def __init__(self):
self.children = dict()
self.is_word = False
@h2rashee
h2rashee / stock_exchange.py
Created March 6, 2020 22:31
Given a stream of buy/sell orders on an exchange, execute the trades favouring the best possible outcome for the trades in order and report the number of executed trade units.
def order_book(orders):
# Ordered in descending order
buy_list = []
# Ordered in ascending order
sell_list = []
num_executed = 0
for order in orders:
if order[2] == "buy":
@h2rashee
h2rashee / trie.py
Created March 3, 2020 17:58
Given a word, find its index location from a list. Data structure of your choice and fuzzy matching to be supported (one character off is possible)
# dictionary = ["foo","bar","bat"]
# getIndex("foo") => 0
# getIndex("bar") => 1
# getIndex("bat") => 2
# getIndex("fox") => 0
# getIndex("fxx") => -1
# getIndex("fo") => -1
# getIndex("fooo") => -1
@h2rashee
h2rashee / paran_matching.py
Created February 25, 2020 18:25
Given a string with parantheses, return the number of parantheses needed to ensure there are enough matching.
# (()) -> 0
# ))(( -> 4
# ()() -> 0
# (())) -> 1
# ()())( -> 2
def find_num_missing_parans(inp):
i = 0
j = 1
res = 0
@h2rashee
h2rashee / monotonic.py
Last active March 4, 2020 16:09
Given a list, determine if the list is monotonic
# 1, 2, 3, 4, 5 -> True
# 64, 32, 16, 8 -> True
# 1, 1, 2, 3, 5 -> True
# 10, 20, 15 -> False
# 0, 0, 0, 0 -> True
# 1 -> True
# -> True
def is_monotonic(nums):
is_ascending = None
@h2rashee
h2rashee / phone_number_chess.py
Last active July 23, 2018 20:07
Given a grid of characters, generate valid phone numbers based on the specified parameters
# International prefixes is a vague statement. Does that mean '+'' is allowed? Not clarified so assuming not.
# Notes on possible improvements:
# - Can create an inheritable/extensible method structure to support any piece's
# possible movements given a current position and a board instead of the current hard-coded methods
# The complexity of this solution depends on the size of the board
# and the length of the phone number being generated
def get_next_bishop_positions(x, y, x_lim, y_lim):