Skip to content

Commit 500f63e

Browse files
committed
Up to #15
1 parent 956c04b commit 500f63e

30 files changed

+98
-207
lines changed

previous/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Daily coding problem
2+
3+
These are my solutions to https://dailycodingproblem.com, a great resource to practice coding interviews.
4+
5+
I have copied problem definitions and examples verbatim from the one I received, but they might occasionally differ
6+
from yours when questions have been updated to improve clarity or provide additional tests.
7+
Notes added after examples are comments of mine.
8+
9+
Pull requests warmly welcomed if you spot issues with any of my solutions!

previous/run_tests.py

Whitespace-only changes.

problems/06/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
##Problem 6
1+
## Problem 6
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.
7+
78
Example:
89

9-
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
11-
... l.add(cnt)
12-
>>> l.get(2) == 2
10+
>>> xll = coding_problem_06()
11+
>>> for cnt in range(0, 4):
12+
... xll.add(cnt)
13+
>>> xll.get(2) == 2
1314
True
14-
15-
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.

problems/06/problem_06.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ def coding_problem_06():
66
and a get(index) which returns the node at index.
77
Example:
88
9-
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
11-
... l.add(cnt)
12-
>>> l.get(2) == 2
9+
>>> xll = coding_problem_06()
10+
>>> for cnt in range(0, 4):
11+
... xll.add(cnt)
12+
>>> xll.get(2) == 2
1313
True
14-
15-
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.
1814
"""
1915
pass
2016

problems/06/solution_06.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ def coding_problem_06():
77
Example:
88
99
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
10+
>>> for cnt in range(0, 4):
1111
... l.add(cnt)
1212
>>> l.get(2) == 2
1313
True
1414
1515
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.
16+
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory.
1817
"""
1918
class XORLinkedListNode(object):
2019

@@ -51,7 +50,7 @@ def add(self, val):
5150

5251
def get(self, index):
5352
current_index, previous_index, current_node = self.head()
54-
for cnt in xrange(index + 1):
53+
for cnt in range(index + 1):
5554
previous_index, current_index = current_index, current_node.next_node(previous_index)
5655
current_node = self.memory[current_index]
5756
return current_node.val

problems/07/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
##Problem 7
1+
## Problem 7
22

33
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
4+
45
Examples:
56

67
>>> coding_problem_07('111') # possible interpretations: 'aaa', 'ka', 'ak'
78
3
9+
810
>>> coding_problem_07('2626') # 'zz', 'zbf', 'bfz', 'bfbf'
9-
4
11+
4

problems/08/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
##Problem 8
1+
## Problem 8
22

33
A unival tree (which stands for "universal value") is a tree where all nodes have the same value.
44
Given the root to a binary tree, count the number of unival subtrees.
55
Example:
66

77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
9-
6
8+
>>> coding_problem_08(btree)
9+
6

problems/08/problem_08.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def coding_problem_08(btree):
55
Example:
66
77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
8+
>>> coding_problem_08(btree)
99
6
1010
"""
1111
pass

problems/08/solution_08.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
def coding_problem_08(btree):
1+
def coding_problem_08(bt):
22
"""
33
A unival tree (which stands for "universal value") is a tree where all nodes have the same value.
44
Given the root to a binary tree, count the number of unival subtrees.
55
Example:
66
77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
8+
>>> coding_problem_08(btree)
99
6
1010
"""
11-
val, ln, rn = btree
12-
if ln is None and rn is None: # leaf case
13-
return 1, True, val
11+
def unival_count(btree):
1412

15-
lcount, is_uni_l, lval = coding_problem_08(ln)
16-
rcount, is_uni_r, rval = coding_problem_08(rn)
13+
val, ln, rn = btree
14+
if ln is None and rn is None: # leaf case
15+
return 1, True, val
1716

18-
is_unival = is_uni_l and is_uni_r and val == lval and val == rval
19-
count = lcount + rcount + is_unival
20-
return count, is_unival, val
17+
lcount, is_uni_l, lval = unival_count(ln)
18+
rcount, is_uni_r, rval = unival_count(rn)
19+
20+
is_unival = is_uni_l and is_uni_r and val == lval and val == rval
21+
count = lcount + rcount + is_unival
22+
return count, is_unival, val
23+
24+
return unival_count(bt)[0]
2125

2226

2327
if __name__ == '__main__':

problems/09/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
##Problem 9
1+
## Problem 9
22

33
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers.
44
The "largest sum of non-adjacent numbers" is the sum of any subset of non-contiguous elements.
5-
Solution courtesy of Kye Jiang (https://github.com/Jedshady).
5+
66
Examples:
77

88
>>> coding_problem_09([2, 4, 6, 8])
99
12
10+
1011
>>> coding_problem_09([5, 1, 1, 5])
1112
10
13+
1214
>>> coding_problem_09([1, 2, 3, 4, 5, 6])
1315
12
16+
1417
>>> coding_problem_09([-8, 4, -3, 2, 3, 4])
1518
10
19+
1620
>>> coding_problem_09([2, 4, 6, 2, 5])
17-
13
21+
13

0 commit comments

Comments
 (0)