mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 01:18:02 +00:00
Fix #13, PEP8-ify notebooks.
This commit is contained in:
@@ -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"
|
||||||
]
|
]
|
||||||
@@ -103,7 +102,7 @@
|
|||||||
"\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",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
@@ -119,12 +118,17 @@
|
|||||||
" 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",
|
" insertion_sort(data)\n",
|
||||||
" assert_equal(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",
|
||||||
"if __name__ == '__main__':\n",
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
" test = TestInsertionSort()\n",
|
" test = TestInsertionSort()\n",
|
||||||
" test.test_insertion_sort()"
|
" test.test_insertion_sort()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" main()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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"
|
||||||
]
|
]
|
||||||
@@ -126,7 +125,7 @@
|
|||||||
"\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",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
@@ -142,12 +141,17 @@
|
|||||||
" 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",
|
" insertion_sort(data)\n",
|
||||||
" assert_equal(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",
|
||||||
"if __name__ == '__main__':\n",
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
" test = TestInsertionSort()\n",
|
" test = TestInsertionSort()\n",
|
||||||
" test.test_insertion_sort()"
|
" test.test_insertion_sort()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" main()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from nose.tools import assert_equal
|
|||||||
|
|
||||||
|
|
||||||
class TestInsertionSort(object):
|
class TestInsertionSort(object):
|
||||||
|
|
||||||
def test_insertion_sort(self):
|
def test_insertion_sort(self):
|
||||||
print('Empty input')
|
print('Empty input')
|
||||||
data = []
|
data = []
|
||||||
@@ -18,9 +18,14 @@ class TestInsertionSort(object):
|
|||||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||||
insertion_sort(data)
|
insertion_sort(data)
|
||||||
assert_equal(data, sorted(data))
|
assert_equal(data, sorted(data))
|
||||||
|
|
||||||
print('Success: test_insertion_sort')
|
print('Success: test_insertion_sort')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
def main():
|
||||||
test = TestInsertionSort()
|
test = TestInsertionSort()
|
||||||
test.test_insertion_sort()
|
test.test_insertion_sort()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -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"
|
||||||
]
|
]
|
||||||
@@ -119,13 +118,15 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" data = merge_sort(data)\n",
|
" data = merge_sort(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_merge_sort')\n",
|
" print('Success: test_merge_sort')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestMergeSort()\n",
|
" test = TestMergeSort()\n",
|
||||||
" test.test_merge_sort()\n",
|
" test.test_merge_sort()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -110,6 +110,7 @@
|
|||||||
" r += 1\n",
|
" r += 1\n",
|
||||||
" return result\n",
|
" return result\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def merge_sort(data):\n",
|
"def merge_sort(data):\n",
|
||||||
" if len(data) < 2:\n",
|
" if len(data) < 2:\n",
|
||||||
" return data\n",
|
" return data\n",
|
||||||
@@ -165,13 +166,15 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" data = merge_sort(data)\n",
|
" data = merge_sort(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_merge_sort')\n",
|
" print('Success: test_merge_sort')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestMergeSort()\n",
|
" test = TestMergeSort()\n",
|
||||||
" test.test_merge_sort()\n",
|
" test.test_merge_sort()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ class TestMergeSort(object):
|
|||||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||||
data = merge_sort(data)
|
data = merge_sort(data)
|
||||||
assert_equal(data, sorted(data))
|
assert_equal(data, sorted(data))
|
||||||
|
|
||||||
print('Success: test_merge_sort')
|
print('Success: test_merge_sort')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestMergeSort()
|
test = TestMergeSort()
|
||||||
test.test_merge_sort()
|
test.test_merge_sort()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
@@ -34,7 +34,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* Is a naiive solution sufficient (ie not in-place)?\n",
|
"* Is a naiive solution sufficient (ie not in-place)?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
@@ -118,9 +117,10 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" data = func(data)\n",
|
" data = func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_quick_sort\\n')\n",
|
" print('Success: test_quick_sort\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQuickSort()\n",
|
" test = TestQuickSort()\n",
|
||||||
" test.test_quick_sort(quick_sort)\n",
|
" test.test_quick_sort(quick_sort)\n",
|
||||||
@@ -130,7 +130,8 @@
|
|||||||
" # Alternate solutions are only defined\n",
|
" # Alternate solutions are only defined\n",
|
||||||
" # in the solutions file\n",
|
" # in the solutions file\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -89,6 +89,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"from __future__ import division\n",
|
"from __future__ import division\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def quick_sort(data):\n",
|
"def quick_sort(data):\n",
|
||||||
" if len(data) < 2:\n",
|
" if len(data) < 2:\n",
|
||||||
" return data\n",
|
" return data\n",
|
||||||
@@ -96,7 +97,7 @@
|
|||||||
" right = []\n",
|
" right = []\n",
|
||||||
" pivot_index = len(data) // 2\n",
|
" pivot_index = len(data) // 2\n",
|
||||||
" pivot_value = data[pivot_index]\n",
|
" pivot_value = data[pivot_index]\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # Build the left and right partitions\n",
|
" # Build the left and right partitions\n",
|
||||||
" for i in range(0, len(data)):\n",
|
" for i in range(0, len(data)):\n",
|
||||||
" if i == pivot_index:\n",
|
" if i == pivot_index:\n",
|
||||||
@@ -105,7 +106,7 @@
|
|||||||
" left.append(data[i])\n",
|
" left.append(data[i])\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" right.append(data[i])\n",
|
" right.append(data[i])\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # Recursively apply quick_sort\n",
|
" # Recursively apply quick_sort\n",
|
||||||
" left = quick_sort(left)\n",
|
" left = quick_sort(left)\n",
|
||||||
" right = quick_sort(right)\n",
|
" right = quick_sort(right)\n",
|
||||||
@@ -118,9 +119,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Pythonic-Code\n",
|
"## Pythonic-Code\n",
|
||||||
"\n",
|
"\n",
|
||||||
"From http://stackoverflow.com/questions/18262306/quick-sort-with-python\n",
|
"The following code from [Stack Overflow](http://stackoverflow.com/questions/18262306/quick-sort-with-python) is very concise, although it might be a little difficult to read:"
|
||||||
"\n",
|
|
||||||
"The following code is very concise, although it might be a little difficult to read:"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -131,11 +130,11 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def quick_sort_alt(arr): \n",
|
"def quick_sort_alt(arr):\n",
|
||||||
" if len(arr) <= 1:\n",
|
" if len(arr) <= 1:\n",
|
||||||
" return arr\n",
|
" return arr\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" return quick_sort_alt([x for x in arr[1:] if x<arr[0]]) + [arr[0]] + quick_sort_alt([x for x in arr[1:] if x>=arr[0]])"
|
" return quick_sort_alt([x for x in arr[1:] if x < arr[0]]) + [arr[0]] + quick_sort_alt([x for x in arr[1:] if x >= arr[0]])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -182,9 +181,10 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" data = func(data)\n",
|
" data = func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_quick_sort\\n')\n",
|
" print('Success: test_quick_sort\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQuickSort()\n",
|
" test = TestQuickSort()\n",
|
||||||
" test.test_quick_sort(quick_sort)\n",
|
" test.test_quick_sort(quick_sort)\n",
|
||||||
@@ -194,7 +194,8 @@
|
|||||||
" # Alternate solutions are only defined\n",
|
" # Alternate solutions are only defined\n",
|
||||||
" # in the solutions file\n",
|
" # in the solutions file\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ class TestQuickSort(object):
|
|||||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||||
data = func(data)
|
data = func(data)
|
||||||
assert_equal(data, sorted(data))
|
assert_equal(data, sorted(data))
|
||||||
|
|
||||||
print('Success: test_quick_sort\n')
|
print('Success: test_quick_sort\n')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestQuickSort()
|
test = TestQuickSort()
|
||||||
test.test_quick_sort(quick_sort)
|
test.test_quick_sort(quick_sort)
|
||||||
@@ -29,6 +30,7 @@ def main():
|
|||||||
# Alternate solutions are only defined
|
# Alternate solutions are only defined
|
||||||
# in the solutions file
|
# in the solutions file
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
@@ -34,7 +34,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## 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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
@@ -116,7 +115,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSelectionSort(object):\n",
|
"class TestSelectionSort(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_selection_sort(self, func):\n",
|
" def test_selection_sort(self, func):\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
@@ -132,9 +131,10 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_selection_sort\\n')\n",
|
" print('Success: test_selection_sort\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSelectionSort()\n",
|
" test = TestSelectionSort()\n",
|
||||||
" test.test_selection_sort(selection_sort)\n",
|
" test.test_selection_sort(selection_sort)\n",
|
||||||
@@ -145,6 +145,7 @@
|
|||||||
" # in the solutions file\n",
|
" # in the solutions file\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -33,7 +33,6 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## 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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
@@ -91,6 +90,7 @@
|
|||||||
" min_index = i\n",
|
" min_index = i\n",
|
||||||
" return min_index\n",
|
" return min_index\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def swap(data, i, j):\n",
|
"def swap(data, i, j):\n",
|
||||||
" if (i != j):\n",
|
" if (i != j):\n",
|
||||||
" data[i], data[j] = data[j], data[i]"
|
" data[i], data[j] = data[j], data[i]"
|
||||||
@@ -154,7 +154,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSelectionSort(object):\n",
|
"class TestSelectionSort(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_selection_sort(self, func):\n",
|
" def test_selection_sort(self, func):\n",
|
||||||
" print('Empty input')\n",
|
" print('Empty input')\n",
|
||||||
" data = []\n",
|
" data = []\n",
|
||||||
@@ -170,9 +170,10 @@
|
|||||||
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
" data = [5, 1, 7, 2, 6, -3, 5, 7, -1]\n",
|
||||||
" func(data)\n",
|
" func(data)\n",
|
||||||
" assert_equal(data, sorted(data))\n",
|
" assert_equal(data, sorted(data))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_selection_sort\\n')\n",
|
" print('Success: test_selection_sort\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSelectionSort()\n",
|
" test = TestSelectionSort()\n",
|
||||||
" test.test_selection_sort(selection_sort)\n",
|
" test.test_selection_sort(selection_sort)\n",
|
||||||
@@ -183,6 +184,7 @@
|
|||||||
" # in the solutions file\n",
|
" # in the solutions file\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from nose.tools import assert_equal
|
|||||||
|
|
||||||
|
|
||||||
class TestSelectionSort(object):
|
class TestSelectionSort(object):
|
||||||
|
|
||||||
def test_selection_sort(self, func):
|
def test_selection_sort(self, func):
|
||||||
print('Empty input')
|
print('Empty input')
|
||||||
data = []
|
data = []
|
||||||
@@ -18,9 +18,10 @@ class TestSelectionSort(object):
|
|||||||
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
data = [5, 1, 7, 2, 6, -3, 5, 7, -1]
|
||||||
func(data)
|
func(data)
|
||||||
assert_equal(data, sorted(data))
|
assert_equal(data, sorted(data))
|
||||||
|
|
||||||
print('Success: test_selection_sort\n')
|
print('Success: test_selection_sort\n')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestSelectionSort()
|
test = TestSelectionSort()
|
||||||
test.test_selection_sort(selection_sort)
|
test.test_selection_sort(selection_sort)
|
||||||
@@ -31,5 +32,6 @@ def main():
|
|||||||
# in the solutions file
|
# in the solutions file
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user