mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 10:58:02 +00:00
added both solutions w/ test
This commit is contained in:
@@ -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>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -18,12 +18,14 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Problem: Implement foo(val), which returns val\n",
|
"## Problem: Implement a function that groups identical items based on their order in the list.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Constraints](#Constraints)\n",
|
"* [Constraints](#Constraints)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm: Modified Selection Sort](#Algorithm: Modified Selection Sort)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code: Modified Selection Sort](#Code: Modified Selection Sort)\n",
|
||||||
|
"* [Algorithm: Ordered Dict](#Algorithm: Ordered Dict)\n",
|
||||||
|
"* [Code: Ordered Dict](#Code:-Ordered-Dict)\n",
|
||||||
"* [Unit Test](#Unit-Test)"
|
"* [Unit Test](#Unit-Test)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -33,9 +35,8 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* Can we use extra data structures?\n",
|
||||||
"* Is it foo or bar?\n",
|
" * Yes"
|
||||||
" * foo"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -44,82 +45,118 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* foo(val) -> val"
|
"* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n",
|
||||||
|
"* group_ordered(['a','b','a') -> ['a','a','b']\n",
|
||||||
|
"* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n",
|
||||||
|
"* group_ordered([]) -> []\n",
|
||||||
|
"* group_ordered([1]) -> [1]\n",
|
||||||
|
"* group_ordered(None) -> None\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm: Modified Selection Sort\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Return the input, val\n",
|
"* Save the relative position of the first-occurence of each item in a list.\n",
|
||||||
|
"* Iterate through list of unique items.\n",
|
||||||
|
" * Keep an outer index; scan rest of list, swapping matching items with outer index and incrementing outer index each time. \n",
|
||||||
" \n",
|
" \n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(1)\n",
|
"* Time: O(n^2)\n",
|
||||||
"* Space: O(1)"
|
"* Space: O(n)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Code"
|
"# Code: Modified Selection Sort"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def foo(val):\n",
|
"def make_order_list(list_in):\n",
|
||||||
" return val"
|
" 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",
|
||||||
|
"\n",
|
||||||
|
"def group_ordered(list_in):\n",
|
||||||
|
" if list_in is None:\n",
|
||||||
|
" 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"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Unit Test"
|
"## Algorithm: Ordered Dict.\n",
|
||||||
|
"\n",
|
||||||
|
"* Use an ordered dict to track insertion order of each key\n",
|
||||||
|
"* Flatten list of values.\n",
|
||||||
|
"\n",
|
||||||
|
"Complexity:\n",
|
||||||
|
"\n",
|
||||||
|
"* Time: O(n)\n",
|
||||||
|
"* Space: O(n)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Code: Ordered Dict"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": true
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Overwriting test_foo.py\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_foo.py\n",
|
"from collections import OrderedDict\n",
|
||||||
"from nose.tools import assert_equal\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
|
"def group_ordered(list_in):\n",
|
||||||
|
" result = OrderedDict()\n",
|
||||||
|
" for value in list_in:\n",
|
||||||
|
" result.setdefault(value, []).append(value)\n",
|
||||||
|
" return [v for group in result.values() for v in group]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestFoo(object):\n",
|
"#### The following unit test is expected to fail until you solve the challenge."
|
||||||
"\n",
|
|
||||||
" def test_foo(self):\n",
|
|
||||||
" assert_equal(foo(None), None)\n",
|
|
||||||
" assert_equal(foo(0), 0)\n",
|
|
||||||
" assert_equal(foo('bar'), 'bar')\n",
|
|
||||||
" print('Success: test_foo')\n",
|
|
||||||
"\n",
|
|
||||||
"def main():\n",
|
|
||||||
" test = TestFoo()\n",
|
|
||||||
" test.test_foo()\n",
|
|
||||||
"\n",
|
|
||||||
"if __name__ == '__main__':\n",
|
|
||||||
" main()"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -133,16 +170,47 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Success: test_foo\n"
|
"Overwriting test_group_ordered.py\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%run -i test_foo.py"
|
"%%writefile test_group_ordered.py\n",
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\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",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = TestGroupOrdered()\n",
|
||||||
|
" test.test_group_ordered()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" main()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run -i test_group_ordered.py"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
"celltoolbar": "Edit Metadata",
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 2",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
@@ -158,7 +226,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython2",
|
||||||
"version": "2.7.10"
|
"version": "2.7.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user