added alternate solution w/ test

This commit is contained in:
wdonahoe
2015-07-09 11:29:28 -04:00
parent 8491eb374b
commit c2ec6fe616
3 changed files with 39 additions and 26 deletions

View File

@@ -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"