Fix #13, PEP8-ify notebooks.

This commit is contained in:
Donne Martin
2015-07-11 15:35:12 -04:00
parent 04083b2011
commit 03f04fbc4c
12 changed files with 71 additions and 43 deletions

View File

@@ -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()"
]

View File

@@ -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()"
]

View File

@@ -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()