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

@ -4,7 +4,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"<small><i>This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>" "<small><i>This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
] ]
}, },
{ {

View File

@ -135,7 +135,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -143,7 +143,7 @@
"source": [ "source": [
"from collections import OrderedDict\n", "from collections import OrderedDict\n",
"\n", "\n",
"def group_ordered(list_in):\n", "def group_ordered_alt(list_in):\n",
" result = OrderedDict()\n", " result = OrderedDict()\n",
" for value in list_in:\n", " for value in list_in:\n",
" result.setdefault(value, []).append(value)\n", " result.setdefault(value, []).append(value)\n",
@ -161,7 +161,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -180,18 +180,25 @@
"\n", "\n",
"class TestGroupOrdered(object):\n", "class TestGroupOrdered(object):\n",
"\n", "\n",
" def test_group_ordered(self):\n", " def test_group_ordered(self,func):\n",
" assert_equal(group_ordered(None), None)\n", "\n",
" assert_equal(group_ordered([]), [])\n", " assert_equal(func(None), None)\n",
" assert_equal(group_ordered([1]), [1])\n", " assert_equal(func([]), [])\n",
" assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", " assert_equal(func([1]), [1])\n",
" assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n",
" assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", " assert_equal(func(['a','b','a']),['a','a','b'])\n",
" assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\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", "\n",
"def main():\n", "def main():\n",
" test = TestGroupOrdered()\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", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()\n" " main()\n"

View File

@ -1,20 +1,26 @@
from nose.tools import assert_equal from nose.tools import assert_equal
class TestFoo(object): class TestGroupOrdered(object):
def test_foo(self): def test_group_ordered(self,func):
assert_equal(group_ordered(None), None)
assert_equal(group_ordered([]), []) assert_equal(func(None), None)
assert_equal(group_ordered([1]), [1]) assert_equal(func([]), [])
assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3]) assert_equal(func([1]), [1])
assert_equal(group_ordered(['a','b','a']),['a','a','b']) assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])
assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) assert_equal(func(['a','b','a']),['a','a','b'])
assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4]) assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])
print('Success: test_foo') assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])
def main(): def main():
test = TestFoo() test = TestGroupOrdered()
test.test_foo() 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__': if __name__ == '__main__':
main() main()