Fixed #21: Partitioning linked list problem: Does not work if item greater than partition value is to the left.

This commit is contained in:
Donne Martin 2015-07-14 07:25:17 -04:00
parent 35e6cc13d8
commit 0667df9b03
3 changed files with 19 additions and 14 deletions

View File

@ -151,19 +151,20 @@
"\n",
" print('Test: General case')\n",
" # Partition = 10\n",
" # Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
" # Output: 4, 3, 7, 8, 1, 10, 10, 12\n",
" # Input: 4, 3, 13, 8, 10, 1, 14, 10, 12\n",
" # Output: 4, 3, 8, 1, 10, 10, 13, 14, 12\n",
" linked_list = MyLinkedList(Node(12))\n",
" linked_list.insert_to_front(10)\n",
" linked_list.insert_to_front(14)\n",
" linked_list.insert_to_front(1)\n",
" linked_list.insert_to_front(10)\n",
" linked_list.insert_to_front(8)\n",
" linked_list.insert_to_front(7)\n",
" linked_list.insert_to_front(13)\n",
" linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(4)\n",
" partitioned_list = linked_list.partition(10)\n",
" assert_equal(partitioned_list.get_all_data(),\n",
" [4, 3, 7, 8, 1, 10, 10, 12])\n",
" [4, 3, 8, 1, 10, 10, 13, 14, 12])\n",
"\n",
" print('Success: test_partition')\n",
"\n",

View File

@ -55,8 +55,8 @@
"* Right linked list is empty\n",
"* General case\n",
" * Partition = 10\n",
" * Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
" * Output: 4, 3, 7, 8, 1, 10, 10, 12"
" * Input: 4, 3, 13, 8, 10, 1, 10, 12\n",
" * Output: 4, 3, 8, 1, 10, 10, 13, 12"
]
},
{
@ -115,6 +115,8 @@
" while curr is not None:\n",
" if curr.data < data:\n",
" left.append(curr.data)\n",
" elif curr.data == data:\n",
" right.insert_to_front(curr.data)\n",
" else:\n",
" right.append(curr.data)\n",
" curr = curr.next\n",
@ -176,19 +178,20 @@
"\n",
" print('Test: General case')\n",
" # Partition = 10\n",
" # Input: 4, 3, 7, 8, 10, 1, 10, 12\n",
" # Output: 4, 3, 7, 8, 1, 10, 10, 12\n",
" # Input: 4, 3, 13, 8, 10, 1, 14, 10, 12\n",
" # Output: 4, 3, 8, 1, 10, 10, 13, 14, 12\n",
" linked_list = MyLinkedList(Node(12))\n",
" linked_list.insert_to_front(10)\n",
" linked_list.insert_to_front(14)\n",
" linked_list.insert_to_front(1)\n",
" linked_list.insert_to_front(10)\n",
" linked_list.insert_to_front(8)\n",
" linked_list.insert_to_front(7)\n",
" linked_list.insert_to_front(13)\n",
" linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(4)\n",
" partitioned_list = linked_list.partition(10)\n",
" assert_equal(partitioned_list.get_all_data(),\n",
" [4, 3, 7, 8, 1, 10, 10, 12])\n",
" [4, 3, 8, 1, 10, 10, 13, 14, 12])\n",
"\n",
" print('Success: test_partition')\n",
"\n",

View File

@ -21,19 +21,20 @@ class TestPartition(object):
print('Test: General case')
# Partition = 10
# Input: 4, 3, 7, 8, 10, 1, 10, 12
# Output: 4, 3, 7, 8, 1, 10, 10, 12
# Input: 4, 3, 13, 8, 10, 1, 14, 10, 12
# Output: 4, 3, 8, 1, 10, 10, 13, 14, 12
linked_list = MyLinkedList(Node(12))
linked_list.insert_to_front(10)
linked_list.insert_to_front(14)
linked_list.insert_to_front(1)
linked_list.insert_to_front(10)
linked_list.insert_to_front(8)
linked_list.insert_to_front(7)
linked_list.insert_to_front(13)
linked_list.insert_to_front(3)
linked_list.insert_to_front(4)
partitioned_list = linked_list.partition(10)
assert_equal(partitioned_list.get_all_data(),
[4, 3, 7, 8, 1, 10, 10, 12])
[4, 3, 8, 1, 10, 10, 13, 14, 12])
print('Success: test_partition')