Skip to content

Commit 47bf39f

Browse files
author
Jessica Yung
committed
feat(ctci2): add p4-6a solutions and tests
1 parent 2b8efa1 commit 47bf39f

File tree

3 files changed

+222
-22
lines changed

3 files changed

+222
-22
lines changed

laakmann-mcdowell/2-linked-lists/4_partition.py

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,122 @@
99
appear between the left and right partitions.
1010
1111
EXAMPLE:
12-
3->5->8-5->10->2->1 [partition = 5]
12+
3->5->8->5->10->2->1 [partition = 5]
13+
has possible solution
14+
3->2->1->5->8->5->10
1315
16+
Author: Jessica Yung
17+
16 October 2016
1418
19+
WRITE have-two-lists-(before and after)-that-you-merge version of solution.
1520
"""
1621
from linked_list_classes import LinkedListNode
1722
import unittest
1823

24+
def partition_linked_list(head, partition):
25+
# partition is a float or an int.
26+
complete = False
27+
# For each pass:
28+
while complete == False:
29+
# Initialise or reset variables
30+
current_node = head
31+
swaps = 0
32+
prev_prev_node = None
33+
prev_node = None
34+
passed_greater_than_partition = 0
35+
# Loop through nodes
36+
while current_node is not None:
37+
# if number >= partition, toggle = 1
38+
if passed_greater_than_partition == 1 and current_node.value < partition:
39+
# Swap current node with prev node
40+
swaps = 1
41+
if prev_prev_node is not None:
42+
# a b c d, c current
43+
# a.next = c
44+
# b.next = d
45+
# c.next = b
46+
# a c b d
47+
# SWAP
48+
prev_prev_node.next = current_node
49+
prev_node.next = current_node.next
50+
current_node.next = prev_node
51+
if prev_node is not None and current_node is not None:
52+
print("swapped ", prev_node.value, " with ", current_node.value)
53+
# Shift
54+
# c current, next current is d.
55+
else:
56+
# a b c, b current
57+
# a.next = c
58+
# b.next = a
59+
# b a c
60+
# SWAP
61+
prev_node.next = current_node.next
62+
current_node.next = prev_node
63+
if prev_node is not None and current_node is not None:
64+
print("swapped ", prev_node.value, " with ", current_node.value)
65+
# SHIFT nodes
66+
# current is still b
67+
# Next current is c
68+
head = current_node
69+
# Shift nodes
70+
if current_node.next.next is not None:
71+
prev_prev_node = current_node
72+
current_node = current_node.next.next
73+
else:
74+
# a b d c
75+
current_node = current_node.next
76+
else:
77+
if current_node.value >= partition:
78+
passed_greater_than_partition = 1
79+
# No swap
80+
# Shift nodes
81+
# a b c d, current is c.
82+
# Next current is d
83+
prev_prev_node = prev_node
84+
prev_node = current_node
85+
current_node = current_node.next
86+
# If there were no alterations, we're done
87+
if swaps == 0:
88+
complete = True
89+
if head.next.next.next is not None:
90+
print(head.value, head.next.value, head.next.next.value, head.next.next.next.value)
91+
return head
1992

93+
def partition_linked_list_split_solution(head, partition):
94+
pass
2095

2196
class Tests(unittest.TestCase):
97+
def test_partition_short(self):
98+
a = LinkedListNode(9)
99+
b = LinkedListNode(7)
100+
c = LinkedListNode(2)
101+
d = LinkedListNode(1)
102+
a.next = b
103+
b.next = c
104+
c.next = d
105+
h = partition_linked_list(a, 5)
106+
print(h, h.next.value, h.next.next.value, h.next.next.next.value)
22107

23-
def set_up_linked_list(self):
24-
a = LinkedListNode(1)
25-
b = LinkedListNode(2)
26-
c = LinkedListNode(3)
27-
d = LinkedListNode(4)
28-
e = LinkedListNode(5)
29-
f = LinkedListNode(6)
30-
g = LinkedListNode(7)
108+
109+
def test_partition(self):
110+
a = LinkedListNode(9)
111+
b = LinkedListNode(7)
112+
c = LinkedListNode(6)
113+
d = LinkedListNode(5)
114+
e = LinkedListNode(4)
115+
f = LinkedListNode(3)
116+
g = LinkedListNode(2)
31117
a.next = b
32118
b.next = c
33119
c.next = d
34120
d.next = e
35121
e.next = f
36122
f.next = g
37-
return a
38-
123+
h = partition_linked_list(a, 5)
124+
fourh = h.next.next.next
125+
print(h.value, h.next.value, h.next.next.value, fourh.value, fourh.next.value,
126+
fourh.next.next.value, fourh.next.next.next.value)
39127

40-
def test_solution_one(self):
41-
a = self.set_up_linked_list()
42-
self.assertEqual(solution_one(a, 3).value, 4)
43-
self.assertEqual(solution_one(a, 0).value, 7)
44128

45129
if __name__ == '__main__':
46-
unittest.main()
130+
unittest.main()
Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,66 @@
1+
"""
2+
2.5 Sum Lists
13
2-
def ll_length(node):
3-
pass
4+
Author: Jessica Yung
5+
16 October 2016
6+
"""
7+
from linked_list_classes import LinkedListNode
8+
import unittest
49

5-
def sum_lists(ll_one_node, ll_two_node):
6-
if ll_length(ll_one_node) != ll_length(ll_two_node):
7-
pass
8-
#
10+
11+
def sum_lists(list_one_head, list_two_head):
12+
pointer_one = list_one_head
13+
pointer_two = list_two_head
14+
sum_list_start = 0
15+
carry = 0
16+
sum_head = None
17+
pointer_sum = None
18+
while pointer_one is not None or pointer_two is not None:
19+
if pointer_one is not None and pointer_two is not None:
20+
digit_sum = pointer_one.value + pointer_two.value + carry
21+
elif pointer_one is not None:
22+
digit_sum = pointer_one.value + carry
23+
else:
24+
digit_sum = pointer_two.value + carry
25+
print("Digit Sum: ", digit_sum)
26+
carry, new_digit = divmod(digit_sum, 10)
27+
print("Carry: ", carry, "New digit: ", new_digit)
28+
# Add resulting digit to 'sum' linked list and move pointer to that digit
29+
if sum_list_start == 0:
30+
sum_head = LinkedListNode(new_digit)
31+
pointer_sum = sum_head
32+
sum_list_start = 1
33+
else:
34+
pointer_sum.next = LinkedListNode(new_digit)
35+
pointer_sum = pointer_sum.next
36+
# Move to the next digit in the lists
37+
if pointer_one is not None:
38+
pointer_one = pointer_one.next
39+
if pointer_two is not None:
40+
pointer_two = pointer_two.next
41+
return sum_head
42+
43+
44+
class Tests(unittest.TestCase):
45+
46+
def test_partition(self):
47+
a = LinkedListNode(9)
48+
b = LinkedListNode(7)
49+
c = LinkedListNode(6)
50+
d = LinkedListNode(5)
51+
e = LinkedListNode(4)
52+
f = LinkedListNode(3)
53+
g = LinkedListNode(2)
54+
# List 1: 679
55+
a.next = b
56+
b.next = c
57+
# List 2: 2345
58+
d.next = e
59+
e.next = f
60+
f.next = g
61+
# Sum:
62+
h = sum_lists(a, d)
63+
print(h.value, h.next.value, h.next.next.value, h.next.next.next.value)
64+
65+
if __name__ == '__main__':
66+
unittest.main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
6 Palindrome
3+
4+
Author: Jessica Yung
5+
16 October 2016
6+
"""
7+
from linked_list_classes import LinkedListNode
8+
import unittest
9+
10+
# If it's a doubly-linked list, it's easy. We just send a pointer to the end
11+
# then have it trace back and compare the values one by one until ceiling(len/2).
12+
13+
def is_dll_palindrome(head):
14+
"""`head` is a DoublyLinkedListNode."""
15+
current = head
16+
runner = head
17+
list_length = 1
18+
number_checked = 0
19+
# Send runner to the back
20+
while runner.next is not None:
21+
runner = runner.next
22+
list_length += 1
23+
while number_checked < int(list_length / 2 + 1)
24+
if current.value != runner.value:
25+
return False
26+
else:
27+
current = current.next
28+
runner = runner.prev
29+
number_checked += 1
30+
# All checked
31+
return True
32+
33+
# If it's a singly-linked list, then we go through it once and get the length.
34+
# We may as well get all the values and put it in an array and compare?
35+
36+
class Tests(unittest.TestCase):
37+
38+
def test_sll_palindrome(self):
39+
a = LinkedListNode(9)
40+
b = LinkedListNode(7)
41+
c = LinkedListNode(6)
42+
d = LinkedListNode(5)
43+
e = LinkedListNode(6)
44+
f = LinkedListNode(7)
45+
g = LinkedListNode(9)
46+
# List 1: 679
47+
a.next = b
48+
b.next = c
49+
c.next = d
50+
d.next = e
51+
e.next = f
52+
f.next = g
53+
# Sum:
54+
h = sum_lists(a, d)
55+
print(h.value, h.next.value, h.next.next.value, h.next.next.next.value)
56+
57+
if __name__ == '__main__':
58+
unittest.main()

0 commit comments

Comments
 (0)