fixed problems, new pull

This commit is contained in:
wdonahoe
2015-07-11 10:43:28 -04:00
parent f703014dec
commit b4476d3f77
10 changed files with 193 additions and 65 deletions

View File

@@ -0,0 +1,34 @@
def make_order_list(list_in):
order_list = []
for item in list_in:
if item not in order_list:
order_list.append(item)
return order_list
def group_ordered(list_in):
if list_in is None:
return None
order_list = make_order_list(list_in)
current = 0
for item in order_list:
search = current + 1
while True:
try:
if list_in[search] != item:
search += 1
else:
current += 1
list_in[current], list_in[search] = list_in[search], list_in[current]
search += 1
except IndexError:
break
return list_in
def group_ordered2(list_in):
from collections import OrderedDict
result = OrderedDict()
for value in list_in:
result.setdefault(value, []).append(value)
return result

View File

@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<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": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we use extra data structures?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* 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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def group_ordered(list_in):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_group_ordered.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestGroupOrdered(object):\n",
" def test_group_ordered(self, func):\n",
"\n",
" assert_equal(func(None), None)\n",
" print('Success: ' + func.__name__ + \" None case.\")\n",
" assert_equal(func([]), [])\n",
" print('Success: ' + func.__name__ + \" Empty case.\")\n",
" assert_equal(func([1]), [1])\n",
" print('Success: ' + func.__name__ + \" Single element case.\")\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",
" print('Success: ' + func.__name__)\n",
"\n",
"\n",
"def main():\n",
" test = TestGroupOrdered()\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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/sorting_searching/group_ordered/group_ordered_solution.ipynb) for a discussion on algorithms and code solutions."
]
}
],
"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",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,262 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<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": [
"## 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",
"* [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",
"* Can we use extra data structures?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* 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": [
"## Algorithm: Modified Selection Sort\n",
"\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",
"Complexity:\n",
"* Time: O(n^2)\n",
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Code: Modified Selection Sort"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"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",
"\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",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"\n",
"def group_ordered_alt(list_in):\n",
" if list_in is None:\n",
" return None\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",
"#### The following unit test is expected to fail until you solve the challenge."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_group_ordered.py\n"
]
}
],
"source": [
"%%writefile test_group_ordered.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestGroupOrdered(object):\n",
" def test_group_ordered(self, func):\n",
"\n",
" assert_equal(func(None), None)\n",
" print('Success: ' + func.__name__ + \" None case.\")\n",
" assert_equal(func([]), [])\n",
" print('Success: ' + func.__name__ + \" Empty case.\")\n",
" assert_equal(func([1]), [1])\n",
" print('Success: ' + func.__name__ + \" Single element case.\")\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",
" print('Success: ' + func.__name__)\n",
"\n",
"\n",
"def main():\n",
" test = TestGroupOrdered()\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()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: group_ordered None case.\n",
"Success: group_ordered Empty case.\n",
"Success: group_ordered Single element case.\n",
"Success: group_ordered\n",
"Success: group_ordered_alt None case.\n",
"Success: group_ordered_alt Empty case.\n",
"Success: group_ordered_alt Single element case.\n",
"Success: group_ordered_alt\n"
]
}
],
"source": [
"%run -i test_group_ordered.py"
]
}
],
"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",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@@ -0,0 +1,31 @@
from nose.tools import assert_equal
class TestGroupOrdered(object):
def test_group_ordered(self, func):
assert_equal(func(None), None)
print('Success: ' + func.__name__ + " None case.")
assert_equal(func([]), [])
print('Success: ' + func.__name__ + " Empty case.")
assert_equal(func([1]), [1])
print('Success: ' + func.__name__ + " Single element case.")
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])
print('Success: ' + func.__name__)
def main():
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()

View File

@@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* Is a naiive solution sufficient?\n",
" * Yes"
]
@@ -152,7 +151,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {
"collapsed": false
},
@@ -161,16 +160,34 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Empty input\n",
"One element\n",
"Two or more elements\n",
"Success: test_insertion_sort\n"
"Empty input\n"
]
},
{
"ename": "NameError",
"evalue": "global name 'insertion_sort' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 24\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mtest\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTestInsertionSort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 26\u001b[1;33m \u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest_insertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36mtest_insertion_sort\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Empty input'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0minsertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: global name 'insertion_sort' is not defined"
]
}
],
"source": [
"%run -i test_insertion_sort.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
@@ -189,7 +206,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"version": "2.7.6"
}
},
"nbformat": 4,

View File

@@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Is a naiive solution sufficient?\n",
" * Yes"
]
@@ -68,15 +67,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def insert(l,item_pos):\n",
" item = l[item_pos]\n",
" while item_pos > 0:\n",
" if l[item_pos - 1] > item:\n",
" l[item_pos - 1], l[item_pos] = l[item_pos], l[item_pos - 1]\n",
" item_pos -= 1\n",
" else:\n",
" break\n",
"\n",
"def insertion_sort(data):\n",
" # TODO: Implement me\n",
" pass"
" for i,item in enumerate(data):\n",
" insert(data,i)\n",
" return data"
]
},
{
@@ -92,11 +101,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Empty input\n",
"One element\n",
"Two or more elements\n",
"Success: test_insertion_sort\n"
]
}
],
"source": [
"# %load test_insertion_sort.py\n",
"from nose.tools import assert_equal\n",
@@ -153,7 +173,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"version": "2.7.6"
}
},
"nbformat": 4,