This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def random_array(sz,low=10,high = 100): | |
return map(lambda i:random.randint(low,high),xrange(sz)) | |
R = random_array(50,1,10) | |
print R | |
def minhops_dfs(lst): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transpose(X): | |
return map(list,zip(*X)) | |
def reverse_rows(X): | |
return map(lambda x:list(reversed(x)),X) | |
def reverse_cols(X): | |
return list(reversed(X)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rotate_right_90(X): | |
return reverse_rows(transpose(X)) | |
def rotate_right_180(X): | |
return reverse_rows(reverse_cols(X)) | |
def rotate_left_90(X): | |
return reverse_cols(transpose(X)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_increasing_subsequence_dfs(lst): | |
lis_seq = [] | |
sz = len(lst) | |
def lis(index,seq): | |
if len(seq) + (sz - index) < len(lis_seq): return #prune the rest. | |
for i in xrange(index,sz): | |
if seq[-1] < lst[i]: | |
seq.append(lst[i]) | |
lis(i+1,seq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_increasing_subsequence_dp(lst): | |
if not lst: return None | |
lis_len = [] | |
prev = [] | |
for i,v in enumerate(lst): | |
mx = 0 | |
mxi = -1 | |
for j in xrange(i): | |
if v > lst[j] and mx < lis_len[j]: #can v extend the current longest increasing subsequence? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
For a +ve integer n, print numbers from 0 to 2^n-1 such that numbers next to each other differ in exactly 1 bit. | |
""" | |
n = 3 | |
gray = lambda x : (x >> 1) ^ x | |
pad_left = lambda s,l: ''.join(['0']*(l-len(s))) + s | |
for i in xrange(1 << n): | |
print i,pad_left(bin(gray(i))[2:],n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
N = 10 | |
#prepare testcase | |
R = range(N) | |
rand_celeb = random.randint(0,N-1) | |
m = {} | |
for i in xrange(N): | |
if i != rand_celeb: | |
m[i] = [random.randint(0,N-1) for j in xrange(10)] + [rand_celeb] | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
N = 100 | |
#prepare testcase | |
R = range(N) | |
rand_celeb = list(set(random.randint(0,N-1) for i in xrange(5))) | |
m = {} | |
for i in xrange(N): | |
if i not in rand_celeb: | |
m[i] = [random.randint(0,N-1) for j in xrange(10)] +rand_celeb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def minhops_dfs(lst): | |
sz = len(lst) | |
min_hop = [0] * sz | |
def dfs(index,s,hops,min_hop): | |
if index >= sz: | |
if len(min_hop) > s: | |
min_hop[:] = hops | |
#min_hop.extend(hops) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
public class Main implements Callable<Integer> { |