mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Merge pull request #65 from donnemartin/develop
Polish selection sort challenge and solution
This commit is contained in:
commit
e22ee5fbe2
|
@ -35,6 +35,8 @@
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Are duplicates allowed?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -134,7 +136,7 @@
|
||||||
" assert_equal(data, [5])\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, -10]\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -183,7 +185,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -34,6 +34,8 @@
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||||
|
" * Yes\n",
|
||||||
|
"* Are duplicates allowed?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -66,7 +68,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n^2) average, worst, best\n",
|
"* Time: O(n^2) average, worst, best\n",
|
||||||
"* Space: O(1) iterative, O(n) recursive (unless tail-call elimination is available, then O(1)), generally not stable\n",
|
"* Space: O(1) iterative, O(m) recursive where m is the recursion depth (unless tail-call elimination is available, then O(1)), generally not stable\n",
|
||||||
" * Note: Tail call elimination is not inherently available in Python. See the following [StackOverflow post](http://stackoverflow.com/a/13592002)."
|
" * Note: Tail call elimination is not inherently available in Python. See the following [StackOverflow post](http://stackoverflow.com/a/13592002)."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -112,6 +114,9 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def find_min_index(data, start):\n",
|
"def find_min_index(data, start):\n",
|
||||||
|
" # Raises an exception if: \n",
|
||||||
|
" # -data is None\n",
|
||||||
|
" # -start >= len(data) - 1\n",
|
||||||
" min_index = start\n",
|
" min_index = start\n",
|
||||||
" for i in range(start + 1, len(data)):\n",
|
" for i in range(start + 1, len(data)):\n",
|
||||||
" if data[i] < data[min_index]:\n",
|
" if data[i] < data[min_index]:\n",
|
||||||
|
@ -120,13 +125,16 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def swap(data, i, j):\n",
|
"def swap(data, i, j):\n",
|
||||||
" if (i != j):\n",
|
" # Raises an exception if: \n",
|
||||||
|
" # -data is None\n",
|
||||||
|
" # -i or j are >= len(data)\n",
|
||||||
|
" if i != j:\n",
|
||||||
" data[i], data[j] = data[j], data[i]\n",
|
" data[i], data[j] = data[j], data[i]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def selection_sort_recursive(data, start=0):\n",
|
"def selection_sort_recursive(data, start=0):\n",
|
||||||
" if data is None:\n",
|
" if data is None:\n",
|
||||||
" return None\n",
|
" return\n",
|
||||||
" if start < len(data) - 1:\n",
|
" if start < len(data) - 1:\n",
|
||||||
" swap(data, start, find_min_index(data, start))\n",
|
" swap(data, start, find_min_index(data, start))\n",
|
||||||
" selection_sort_recursive(data, start+1)\n",
|
" selection_sort_recursive(data, start+1)\n",
|
||||||
|
@ -135,7 +143,7 @@
|
||||||
"def selection_sort_iterative_alt(data):\n",
|
"def selection_sort_iterative_alt(data):\n",
|
||||||
" if data is None or len(data) < 2:\n",
|
" if data is None or len(data) < 2:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" for i in range(0, len(data) - 1):\n",
|
" for i in range(len(data)-1):\n",
|
||||||
" swap(data, i, find_min_index(data, i))"
|
" swap(data, i, find_min_index(data, i))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -186,7 +194,7 @@
|
||||||
" assert_equal(data, [5])\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, -10]\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -262,7 +270,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -20,7 +20,7 @@ class TestSelectionSort(object):
|
||||||
assert_equal(data, [5])
|
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, -10]
|
||||||
func(data)
|
func(data)
|
||||||
assert_equal(data, sorted(data))
|
assert_equal(data, sorted(data))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user