From c2ec6fe616f5571a4af25e75ebd92e10a8565e9e Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 11:29:28 -0400 Subject: [PATCH] added alternate solution w/ test --- .../group_items/group_ordered_challenge.ipynb | 2 +- .../group_items/group_ordered_solution.ipynb | 31 +++++++++++------- .../group_items/test_group_ordered.py | 32 +++++++++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index a01eee6..6bc87a6 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { diff --git a/arrays_strings/group_items/group_ordered_solution.ipynb b/arrays_strings/group_items/group_ordered_solution.ipynb index 9441421..e312b8c 100644 --- a/arrays_strings/group_items/group_ordered_solution.ipynb +++ b/arrays_strings/group_items/group_ordered_solution.ipynb @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -143,7 +143,7 @@ "source": [ "from collections import OrderedDict\n", "\n", - "def group_ordered(list_in):\n", + "def group_ordered_alt(list_in):\n", " result = OrderedDict()\n", " for value in list_in:\n", " result.setdefault(value, []).append(value)\n", @@ -161,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -180,18 +180,25 @@ "\n", "class TestGroupOrdered(object):\n", "\n", - " def test_group_ordered(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", + " def test_group_ordered(self,func):\n", + "\n", + " assert_equal(func(None), None)\n", + " assert_equal(func([]), [])\n", + " assert_equal(func([1]), [1])\n", + " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(func(['a','b','a']),['a','a','b'])\n", + " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", "\n", "def main():\n", " test = TestGroupOrdered()\n", - " test.test_group_ordered()\n", + " test.test_group_ordered(group_ordered)\n", + " try:\n", + " test.test_group_ordered(group_ordered_alt)\n", + " except NameError:\n", + " # Alternate solutions are only defined\n", + " # in the solutions file\n", + " pass\n", "\n", "if __name__ == '__main__':\n", " main()\n" diff --git a/arrays_strings/group_items/test_group_ordered.py b/arrays_strings/group_items/test_group_ordered.py index 943ac67..a1176ff 100644 --- a/arrays_strings/group_items/test_group_ordered.py +++ b/arrays_strings/group_items/test_group_ordered.py @@ -1,20 +1,26 @@ from nose.tools import assert_equal -class TestFoo(object): +class TestGroupOrdered(object): - def test_foo(self): - assert_equal(group_ordered(None), None) - assert_equal(group_ordered([]), []) - assert_equal(group_ordered([1]), [1]) - assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3]) - assert_equal(group_ordered(['a','b','a']),['a','a','b']) - assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) - assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4]) - print('Success: test_foo') + def test_group_ordered(self,func): + + assert_equal(func(None), None) + assert_equal(func([]), []) + assert_equal(func([1]), [1]) + assert_equal(func([1,2,1,3,2]),[1,1,2,2,3]) + assert_equal(func(['a','b','a']),['a','a','b']) + assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) + assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4]) def main(): - test = TestFoo() - test.test_foo() + test = TestGroupOrdered() + test.test_group_ordered(group_ordered) + try: + test.test_group_ordered(group_ordered_alt) + except NameError: + # Alternate solutions are only defined + # in the solutions file + pass if __name__ == '__main__': - main() + main() \ No newline at end of file