mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
fixed problems, new pull
This commit is contained in:
parent
f703014dec
commit
b4476d3f77
|
@ -35,7 +35,6 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
|
||||
"* Can we assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language\n",
|
||||
|
@ -164,7 +163,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -242,7 +241,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
|
||||
"* Can you assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language\n",
|
||||
|
@ -75,20 +74,38 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_substring(s1, s2):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass\n",
|
||||
"def is_substring(substr, s):\n",
|
||||
" if len(s) < len(substr):\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",
|
||||
"def is_rotation(s1, s2):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" # Call is_substring only once\n",
|
||||
" pass"
|
||||
" if s1 is None or s2 is None:\n",
|
||||
" return False\n",
|
||||
" elif len(s1) != len(s2):\n",
|
||||
" return False\n",
|
||||
" else:\n",
|
||||
" return is_substring(s2,s1+s1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -109,11 +126,19 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_rotation\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# %load test_rotation.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
|
@ -163,7 +188,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
|
||||
"* Can you assume the string is ASCII?\n",
|
||||
" * Yes\n",
|
||||
" * Note: Unicode strings could require special handling depending on your language\n",
|
||||
|
@ -73,15 +72,22 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def unique_chars(string):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
" from collections import defaultdict\n",
|
||||
" \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",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_unique_chars\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# %load test_unique_chars.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
|
@ -159,7 +173,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
34
sorting_searching/group_ordered/group_ordered.py
Normal file
34
sorting_searching/group_ordered/group_ordered.py
Normal 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
|
|
@ -70,7 +70,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -97,29 +97,32 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_group_ordered.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class TestGroupOrdered(object):\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",
|
||||
|
@ -140,7 +143,7 @@
|
|||
"source": [
|
||||
"## Solution Notebook\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."
|
||||
]
|
||||
}
|
||||
],
|
|
@ -77,7 +77,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -135,7 +135,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
|
@ -144,6 +144,8 @@
|
|||
"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",
|
||||
|
@ -161,7 +163,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -178,19 +180,23 @@
|
|||
"%%writefile test_group_ordered.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class TestGroupOrdered(object):\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",
|
||||
|
@ -202,12 +208,12 @@
|
|||
" pass\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()\n"
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -216,7 +222,14 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"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": {
|
||||
"celltoolbar": "Edit Metadata",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"language": "python",
|
|
@ -1,18 +1,22 @@
|
|||
from nose.tools import assert_equal
|
||||
|
||||
class TestGroupOrdered(object):
|
||||
|
||||
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)
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
Loading…
Reference in New Issue
Block a user