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