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",
"metadata": {},
"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",
"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"

View File

@ -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()