9
9
appear between the left and right partitions.
10
10
11
11
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
13
15
16
+ Author: Jessica Yung
17
+ 16 October 2016
14
18
19
+ WRITE have-two-lists-(before and after)-that-you-merge version of solution.
15
20
"""
16
21
from linked_list_classes import LinkedListNode
17
22
import unittest
18
23
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
19
92
93
+ def partition_linked_list_split_solution (head , partition ):
94
+ pass
20
95
21
96
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 )
22
107
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 )
31
117
a .next = b
32
118
b .next = c
33
119
c .next = d
34
120
d .next = e
35
121
e .next = f
36
122
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 )
39
127
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 )
44
128
45
129
if __name__ == '__main__' :
46
- unittest .main ()
130
+ unittest .main ()
0 commit comments