Skip to content

Commit 5b48066

Browse files
committed
change to pivot, attempt at avoiding anti-pattern
1 parent 81287c3 commit 5b48066

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

Python/chapter02/2.4 - Partition/miguel_2.4_soln.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
not need to appear between the left and right
1111
partitions.
1212
EXAMPLE
13-
Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition = 5]
13+
Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [pivot = 5]
1414
Result: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
1515
"""
1616
import unittest
@@ -94,7 +94,7 @@ def __eq__(self, other: object):
9494
return a is None and b is None
9595

9696

97-
def check_partitioned(ll: LinkedList, partition: int) -> bool:
97+
def check_partitioned(ll: LinkedList, pivot: int) -> bool:
9898
"""
9999
Will check if a linked list is partitioned
100100
around a value such that all values less
@@ -106,31 +106,30 @@ def check_partitioned(ll: LinkedList, partition: int) -> bool:
106106
Runtime: O(n)
107107
Space Complexity: O(1)
108108
:param ll: an input linked list
109-
:param partition: a number to partition around
109+
:param pivot: a number to partition around
110110
:return:
111111
"""
112-
if ll.size == 1:
113-
return True
114112
# counter for keeping track of how many times
115-
# we change from < partition to >= partition
116-
partition_change_count = 0
117-
changed = False
113+
# we change from < pivot to >= pivot
114+
pivot_change_count = 0
118115
n = ll.head
119116
while n is not None:
120-
if n.data < partition and changed:
121-
changed = False
122-
partition_change_count += 1
123-
elif n.data >= partition and not changed:
124-
changed = True
125-
partition_change_count += 1
117+
if n.data >= pivot:
118+
pivot_change_count += 1
119+
break
126120
n = n.next
127-
return partition_change_count <= 1
121+
while n is not None:
122+
if n.data < pivot:
123+
pivot_change_count += 1
124+
break
125+
n = n.next
126+
return pivot_change_count <= 1
128127

129128

130-
def partition_ll(ll: LinkedList, partition: int) -> LinkedList:
129+
def partition_ll(ll: LinkedList, pivot: int) -> LinkedList:
131130
"""
132131
This function will take in a linked list, a
133-
partition value, and will partition a linked
132+
pivot value, and will partition a linked
134133
list around a value x such that all nodes
135134
less than x come before all nodes greater than
136135
or equal to x. If x is in the list, then
@@ -140,15 +139,15 @@ def partition_ll(ll: LinkedList, partition: int) -> LinkedList:
140139
Runtime: O(n)
141140
Space Complexity: O(n)
142141
:param ll: an input linked list
143-
:param partition: a number to partition around
142+
:param pivot: a number to partition around
144143
:return: a linked list that is 'partitioned'
145144
"""
146-
left_partition = [] # will contain values < partition
147-
right_partition = [] # will contain values >= partition
145+
left_partition = [] # will contain values < pivot
146+
right_partition = [] # will contain values >= pivot
148147

149148
n = ll.head
150149
while n is not None:
151-
if n.data < partition:
150+
if n.data < pivot:
152151
left_partition.append(n.data)
153152
else:
154153
right_partition.append(n.data)
@@ -228,13 +227,13 @@ def setUp(self):
228227
]
229228

230229
def test_check_partitioned(self):
231-
for ll, partition, output in self.check_partitioned_test_cases:
232-
self.assertEqual(check_partitioned(ll, partition), output, msg=(ll, partition))
230+
for ll, pivot, output in self.check_partitioned_test_cases:
231+
self.assertEqual(check_partitioned(ll, pivot), output, msg=(ll, pivot))
233232

234233
def test_partition(self):
235-
for ll, partition in self.test_cases:
236-
partitioned_ll = partition_ll(ll, partition)
237-
self.assertTrue(check_partitioned(partitioned_ll, partition), msg=(ll, partition))
234+
for ll, pivot in self.test_cases:
235+
partitioned_ll = partition_ll(ll, pivot)
236+
self.assertTrue(check_partitioned(partitioned_ll, pivot), msg=(ll, pivot))
238237

239238

240239
if __name__ == '__main__':

0 commit comments

Comments
 (0)