mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update insertion sort challenge (#129)
Update constraints, test cases, code, and tests.
This commit is contained in:
parent
2399eb60d3
commit
3cce1ffb51
|
@ -37,6 +37,10 @@
|
||||||
"* Is a naiive solution sufficient?\n",
|
"* Is a naiive solution sufficient?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Are duplicates allowed?\n",
|
"* Are duplicates allowed?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Can we assume the input is valid?\n",
|
||||||
|
" * No\n",
|
||||||
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -46,7 +50,7 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None -> None\n",
|
"* None -> Exception\n",
|
||||||
"* Empty input -> []\n",
|
"* Empty input -> []\n",
|
||||||
"* One element -> [element]\n",
|
"* One element -> [element]\n",
|
||||||
"* Two or more elements"
|
"* Two or more elements"
|
||||||
|
@ -76,9 +80,11 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def insertion_sort(data):\n",
|
"class SelectionSort(object):\n",
|
||||||
" # TODO: Implement me\n",
|
"\n",
|
||||||
" pass"
|
" def sort(data):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -101,31 +107,26 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"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, assert_raises\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestInsertionSort(object):\n",
|
"class TestInsertionSort(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_insertion_sort(self):\n",
|
" def test_insertion_sort(self):\n",
|
||||||
|
" insertion_sort = InsertionSort()\n",
|
||||||
|
"\n",
|
||||||
" print('None input')\n",
|
" print('None input')\n",
|
||||||
" data = None\n",
|
" assert_raises(TypeError, insertion_sort.sort, None)\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, None)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" assert_equal(insertion_sort.sort([]), [])\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, [])\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('One element')\n",
|
" print('One element')\n",
|
||||||
" data = [5]\n",
|
" assert_equal(insertion_sort.sort([5]), [5])\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, [5])\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Two or more elements')\n",
|
" print('Two or more elements')\n",
|
||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" insertion_sort(data)\n",
|
" assert_equal(insertion_sort.sort(data), sorted(data))\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_insertion_sort')\n",
|
" print('Success: test_insertion_sort')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
|
@ -36,6 +36,10 @@
|
||||||
"* Is a naiive solution sufficient?\n",
|
"* Is a naiive solution sufficient?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Are duplicates allowed?\n",
|
"* Are duplicates allowed?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Can we assume the input is valid?\n",
|
||||||
|
" * No\n",
|
||||||
|
"* Can we assume this fits memory?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -45,7 +49,7 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* None -> None\n",
|
"* None -> Exception\n",
|
||||||
"* Empty input -> []\n",
|
"* Empty input -> []\n",
|
||||||
"* One element -> [element]\n",
|
"* One element -> [element]\n",
|
||||||
"* Two or more elements"
|
"* Two or more elements"
|
||||||
|
@ -87,16 +91,20 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def insertion_sort(data):\n",
|
"class InsertionSort(object):\n",
|
||||||
" if data is None or len(data) < 2:\n",
|
"\n",
|
||||||
" return\n",
|
" def sort(self, data):\n",
|
||||||
" for r in range(1, len(data)):\n",
|
" if data is None:\n",
|
||||||
" for l in range(r):\n",
|
" raise TypeError('data cannot be None')\n",
|
||||||
" if data[l] > data[r]:\n",
|
" if len(data) < 2:\n",
|
||||||
" temp = data[r]\n",
|
" return data\n",
|
||||||
" data[l+1:r+1] = data[l:r]\n",
|
" for l in range(len(data)-1):\n",
|
||||||
" data[l] = temp\n",
|
" for r in range(l+1, len(data)):\n",
|
||||||
" break"
|
" if data[r] < data[l]:\n",
|
||||||
|
" temp = data[r]\n",
|
||||||
|
" data[l+1:r+1] = data[l:r]\n",
|
||||||
|
" data[l] = temp\n",
|
||||||
|
" return data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -124,31 +132,26 @@
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_insertion_sort.py\n",
|
"%%writefile test_insertion_sort.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal, assert_raises\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestInsertionSort(object):\n",
|
"class TestInsertionSort(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_insertion_sort(self):\n",
|
" def test_insertion_sort(self):\n",
|
||||||
|
" insertion_sort = InsertionSort()\n",
|
||||||
|
"\n",
|
||||||
" print('None input')\n",
|
" print('None input')\n",
|
||||||
" data = None\n",
|
" assert_raises(TypeError, insertion_sort.sort, None)\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, None)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" assert_equal(insertion_sort.sort([]), [])\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, [])\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('One element')\n",
|
" print('One element')\n",
|
||||||
" data = [5]\n",
|
" assert_equal(insertion_sort.sort([5]), [5])\n",
|
||||||
" insertion_sort(data)\n",
|
|
||||||
" assert_equal(data, [5])\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Two or more elements')\n",
|
" print('Two or more elements')\n",
|
||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" insertion_sort(data)\n",
|
" assert_equal(insertion_sort.sort(data), sorted(data))\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_insertion_sort')\n",
|
" print('Success: test_insertion_sort')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
|
@ -1,28 +1,23 @@
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal, assert_raises
|
||||||
|
|
||||||
|
|
||||||
class TestInsertionSort(object):
|
class TestInsertionSort(object):
|
||||||
|
|
||||||
def test_insertion_sort(self):
|
def test_insertion_sort(self):
|
||||||
|
insertion_sort = InsertionSort()
|
||||||
|
|
||||||
print('None input')
|
print('None input')
|
||||||
data = None
|
assert_raises(TypeError, insertion_sort.sort, None)
|
||||||
insertion_sort(data)
|
|
||||||
assert_equal(data, None)
|
|
||||||
|
|
||||||
print('Empty input')
|
print('Empty input')
|
||||||
data = []
|
assert_equal(insertion_sort.sort([]), [])
|
||||||
insertion_sort(data)
|
|
||||||
assert_equal(data, [])
|
|
||||||
|
|
||||||
print('One element')
|
print('One element')
|
||||||
data = [5]
|
assert_equal(insertion_sort.sort([5]), [5])
|
||||||
insertion_sort(data)
|
|
||||||
assert_equal(data, [5])
|
|
||||||
|
|
||||||
print('Two or more elements')
|
print('Two or more elements')
|
||||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||||
insertion_sort(data)
|
assert_equal(insertion_sort.sort(data), sorted(data))
|
||||||
assert_equal(data, sorted(data))
|
|
||||||
|
|
||||||
print('Success: test_insertion_sort')
|
print('Success: test_insertion_sort')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user