Skip to content

Commit 792434e

Browse files
author
bt3
committed
warmup, preparing to give interviews
1 parent 9b8453e commit 792434e

File tree

10 files changed

+390
-0
lines changed

10 files changed

+390
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
def binary_search(array, value):
6+
last, first = len(array), 0
7+
8+
while first < last:
9+
mid = (last - first)//2
10+
item = array[mid]
11+
12+
if item == value:
13+
return True
14+
15+
elif item < value:
16+
last = mid
17+
18+
else:
19+
first = mid
20+
21+
return False
22+
23+
def binary_search_rec(array, value, first=0, last=None):
24+
last = last or len(array)
25+
if len(array[first:last]) < 1:
26+
return False
27+
28+
mid = (last - first)//2
29+
if array[mid] == value:
30+
return True
31+
elif array[mid] < value:
32+
return binary_search_rec(array, value, first=first, last=mid)
33+
else:
34+
return binary_search_rec(array, value, first=mid, last=last)
35+
36+
37+
if __name__ == '__main__':
38+
array = [3, 4, 6, 7, 10, 11, 34, 67, 84]
39+
value = 6
40+
assert(binary_search(array, value) == True)
41+
assert(binary_search_rec(array, value) == True)
42+
value = 8
43+
assert(binary_search(array, value) == False)
44+
assert(binary_search_rec(array, value) == False)
45+
array = [8]
46+
assert(binary_search(array, value) == True)
47+
assert(binary_search_rec(array, value) == True)
48+
array = []
49+
assert(binary_search(array, value) == False)
50+
assert(binary_search_rec(array, value) == False)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
from collections import Counter
6+
7+
def check_if_anagram(word1, word2):
8+
counter = Counter()
9+
10+
for c in word1:
11+
counter[c] += 1
12+
13+
for c in word2:
14+
counter[c] -= 1
15+
16+
for values in counter.values():
17+
if values != 0:
18+
return False
19+
20+
return True
21+
22+
23+
24+
if __name__ == '__main__':
25+
word1 = 'abc'
26+
word2 = 'bca'
27+
assert(check_if_anagram(word1, word2) == True)
28+
29+
word2 = 'bcd'
30+
assert(check_if_anagram(word1, word2) == False)
31+
32+
word1 = ''
33+
word2 = ''
34+
assert(check_if_anagram(word1, word2) == True)
35+
36+
word1 = 'a'
37+
word2 = 'a'
38+
assert(check_if_anagram(word1, word2) == True)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
def combination(array):
6+
if len(array) < 2:
7+
return set(array)
8+
9+
result = set()
10+
for index, item in enumerate(array):
11+
new_array = array[:index] + array[index+1:]
12+
result.add(item)
13+
for perm in combination(new_array):
14+
new_item = ''.join(sorted(item + perm))
15+
result.add(new_item)
16+
17+
return result
18+
19+
20+
21+
if __name__ == '__main__':
22+
array = ['a', 'b', 'c']
23+
result = set(['a', 'ac', 'ab', 'abc', 'bc', 'c', 'b'])
24+
assert(combination(array) == result)
25+
26+
array = ['']
27+
result = set([''])
28+
assert(combination(array) == result)
29+
30+
array = ['a']
31+
result = set(['a'])
32+
assert(combination(array) == result)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
6+
def longest_common_substring(s1, s2):
7+
p1 = 0
8+
aux, lcp = '', ''
9+
string1 = max(s1, s2)
10+
string2 = min(s1, s2)
11+
12+
while p1 < len(string1):
13+
p2 = 0
14+
while p2 < len(string2) and p1+p2 < len(string1):
15+
if string1[p1+p2] == string2[p2]:
16+
aux += string1[p1+p2]
17+
else:
18+
if len(lcp) < len(aux):
19+
lcp = aux
20+
aux = ''
21+
p2 += 1
22+
p1 += 1
23+
24+
return lcp
25+
26+
27+
28+
if __name__ == '__main__':
29+
str1 = 'hasfgeaae'
30+
str2 = 'bafgekk'
31+
result = 'fge'
32+
assert(longest_common_substring(str1, str2) == result)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
6+
def longest_increasing_subsequence(seq):
7+
result, aux = [], []
8+
seq.append(-float('infinity'))
9+
10+
for i, value in enumerate(seq[:-1]):
11+
aux.append(value)
12+
if value > seq[i+1]:
13+
if len(result) < len(aux):
14+
result = aux[:]
15+
aux = []
16+
return result
17+
18+
19+
20+
if __name__ == '__main__':
21+
seq = [10, -12, 2, 3, -3, 5, -1, 2, -10]
22+
result = [-12, 2, 3]
23+
assert(longest_increasing_subsequence(seq) == result)
24+
25+
seq = [2]
26+
result = [2]
27+
assert(longest_increasing_subsequence(seq) == result)
28+
29+
seq = []
30+
result = []
31+
assert(longest_increasing_subsequence(seq) == result)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
# AKA: do you believe in magic?
3+
4+
__author__ = "bt3"
5+
6+
def merge_sort(array):
7+
if len(array) < 2:
8+
return array
9+
10+
# divide
11+
mid = len(array)//2
12+
left = merge_sort(array[:mid])
13+
right = merge_sort(array[mid:])
14+
15+
# merge
16+
result = []
17+
i, j = 0, 0
18+
19+
while i < len(left) and j < len(right):
20+
if left[i] < right[j]:
21+
result.append(left[i])
22+
i += 1
23+
else:
24+
result.append(right[j])
25+
j+= 1
26+
27+
# make sure nothing is left behind
28+
if left[i:]:
29+
result.extend(left[i:])
30+
if right[j:]:
31+
result.extend(right[j:])
32+
33+
return result
34+
35+
36+
37+
38+
if __name__ == '__main__':
39+
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
40+
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
41+
assert(merge_sort(array) == sorted)
42+
43+
array = []
44+
assert(merge_sort(array) == array)
45+
46+
array = [1]
47+
assert(merge_sort(array) == array)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
import string
6+
7+
def sanitize(sentence):
8+
array = sentence.lower()
9+
array = array.strip()
10+
array = array.strip(string.punctuation)
11+
return array
12+
13+
def check_if_palindrome(array):
14+
if len(array) < 2:
15+
return True
16+
17+
if array[0] == array[-1]:
18+
return check_if_palindrome(array[1:-1])
19+
else:
20+
return False
21+
22+
def check_if_palindrome_iter(array):
23+
i, j = 0, len(array)-1
24+
25+
while i <= j:
26+
if array[i] != array[j]:
27+
return False
28+
i += 1
29+
j -= 1
30+
31+
return True
32+
33+
34+
if __name__ == '__main__':
35+
sentence = 'hello there'
36+
array = sanitize(sentence)
37+
assert(check_if_palindrome(array) == False)
38+
assert(check_if_palindrome_iter(array) == False)
39+
40+
sentence = ''
41+
array = sanitize(sentence)
42+
assert(check_if_palindrome(array) == True)
43+
assert(check_if_palindrome_iter(array) == True)
44+
45+
sentence = 'h'
46+
array = sanitize(sentence)
47+
assert(check_if_palindrome(array) == True)
48+
assert(check_if_palindrome_iter(array) == True)
49+
50+
sentence = 'Noel sees Leon'
51+
array = sanitize(sentence)
52+
assert(check_if_palindrome(array) == True)
53+
assert(check_if_palindrome_iter(array) == True)
54+
55+
sentence = 'Noel sees Leon!'
56+
array = sanitize(sentence)
57+
assert(check_if_palindrome(array) == True)
58+
assert(check_if_palindrome_iter(array) == True)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
6+
def permutation(array):
7+
if len(array) < 2:
8+
return [array]
9+
10+
result = []
11+
for index, letter in enumerate(array):
12+
new_array = array[:index] + array[index+1:]
13+
for perm in permutation(new_array):
14+
result.append(letter + perm)
15+
16+
return result
17+
18+
19+
20+
if __name__ == '__main__':
21+
word = 'abc'
22+
result = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
23+
assert(permutation(word) == result)
24+
25+
word = ''
26+
result = ['']
27+
assert(permutation(word) == result)
28+
29+
word = 'a'
30+
result = ['a']
31+
assert(permutation(word) == result)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
__author__ = "bt3"
4+
5+
def quick_sort(array):
6+
if len(array) < 2:
7+
return array
8+
9+
# partition
10+
ipivot = len(array)//2
11+
pivot = array[ipivot]
12+
new_array = array[:ipivot] + array[ipivot+1:]
13+
14+
left = [x for x in new_array if x <= pivot]
15+
right = [x for x in new_array if x > pivot]
16+
17+
return quick_sort(left) + [pivot] + quick_sort(right)
18+
19+
20+
21+
22+
if __name__ == '__main__':
23+
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
24+
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
25+
assert(quick_sort(array) == sorted)
26+
27+
array = []
28+
assert(quick_sort(array) == array)
29+
30+
array = [1]
31+
assert(quick_sort(array) == array)

0 commit comments

Comments
 (0)