From 0667df9b030cbfdfd6edc0c5442fc93c8b3cd33d Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 14 Jul 2015 07:25:17 -0400 Subject: [PATCH] Fixed #21: Partitioning linked list problem: Does not work if item greater than partition value is to the left. --- linked_lists/partition/partition_challenge.ipynb | 9 +++++---- linked_lists/partition/partition_solution.ipynb | 15 +++++++++------ linked_lists/partition/test_partition.py | 9 +++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/linked_lists/partition/partition_challenge.ipynb b/linked_lists/partition/partition_challenge.ipynb index a8c2d11..eb9cd72 100644 --- a/linked_lists/partition/partition_challenge.ipynb +++ b/linked_lists/partition/partition_challenge.ipynb @@ -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", diff --git a/linked_lists/partition/partition_solution.ipynb b/linked_lists/partition/partition_solution.ipynb index f6e3904..c7b1555 100644 --- a/linked_lists/partition/partition_solution.ipynb +++ b/linked_lists/partition/partition_solution.ipynb @@ -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", diff --git a/linked_lists/partition/test_partition.py b/linked_lists/partition/test_partition.py index 99e82d5..c9c80ea 100644 --- a/linked_lists/partition/test_partition.py +++ b/linked_lists/partition/test_partition.py @@ -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')