mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
simple examples, glassdoor
This commit is contained in:
parent
87a257c09c
commit
ee01cd9446
15
src/extra_interview_problems/advanced/lru_cache.py
Normal file
15
src/extra_interview_problems/advanced/lru_cache.py
Normal file
@ -0,0 +1,15 @@
|
||||
#!/bin/python3
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
@lru_cache(maxsize=20)
|
||||
def fib(n):
|
||||
if n < 2:
|
||||
return n
|
||||
return fib(n-1) + fib(n-2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print([fib(n) for n in range(10)])
|
||||
print(fib.cache_info())
|
@ -0,0 +1,11 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
anagram sorting. huge list that does not fit in memory?
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
@ -0,0 +1,33 @@
|
||||
#!/bin/python
|
||||
|
||||
''' Determine if an Array of integers contains 3 numbers that sum to 0 '''
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def find_3_number(array):
|
||||
'''
|
||||
>>> find_3_number([1,5,56,11,-3,-12])
|
||||
1 11 -12
|
||||
True
|
||||
>>> find_3_number([] )
|
||||
False
|
||||
'''
|
||||
hash_ = defaultdict()
|
||||
for i in array:
|
||||
hash_[i] = 1
|
||||
|
||||
for i, x in enumerate(array):
|
||||
for y in array[i+1:]:
|
||||
if -(x+y) in hash_:
|
||||
print x, y, -(x+y)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
53
src/extra_interview_problems/glassdoor/2015/binary_search.py
Normal file
53
src/extra_interview_problems/glassdoor/2015/binary_search.py
Normal file
@ -0,0 +1,53 @@
|
||||
#!/bin/python
|
||||
|
||||
|
||||
def binary_search_rec(array, item, lo=0, hi = None):
|
||||
'''
|
||||
>>> binary_search_rec([2,3,5,6,8,10,15,23], 15)
|
||||
(True, 6)
|
||||
>>> binary_search_rec([2,3,5,6,8,10,15,23], 4)
|
||||
False
|
||||
'''
|
||||
hi = hi or len(array)
|
||||
if hi < lo :
|
||||
return False
|
||||
|
||||
mid = (hi + lo)//2
|
||||
|
||||
if array[mid] == item:
|
||||
return True, mid
|
||||
elif array[mid] < item:
|
||||
return binary_search_rec(array, item, mid + 1, hi)
|
||||
else:
|
||||
return binary_search_rec(array[:mid], item, lo, mid -1)
|
||||
|
||||
|
||||
|
||||
def binary_search_iter(array, item):
|
||||
'''
|
||||
>>> binary_search_iter([2,3,5,6,8,10,15,23], 15)
|
||||
(True, 6)
|
||||
>>> binary_search_iter([2,3,5,6,8,10,15,23], 4)
|
||||
False
|
||||
'''
|
||||
hi = len(array)
|
||||
lo = 0
|
||||
|
||||
while lo < hi:
|
||||
mid = (hi+lo)//2
|
||||
if array[mid] == item:
|
||||
return True, mid
|
||||
elif array[mid] > item:
|
||||
hi = mid
|
||||
else:
|
||||
lo = mid + 1
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
19
src/extra_interview_problems/glassdoor/2015/bucket.py
Normal file
19
src/extra_interview_problems/glassdoor/2015/bucket.py
Normal file
@ -0,0 +1,19 @@
|
||||
#!/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()
|
||||
|
11
src/extra_interview_problems/glassdoor/2015/comb.py
Normal file
11
src/extra_interview_problems/glassdoor/2015/comb.py
Normal file
@ -0,0 +1,11 @@
|
||||
#!/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()
|
||||
|
13
src/extra_interview_problems/glassdoor/2015/fib.py
Normal file
13
src/extra_interview_problems/glassdoor/2015/fib.py
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
Write a function to find the nth Fibonacci number.
|
||||
Optimize your code so that the numbers
|
||||
don't have to be recalculated on consecutive function call
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
12
src/extra_interview_problems/glassdoor/2015/long_string.py
Normal file
12
src/extra_interview_problems/glassdoor/2015/long_string.py
Normal file
@ -0,0 +1,12 @@
|
||||
#!/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()
|
||||
|
11
src/extra_interview_problems/glassdoor/2015/longest_str.py
Normal file
11
src/extra_interview_problems/glassdoor/2015/longest_str.py
Normal file
@ -0,0 +1,11 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
given a string, find longest string with unique characters
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
44
src/extra_interview_problems/glassdoor/2015/merge.py
Normal file
44
src/extra_interview_problems/glassdoor/2015/merge.py
Normal file
@ -0,0 +1,44 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
You have two arrays with N integers in them. Merge those arrays using a
|
||||
recursive algorithm so that the integers in the final array are sorted.
|
||||
'''
|
||||
|
||||
def merge_arrays(a1, a2):
|
||||
'''
|
||||
>>> merge_arrays([5, 4, 3], [6, 2, 9])
|
||||
[2, 3, 4, 5, 6, 9]
|
||||
>>> merge_arrays([2, 6], [6, 2])
|
||||
[2, 2, 6, 6]
|
||||
>>> merge_arrays([], [])
|
||||
[]
|
||||
'''
|
||||
|
||||
a1.sort()
|
||||
a2.sort()
|
||||
|
||||
merge = []
|
||||
p1, p2 = 0, 0
|
||||
|
||||
while p1 < len(a1) and p2 < len(a2):
|
||||
if a1[p1] <= a2[p2]:
|
||||
merge.append(a1[p1])
|
||||
p1 += 1
|
||||
else:
|
||||
merge.append(a2[p2])
|
||||
p2 +=1
|
||||
|
||||
if a1[p1:]:
|
||||
merge.extend(a1[p1:])
|
||||
|
||||
if a2[p2:]:
|
||||
merge.extend(a2[p2:])
|
||||
|
||||
return merge
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
32
src/extra_interview_problems/glassdoor/2015/overlapping.py
Normal file
32
src/extra_interview_problems/glassdoor/2015/overlapping.py
Normal file
@ -0,0 +1,32 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
given an array of intervals, return max number of non-overlapping intervals
|
||||
'''
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def non_overlapping(array):
|
||||
'''
|
||||
>>> non_overlapping([(1,2), (2,5), (1, 6)])
|
||||
[[(1, 2), (2, 5)]]
|
||||
>>> non_overlapping([(1,4), (2,5), (3, 6)])
|
||||
[]
|
||||
'''
|
||||
total = []
|
||||
|
||||
for i, t in enumerate(array):
|
||||
start = t[0]
|
||||
end = t[1]
|
||||
for tt in array[i+1:] :
|
||||
if end <= tt[0]:
|
||||
total.append([(start, end), (tt[0], tt[1])])
|
||||
|
||||
return total
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
41
src/extra_interview_problems/glassdoor/2015/pairs.py
Normal file
41
src/extra_interview_problems/glassdoor/2015/pairs.py
Normal file
@ -0,0 +1,41 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
Given an unsorted array of numbers (that may contain repeated numbers),
|
||||
* data structure that contains all the pairs w/sum equal to k.
|
||||
* Do not include pairs that are the same numbers in a different order.
|
||||
'''
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def sum_pairs(array, k):
|
||||
'''
|
||||
>>> sum_pairs([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 6)
|
||||
set([(3, 3)])
|
||||
>>> sum_pairs([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 0)
|
||||
set([])
|
||||
'''
|
||||
|
||||
results = set()
|
||||
dict = Counter()
|
||||
|
||||
for i in array:
|
||||
dict[i] += 1
|
||||
|
||||
for i in array:
|
||||
if dict[k-i] > 0:
|
||||
if i == k-i and dict[k-i] > 1:
|
||||
results.add((i, k-i))
|
||||
dict[k-i] -= 2
|
||||
elif i == k-i:
|
||||
results.add((i, k-i))
|
||||
dict[k-i] -= 1
|
||||
|
||||
|
||||
return results
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
31
src/extra_interview_problems/glassdoor/2015/palindrome.py
Normal file
31
src/extra_interview_problems/glassdoor/2015/palindrome.py
Normal file
@ -0,0 +1,31 @@
|
||||
#!/bin/python
|
||||
|
||||
''' Determine if an Array of integers contains 3 numbers that sum to 0 '''
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
def is_palindrome(array):
|
||||
'''
|
||||
>>> is_palindrome('subi no onibus')
|
||||
True
|
||||
>>> is_palindrome('helllo there')
|
||||
False
|
||||
>>> is_palindrome('h')
|
||||
True
|
||||
>>> is_palindrome('')
|
||||
True
|
||||
'''
|
||||
array = array.strip(' ')
|
||||
if len(array) < 2:
|
||||
return True
|
||||
|
||||
if array[0] == array[-1]:
|
||||
return is_palindrome(array[1:-1])
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
20
src/extra_interview_problems/glassdoor/2015/permutations.py
Normal file
20
src/extra_interview_problems/glassdoor/2015/permutations.py
Normal file
@ -0,0 +1,20 @@
|
||||
#!/bin/python
|
||||
|
||||
''' Generate all permutations of an alphanumeric string '''
|
||||
|
||||
def get_permutations(word):
|
||||
if len(word) < 2:
|
||||
yield word
|
||||
return
|
||||
|
||||
for i in range(len(word)):
|
||||
rest = word[:i] + word[i+1:]
|
||||
for tail in get_permutations(rest):
|
||||
yield word[i] + tail
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
11
src/extra_interview_problems/glassdoor/2015/primes.py
Normal file
11
src/extra_interview_problems/glassdoor/2015/primes.py
Normal file
@ -0,0 +1,11 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
find prime factors of a number.
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
12
src/extra_interview_problems/glassdoor/2015/rec_bt.py
Normal file
12
src/extra_interview_problems/glassdoor/2015/rec_bt.py
Normal file
@ -0,0 +1,12 @@
|
||||
#!/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()
|
||||
|
11
src/extra_interview_problems/glassdoor/2015/smallest_bst.py
Normal file
11
src/extra_interview_problems/glassdoor/2015/smallest_bst.py
Normal file
@ -0,0 +1,11 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
find the smallest number in a BST
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
38
src/extra_interview_problems/glassdoor/2015/sum_str.py
Normal file
38
src/extra_interview_problems/glassdoor/2015/sum_str.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
find the sum of two integers represented as strings,
|
||||
return the sum as string, i.e "123" and "456" would return "579"
|
||||
'''
|
||||
|
||||
|
||||
def get_number(s):
|
||||
count = 1
|
||||
num = 0
|
||||
p = len(s) -1
|
||||
while p >= 0:
|
||||
num = num + int(s[p])*count
|
||||
count *= 10
|
||||
p -= 1
|
||||
return num
|
||||
|
||||
|
||||
def sum_string(s1, s2):
|
||||
'''
|
||||
>>> sum_string('10', '5')
|
||||
'15'
|
||||
>>> sum_string('0', '1')
|
||||
'1'
|
||||
>>> sum_string('123', '456')
|
||||
'579'
|
||||
'''
|
||||
|
||||
n1 = get_number(s1)
|
||||
n2 = get_number(s2)
|
||||
return str(n2 + n1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
53
src/extra_interview_problems/glassdoor/2015/tree.py
Normal file
53
src/extra_interview_problems/glassdoor/2015/tree.py
Normal file
@ -0,0 +1,53 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
Given a tree find out whether is a BST or not
|
||||
'''
|
||||
|
||||
class Tree(object):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
self.right = None
|
||||
self.left = None
|
||||
|
||||
def create_tree_bst():
|
||||
tree = Tree(4)
|
||||
tree.right = Tree(6)
|
||||
tree.left = Tree(2)
|
||||
tree.right.left = Tree(5)
|
||||
tree.right.right = Tree(7)
|
||||
tree.left.left = Tree(1)
|
||||
tree.left.right = Tree(3)
|
||||
return tree
|
||||
|
||||
def create_tree_not_bst():
|
||||
tree = Tree(4)
|
||||
tree.right = Tree(6)
|
||||
tree.left = Tree(2)
|
||||
tree.right.left = Tree(5)
|
||||
tree.right.right = Tree(7)
|
||||
tree.left.left = Tree(3)
|
||||
tree.left.right = Tree(1)
|
||||
return tree
|
||||
|
||||
|
||||
INFINITY = float("infinity")
|
||||
NEG_INFINITY = float("-infinity")
|
||||
|
||||
def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY):
|
||||
if not tree:
|
||||
return True
|
||||
|
||||
if not minVal <= tree.value <= maxVal:
|
||||
return False
|
||||
|
||||
return isBST(tree.left, minVal, tree.value) and \
|
||||
isBST(tree.right, tree.value, maxVal)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tree = create_tree_bst()
|
||||
print isBST(tree)
|
||||
|
||||
tree = create_tree_not_bst()
|
||||
print isBST(tree)
|
12
src/extra_interview_problems/glassdoor/2015/trie.py
Normal file
12
src/extra_interview_problems/glassdoor/2015/trie.py
Normal file
@ -0,0 +1,12 @@
|
||||
#!/bin/python
|
||||
|
||||
'''
|
||||
Implement a trie. (Write the API and code for inserting into a trie).
|
||||
|
||||
'''
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
@ -1,7 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
__email__ = "bt33gl@gmail.com"
|
||||
|
||||
|
||||
from binary_tree import NodeBT, BinaryTree
|
||||
|
@ -1,7 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
__email__ = "bt33gl@gmail.com"
|
||||
|
||||
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
__author__ = "bt3"
|
||||
__email__ = "bt33gl@gmail.com"
|
||||
|
||||
|
||||
""" need to keep track of min and max!!!"""
|
||||
@ -38,7 +37,7 @@ def isBST(bt, mintree=None, maxtree=None):
|
||||
else:
|
||||
right = True
|
||||
|
||||
return left or right
|
||||
return left and right
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
__email__ = "bt33gl@gmail.com"
|
||||
|
||||
|
||||
from binary_tree import BinaryTree
|
||||
|
@ -1,7 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
__email__ = "bt33gl@gmail.com"
|
||||
|
||||
|
||||
''' find the lowest ancestor in a BST '''
|
||||
|
Loading…
x
Reference in New Issue
Block a user