Skip to content

Commit c2ca11f

Browse files
author
Mari Wahl
committed
all interview problems organized, and cleaned up repeats
1 parent a4637a3 commit c2ca11f

File tree

13 files changed

+98
-268
lines changed

13 files changed

+98
-268
lines changed

src/extra_interview_problems/math_arrays_and_strings/anagram.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@ def is_anagram(s1, s2):
2525

2626
return True
2727

28+
''' verify if words are anagrams by comparing a sum of Unicode code
29+
point of the character'''
2830

31+
def get_unicode_sum(word):
32+
s = 0
33+
for p in word:
34+
s += ord(p)
35+
return s
36+
37+
38+
def is_anagram2(word1, word2):
39+
'''
40+
>>> is_anagram2('cat', 'tac')
41+
True
42+
>>> is_anagram2('cat', 'hat')
43+
False
44+
'''
45+
return get_unicode_sum(word1) == get_unicode_sum(word2)
2946

3047

3148
if __name__ == '__main__':

src/extra_interview_problems/math_arrays_and_strings/anagrams_hash_table.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/extra_interview_problems/math_arrays_and_strings/check_2_numbers_array_sum.py renamed to src/extra_interview_problems/math_arrays_and_strings/check_if_2_numbers_sum_to_k.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
It takes O(nlog n) times two.
1818
"""
1919

20-
def check_sum_hash_table(array, k):
20+
from collections import defaultdict, Counter
21+
22+
def check_sum(array, k):
2123
'''
22-
>>> check_sum_hash_table([3, 2, 6, 7, 9, 1], 8)
24+
>>> check_sum([3, 2, 6, 7, 9, 1], 8)
2325
[(6, 2), (1, 7)]
24-
>>> check_sum_hash_table([5, 2, 6, 7, 9, 1], 4)
26+
>>> check_sum([5, 2, 6, 7, 9, 1], 4)
2527
[]
2628
>>>
2729
'''
2830

29-
from collections import defaultdict
30-
3131
dict = defaultdict()
3232
res = []
3333

@@ -40,6 +40,32 @@ def check_sum_hash_table(array, k):
4040

4141
return res
4242

43+
44+
def check_sum2(array, k):
45+
'''
46+
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 6)
47+
set([(3, 3)])
48+
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 0)
49+
set([])
50+
'''
51+
52+
dict = Counter()
53+
res = set()
54+
55+
for i in array:
56+
dict[i] += 1
57+
58+
for i in array:
59+
if dict[k-i] > 0:
60+
if i == k-i and dict[k-i] > 1:
61+
res.add((i, k-i))
62+
dict[k-i] -= 2
63+
elif i == k-i:
64+
res.add((i, k-i))
65+
dict[k-i] -= 1
66+
67+
return res
68+
4369
if __name__ == '__main__':
4470
import doctest
4571
doctest.testmod()

src/extra_interview_problems/math_arrays_and_strings/check_if_anagrams.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/extra_interview_problems/math_arrays_and_strings/fibonacci.py

100755100644
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,38 @@
55

66
def fib_generator():
77
a, b = 0, 1
8+
89
while True:
910
yield b
1011
a, b = b, a+b
1112

1213

14+
def fib(n):
15+
'''
16+
>>> fib(2)
17+
1
18+
>>> fib(5)
19+
5
20+
>>> fib(7)
21+
13
22+
'''
23+
if n < 3:
24+
return 1
25+
26+
a, b = 0, 1
27+
count = 1
28+
29+
while count < n:
30+
count += 1
31+
a, b = b, a+b
32+
33+
return b
34+
35+
1336
if __name__ == '__main__':
37+
import doctest
38+
doctest.testmod()
39+
1440
fib = fib_generator()
1541
print(next(fib))
1642
print(next(fib))

src/extra_interview_problems/math_arrays_and_strings/find_if_numbers_sum_to_k.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/extra_interview_problems/math_arrays_and_strings/find_nth_fibonacci.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/extra_interview_problems/math_arrays_and_strings/finding_if_prime.py

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/extra_interview_problems/math_arrays_and_strings/permutations.py

100755100644
File mode changed.

0 commit comments

Comments
 (0)