Skip to content

Commit e8594cb

Browse files
committed
Split problems into separate files
1 parent eabd1a3 commit e8594cb

File tree

161 files changed

+4232
-31
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+4232
-31
lines changed

daily_coding_problem_01_05.py renamed to previous/daily_coding_problem_01_05.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from collections import deque
22

33

4-
def coding_problem_1(stack):
4+
def coding_problem_01(stack):
55
"""
66
Given a stack of N elements, interleave the first half of the stack
77
with the second half reversed using one other queue.
88
Example:
99
10-
>>> coding_problem_1([1, 2, 3, 4, 5])
10+
>>> coding_problem_01([1, 2, 3, 4, 5])
1111
[1, 5, 2, 4, 3]
12-
>>> coding_problem_1([1, 2, 3, 4, 5, 6])
12+
>>> coding_problem_01([1, 2, 3, 4, 5, 6])
1313
[1, 6, 2, 5, 3, 4]
1414
1515
Note: with Python lists, you could instead islice(chain.from_iterable(izip(l, reversed(l))), len(l))
@@ -27,14 +27,14 @@ def coding_problem_1(stack):
2727
return stack
2828

2929

30-
def coding_problem_2(l):
30+
def coding_problem_02(l):
3131
"""
3232
Given an array of integers, return a new array such that each element at index i of
3333
the new array is the product of all the numbers in the original array except the one at i.
3434
Solve it without using division and in O(n).
3535
Example:
3636
37-
>>> coding_problem_2([1, 2, 3, 4, 5])
37+
>>> coding_problem_02([1, 2, 3, 4, 5])
3838
[120, 60, 40, 30, 24]
3939
"""
4040
forward = [1] * len(l)
@@ -47,14 +47,14 @@ def coding_problem_2(l):
4747
return [f * b for f, b in zip(forward, backward)]
4848

4949

50-
def coding_problem_3(s):
50+
def coding_problem_03(s):
5151
"""
5252
Given the root to a binary tree, implement serialize(root), which serializes the tree
5353
into a string, and deserialize(s), which deserializes the string back into the tree.
5454
Example:
5555
5656
>>> s = '3 2 1 None None None 4 5 None None 6 None None'
57-
>>> de_serialized = coding_problem_3(s)
57+
>>> de_serialized = coding_problem_03(s)
5858
>>> re_serialized = de_serialized.serialize_to_string()
5959
>>> s == re_serialized
6060
True
@@ -92,17 +92,17 @@ def deserialize_from_string(cls, s):
9292
return BinaryNode.deserialize_from_string(s)
9393

9494

95-
def coding_problem_4(array):
95+
def coding_problem_04(array):
9696
"""
9797
Given an array of integers, find the first missing positive integer in linear time and constant space.
9898
You can modify the input array in-place.
9999
Example:
100100
101-
>>> coding_problem_4([3, 4, -1, 1])
101+
>>> coding_problem_04([3, 4, -1, 1])
102102
2
103-
>>> coding_problem_4([1, 2, 0])
103+
>>> coding_problem_04([1, 2, 0])
104104
3
105-
>>> coding_problem_4([4, 1, 2, 2, 2, 1, 0])
105+
>>> coding_problem_04([4, 1, 2, 2, 2, 1, 0])
106106
3
107107
108108
Notes: the code below is a bucket sort variant, and therefore has linear time complexity equal to O(n).
@@ -147,13 +147,13 @@ def coding_problem_4(array):
147147
return len(array) # if here, the sought integer is past the array end
148148

149149

150-
def coding_problem_5():
150+
def coding_problem_05():
151151
"""
152152
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair.
153153
Given this implementation of cons below, implement car and cdr.
154154
Examples: car(cons(3, 4)) == 3, cdr(cons(3, 4)) == 4
155155
156-
>>> coding_problem_5()
156+
>>> coding_problem_05()
157157
True
158158
"""
159159
def cons(a, b):

daily_coding_problem_06_10.py renamed to previous/daily_coding_problem_06_10.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
def coding_problem_6():
1+
def coding_problem_06():
22
"""
33
An XOR linked list is a more memory efficient doubly linked list.
44
Instead of each node holding next and prev fields, it holds a field named both, which is a XOR of the next node
55
and the previous node. Implement a XOR linked list; it has an add(element) which adds the element to the end,
66
and a get(index) which returns the node at index.
77
Example:
88
9-
>>> l = coding_problem_6()
9+
>>> l = coding_problem_06()
1010
>>> for cnt in xrange(0, 4):
1111
... l.add(cnt)
1212
>>> l.get(2) == 2
@@ -59,63 +59,63 @@ def get(self, index):
5959
return XORLinkedList()
6060

6161

62-
def coding_problem_7(s):
62+
def coding_problem_07(s):
6363
"""
6464
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
6565
Examples:
6666
67-
>>> coding_problem_7('111') # possible interpretations: 'aaa', 'ka', 'ak'
67+
>>> coding_problem_07('111') # possible interpretations: 'aaa', 'ka', 'ak'
6868
3
69-
>>> coding_problem_7('2626') # 'zz', 'zbf', 'bfz', 'bfbf'
69+
>>> coding_problem_07('2626') # 'zz', 'zbf', 'bfz', 'bfbf'
7070
4
7171
"""
7272
symbols = map(str, range(1, 27))
7373
if not s:
7474
return 1
7575

7676
matches = filter(lambda symbol: s.startswith(symbol), symbols)
77-
encodings = [coding_problem_7(s[len(m):]) for m in matches]
77+
encodings = [coding_problem_07(s[len(m):]) for m in matches]
7878
return sum(encodings)
7979

8080

81-
def coding_problem_8(btree):
81+
def coding_problem_08(btree):
8282
"""
8383
A unival tree (which stands for "universal value") is a tree where all nodes have the same value.
8484
Given the root to a binary tree, count the number of unival subtrees.
8585
Example:
8686
8787
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
88-
>>> coding_problem_8(btree)[0]
88+
>>> coding_problem_08(btree)[0]
8989
6
9090
"""
9191
val, ln, rn = btree
9292
if ln is None and rn is None: # leaf case
9393
return 1, True, val
9494

95-
lcount, is_uni_l, lval = coding_problem_8(ln)
96-
rcount, is_uni_r, rval = coding_problem_8(rn)
95+
lcount, is_uni_l, lval = coding_problem_08(ln)
96+
rcount, is_uni_r, rval = coding_problem_08(rn)
9797

9898
is_unival = is_uni_l and is_uni_r and val == lval and val == rval
9999
count = lcount + rcount + is_unival
100100
return count, is_unival, val
101101

102102

103-
def coding_problem_9(numbers):
103+
def coding_problem_09(numbers):
104104
"""
105105
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers.
106106
The "largest sum of non-adjacent numbers" is the sum of any subset of non-contiguous elements.
107107
Solution courtesy of Kye Jiang (https://github.com/Jedshady).
108108
Examples:
109109
110-
>>> coding_problem_9([2, 4, 6, 8])
110+
>>> coding_problem_09([2, 4, 6, 8])
111111
12
112-
>>> coding_problem_9([5, 1, 1, 5])
112+
>>> coding_problem_09([5, 1, 1, 5])
113113
10
114-
>>> coding_problem_9([1, 2, 3, 4, 5, 6])
114+
>>> coding_problem_09([1, 2, 3, 4, 5, 6])
115115
12
116-
>>> coding_problem_9([-8, 4, -3, 2, 3, 4])
116+
>>> coding_problem_09([-8, 4, -3, 2, 3, 4])
117117
10
118-
>>> coding_problem_9([2, 4, 6, 2, 5])
118+
>>> coding_problem_09([2, 4, 6, 2, 5])
119119
13
120120
"""
121121
if not numbers:
@@ -124,8 +124,8 @@ def coding_problem_9(numbers):
124124
if len(numbers) <= 2:
125125
return max(numbers)
126126

127-
with_last = coding_problem_9(numbers[:-2]) + numbers[-1] # sum include last number
128-
without_last = coding_problem_9(numbers[:-1]) # sum without last number
127+
with_last = coding_problem_09(numbers[:-2]) + numbers[-1] # sum include last number
128+
without_last = coding_problem_09(numbers[:-1]) # sum without last number
129129
return max(with_last, without_last)
130130

131131

problems/01/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
##Problem 1
2+
3+
Given a stack of N elements, interleave the first half of the stack
4+
with the second half reversed using one other queue.
5+
Example:
6+
7+
>>> coding_problem_01([1, 2, 3, 4, 5])
8+
[1, 5, 2, 4, 3]
9+
10+
>>> coding_problem_01([1, 2, 3, 4, 5, 6])
11+
[1, 6, 2, 5, 3, 4]
12+
13+
Note: with the **itertools** module, you could instead:
14+
15+
islice(chain.from_iterable(izip(l, reversed(l))), len(l))

problems/01/problem_01.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def coding_problem_01(stack):
2+
"""
3+
Given a stack of N elements, interleave the first half of the stack
4+
with the second half reversed using one other queue.
5+
Example:
6+
7+
>>> coding_problem_01([1, 2, 3, 4, 5])
8+
[1, 5, 2, 4, 3]
9+
>>> coding_problem_01([1, 2, 3, 4, 5, 6])
10+
[1, 6, 2, 5, 3, 4]
11+
12+
Note: with itertools, you could instead islice(chain.from_iterable(izip(l, reversed(l))), len(l))
13+
"""
14+
pass
15+
16+
17+
if __name__ == '__main__':
18+
19+
import doctest
20+
doctest.testmod(verbose=True)

problems/01/solution_01.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import deque
2+
3+
4+
def coding_problem_01(stack):
5+
"""
6+
Given a stack of N elements, interleave the first half of the stack
7+
with the second half reversed using one other queue.
8+
Example:
9+
10+
>>> coding_problem_01([1, 2, 3, 4, 5])
11+
[1, 5, 2, 4, 3]
12+
>>> coding_problem_01([1, 2, 3, 4, 5, 6])
13+
[1, 6, 2, 5, 3, 4]
14+
15+
Note: with itertools, you could instead islice(chain.from_iterable(izip(l, reversed(l))), len(l))
16+
"""
17+
queue = deque([]) # stack S:[1,2,3,4,5], queue Q:[]
18+
for cnt in range(len(stack) - 1): # move stack into queue. S:[1], Q:[5,4,3,2]
19+
queue.append(stack.pop())
20+
for cnt in range(len(queue) // 2):
21+
stack.append(queue.popleft()) # S:[1,5], Q:[4,3,2]
22+
for cnt2 in range(len(queue) - 1): # rotate last element to front, S:[1,5], Q:[2,4,3]
23+
queue.append(queue.popleft())
24+
stack.append(queue.popleft()) # S:[1,5,2], Q:[4,3]
25+
if queue:
26+
stack.append(queue.popleft())
27+
return stack
28+
29+
30+
if __name__ == '__main__':
31+
32+
import doctest
33+
doctest.testmod(verbose=True)

problems/02/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
##Problem 2
2+
3+
Given an array of integers, return a new array such that each element at index i of
4+
the new array is the product of all the numbers in the original array except the one at i.
5+
Solve it without using division and in O(n).
6+
Example:
7+
8+
>>> coding_problem_02([1, 2, 3, 4, 5])
9+
[120, 60, 40, 30, 24]

problems/02/problem_02.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
3+
4+
def coding_problem_02(l):
5+
"""
6+
Given an array of integers, return a new array such that each element at index i of
7+
the new array is the product of all the numbers in the original array except the one at i.
8+
Solve it without using division and in O(n).
9+
Example:
10+
11+
>>> coding_problem_02([1, 2, 3, 4, 5])
12+
[120, 60, 40, 30, 24]
13+
"""
14+
pass
15+
16+
17+
if __name__ == '__main__':
18+
19+
import doctest
20+
doctest.testmod(verbose=True)

problems/02/solution_02.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import deque
2+
3+
4+
def coding_problem_02(l):
5+
"""
6+
Given an array of integers, return a new array such that each element at index i of
7+
the new array is the product of all the numbers in the original array except the one at i.
8+
Solve it without using division and in O(n).
9+
Example:
10+
11+
>>> coding_problem_02([1, 2, 3, 4, 5])
12+
[120, 60, 40, 30, 24]
13+
"""
14+
forward = [1] * len(l)
15+
backward = [1] * len(l)
16+
for idx in xrange(1, len(l)):
17+
18+
forward[idx] = forward[idx - 1] * l[idx - 1]
19+
backward[-idx - 1] = backward[-idx] * l[-idx]
20+
21+
return [f * b for f, b in zip(forward, backward)]
22+
23+
24+
if __name__ == '__main__':
25+
26+
import doctest
27+
doctest.testmod(verbose=True)

problems/03/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##Problem 3
2+
3+
Given the root to a binary tree, implement serialize(root), which serializes the tree
4+
into a string, and deserialize(s), which deserializes the string back into the tree.
5+
Example:
6+
7+
>>> s = '3 2 1 None None None 4 5 None None 6 None None'
8+
>>> de_serialized = coding_problem_03(s)
9+
>>> re_serialized = de_serialized.serialize_to_string()
10+
>>> s == re_serialized
11+
True

problems/03/problem_03.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import deque
2+
3+
4+
def coding_problem_03(s):
5+
"""
6+
Given the root to a binary tree, implement serialize(root), which serializes the tree
7+
into a string, and deserialize(s), which deserializes the string back into the tree.
8+
Example:
9+
10+
>>> s = '3 2 1 None None None 4 5 None None 6 None None'
11+
>>> de_serialized = coding_problem_03(s)
12+
>>> re_serialized = de_serialized.serialize_to_string()
13+
>>> s == re_serialized
14+
True
15+
"""
16+
pass
17+
18+
19+
if __name__ == '__main__':
20+
21+
import doctest
22+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)