Skip to content

Commit 0667df9

Browse files
committed
Fixed donnemartin#21: Partitioning linked list problem: Does not work if item greater than partition value is to the left.
1 parent 35e6cc1 commit 0667df9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

linked_lists/partition/partition_challenge.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,20 @@
151151
"\n",
152152
" print('Test: General case')\n",
153153
" # Partition = 10\n",
154-
" # Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
155-
" # Output: 4, 3, 7, 8, 1, 10, 10, 12\n",
154+
" # Input: 4, 3, 13, 8, 10, 1, 14, 10, 12\n",
155+
" # Output: 4, 3, 8, 1, 10, 10, 13, 14, 12\n",
156156
" linked_list = MyLinkedList(Node(12))\n",
157157
" linked_list.insert_to_front(10)\n",
158+
" linked_list.insert_to_front(14)\n",
158159
" linked_list.insert_to_front(1)\n",
159160
" linked_list.insert_to_front(10)\n",
160161
" linked_list.insert_to_front(8)\n",
161-
" linked_list.insert_to_front(7)\n",
162+
" linked_list.insert_to_front(13)\n",
162163
" linked_list.insert_to_front(3)\n",
163164
" linked_list.insert_to_front(4)\n",
164165
" partitioned_list = linked_list.partition(10)\n",
165166
" assert_equal(partitioned_list.get_all_data(),\n",
166-
" [4, 3, 7, 8, 1, 10, 10, 12])\n",
167+
" [4, 3, 8, 1, 10, 10, 13, 14, 12])\n",
167168
"\n",
168169
" print('Success: test_partition')\n",
169170
"\n",

linked_lists/partition/partition_solution.ipynb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"* Right linked list is empty\n",
5656
"* General case\n",
5757
" * Partition = 10\n",
58-
" * Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
59-
" * Output: 4, 3, 7, 8, 1, 10, 10, 12"
58+
" * Input: 4, 3, 13, 8, 10, 1, 10, 12\n",
59+
" * Output: 4, 3, 8, 1, 10, 10, 13, 12"
6060
]
6161
},
6262
{
@@ -115,6 +115,8 @@
115115
" while curr is not None:\n",
116116
" if curr.data < data:\n",
117117
" left.append(curr.data)\n",
118+
" elif curr.data == data:\n",
119+
" right.insert_to_front(curr.data)\n",
118120
" else:\n",
119121
" right.append(curr.data)\n",
120122
" curr = curr.next\n",
@@ -176,19 +178,20 @@
176178
"\n",
177179
" print('Test: General case')\n",
178180
" # Partition = 10\n",
179-
" # Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
180-
" # Output: 4, 3, 7, 8, 1, 10, 10, 12\n",
181+
" # Input: 4, 3, 13, 8, 10, 1, 14, 10, 12\n",
182+
" # Output: 4, 3, 8, 1, 10, 10, 13, 14, 12\n",
181183
" linked_list = MyLinkedList(Node(12))\n",
182184
" linked_list.insert_to_front(10)\n",
185+
" linked_list.insert_to_front(14)\n",
183186
" linked_list.insert_to_front(1)\n",
184187
" linked_list.insert_to_front(10)\n",
185188
" linked_list.insert_to_front(8)\n",
186-
" linked_list.insert_to_front(7)\n",
189+
" linked_list.insert_to_front(13)\n",
187190
" linked_list.insert_to_front(3)\n",
188191
" linked_list.insert_to_front(4)\n",
189192
" partitioned_list = linked_list.partition(10)\n",
190193
" assert_equal(partitioned_list.get_all_data(),\n",
191-
" [4, 3, 7, 8, 1, 10, 10, 12])\n",
194+
" [4, 3, 8, 1, 10, 10, 13, 14, 12])\n",
192195
"\n",
193196
" print('Success: test_partition')\n",
194197
"\n",

linked_lists/partition/test_partition.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ def test_partition(self):
2121

2222
print('Test: General case')
2323
# Partition = 10
24-
# Input: 4, 3, 7, 8, 10, 1, 10, 12
25-
# Output: 4, 3, 7, 8, 1, 10, 10, 12
24+
# Input: 4, 3, 13, 8, 10, 1, 14, 10, 12
25+
# Output: 4, 3, 8, 1, 10, 10, 13, 14, 12
2626
linked_list = MyLinkedList(Node(12))
2727
linked_list.insert_to_front(10)
28+
linked_list.insert_to_front(14)
2829
linked_list.insert_to_front(1)
2930
linked_list.insert_to_front(10)
3031
linked_list.insert_to_front(8)
31-
linked_list.insert_to_front(7)
32+
linked_list.insert_to_front(13)
3233
linked_list.insert_to_front(3)
3334
linked_list.insert_to_front(4)
3435
partitioned_list = linked_list.partition(10)
3536
assert_equal(partitioned_list.get_all_data(),
36-
[4, 3, 7, 8, 1, 10, 10, 12])
37+
[4, 3, 8, 1, 10, 10, 13, 14, 12])
3738

3839
print('Success: test_partition')
3940

0 commit comments

Comments
 (0)