From 84ca16fbb5ad744db4f3280a43a3a19de5efff0b Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 09:04:37 -0400 Subject: [PATCH] finshed challenge notebook --- .../group_items/group_ordered_challenge.ipynb | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index e061433..a0a5b06 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -70,15 +70,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ + "def make_order_list(list_in):\n", + " order_list = []\n", + " for item in list_in:\n", + " if item not in order_list:\n", + " order_list.append(item)\n", + " return order_list\n", + "\n", "def group_ordered(list_in):\n", - " # TODO: Implement me\n", - " pass" + " if list_in is None:\n", + " return None\n", + " order_list = make_order_list(list_in)\n", + " current = 0\n", + " for item in order_list:\n", + " search = current + 1\n", + " while True:\n", + " try:\n", + " if list_in[search] != item:\n", + " search += 1\n", + " else:\n", + " current += 1\n", + " list_in[current], list_in[search] = list_in[search], list_in[current]\n", + " search += 1\n", + " except IndexError:\n", + " break\n", + " return list_in" ] }, { @@ -97,13 +119,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_foo\n" + ] + } + ], "source": [ - "%load test_group_ordered.py" + "# %load test_group_ordered.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestFoo(object):\n", + "\n", + " def test_foo(self):\n", + " assert_equal(group_ordered(None), None)\n", + " assert_equal(group_ordered([]), [])\n", + " assert_equal(group_ordered([1]), [1])\n", + " assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", + " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: test_foo')\n", + "\n", + "def main():\n", + " test = TestFoo()\n", + " test.test_foo()\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n" ] }, {