Skip to content

Commit 18de8f4

Browse files
committed
Up to #25
1 parent 500f63e commit 18de8f4

24 files changed

+246
-110
lines changed

problems/16/README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
You run a sneaker website and want to record the last N order ids in a log. Implement a data structure to
44
accomplish this, with the following API:
55

6-
record(order_id): adds the order_id to the log
7-
get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
6+
* record(order_id): adds the order_id to the log
7+
* get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
88

99
You should be as efficient with time and space as possible.
10+
1011
Example:
1112

1213
>>> log = coding_problem_16(10)
13-
>>> for id in xrange(20):
14+
>>> for id in range(20):
1415
... log.record(id)
1516

1617
>>> log.get_last(0)
@@ -26,4 +27,4 @@ Example:
2627
>>> log.get_last(1)
2728
[21]
2829
>>> log.get_last(3)
29-
[19, 20, 21]
30+
[19, 20, 21]

problems/16/problem_16.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def coding_problem_16(length):
1010
Example:
1111
1212
>>> log = coding_problem_16(10)
13-
>>> for id in xrange(20):
13+
>>> for id in range(20):
1414
... log.record(id)
1515
1616
>>> log.get_last(0)

problems/16/solution_16.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def coding_problem_16(length):
1010
Example:
1111
1212
>>> log = coding_problem_16(10)
13-
>>> for id in xrange(20):
13+
>>> for id in range(20):
1414
... log.record(id)
1515
1616
>>> log.get_last(0)

problems/17/README.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
##Problem 17
1+
## Problem 17
22

33
Suppose we represent our file system by a string in the following manner:
44
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
55

6-
dir
7-
subdir1
8-
subdir2
9-
file.ext
6+
dir
7+
subdir1
8+
subdir2
9+
file.ext
1010

1111
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
1212
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
1313

14-
dir
15-
subdir1
16-
file1.ext
17-
subsubdir1
18-
subdir2
19-
subsubdir2
20-
file2.ext
14+
dir
15+
subdir1
16+
file1.ext
17+
subsubdir1
18+
subdir2
19+
subsubdir2
20+
file2.ext
2121

2222
The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty
2323
second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a
@@ -32,6 +32,7 @@ a file in the abstracted file system. If there is no file in the system, return
3232

3333
The name of a file contains at least a period and an extension.
3434
The name of a directory or sub-directory will not contain a period.
35+
3536
Examples:
3637

3738
>>> coding_problem_17('file1.ext')
@@ -45,13 +46,13 @@ Examples:
4546

4647
>>> coding_problem_17('dir\n\t\tfile1.ext')
4748
Traceback (most recent call last):
48-
...
49-
RuntimeError: Malformed path string: nesting more than one level at a time.
49+
...
50+
RuntimeError: Malformed path string: nesting more than one level at a time.
5051

5152
>>> coding_problem_17('dir\n\tfile1.ext\n\t\tchild_of_a_file.ext')
5253
Traceback (most recent call last):
53-
...
54-
RuntimeError: Malformed path string: a file cannot contain something else.
54+
...
55+
RuntimeError: Malformed path string: a file cannot contain something else.
5556

5657
>>> coding_problem_17('dir\n\tfile1.ext\n\tsubdir\n\t\tsubsubdir\n\t\t\ttsubsubsubdir')
5758
13
@@ -61,4 +62,4 @@ RuntimeError: Malformed path string: a file cannot contain something else.
6162

6263
>>> coding_problem_17('dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext' +
6364
... '\ndir2\n\tsubdir1\n\tsubdir2\n\t\tsubsubdir1\n\t\t\tsubsubsubdir3\n\t\t\t\tfile3.ext')
64-
47
65+
47

problems/18/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
##Problem 18
1+
## Problem 18
22

33
Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each
44
sub-array of length k. Do this in O(n) time and O(k) space. You can modify the input array in-place and you do not
55
need to store the results. You can simply print them out as you compute them.
6+
67
Example:
78

89
>>> coding_problem_18([10, 5, 2, 7, 8, 7], 3)
9-
[10, 7, 8, 8]
10+
[10, 7, 8, 8]

problems/18/solution_18.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def coding_problem_18(arr, k):
88
>>> coding_problem_18([10, 5, 2, 7, 8, 7], 3)
99
[10, 7, 8, 8]
1010
"""
11-
for cnt in xrange(k - 1):
11+
for _ in range(k - 1):
1212
arr = [max(value, other) for value, other in zip(arr[:-1], arr[1:])]
1313

1414
return arr

problems/19/README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
##Problem 19
1+
## Problem 19
22

33
A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost
44
while ensuring that no two neighboring houses are of the same color. Given an N by K matrix where the nth row and
5-
kth column represents the cost to build the nth house with kth color, return the minimum cost.
5+
kth column represents the cost to build the nth house with kth color, return the minimum cost.
6+
7+
Example:
8+
9+
>>> house_size = [1, 2, 4, 2] # size of the house
10+
>>> color_cost = [1, 2, 4] # cost of paint for each color
11+
>>> house_costs = [[cc * hs for cc in color_cost] for hs in house_size] # 4 houses, 3 colors
12+
>>> coding_problem_19(house_costs) # [1,2,4,2] . [1,2,1,2] == 1*1 + 2*2 + 1*4 + 2*2 == 13
13+
13

problems/19/problem_19.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
def coding_problem_19(costs):
1+
def coding_problem_19(house_costs):
22
"""
33
A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost
44
while ensuring that no two neighboring houses are of the same color. Given an N by K matrix where the nth row and
55
kth column represents the cost to build the nth house with kth color, return the minimum cost.
6+
Example:
7+
8+
>>> house_size = [1, 2, 4, 2] # size of the house
9+
>>> color_cost = [1, 2, 4] # cost of paint for each color
10+
>>> house_costs = [[cc * hs for cc in color_cost] for hs in house_size] # 4 houses, 3 colors
11+
>>> coding_problem_19(house_costs) # [1,2,4,2] . [1,2,1,2] == 1*1 + 2*2 + 1*4 + 2*2 == 13
12+
13
613
"""
714
pass
815

problems/19/solution_19.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
def coding_problem_19(costs):
1+
def coding_problem_19(house_costs):
22
"""
33
A builder is looking to build a row of N houses that can be of K different colors. He has a goal of minimizing cost
44
while ensuring that no two neighboring houses are of the same color. Given an N by K matrix where the nth row and
55
kth column represents the cost to build the nth house with kth color, return the minimum cost.
6+
Example:
7+
8+
>>> house_size = [1, 2, 4, 2] # size of the house
9+
>>> color_cost = [1, 2, 4] # cost of paint for each color
10+
>>> house_costs = [[cc * hs for cc in color_cost] for hs in house_size] # 4 houses, 3 colors
11+
>>> coding_problem_19(house_costs) # [1,2,4,2] . [1,2,1,2] == 1*1 + 2*2 + 1*4 + 2*2 == 13
12+
13
13+
14+
Notes: this is dynamic programming in disguise. We assign a color to each house in order, and keep
15+
track of the minimum cost associated with each choice of color. These costs are the minimum over all
16+
possible permutation in which the last added house is of a particular color.
617
"""
7-
best_cost = [0] * len(costs[0])
8-
for cost in costs: # add a house at a time
9-
for index in xrange(len(cost)): # best cost is the one for that color plus min cost between every other color
10-
best_cost[index] = cost[index] + min(best_cost[:index] + best_cost[index + 1:])
18+
best_costs = [0] * len(house_costs[0])
19+
for color_costs in house_costs: # color_costs are those paid to paint last added house with a certain color
20+
best_costs = [color_costs[i] + min(best_costs[:i] + best_costs[i + 1:]) for i in range(len(best_costs))]
1121

12-
return min(best_cost)
22+
return min(best_costs)
1323

1424

1525
if __name__ == '__main__':

problems/20/README.md

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
##Problem 20
1+
## Problem 20
22

33
Given two singly linked lists that intersect at some point, find the intersecting node.
44
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.
55
For example, given A = 3 -> 7 -> 8 -> 10 -> 1 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.
6+
67
Example:
78

89
>>> class LinkedListNode(object):
910
...
10-
... def __init__(self, value, child=None):
11-
... self.value = value
12-
... self.next = child
13-
...
14-
... def add(self, value):
15-
... return LinkedListNode(value, self)
16-
...
17-
... @classmethod
18-
... def len(cls, node):
19-
... count = 0
20-
... while node:
21-
... node = node.next
22-
... count += 1
23-
... return count
11+
... def __init__(self, value, child=None):
12+
... self.value = value
13+
... self.next = child
14+
...
15+
... def add(self, value):
16+
... return LinkedListNode(value, self)
17+
...
18+
... @classmethod
19+
... def len(cls, node):
20+
... count = 0
21+
... while node:
22+
... node = node.next
23+
... count += 1
24+
... return count
2425

2526
>>> common_tail = LinkedListNode(1).add(10).add(8)
2627
>>> list_a = LinkedListNode(7, common_tail).add(3)
2728
>>> list_b = LinkedListNode(1, common_tail).add(99).add(14)
2829
>>> coding_problem_20(list_a, list_b)
2930
8
30-
31-
Note: the problem statement above is ambiguous and misleading. I think B list was originally supposed to end
32-
up with a -> 1. This is how most problems of this type I googled are formulated. If this is not the case, this
33-
is akin to finding the longest common list between the lists, which I believe cannot be solved in O(M+N).

problems/20/problem_20.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ def coding_problem_20(list_a, list_b):
2727
>>> list_b = LinkedListNode(1, common_tail).add(99).add(14)
2828
>>> coding_problem_20(list_a, list_b)
2929
8
30-
31-
Note: the problem statement above is ambiguous and misleading. I think B list was originally supposed to end
32-
up with a -> 1. This is how most problems of this type I googled are formulated. If this is not the case, this
33-
is akin to finding the longest common list between the lists, which I believe cannot be solved in O(M+N).
3430
"""
35-
pass
31+
pass

problems/20/solution_20.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def coding_problem_20(list_a, list_b):
3737
if len_b > len_a:
3838
list_a, list_b = list_b, list_a
3939

40-
for advance in xrange(abs(len_a - len_b)):
40+
for advance in range(abs(len_a - len_b)):
4141
list_a = list_a.next
4242

43-
for check in xrange(len_b):
43+
for check in range(len_b):
4444
if list_a is list_b:
4545
return list_a.value
4646

@@ -53,4 +53,4 @@ def coding_problem_20(list_a, list_b):
5353
if __name__ == '__main__':
5454

5555
import doctest
56-
doctest.testmod(verbose=True)
56+
doctest.testmod(verbose=True)

problems/21/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
##Problem 21
1+
## Problem 21
22

33
Given an array of time intervals (start, end) for classroom lectures (possibly overlapping),
44
find the minimum number of rooms required.
5+
56
Example:
67

78
>>> coding_problem_21([(30, 75), (0, 50), (60, 150)])

problems/22/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
##Problem 22
1+
## Problem 22
22

33
Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a
44
list. If there is more than one possible reconstruction, return any of them. If there is no possible
55
reconstruction, then return null.
6+
67
Examples:
78

89
>>> coding_problem_22(['Riccardo', 'Brigittie', 'and', 'lollipop'], 'RiccardoandBrigittie')
@@ -12,4 +13,4 @@ Examples:
1213
['the', 'quick', 'brown', 'fox']
1314

1415
>>> coding_problem_22(['bed', 'bath', 'bedbath', 'and', 'beyond'], 'bedbathandbeyond')
15-
['bed', 'bath', 'and', 'beyond']
16+
['bed', 'bath', 'and', 'beyond']

problems/23/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
##Problem 23
1+
## Problem 23
22

33
You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall.
44
Each False boolean represents a tile you can walk on. Given this matrix, a start coordinate, and an end coordinate,
55
return the minimum number of steps required to reach the end coordinate from the start. If there is no possible
66
path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap
77
around the edges of the board.
8+
89
Examples:
910

1011
>>> map = [[False, False, False, False], [True, True, False, True],
1112
... [False, False, False, False], [False, False, False, False]]
1213
>>> coding_problem_23(matrix, (3, 0), (0, 0))
1314
7
15+
1416
>>> map[1][2] = True # close off path
15-
>>> coding_problem_23(matrix, (3, 0), (0, 0)) # None
17+
>>> coding_problem_23(matrix, (3, 0), (0, 0)) # returns None
18+

problems/23/problem_23.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ def coding_problem_23(matrix, start, end):
1111
... [False, False, False, False], [False, False, False, False]]
1212
>>> coding_problem_23(matrix, (3, 0), (0, 0))
1313
7
14+
1415
>>> map[1][2] = True # close off path
1516
>>> coding_problem_23(matrix, (3, 0), (0, 0)) # None
17+
1618
"""
1719
pass
1820

problems/23/solution_23.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ def coding_problem_23(matrix, start, end):
1111
... [False, False, False, False], [False, False, False, False]]
1212
>>> coding_problem_23(matrix, (3, 0), (0, 0))
1313
7
14+
1415
>>> map[1][2] = True # close off path
1516
>>> coding_problem_23(matrix, (3, 0), (0, 0)) # None
17+
1618
"""
1719
coords = [(index_r, index_c) for index_r, row in enumerate(matrix)
1820
for index_c, element in enumerate(row) if not element]
1921

2022
current_distance = 0
21-
distances = [[None for col in xrange(len(matrix[0]))] for row in xrange(len(matrix))]
23+
distances = [[None for col in range(len(matrix[0]))] for row in range(len(matrix))]
2224
distances[start[0]][start[1]] = 0
2325
while True:
2426

0 commit comments

Comments
 (0)