mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
glassdoor 2014
This commit is contained in:
parent
ee01cd9446
commit
4b1fe33f96
@ -1,8 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
__author__ = "Mari Wahl"
|
__author__ = "bt3"
|
||||||
__email__ = "marina.w4hl@gmail.com"
|
|
||||||
|
|
||||||
''' understanding generators'''
|
''' understanding generators'''
|
||||||
|
|
@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
''' Write code to generate all possible case permutations of a given
|
||||||
|
lower-cased string
|
||||||
|
'''
|
||||||
|
|
||||||
|
def alpha_permutation(string):
|
||||||
|
'''
|
||||||
|
>>> alpha_permutation('0ab')
|
||||||
|
['0Ab', '0Ab', '0ab', '0ab', '0Ba', '0Ba', '0ba', '0ba', 'ab0', 'a0b', 'a0b', 'b0a', 'b0a', 'ba0']
|
||||||
|
>>> alpha_permutation('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
|
||||||
|
if len(string) < 2:
|
||||||
|
return string
|
||||||
|
|
||||||
|
result = []
|
||||||
|
|
||||||
|
for i, c in enumerate(string):
|
||||||
|
rest = string[i+1:] + string[:i]
|
||||||
|
for cc in alpha_permutation(rest):
|
||||||
|
if cc.isalpha():
|
||||||
|
result.append(c.upper() + cc)
|
||||||
|
result.append(c + cc)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
31
src/extra_interview_problems/glassdoor/2015/anagram.py
Normal file
31
src/extra_interview_problems/glassdoor/2015/anagram.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def is_anagram(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> is_anagram('cat', 'tac')
|
||||||
|
True
|
||||||
|
>>> is_anagram('cat', 'hat')
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
counter = Counter()
|
||||||
|
for c in s1:
|
||||||
|
counter[c] += 1
|
||||||
|
|
||||||
|
for c in s2:
|
||||||
|
counter[c] -= 1
|
||||||
|
|
||||||
|
for i in counter.values():
|
||||||
|
if i:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
anagram sorting. huge list that does not fit in memory?
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
Given a telephone directory which is broken in number of
|
|
||||||
files, how would you find a telephone number in it (bucket sort)
|
|
||||||
|
|
||||||
- Quick Sort is a good choice when you have to sort a lot of elements.
|
|
||||||
When you are working with smaller collections, Bucket Sort may be a
|
|
||||||
better choice.
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
|
|
||||||
''' Determine if an Array of integers contains 3 numbers that sum to 0 '''
|
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
''' Write code to generate all possible case combinations of a given
|
|
||||||
lower-cased string. (e.g. "0ab" -> ["0ab", "0aB", "0Ab", "0AB"])
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
'''
|
||||||
|
given a string, find longest string with unique characters
|
||||||
|
'''
|
||||||
|
|
||||||
|
def find_longest(string):
|
||||||
|
'''
|
||||||
|
>>> find_longest('abfgrhgtrdsandwejfhdasjcbdsjvrejwghireeej')
|
||||||
|
'wejfhdas'
|
||||||
|
>>> find_longest('abcabcabcabcdefabcccc')
|
||||||
|
'defabc'
|
||||||
|
'''
|
||||||
|
maxs = ''
|
||||||
|
result = ''
|
||||||
|
|
||||||
|
for c in string:
|
||||||
|
if c in result:
|
||||||
|
if len(maxs) < len(result):
|
||||||
|
maxs = result
|
||||||
|
result = ''
|
||||||
|
else:
|
||||||
|
result += c
|
||||||
|
|
||||||
|
if result and len(maxs) < len(result):
|
||||||
|
maxs = result
|
||||||
|
|
||||||
|
return maxs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
@ -6,6 +6,28 @@ Optimize your code so that the numbers
|
|||||||
don't have to be recalculated on consecutive function call
|
don't have to be recalculated on consecutive function call
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def fib(n):
|
||||||
|
'''
|
||||||
|
>>> fib(2)
|
||||||
|
1
|
||||||
|
>>> fib(5)
|
||||||
|
5
|
||||||
|
>>> fib(7)
|
||||||
|
13
|
||||||
|
'''
|
||||||
|
|
||||||
|
if n < 3:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
a, b = 0, 1
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
while count < n:
|
||||||
|
count += 1
|
||||||
|
a, b = b, a+b
|
||||||
|
|
||||||
|
return b
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
import doctest
|
@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
'''
|
||||||
|
find prime factors of a number.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
def find_prime_factors(n):
|
||||||
|
'''
|
||||||
|
>>> find_prime_factors(14)
|
||||||
|
[2, 7]
|
||||||
|
>>> find_prime_factors(19)
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
|
||||||
|
divisors = [d for d in range(2, n//2 + 1) if n % d == 0]
|
||||||
|
primes = [d for d in divisors if is_prime(d)]
|
||||||
|
|
||||||
|
return primes
|
||||||
|
|
||||||
|
|
||||||
|
def is_prime(n):
|
||||||
|
for j in range(2, int(math.sqrt(n))):
|
||||||
|
if (n % j) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
Given a list of strings, write a function
|
|
||||||
to calculate the longest common prefix (LCP) of all those strings.
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
'''
|
||||||
|
Given two strings, write a function
|
||||||
|
to calculate the longest common prefix (LCP) of the strings.
|
||||||
|
'''
|
||||||
|
|
||||||
|
def lcp(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> lcp('dabbd', 'aabbaa')
|
||||||
|
3
|
||||||
|
>>> lcp('abcd', 'hi')
|
||||||
|
0
|
||||||
|
'''
|
||||||
|
|
||||||
|
p1 = 0
|
||||||
|
aux, lcp = '', ''
|
||||||
|
string1 = min(s1, s2)
|
||||||
|
string2 = max(s1, s2)
|
||||||
|
|
||||||
|
while p1 < len(string1):
|
||||||
|
p2 = 0
|
||||||
|
while p2 < len(string2) and p1+p2 < len(string1):
|
||||||
|
if string1[p1+p2] == string2[p2]:
|
||||||
|
aux += string1[p1+p2]
|
||||||
|
else:
|
||||||
|
if len(lcp) < len(aux):
|
||||||
|
lcp = aux
|
||||||
|
aux = ''
|
||||||
|
p2 += 1
|
||||||
|
p1 += 1
|
||||||
|
|
||||||
|
return len(lcp)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
given a string, find longest string with unique characters
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -2,15 +2,22 @@
|
|||||||
|
|
||||||
''' Generate all permutations of an alphanumeric string '''
|
''' Generate all permutations of an alphanumeric string '''
|
||||||
|
|
||||||
def get_permutations(word):
|
def permutations(word):
|
||||||
if len(word) < 2:
|
'''
|
||||||
yield word
|
>>> permutations('abc')
|
||||||
return
|
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
||||||
|
>>> permutations('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
if len(word) < 2:
|
||||||
|
return word
|
||||||
|
|
||||||
for i in range(len(word)):
|
res = []
|
||||||
rest = word[:i] + word[i+1:]
|
for i in range(len(word)):
|
||||||
for tail in get_permutations(rest):
|
rest = word[:i] + word[i+1:]
|
||||||
yield word[i] + tail
|
for p in permutations(rest):
|
||||||
|
res.append(word[i] + p)
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
find prime factors of a number.
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
Reconstruct a binary tree given two sequences of node traversals,
|
|
||||||
one from inorder and one from postorder traversal.
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
#!/bin/python
|
|
||||||
|
|
||||||
'''
|
|
||||||
find the smallest number in a BST
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
|||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
'''
|
||||||
|
Reconstruct a binary tree given two sequences of node traversals,
|
||||||
|
one from inorder and one from postorder traversal.
|
||||||
|
'''
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, value, right=None, left=None):
|
||||||
|
self.value = value
|
||||||
|
self.right = right
|
||||||
|
self.left = left
|
||||||
|
|
||||||
|
def create_tree():
|
||||||
|
tree = Node(4)
|
||||||
|
tree.right = Node(6)
|
||||||
|
tree.left = Node(2)
|
||||||
|
tree.right.left = Node(5)
|
||||||
|
tree.right.right = Node(7)
|
||||||
|
tree.left.left = Node(1)
|
||||||
|
tree.left.right = Node(3)
|
||||||
|
return tree
|
||||||
|
|
||||||
|
def inorder(tree):
|
||||||
|
if tree:
|
||||||
|
if tree.left:
|
||||||
|
inorder(tree.left)
|
||||||
|
print tree.value
|
||||||
|
if tree.right:
|
||||||
|
inorder(tree.right)
|
||||||
|
|
||||||
|
def postorder(tree):
|
||||||
|
if tree:
|
||||||
|
if tree.left:
|
||||||
|
postorder(tree.left)
|
||||||
|
if tree.right:
|
||||||
|
postorder(tree.right)
|
||||||
|
print tree.value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
tree = create_tree()
|
||||||
|
print 'Printing inorder...'
|
||||||
|
inorder(tree)
|
||||||
|
print
|
||||||
|
print 'Printing postorder...'
|
||||||
|
postorder(tree)
|
@ -2,11 +2,61 @@
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
Implement a trie. (Write the API and code for inserting into a trie).
|
Implement a trie. (Write the API and code for inserting into a trie).
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
def make_trie(*args):
|
||||||
|
trie = {}
|
||||||
|
|
||||||
|
for word in args:
|
||||||
|
temp_trie = trie
|
||||||
|
for letter in word:
|
||||||
|
temp_trie = temp_trie.setdefault(letter, {})
|
||||||
|
temp_trie = temp_trie.setdefault('_end_', '_end_')
|
||||||
|
|
||||||
|
return trie
|
||||||
|
|
||||||
|
|
||||||
|
def in_trie(trie, word):
|
||||||
|
temp_trie = trie
|
||||||
|
for letter in word:
|
||||||
|
if letter not in temp_trie:
|
||||||
|
return False
|
||||||
|
temp_trie = temp_trie[letter]
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def remove_from_trie(trie, word, depth):
|
||||||
|
if word and word[depth] not in trie:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if len(word) == depth + 1:
|
||||||
|
del trie[word[depth]]
|
||||||
|
if not trie:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
temp_trie = trie
|
||||||
|
|
||||||
|
if remove_from_trie(temp_trie[word[depth]], word, depth + 1):
|
||||||
|
if temp_trie:
|
||||||
|
del temp_trie[word[depth]]
|
||||||
|
return not temp_trie
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
trie = make_trie('hello', 'abc', 'baz', 'bar', 'barz')
|
||||||
doctest.testmod()
|
print 'This is the trie:'
|
||||||
|
print trie
|
||||||
|
|
||||||
|
assert(in_trie(trie, 'hello') == True)
|
||||||
|
assert(in_trie(trie, 'bar') == True)
|
||||||
|
assert(in_trie(trie, 'bab') == False)
|
||||||
|
assert(in_trie(trie, 'zzz') == False)
|
||||||
|
|
||||||
|
remove_from_trie(trie, 'abc', 0)
|
||||||
|
assert(in_trie(trie, 'abc') == False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user