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

@ -35,7 +35,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we assume the string is ASCII?\n", "* Can we assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
@ -164,7 +163,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -242,7 +241,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,

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can you assume the string is ASCII?\n", "* Can you assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
@ -75,20 +74,38 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def is_substring(s1, s2):\n", "def is_substring(substr, s):\n",
" # TODO: Implement me\n", " if len(s) < len(substr):\n",
" pass\n", " return False\n",
" elif len(substr) == len(s) and len(s) == 0:\n",
" return True\n",
" pos = 0\n",
" while pos < len(s):\n",
" so_far = 0\n",
" while so_far < len(substr):\n",
" if substr[so_far] != s[pos]:\n",
" pos = pos + 1\n",
" break\n",
" else:\n",
" pos = pos + 1\n",
" so_far = so_far + 1\n",
" if so_far == len(substr):\n",
" return True\n",
" return False\n",
"\n", "\n",
"def is_rotation(s1, s2):\n", "def is_rotation(s1, s2):\n",
" # TODO: Implement me\n", " if s1 is None or s2 is None:\n",
" # Call is_substring only once\n", " return False\n",
" pass" " elif len(s1) != len(s2):\n",
" return False\n",
" else:\n",
" return is_substring(s2,s1+s1)"
] ]
}, },
{ {
@ -109,11 +126,19 @@
}, },
{ {
"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_rotation\n"
]
}
],
"source": [ "source": [
"# %load test_rotation.py\n", "# %load test_rotation.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
@ -163,7 +188,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,

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can you assume the string is ASCII?\n", "* Can you assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
@ -73,15 +72,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "def unique_chars(string):\n",
" # TODO: Implement me\n", " from collections import defaultdict\n",
" pass" " \n",
" char_table = defaultdict(int)\n",
" for c in string:\n",
" if char_table[c] > 0:\n",
" return False\n",
" else:\n",
" char_table[c] = 1\n",
" return True"
] ]
}, },
{ {
@ -100,11 +106,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_unique_chars\n"
]
}
],
"source": [ "source": [
"# %load test_unique_chars.py\n", "# %load test_unique_chars.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
@ -159,7 +173,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,

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

@ -70,7 +70,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -97,29 +97,32 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
],
"source": [ "source": [
"# %load test_group_ordered.py\n", "# %load test_group_ordered.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"\n", "\n",
"class TestGroupOrdered(object):\n",
"\n", "\n",
"class TestGroupOrdered(object):\n",
" def test_group_ordered(self, func):\n", " def test_group_ordered(self, func):\n",
"\n", "\n",
" assert_equal(func(None), None)\n", " assert_equal(func(None), None)\n",
" print('Success: ' + func.__name__ + \" None case.\")\n",
" assert_equal(func([]), [])\n", " assert_equal(func([]), [])\n",
" print('Success: ' + func.__name__ + \" Empty case.\")\n",
" assert_equal(func([1]), [1])\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([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])\n",
" assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b'])\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, 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", " assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])\n",
" print('Success: ' + func.__name__)\n", " print('Success: ' + func.__name__)\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestGroupOrdered()\n", " test = TestGroupOrdered()\n",
" test.test_group_ordered(group_ordered)\n", " test.test_group_ordered(group_ordered)\n",
@ -140,7 +143,7 @@
"source": [ "source": [
"## Solution Notebook\n", "## Solution Notebook\n",
"\n", "\n",
"Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_solution.ipynb) for a discussion on algorithms and code solutions." "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."
] ]
} }
], ],

View File

@ -77,7 +77,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -135,7 +135,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -144,6 +144,8 @@
"from collections import OrderedDict\n", "from collections import OrderedDict\n",
"\n", "\n",
"def group_ordered_alt(list_in):\n", "def group_ordered_alt(list_in):\n",
" if list_in is None:\n",
" return None\n",
" result = OrderedDict()\n", " result = OrderedDict()\n",
" for value in list_in:\n", " for value in list_in:\n",
" result.setdefault(value, []).append(value)\n", " result.setdefault(value, []).append(value)\n",
@ -161,7 +163,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -178,19 +180,23 @@
"%%writefile test_group_ordered.py\n", "%%writefile test_group_ordered.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"\n", "\n",
"class TestGroupOrdered(object):\n",
"\n", "\n",
"class TestGroupOrdered(object):\n",
" def test_group_ordered(self, func):\n", " def test_group_ordered(self, func):\n",
"\n", "\n",
" assert_equal(func(None), None)\n", " assert_equal(func(None), None)\n",
" print('Success: ' + func.__name__ + \" None case.\")\n",
" assert_equal(func([]), [])\n", " assert_equal(func([]), [])\n",
" print('Success: ' + func.__name__ + \" Empty case.\")\n",
" assert_equal(func([1]), [1])\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([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])\n",
" assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b'])\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, 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", " assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])\n",
" print('Success: ' + func.__name__)\n", " print('Success: ' + func.__name__)\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestGroupOrdered()\n", " test = TestGroupOrdered()\n",
" test.test_group_ordered(group_ordered)\n", " test.test_group_ordered(group_ordered)\n",
@ -202,12 +208,12 @@
" pass\n", " pass\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()\n" " main()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -216,7 +222,14 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: group_ordered\n" "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"
] ]
} }
], ],
@ -226,7 +239,6 @@
} }
], ],
"metadata": { "metadata": {
"celltoolbar": "Edit Metadata",
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 2",
"language": "python", "language": "python",

View File

@ -1,18 +1,22 @@
from nose.tools import assert_equal from nose.tools import assert_equal
class TestGroupOrdered(object):
class TestGroupOrdered(object):
def test_group_ordered(self, func): def test_group_ordered(self, func):
assert_equal(func(None), None) assert_equal(func(None), None)
print('Success: ' + func.__name__ + " None case.")
assert_equal(func([]), []) assert_equal(func([]), [])
print('Success: ' + func.__name__ + " Empty case.")
assert_equal(func([1]), [1]) 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([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])
assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b']) 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, 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]) assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])
print('Success: ' + func.__name__) print('Success: ' + func.__name__)
def main(): def main():
test = TestGroupOrdered() test = TestGroupOrdered()
test.test_group_ordered(group_ordered) test.test_group_ordered(group_ordered)

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is a naiive solution sufficient?\n", "* Is a naiive solution sufficient?\n",
" * Yes" " * Yes"
] ]
@ -152,7 +151,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -161,16 +160,34 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Empty input\n", "Empty input\n"
"One element\n", ]
"Two or more elements\n", },
"Success: test_insertion_sort\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": [ "source": [
"%run -i test_insertion_sort.py" "%run -i test_insertion_sort.py"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
@ -189,7 +206,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,

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is a naiive solution sufficient?\n", "* Is a naiive solution sufficient?\n",
" * Yes" " * Yes"
] ]
@ -68,15 +67,25 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "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", "def insertion_sort(data):\n",
" # TODO: Implement me\n", " for i,item in enumerate(data):\n",
" pass" " insert(data,i)\n",
" return data"
] ]
}, },
{ {
@ -92,11 +101,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false "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": [ "source": [
"# %load test_insertion_sort.py\n", "# %load test_insertion_sort.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
@ -153,7 +173,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,