Merge branch 'master' of https://bitbucket.org/vishap/test.git
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
|
||||
" Setup templates.
|
||||
"
|
||||
let g:git_root=system("git rev-parse --show-toplevel | tr -d '\\n'")
|
||||
let g:templates_no_autocmd = 0
|
||||
let g:templates_directory=[ g:git_root . '/templates' ]
|
||||
let g:templates_global_name_prefix='_'
|
||||
let g:templates_debug=1
|
||||
|
||||
let s:vimrc=expand('<sfile>:p:r')
|
||||
let s:host=hostname()
|
||||
|
||||
|
||||
35
puzzles/interviews/training/num_valid_comb.py
Normal file
35
puzzles/interviews/training/num_valid_comb.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#
|
||||
# The number of valid combinations of a strings for given input array a[],
|
||||
# where a=>1, z => 26, and 0 <= a[i] <= 9
|
||||
# {1,1,1} => {aaa, ak, ka} => 3
|
||||
# {1,1,0} => {aj} => 1 "/>
|
||||
#
|
||||
|
||||
|
||||
def valid_comb_aux(s, p, b, e, dyn):
|
||||
if b in dyn:
|
||||
print("dyn", p)
|
||||
return dyn[b]
|
||||
if b == e:
|
||||
print("fin", p)
|
||||
return 1
|
||||
count = 0
|
||||
for i in range(b+1, e+1):
|
||||
if 0 < int(s[b:i]) and int(s[b:i]) <= (ord('z')-ord('a')+1):
|
||||
p.extend(chr(ord('a')+int(s[b:i])-1))
|
||||
count = count + valid_comb_aux(s, p, i, e, dyn)
|
||||
p.pop()
|
||||
dyn[b] = count
|
||||
return count
|
||||
|
||||
|
||||
def valid_comb(s):
|
||||
dyn = {}
|
||||
return valid_comb_aux(s, [], 0, len(s), dyn)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(valid_comb("111"))
|
||||
print(valid_comb("110"))
|
||||
print(valid_comb("22222"))
|
||||
# valid_comb("12131456879234522222")
|
||||
48
puzzles/interviews/training/permutation.py
Normal file
48
puzzles/interviews/training/permutation.py
Normal file
@@ -0,0 +1,48 @@
|
||||
#
|
||||
# Prints all permutations.
|
||||
#
|
||||
def permutation(s, b, e):
|
||||
if b+1 == e:
|
||||
print(s)
|
||||
else:
|
||||
for i in range(b, e-1):
|
||||
permutation(s, b+1, e)
|
||||
c = s[i+1]
|
||||
s[i+1] = s[i]
|
||||
s[i] = s[b]
|
||||
s[b] = c
|
||||
permutation(s, b+1, e)
|
||||
c = s[b]
|
||||
s[b] = s[e-1]
|
||||
s[e-1] = c
|
||||
|
||||
#
|
||||
# Prints all permutations. But in addition doesn't print
|
||||
# multiple instances of same value.
|
||||
# e.g. abb bab bba ( doesn't print two abb )
|
||||
#
|
||||
def permutation2(s):
|
||||
print(s)
|
||||
l = len(s)
|
||||
|
||||
while True:
|
||||
i = l-1
|
||||
while True:
|
||||
ii = i
|
||||
i = i - 1
|
||||
if ord(s[i]) < ord(s[ii]):
|
||||
j = l-1
|
||||
while ord(s[i]) >= ord(s[j]):
|
||||
j = j - 1
|
||||
s[i], s[j] = s[j], s[i]
|
||||
s[ii:l] = s[ii:l][::-1]
|
||||
print(s)
|
||||
break
|
||||
if i == 0:
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = ['a', 'b', 'c', 'd', 'e', 'e']
|
||||
permutation(s, 0, len(s))
|
||||
print("--------------------------------------------")
|
||||
permutation2(s)
|
||||
41
puzzles/interviews/training/print_max_evenly.py
Normal file
41
puzzles/interviews/training/print_max_evenly.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import random
|
||||
|
||||
|
||||
#
|
||||
# You have a array with integers: e.g. [ 1, -2, 0, 6, 2, -4, 6, 6 ]
|
||||
# You need to write a function which will evenly return indexes of a
|
||||
# max value in the array.
|
||||
# In the example above max value is 6, and its positions are 3, 6 and 7.
|
||||
# So each run function should return random index from the set.
|
||||
#
|
||||
# Try to implement with O(n) for computation and memory.
|
||||
# Try to reduce memory complexity to O(1).
|
||||
#
|
||||
def return_max_evenly(arr):
|
||||
mv = float('-inf')
|
||||
mi = -1
|
||||
mc = 0
|
||||
for i in range(len(arr)):
|
||||
if mv < arr[i]:
|
||||
mv = arr[i]
|
||||
mi = i
|
||||
mc = 1
|
||||
elif mv == arr[i]:
|
||||
mc = mc + 1
|
||||
if random.random() < 1./mc:
|
||||
mi = i
|
||||
if mi != -1:
|
||||
return mi
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
random.seed()
|
||||
arr = [1, -2, 0, 6, 2, -4, 6, 6]
|
||||
ret = {}
|
||||
for i in range(10000):
|
||||
r = return_max_evenly(arr)
|
||||
if r in ret:
|
||||
ret[r] = ret[r] + 1
|
||||
else:
|
||||
ret[r] = 1
|
||||
print(ret)
|
||||
10
templates/_.py
Normal file
10
templates/_.py
Normal file
@@ -0,0 +1,10 @@
|
||||
#VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
|
||||
|
||||
#import
|
||||
|
||||
def myfunction():
|
||||
#some code here
|
||||
print( "kuku" )
|
||||
|
||||
if __name__ == "__main__":
|
||||
myfunction()
|
||||
Reference in New Issue
Block a user