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",
|
||||
" * Yes\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"
|
||||
]
|
||||
},
|
||||
|
@ -46,7 +50,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* None -> Exception\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements"
|
||||
|
@ -76,7 +80,9 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def insertion_sort(data):\n",
|
||||
"class SelectionSort(object):\n",
|
||||
"\n",
|
||||
" def sort(data):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
|
@ -101,31 +107,26 @@
|
|||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_insertion_sort.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestInsertionSort(object):\n",
|
||||
"\n",
|
||||
" def test_insertion_sort(self):\n",
|
||||
" insertion_sort = InsertionSort()\n",
|
||||
"\n",
|
||||
" print('None input')\n",
|
||||
" data = None\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, None)\n",
|
||||
" assert_raises(TypeError, insertion_sort.sort, None)\n",
|
||||
"\n",
|
||||
" print('Empty input')\n",
|
||||
" data = []\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, [])\n",
|
||||
" assert_equal(insertion_sort.sort([]), [])\n",
|
||||
"\n",
|
||||
" print('One element')\n",
|
||||
" data = [5]\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, [5])\n",
|
||||
" assert_equal(insertion_sort.sort([5]), [5])\n",
|
||||
"\n",
|
||||
" print('Two or more elements')\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, sorted(data))\n",
|
||||
" assert_equal(insertion_sort.sort(data), sorted(data))\n",
|
||||
"\n",
|
||||
" print('Success: test_insertion_sort')\n",
|
||||
"\n",
|
||||
|
|
|
@ -36,6 +36,10 @@
|
|||
"* Is a naiive solution sufficient?\n",
|
||||
" * Yes\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"
|
||||
]
|
||||
},
|
||||
|
@ -45,7 +49,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* None -> None\n",
|
||||
"* None -> Exception\n",
|
||||
"* Empty input -> []\n",
|
||||
"* One element -> [element]\n",
|
||||
"* Two or more elements"
|
||||
|
@ -87,16 +91,20 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def insertion_sort(data):\n",
|
||||
" if data is None or len(data) < 2:\n",
|
||||
" return\n",
|
||||
" for r in range(1, len(data)):\n",
|
||||
" for l in range(r):\n",
|
||||
" if data[l] > data[r]:\n",
|
||||
"class InsertionSort(object):\n",
|
||||
"\n",
|
||||
" def sort(self, data):\n",
|
||||
" if data is None:\n",
|
||||
" raise TypeError('data cannot be None')\n",
|
||||
" if len(data) < 2:\n",
|
||||
" return data\n",
|
||||
" for l in range(len(data)-1):\n",
|
||||
" for r in range(l+1, len(data)):\n",
|
||||
" if data[r] < data[l]:\n",
|
||||
" temp = data[r]\n",
|
||||
" data[l+1:r+1] = data[l:r]\n",
|
||||
" data[l] = temp\n",
|
||||
" break"
|
||||
" return data"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -124,31 +132,26 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_insertion_sort.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestInsertionSort(object):\n",
|
||||
"\n",
|
||||
" def test_insertion_sort(self):\n",
|
||||
" insertion_sort = InsertionSort()\n",
|
||||
"\n",
|
||||
" print('None input')\n",
|
||||
" data = None\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, None)\n",
|
||||
" assert_raises(TypeError, insertion_sort.sort, None)\n",
|
||||
"\n",
|
||||
" print('Empty input')\n",
|
||||
" data = []\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, [])\n",
|
||||
" assert_equal(insertion_sort.sort([]), [])\n",
|
||||
"\n",
|
||||
" print('One element')\n",
|
||||
" data = [5]\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, [5])\n",
|
||||
" assert_equal(insertion_sort.sort([5]), [5])\n",
|
||||
"\n",
|
||||
" print('Two or more elements')\n",
|
||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||
" insertion_sort(data)\n",
|
||||
" assert_equal(data, sorted(data))\n",
|
||||
" assert_equal(insertion_sort.sort(data), sorted(data))\n",
|
||||
"\n",
|
||||
" print('Success: test_insertion_sort')\n",
|
||||
"\n",
|
||||
|
|
|
@ -1,28 +1,23 @@
|
|||
from nose.tools import assert_equal
|
||||
from nose.tools import assert_equal, assert_raises
|
||||
|
||||
|
||||
class TestInsertionSort(object):
|
||||
|
||||
def test_insertion_sort(self):
|
||||
insertion_sort = InsertionSort()
|
||||
|
||||
print('None input')
|
||||
data = None
|
||||
insertion_sort(data)
|
||||
assert_equal(data, None)
|
||||
assert_raises(TypeError, insertion_sort.sort, None)
|
||||
|
||||
print('Empty input')
|
||||
data = []
|
||||
insertion_sort(data)
|
||||
assert_equal(data, [])
|
||||
assert_equal(insertion_sort.sort([]), [])
|
||||
|
||||
print('One element')
|
||||
data = [5]
|
||||
insertion_sort(data)
|
||||
assert_equal(data, [5])
|
||||
assert_equal(insertion_sort.sort([5]), [5])
|
||||
|
||||
print('Two or more elements')
|
||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||
insertion_sort(data)
|
||||
assert_equal(data, sorted(data))
|
||||
assert_equal(insertion_sort.sort(data), sorted(data))
|
||||
|
||||
print('Success: test_insertion_sort')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user