Skip to content

Commit f2f1944

Browse files
committed
Finished formatting problems
1 parent d6aa740 commit f2f1944

Some content is hidden

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

62 files changed

+145
-298
lines changed

Diff for: previous/README.md renamed to legacy2.7/README.md

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: problems/34/README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
##Problem 34
1+
## Problem 34
22

33
Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible
44
anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the
55
lexicographically earliest one (the first one alphabetically).
6+
67
Examples:
78

89
>>> coding_problem_34("race")
910
'ecarace'
11+
1012
>>> coding_problem_34("google")
1113
'elgoogle'
14+
1215
>>> coding_problem_34("aibohphobia")
1316
'aibohphobia'
14-
15-
Note: this is similar to #31.
16-
For each given word w, there are 2*len(w)-1 possible palindromes made using as centers either a character (len(w))
17-
or the location between two characters (len(w)-1).

Diff for: problems/34/problem_34.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
3-
41
def coding_problem_34(s):
52
"""
63
Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible
@@ -14,10 +11,6 @@ def coding_problem_34(s):
1411
'elgoogle'
1512
>>> coding_problem_34("aibohphobia")
1613
'aibohphobia'
17-
18-
Note: this is similar to #31.
19-
For each given word w, there are 2*len(w)-1 possible palindromes made using as centers either a character (len(w))
20-
or the location between two characters (len(w)-1).
2114
"""
2215
pass
2316

Diff for: problems/34/solution_34.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
3-
41
def coding_problem_34(s):
52
"""
63
Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible
@@ -16,8 +13,8 @@ def coding_problem_34(s):
1613
'aibohphobia'
1714
1815
Note: this is similar to #31.
19-
For each given word w, there are 2*len(w)-1 possible palindromes made using as centers either a character (len(w))
20-
or the location between two characters (len(w)-1).
16+
For each given word w, there are 2*len(w)-1 possible palindromes made using as
17+
centers either a character (len(w)) or the location between two characters (len(w)-1).
2118
"""
2219
def recurse(palindrome, before, after):
2320
if not before or not after:

Diff for: problems/35/README.md

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
##Problem 35
1+
## Problem 35
22

33
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the 'R's
44
come first, the 'G's come second, and the 'B's come last. You can only swap elements of the array.
5-
Do this in linear time and in-place. For example:
5+
Do this in linear time and in-place.
66

7-
>>> coding_problem_35(['G', 'B', 'R', 'R', 'B', 'R', 'G'])
8-
['R', 'R', 'R', 'G', 'G', 'B', 'B']
9-
10-
The problem can be solved by (insertion) sorting as follows.
11-
This solution does not take advantage of the expected high number of equal value elements.
7+
For example:
128

13-
>>> rgbs = ['G', 'B', 'R', 'R', 'B', 'R', 'G']
14-
>>> for i in range(len(rgbs) - 1):
15-
... for j in range(i + 1, len(rgbs)):
16-
... if rgbs[i] < rgbs[j]: # 'R' > 'G' > 'B'
17-
... rgbs[i], rgbs[j] = rgbs[j], rgbs[i]
18-
>>> rgbs
9+
>>> coding_problem_35(['G', 'B', 'R', 'R', 'B', 'R', 'G'])
1910
['R', 'R', 'R', 'G', 'G', 'B', 'B']
20-
21-
Sorting as described above has a complexity of O(n^2).
22-
The following solution does at most two passes, and therefore is O(n).

Diff for: problems/35/problem_35.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
3-
41
def coding_problem_35(rgbs):
52
"""
63
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the 'R's
@@ -9,19 +6,5 @@ def coding_problem_35(rgbs):
96
107
>>> coding_problem_35(['G', 'B', 'R', 'R', 'B', 'R', 'G'])
118
['R', 'R', 'R', 'G', 'G', 'B', 'B']
12-
13-
The problem can be solved by (insertion) sorting as follows.
14-
This solution does not take advantage of the expected high number of equal value elements.
15-
16-
>>> rgbs = ['G', 'B', 'R', 'R', 'B', 'R', 'G']
17-
>>> for i in range(len(rgbs) - 1):
18-
... for j in range(i + 1, len(rgbs)):
19-
... if rgbs[i] < rgbs[j]: # 'R' > 'G' > 'B'
20-
... rgbs[i], rgbs[j] = rgbs[j], rgbs[i]
21-
>>> rgbs
22-
['R', 'R', 'R', 'G', 'G', 'B', 'B']
23-
24-
Sorting as described above has a complexity of O(n^2).
25-
The following solution does at most two passes, and therefore is O(n).
269
"""
27-
pass
10+
pass

Diff for: problems/35/solution_35.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
3-
41
def coding_problem_35(rgbs):
52
"""
63
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the 'R's
@@ -58,4 +55,4 @@ def coding_problem_35(rgbs):
5855
if __name__ == '__main__':
5956

6057
import doctest
61-
doctest.testmod(verbose=True)
58+
doctest.testmod(verbose=True)

Diff for: problems/36/README.md

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

33
Given the root to a binary search tree, find the second largest node in the tree.
4+
45
Example:
56

67
>>> tree = [9, [4, [1, None, None], [7, [5, None, None], None]], [31, [14, None, None], [82, None, None]]]
78
>>> coding_problem_36(tree)
89
31
9-
10-
Note: in a binary search tree, the second largest node is the root of the largest element.

Diff for: problems/36/problem_36.py

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import math
2-
3-
41
def coding_problem_36(tree):
52
"""
63
Given the root to a binary search tree, find the second largest node in the tree.
@@ -9,8 +6,6 @@ def coding_problem_36(tree):
96
>>> tree = [9, [4, [1, None, None], [7, [5, None, None], None]], [31, [14, None, None], [82, None, None]]]
107
>>> coding_problem_36(tree)
118
31
12-
13-
Note: in a binary search tree, the second largest node is the root of the largest element.
149
"""
1510
pass
1611

Diff for: problems/36/solution_36.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import math
2-
3-
41
def coding_problem_36(tree):
52
"""
63
Given the root to a binary search tree, find the second largest node in the tree.

Diff for: problems/37/README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
##Problem 37
1+
## Problem 37
22

33
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
4-
You may also use a list or array to represent a set. Example:
4+
You may also use a list or array to represent a set.
55

6-
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], cmp=lambda a, b: cmp(len(a), len(b)))
7-
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
6+
Example:
7+
8+
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], key=len)
9+
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

Diff for: problems/37/problem_37.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import math
2-
3-
41
def coding_problem_37(s):
52
"""
63
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
74
You may also use a list or array to represent a set. Example:
85
9-
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], cmp=lambda a, b: cmp(len(a), len(b)))
6+
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], key=len)
107
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
118
"""
129
pass

Diff for: problems/37/solution_37.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import math
2-
3-
41
def coding_problem_37(s):
52
"""
63
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
74
You may also use a list or array to represent a set. Example:
85
9-
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], cmp=lambda a, b: cmp(len(a), len(b)))
6+
>>> sorted([sorted(subset) for subset in coding_problem_37({1, 2, 3})], key=len)
107
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
118
"""
129
power_set = [[]]

Diff for: problems/38/README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
##Problem 38
1+
## Problem 38
22

33
You have an N by N board. Write a function that, given N, returns the number of possible arrangements of the board
44
where N queens can be placed on the board without threatening each other, i.e. no two queens share the same row,
5-
column, or diagonal. From this Wikipedia article https://en.wikipedia.org/wiki/Eight_queens_puzzle :
5+
column, or diagonal.
6+
7+
The following are the first 7 terms of the sequence (from [Wikipedia's Eight queens puzzle](https://en.wikipedia.org/wiki/Eight_queens_puzzle) page):
68

79
>>> [coding_problem_38(n + 1) for n in range(7)]
810
[1, 0, 0, 2, 10, 4, 40]
9-
10-
Note: this could be made much faster by passing only valid coordinates instead of all of them each time, but
11-
it is an exercise left for the reader ;)

Diff for: problems/38/problem_38.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import math
2-
3-
41
def coding_problem_38(n):
52
"""
63
You have an N by N board. Write a function that, given N, returns the number of possible arrangements of the board
74
where N queens can be placed on the board without threatening each other, i.e. no two queens share the same row,
8-
column, or diagonal. From this Wikipedia article https://en.wikipedia.org/wiki/Eight_queens_puzzle :
5+
column, or diagonal. The following are the first 7 terms of the sequence (from [Wikipedia's
6+
https://en.wikipedia.org/wiki/Eight_queens_puzzle page):
97
108
>>> [coding_problem_38(n + 1) for n in range(7)]
119
[1, 0, 0, 2, 10, 4, 40]
12-
13-
Note: this could be made much faster by passing only valid coordinates instead of all of them each time, but
14-
it is an exercise left for the reader ;)
1510
"""
1611
pass
1712

Diff for: problems/38/solution_38.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import math
2-
3-
41
def coding_problem_38(n):
52
"""
63
You have an N by N board. Write a function that, given N, returns the number of possible arrangements of the board
74
where N queens can be placed on the board without threatening each other, i.e. no two queens share the same row,
8-
column, or diagonal. From this Wikipedia article https://en.wikipedia.org/wiki/Eight_queens_puzzle :
5+
column, or diagonal. The following are the first 7 terms of the sequence (from [Wikipedia's
6+
https://en.wikipedia.org/wiki/Eight_queens_puzzle page):
97
108
>>> [coding_problem_38(n + 1) for n in range(7)]
119
[1, 0, 0, 2, 10, 4, 40]
1210
13-
Note: this could be made much faster by passing only valid coordinates instead of all of them each time, but
14-
it is an exercise left for the reader ;)
11+
Note: could be made faster by passing only valid coordinates instead of all of them each time.
1512
"""
1613
def is_threat(new_queen, board_arrangement):
1714
for already_placed in board_arrangement:

Diff for: problems/39/README.md

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1-
##Problem 39
1+
## Problem 39
22

33
Conway's Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or
44
alive, and at each tick, the following rules apply:
55

6-
Any live cell with less than two live neighbours dies.
7-
Any live cell with two or three live neighbours remains living.
8-
Any live cell with more than three live neighbours dies.
9-
Any dead cell with exactly three live neighbours becomes a live cell.
10-
A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
6+
* Any live cell with less than two live neighbours dies.
7+
* Any live cell with two or three live neighbours remains living.
8+
* Any live cell with more than three live neighbours dies.
9+
* Any dead cell with exactly three live neighbours becomes a live cell.
10+
* A cell neighbours another cell if it is horizontally, vertically, or diagonally adjacent.
1111

1212
Implement Conway's Game of Life. It should be able to be initialized with a starting list of live cell coordinates
1313
and the number of steps it should run for. Once initialized, it should print out the board state at each step.
1414
Since it's an infinite board, print out only the relevant coordinates, i.e. from the top-leftmost live cell to
1515
bottom-rightmost live cell.
1616

1717
You can represent a live cell with an asterisk (*) and a dead cell with a dot (.).
18-
Example: simulate a glider (https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)) advancing one step.
18+
19+
Example. Simulate a [glider](https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life\)) advancing five steps:
1920

2021
>>> gol = coding_problem_39(((0, 1), (1, 2), (2, 0), (2, 1), (2, 2)))
2122
>>> for _ in range(5):
2223
... print gol
23-
... gol.simulate()
24-
..*
25-
*.*
26-
.**
27-
<BLANKLINE>
28-
*..
29-
.**
30-
**.
31-
<BLANKLINE>
32-
.*.
33-
..*
34-
***
35-
<BLANKLINE>
36-
*.*
37-
.**
38-
.*.
39-
<BLANKLINE>
40-
..*
41-
*.*
42-
.**
43-
<BLANKLINE>
24+
... gol.simulate()
25+
..*
26+
*.*
27+
.**
28+
<BLANKLINE>
29+
*..
30+
.**
31+
**.
32+
<BLANKLINE>
33+
.*.
34+
..*
35+
***
36+
<BLANKLINE>
37+
*.*
38+
.**
39+
.*.
40+
<BLANKLINE>
41+
..*
42+
*.*
43+
.**
44+
<BLANKLINE>

Diff for: problems/39/problem_39.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import math
2-
3-
41
def coding_problem_39(cells):
52
"""
63
Conway's Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or
@@ -18,7 +15,7 @@ def coding_problem_39(cells):
1815
bottom-rightmost live cell.
1916
2017
You can represent a live cell with an asterisk (*) and a dead cell with a dot (.).
21-
Example: simulate a glider (https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)) advancing one step.
18+
Example. Simulate a glider https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life) advancing five steps:
2219
2320
>>> gol = coding_problem_39(((0, 1), (1, 2), (2, 0), (2, 1), (2, 2)))
2421
>>> for _ in range(5):

Diff for: problems/39/solution_39.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import math
2-
3-
41
def coding_problem_39(cells):
52
"""
63
Conway's Game of Life takes place on an infinite two-dimensional board of square cells. Each cell is either dead or
@@ -18,7 +15,7 @@ def coding_problem_39(cells):
1815
bottom-rightmost live cell.
1916
2017
You can represent a live cell with an asterisk (*) and a dead cell with a dot (.).
21-
Example: simulate a glider (https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)) advancing one step.
18+
Example. Simulate a glider https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life) advancing five steps:
2219
2320
>>> gol = coding_problem_39(((0, 1), (1, 2), (2, 0), (2, 1), (2, 2)))
2421
>>> for _ in range(5):

Diff for: problems/40/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
##Problem 40
1+
## Problem 40
22

33
Given an array of integers where every integer occurs three times except for one integer, which only occurs once,
44
find and return the non-duplicated integer. Do this in O(N) time and O(1) space.
5+
56
Examples:
67

78
>>> coding_problem_40([6, 1, 3, 3, 3, 6, 6])
89
1
10+
911
>>> coding_problem_40([13, 19, 13, 13])
1012
19
11-
12-
Note: the code below fails for negative numbers, but you could shift the entire array by the minimum.
13-
Note2: the implementation below makes it evident that the space complexity is O(log_2(n)) rather than O(1).
14-
This is always the case, unless a specific integer type or a maximum value are specified (they are not above).

0 commit comments

Comments
 (0)