mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
finshed challenge notebook
This commit is contained in:
parent
8c23acd55a
commit
84ca16fbb5
|
@ -70,15 +70,37 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"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",
|
"def group_ordered(list_in):\n",
|
||||||
" # TODO: Implement me\n",
|
" if list_in is None:\n",
|
||||||
" pass"
|
" 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",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Success: test_foo\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"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"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user