warmup, preparing to give interviews

This commit is contained in:
bt3 2015-10-25 17:12:45 -07:00
parent 9b8453e57e
commit 792434e3af
10 changed files with 390 additions and 0 deletions

View File

@ -0,0 +1,50 @@
#!/usr/bin/env python
__author__ = "bt3"
def binary_search(array, value):
last, first = len(array), 0
while first < last:
mid = (last - first)//2
item = array[mid]
if item == value:
return True
elif item < value:
last = mid
else:
first = mid
return False
def binary_search_rec(array, value, first=0, last=None):
last = last or len(array)
if len(array[first:last]) < 1:
return False
mid = (last - first)//2
if array[mid] == value:
return True
elif array[mid] < value:
return binary_search_rec(array, value, first=first, last=mid)
else:
return binary_search_rec(array, value, first=mid, last=last)
if __name__ == '__main__':
array = [3, 4, 6, 7, 10, 11, 34, 67, 84]
value = 6
assert(binary_search(array, value) == True)
assert(binary_search_rec(array, value) == True)
value = 8
assert(binary_search(array, value) == False)
assert(binary_search_rec(array, value) == False)
array = [8]
assert(binary_search(array, value) == True)
assert(binary_search_rec(array, value) == True)
array = []
assert(binary_search(array, value) == False)
assert(binary_search_rec(array, value) == False)

View File

@ -0,0 +1,38 @@
#!/usr/bin/env python
__author__ = "bt3"
from collections import Counter
def check_if_anagram(word1, word2):
counter = Counter()
for c in word1:
counter[c] += 1
for c in word2:
counter[c] -= 1
for values in counter.values():
if values != 0:
return False
return True
if __name__ == '__main__':
word1 = 'abc'
word2 = 'bca'
assert(check_if_anagram(word1, word2) == True)
word2 = 'bcd'
assert(check_if_anagram(word1, word2) == False)
word1 = ''
word2 = ''
assert(check_if_anagram(word1, word2) == True)
word1 = 'a'
word2 = 'a'
assert(check_if_anagram(word1, word2) == True)

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python
__author__ = "bt3"
def combination(array):
if len(array) < 2:
return set(array)
result = set()
for index, item in enumerate(array):
new_array = array[:index] + array[index+1:]
result.add(item)
for perm in combination(new_array):
new_item = ''.join(sorted(item + perm))
result.add(new_item)
return result
if __name__ == '__main__':
array = ['a', 'b', 'c']
result = set(['a', 'ac', 'ab', 'abc', 'bc', 'c', 'b'])
assert(combination(array) == result)
array = ['']
result = set([''])
assert(combination(array) == result)
array = ['a']
result = set(['a'])
assert(combination(array) == result)

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python
__author__ = "bt3"
def longest_common_substring(s1, s2):
p1 = 0
aux, lcp = '', ''
string1 = max(s1, s2)
string2 = min(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 lcp
if __name__ == '__main__':
str1 = 'hasfgeaae'
str2 = 'bafgekk'
result = 'fge'
assert(longest_common_substring(str1, str2) == result)

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
__author__ = "bt3"
def longest_increasing_subsequence(seq):
result, aux = [], []
seq.append(-float('infinity'))
for i, value in enumerate(seq[:-1]):
aux.append(value)
if value > seq[i+1]:
if len(result) < len(aux):
result = aux[:]
aux = []
return result
if __name__ == '__main__':
seq = [10, -12, 2, 3, -3, 5, -1, 2, -10]
result = [-12, 2, 3]
assert(longest_increasing_subsequence(seq) == result)
seq = [2]
result = [2]
assert(longest_increasing_subsequence(seq) == result)
seq = []
result = []
assert(longest_increasing_subsequence(seq) == result)

View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
# AKA: do you believe in magic?
__author__ = "bt3"
def merge_sort(array):
if len(array) < 2:
return array
# divide
mid = len(array)//2
left = merge_sort(array[:mid])
right = merge_sort(array[mid:])
# merge
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j+= 1
# make sure nothing is left behind
if left[i:]:
result.extend(left[i:])
if right[j:]:
result.extend(right[j:])
return result
if __name__ == '__main__':
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
assert(merge_sort(array) == sorted)
array = []
assert(merge_sort(array) == array)
array = [1]
assert(merge_sort(array) == array)

View File

@ -0,0 +1,58 @@
#!/usr/bin/env python
__author__ = "bt3"
import string
def sanitize(sentence):
array = sentence.lower()
array = array.strip()
array = array.strip(string.punctuation)
return array
def check_if_palindrome(array):
if len(array) < 2:
return True
if array[0] == array[-1]:
return check_if_palindrome(array[1:-1])
else:
return False
def check_if_palindrome_iter(array):
i, j = 0, len(array)-1
while i <= j:
if array[i] != array[j]:
return False
i += 1
j -= 1
return True
if __name__ == '__main__':
sentence = 'hello there'
array = sanitize(sentence)
assert(check_if_palindrome(array) == False)
assert(check_if_palindrome_iter(array) == False)
sentence = ''
array = sanitize(sentence)
assert(check_if_palindrome(array) == True)
assert(check_if_palindrome_iter(array) == True)
sentence = 'h'
array = sanitize(sentence)
assert(check_if_palindrome(array) == True)
assert(check_if_palindrome_iter(array) == True)
sentence = 'Noel sees Leon'
array = sanitize(sentence)
assert(check_if_palindrome(array) == True)
assert(check_if_palindrome_iter(array) == True)
sentence = 'Noel sees Leon!'
array = sanitize(sentence)
assert(check_if_palindrome(array) == True)
assert(check_if_palindrome_iter(array) == True)

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
__author__ = "bt3"
def permutation(array):
if len(array) < 2:
return [array]
result = []
for index, letter in enumerate(array):
new_array = array[:index] + array[index+1:]
for perm in permutation(new_array):
result.append(letter + perm)
return result
if __name__ == '__main__':
word = 'abc'
result = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
assert(permutation(word) == result)
word = ''
result = ['']
assert(permutation(word) == result)
word = 'a'
result = ['a']
assert(permutation(word) == result)

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
__author__ = "bt3"
def quick_sort(array):
if len(array) < 2:
return array
# partition
ipivot = len(array)//2
pivot = array[ipivot]
new_array = array[:ipivot] + array[ipivot+1:]
left = [x for x in new_array if x <= pivot]
right = [x for x in new_array if x > pivot]
return quick_sort(left) + [pivot] + quick_sort(right)
if __name__ == '__main__':
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
assert(quick_sort(array) == sorted)
array = []
assert(quick_sort(array) == array)
array = [1]
assert(quick_sort(array) == array)

View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
__author__ = "bt3"
def reverse_str_inplace(_str):
if len(_str) < 2:
return _str
return _str[-1] + reverse_str(_str[1:-1]) + _str[0]
def reverse_str(_str):
result = ''
j = len(_str) - 1
while j >= 0:
result += _str[j]
return result
if __name__ == '__main__':
_str = ''
result = ''
assert(reverse_str(_str) == result)
assert(reverse_str_inplace(_str) == result)
_str = 'a'
result = 'a'
assert(reverse_str(_str) == result)
assert(reverse_str_inplace(_str) == result)
_str = 'abcde'
result = 'edcba'
assert(reverse_str(_str) == result)
assert(reverse_str_inplace(_str) == result)
_str = 'abcdef'
result = 'fedcba'
assert(reverse_str(_str) == result)
assert(reverse_str_inplace(_str) == result)