Skip to content

Instantly share code, notes, and snippets.

@binhngoc17
binhngoc17 / even_tree.py
Created November 23, 2014 09:55
Break the tree into forest of event trees
import fileinput
inputs = fileinput.input()
input_vals = []
for n in inputs:
input_vals.append(n.replace('\n', ''))
numVertices, numEdges = [int(k) for k in input_vals[0].split(' ')]
@binhngoc17
binhngoc17 / matrix_flip.py
Created November 23, 2014 04:44
Matrix Flip Problem
import fileinput
inputs = fileinput.input()
input_vals = []
for n in inputs:
input_vals.append(n.replace('\n', ''))
numRows, numCols=[int(i) for i in input_vals[0].split(' ')]
countMaxWishes = {}
@binhngoc17
binhngoc17 / mod_func.cpp
Created November 22, 2014 19:25
mod funcs
long modPow(long a, long x, long p) {
//calculates a^x mod p in logarithmic time.
long res = 1;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x /= 2;
}
@binhngoc17
binhngoc17 / play_game.py
Created November 22, 2014 15:11
Play Game Problem in Hacker Rank
import sys
import string
import fileinput
import pprint
inputs = fileinput.input()
input_vals = []
for n in inputs:
input_vals.append(n.replace('\n', ''))
@binhngoc17
binhngoc17 / stock_price.py
Created November 22, 2014 13:08
Stock Price Calculation
import sys
import string
import fileinput
import pprint
inputs = fileinput.input()
input_vals = []
for n in inputs:
input_vals.append(n.replace('\n', ''))
@binhngoc17
binhngoc17 / tilling_filling.py
Created November 22, 2014 11:04
Calculate Primes & Tile filling
import sys
import string
import fileinput
import pprint
inputs = fileinput.input()
# def calc_prime(N):
vals = [int(N.replace('\n', '')) for N in inputs]
@binhngoc17
binhngoc17 / coin_change.py
Created November 22, 2014 05:25
Coin Change Dynamic Programming
import sys
import string
import fileinput
import pprint
inputs = fileinput.input()
coins = [int(num) for num in inputs[0].rstrip().split(', ')]
total = int(inputs[1])
# coins = [int(num) for num in numbers[:-1]]
@binhngoc17
binhngoc17 / min_cost_toys.py
Created November 21, 2014 06:08
(Dynamic Programming) Minimum Cost Problem
import sys
import string
import fileinput
inputs = fileinput.input()
numToys = int(inputs[0].replace('\n', ''))
toys = inputs[1].replace('\n', '')
toys = sorted([int(w) for w in toys.split(' ')])
minus_four = [w - 4 for w in toys]
prev_toys = {}
@binhngoc17
binhngoc17 / count_ending.py
Created November 19, 2014 14:42
Count ending number
import sys
import pprint
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
str_val = test.replace('\n', '')
n,p = [int(k) for k in str_val.split(' ')]
res = {}
@binhngoc17
binhngoc17 / postfix_eval.py
Created November 19, 2014 13:57
Evaluation of Postfix Expression
import sys
test_cases = open(sys.argv[1], 'r')
def eval_postfix(exp):
stack = []
for i in exp:
try:
val = int(i)
stack.append(val)
except: