interactive-coding-challenges/arrays_strings/group_items/group_ordered_solution.ipynb

242 lines
6.1 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-07-09 23:03:36 +08:00
"<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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-07-09 23:03:36 +08:00
"## Problem: Implement a function that groups identical items based on their order in the list.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
2015-07-09 23:03:36 +08:00
"* [Algorithm: Modified Selection Sort](#Algorithm: Modified Selection Sort)\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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
2015-07-09 23:03:36 +08:00
"* Can we use extra data structures?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
2015-07-09 23:03:36 +08:00
"* 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",
"metadata": {},
"source": [
2015-07-09 23:03:36 +08:00
"## Algorithm: Modified Selection Sort\n",
"\n",
2015-07-09 23:03:36 +08:00
"* 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",
"Complexity:\n",
2015-07-09 23:03:36 +08:00
"* Time: O(n^2)\n",
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-07-09 23:03:36 +08:00
"# Code: Modified Selection Sort"
]
},
{
"cell_type": "code",
2015-07-09 23:03:36 +08:00
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2015-07-09 23:03:36 +08:00
"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",
"\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",
"metadata": {},
"source": [
"## 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",
2015-07-09 23:29:28 +08:00
"execution_count": 2,
2015-07-09 23:03:36 +08:00
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"\n",
2015-07-09 23:29:28 +08:00
"def group_ordered_alt(list_in):\n",
2015-07-09 23:03:36 +08:00
" 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": [
2015-07-09 23:03:36 +08:00
"## Unit Test\n",
"\n",
"#### The following unit test is expected to fail until you solve the challenge."
]
},
{
"cell_type": "code",
2015-07-09 23:29:28 +08:00
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2015-07-09 23:03:36 +08:00
"Overwriting test_group_ordered.py\n"
]
}
],
"source": [
2015-07-09 23:03:36 +08:00
"%%writefile test_group_ordered.py\n",
"from nose.tools import assert_equal\n",
"\n",
2015-07-09 23:03:36 +08:00
"class TestGroupOrdered(object):\n",
"\n",
2015-07-09 23:29:28 +08:00
" 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",
2015-07-09 23:03:36 +08:00
" test = TestGroupOrdered()\n",
2015-07-09 23:29:28 +08:00
" 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",
2015-07-09 23:03:36 +08:00
" main()\n"
]
},
{
"cell_type": "code",
2015-07-09 23:03:36 +08:00
"execution_count": 4,
"metadata": {
"collapsed": false
},
2015-07-09 23:03:36 +08:00
"outputs": [],
"source": [
2015-07-09 23:03:36 +08:00
"%run -i test_group_ordered.py"
]
}
],
"metadata": {
2015-07-09 23:03:36 +08:00
"celltoolbar": "Edit Metadata",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
2015-07-09 23:03:36 +08:00
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}