Fix #13, PEP8-ify notebooks.

This commit is contained in:
Donne Martin 2015-07-11 15:39:59 -04:00
parent 4566d1a803
commit 3712839cc9
26 changed files with 139 additions and 92 deletions

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes"
]
@ -116,7 +115,7 @@
"\n",
"\n",
"class TestHanoi(object):\n",
" \n",
"\n",
" def test_hanoi(self):\n",
" num_disks = 3\n",
" src = Stack()\n",
@ -144,10 +143,12 @@
"\n",
" print('Success: test_hanoi')\n",
"\n",
"\n",
"def main():\n",
" test = TestHanoi()\n",
" test.test_hanoi()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes"
]
@ -133,7 +132,7 @@
"\n",
"\n",
"class TestHanoi(object):\n",
" \n",
"\n",
" def test_hanoi(self):\n",
" num_disks = 3\n",
" src = Stack()\n",
@ -161,10 +160,12 @@
"\n",
" print('Success: test_hanoi')\n",
"\n",
"\n",
"def main():\n",
" test = TestHanoi()\n",
" test.test_hanoi()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -30,9 +30,11 @@ class TestHanoi(object):
print('Success: test_hanoi')
def main():
test = TestHanoi()
test.test_hanoi()
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Are the stacks and array a fixed size?\n",
" * Yes"
]
@ -77,7 +76,7 @@
"outputs": [],
"source": [
"class Stacks(object):\n",
" \n",
"\n",
" def __init__(self, num_stacks, stack_size):\n",
" # TODO: Implement me\n",
" pass\n",
@ -120,7 +119,7 @@
"\n",
"\n",
"class TestStacks(object):\n",
" \n",
"\n",
" @raises(Exception)\n",
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
" print('Test: Pop on empty stack')\n",
@ -151,14 +150,16 @@
"\n",
" print('Success: test_stacks\\n')\n",
"\n",
"\n",
"def main():\n",
" num_stacks = 3\n",
" stack_size = 100 \n",
" stack_size = 100\n",
" test = TestStacks()\n",
" test.test_pop_on_empty(num_stacks, stack_size)\n",
" test.test_push_on_full(num_stacks, stack_size)\n",
" test.test_stacks(num_stacks, stack_size)\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* Are the stacks and array a fixed size?\n",
" * Yes"
]
@ -107,7 +106,7 @@
"outputs": [],
"source": [
"class Stacks(object):\n",
" \n",
"\n",
" def __init__(self, num_stacks, stack_size):\n",
" self.num_stacks = num_stacks\n",
" self.stack_size = stack_size\n",
@ -129,7 +128,7 @@
" if self.stack_pointers[stack_index] == -1:\n",
" raise Exception('Stack is empty')\n",
" else:\n",
" array_index = self.abs_index(stack_index) \n",
" array_index = self.abs_index(stack_index)\n",
" data = self.stack_array[array_index]\n",
" self.stack_array[array_index] = None\n",
" self.stack_pointers[stack_index] -= 1\n",
@ -166,7 +165,7 @@
"\n",
"\n",
"class TestStacks(object):\n",
" \n",
"\n",
" @raises(Exception)\n",
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
" print('Test: Pop on empty stack')\n",
@ -197,14 +196,16 @@
"\n",
" print('Success: test_stacks\\n')\n",
"\n",
"\n",
"def main():\n",
" num_stacks = 3\n",
" stack_size = 100 \n",
" stack_size = 100\n",
" test = TestStacks()\n",
" test.test_pop_on_empty(num_stacks, stack_size)\n",
" test.test_push_on_full(num_stacks, stack_size)\n",
" test.test_stacks(num_stacks, stack_size)\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -34,6 +34,7 @@ class TestStacks(object):
print('Success: test_stacks\n')
def main():
num_stacks = 3
stack_size = 100
@ -42,5 +43,6 @@ def main():
test.test_push_on_full(num_stacks, stack_size)
test.test_stacks(num_stacks, stack_size)
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Do you expect the methods to be enqueue and dequeue?\n",
" * Yes\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
@ -92,7 +91,7 @@
"outputs": [],
"source": [
"class QueueFromStacks(object):\n",
" \n",
"\n",
" def __init__(self):\n",
" # TODO: Implement me\n",
" pass\n",
@ -131,7 +130,7 @@
"\n",
"\n",
"class TestQueueFromStacks(object):\n",
" \n",
"\n",
" def test_queue_from_stacks(self):\n",
" print('Test: Dequeue on empty stack')\n",
" queue = QueueFromStacks()\n",
@ -141,7 +140,7 @@
" print('Test: Enqueue on non-empty stack')\n",
" print('Test: Multiple enqueue in a row')\n",
" num_items = 3\n",
" for i in range (0, num_items):\n",
" for i in range(0, num_items):\n",
" queue.enqueue(i)\n",
"\n",
" print('Test: Dequeue on non-empty stack')\n",
@ -158,10 +157,12 @@
"\n",
" print('Success: test_queue_from_stacks')\n",
"\n",
"\n",
"def main():\n",
" test = TestQueueFromStacks()\n",
" test.test_queue_from_stacks()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* Do you expect the methods to be enqueue and dequeue?\n",
" * Yes\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
@ -121,7 +120,7 @@
"outputs": [],
"source": [
"class QueueFromStacks(object):\n",
" \n",
"\n",
" def __init__(self):\n",
" self.left_stack = Stack()\n",
" self.right_stack = Stack()\n",
@ -170,7 +169,7 @@
"\n",
"\n",
"class TestQueueFromStacks(object):\n",
" \n",
"\n",
" def test_queue_from_stacks(self):\n",
" print('Test: Dequeue on empty stack')\n",
" queue = QueueFromStacks()\n",
@ -180,7 +179,7 @@
" print('Test: Enqueue on non-empty stack')\n",
" print('Test: Multiple enqueue in a row')\n",
" num_items = 3\n",
" for i in range (0, num_items):\n",
" for i in range(0, num_items):\n",
" queue.enqueue(i)\n",
"\n",
" print('Test: Dequeue on non-empty stack')\n",
@ -197,10 +196,12 @@
"\n",
" print('Success: test_queue_from_stacks')\n",
"\n",
"\n",
"def main():\n",
" test = TestQueueFromStacks()\n",
" test.test_queue_from_stacks()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestQueueFromStacks(object):
def test_queue_from_stacks(self):
print('Test: Dequeue on empty stack')
queue = QueueFromStacks()
@ -12,7 +12,7 @@ class TestQueueFromStacks(object):
print('Test: Enqueue on non-empty stack')
print('Test: Multiple enqueue in a row')
num_items = 3
for i in range (0, num_items):
for i in range(0, num_items):
queue.enqueue(i)
print('Test: Dequeue on non-empty stack')
@ -29,9 +29,11 @@ class TestQueueFromStacks(object):
print('Success: test_queue_from_stacks')
def main():
test = TestQueueFromStacks()
test.test_queue_from_stacks()
if __name__ == '__main__':
main()

View File

@ -1,11 +1,12 @@
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class Queue(object):
def __init__(self):
self.first = None
self.last = None
@ -23,7 +24,7 @@ class Queue(object):
# Empty list
if self.first is None and self.last is None:
return None
# Remove only element from a one element list
elif self.first == self.last:
data = self.first.data

View File

@ -35,7 +35,6 @@
"source": [
"## Constraints\n",
"\n",
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\n",
" * Yes\n",
"* If there are no items on the list, do you expect the first and last pointers to be None?\n",
@ -87,13 +86,14 @@
"outputs": [],
"source": [
"class Node(object):\n",
" \n",
"\n",
" def __init__(self, data):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"class Queue(object):\n",
" \n",
"\n",
" def __init__(self):\n",
" # TODO: Implement me\n",
" pass\n",
@ -131,7 +131,7 @@
"\n",
"\n",
"class TestQueue(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
@ -154,13 +154,15 @@
" assert_equal(queue.dequeue(), 2)\n",
" assert_equal(queue.dequeue(), 3)\n",
" assert_equal(queue.dequeue(), 4)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestQueue()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\n",
" * Yes\n",
"* If there are no items on the list, do you expect the first and last pointers to be None?\n",
@ -118,13 +117,14 @@
"source": [
"%%writefile queue_list.py\n",
"class Node(object):\n",
" \n",
"\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.next = None\n",
"\n",
"\n",
"class Queue(object):\n",
" \n",
"\n",
" def __init__(self):\n",
" self.first = None\n",
" self.last = None\n",
@ -142,7 +142,7 @@
" # Empty list\n",
" if self.first is None and self.last is None:\n",
" return None\n",
" \n",
"\n",
" # Remove only element from a one element list\n",
" elif self.first == self.last:\n",
" data = self.first.data\n",
@ -195,7 +195,7 @@
"\n",
"\n",
"class TestQueue(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
@ -218,13 +218,15 @@
" assert_equal(queue.dequeue(), 2)\n",
" assert_equal(queue.dequeue(), 3)\n",
" assert_equal(queue.dequeue(), 4)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestQueue()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestQueue(object):
# TODO: It would be better if we had unit tests for each
# method in addition to the following end-to-end test
def test_end_to_end(self):
@ -25,12 +25,14 @@ class TestQueue(object):
assert_equal(queue.dequeue(), 2)
assert_equal(queue.dequeue(), 3)
assert_equal(queue.dequeue(), 4)
print('Success: test_end_to_end')
def main():
test = TestQueue()
test.test_end_to_end()
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\n",
"* If a stack becomes full, we should automatically create one?\n",
@ -92,7 +91,7 @@
"outputs": [],
"source": [
"class StackWithCapacity(Stack):\n",
" \n",
"\n",
" def __init__(self, top=None, capacity=10):\n",
" # TODO: Implement me\n",
" pass\n",
@ -109,8 +108,9 @@
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"class SetOfStacks(object):\n",
" \n",
"\n",
" def __init__(self, capacity):\n",
" # TODO: Implement me\n",
" pass\n",
@ -148,7 +148,7 @@
"\n",
"\n",
"class TestSetOfStacks(object):\n",
" \n",
"\n",
" def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n",
" capacity = 2\n",
@ -170,13 +170,15 @@
"\n",
" print('Test: Pop on no elements')\n",
" assert_equal(stacks.pop(), None)\n",
" \n",
"\n",
" print('Success: test_set_of_stacks')\n",
"\n",
"\n",
"def main():\n",
" test = TestSetOfStacks()\n",
" test.test_set_of_stacks()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume we already have a stack class that can be used for this problem?\n",
" * Yes\n",
"* If a stack becomes full, we should automatically create one?\n",
@ -112,7 +111,7 @@
"outputs": [],
"source": [
"class StackWithCapacity(Stack):\n",
" \n",
"\n",
" def __init__(self, top=None, capacity=10):\n",
" self.capacity = capacity\n",
" self.num_items = 0\n",
@ -133,8 +132,9 @@
" def is_full(self):\n",
" return self.num_items == self.capacity\n",
"\n",
"\n",
"class SetOfStacks(object):\n",
" \n",
"\n",
" def __init__(self, capacity):\n",
" self.capacity = capacity\n",
" self.stacks = []\n",
@ -191,7 +191,7 @@
"\n",
"\n",
"class TestSetOfStacks(object):\n",
" \n",
"\n",
" def test_set_of_stacks(self):\n",
" print('Test: Push on an empty stack')\n",
" capacity = 2\n",
@ -213,13 +213,15 @@
"\n",
" print('Test: Pop on no elements')\n",
" assert_equal(stacks.pop(), None)\n",
" \n",
"\n",
" print('Success: test_set_of_stacks')\n",
"\n",
"\n",
"def main():\n",
" test = TestSetOfStacks()\n",
" test.test_set_of_stacks()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestSetOfStacks(object):
def test_set_of_stacks(self):
print('Test: Push on an empty stack')
capacity = 2
@ -24,12 +24,14 @@ class TestSetOfStacks(object):
print('Test: Pop on no elements')
assert_equal(stacks.pop(), None)
print('Success: test_set_of_stacks')
def main():
test = TestSetOfStacks()
test.test_set_of_stacks()
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* When sorted, should the largest element be at the top or bottom?\n",
" * Top\n",
"* Can you have duplicate values like 5, 5?\n",
@ -92,7 +91,7 @@
"outputs": [],
"source": [
"class MyStack(Stack):\n",
" \n",
"\n",
" def sort(self):\n",
" # TODO: Implement me\n",
" pass"
@ -123,7 +122,7 @@
"\n",
"\n",
"class TestSortStack(object):\n",
" \n",
"\n",
" def get_sorted_stack(self, numbers):\n",
" stack = MyStack()\n",
" for x in numbers:\n",
@ -148,13 +147,15 @@
" for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n",
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
" \n",
"\n",
" print('Success: test_sort_stack')\n",
"\n",
"\n",
"def main():\n",
" test = TestSortStack()\n",
" test.test_sort_stack()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* When sorted, should the largest element be at the top or bottom?\n",
" * Top\n",
"* Can you have duplicate values like 5, 5?\n",
@ -101,7 +100,7 @@
"outputs": [],
"source": [
"class MyStack(Stack):\n",
" \n",
"\n",
" def sort(self):\n",
" buff = MyStack()\n",
" while not self.is_empty():\n",
@ -142,7 +141,7 @@
"\n",
"\n",
"class TestSortStack(object):\n",
" \n",
"\n",
" def get_sorted_stack(self, numbers):\n",
" stack = MyStack()\n",
" for x in numbers:\n",
@ -167,13 +166,15 @@
" for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n",
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
" \n",
"\n",
" print('Success: test_sort_stack')\n",
"\n",
"\n",
"def main():\n",
" test = TestSortStack()\n",
" test.test_sort_stack()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -31,9 +31,11 @@ class TestSortStack(object):
print('Success: test_sort_stack')
def main():
test = TestSortStack()
test.test_sort_stack()
if __name__ == '__main__':
main()

View File

@ -1,11 +1,12 @@
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class Stack(object):
def __init__(self, top=None):
self.top = top

View File

@ -35,7 +35,6 @@
"source": [
"## Constraints\n",
"\n",
"* None"
]
},
@ -92,13 +91,14 @@
"outputs": [],
"source": [
"class Node(object):\n",
" \n",
"\n",
" def __init__(self, data):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"class Stack(object):\n",
" \n",
"\n",
" def __init__(self, top=None):\n",
" # TODO: Implement me\n",
" pass\n",
@ -144,7 +144,7 @@
"\n",
"\n",
"class TestStack(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
@ -172,13 +172,15 @@
" assert_equal(stack.pop(), 1)\n",
" assert_equal(stack.peek(), None)\n",
" assert_equal(stack.is_empty(), True)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestStack()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* None"
]
},
@ -137,13 +136,14 @@
"source": [
"%%writefile stack.py\n",
"class Node(object):\n",
" \n",
"\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.next = None\n",
"\n",
"\n",
"class Stack(object):\n",
" \n",
"\n",
" def __init__(self, top=None):\n",
" self.top = top\n",
"\n",
@ -208,7 +208,7 @@
"\n",
"\n",
"class TestStack(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
@ -236,13 +236,15 @@
" assert_equal(stack.pop(), 1)\n",
" assert_equal(stack.peek(), None)\n",
" assert_equal(stack.is_empty(), True)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestStack()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestStack(object):
# TODO: It would be better if we had unit tests for each
# method in addition to the following end-to-end test
def test_end_to_end(self):
@ -30,12 +30,14 @@ class TestStack(object):
assert_equal(stack.pop(), 1)
assert_equal(stack.peek(), None)
assert_equal(stack.is_empty(), True)
print('Success: test_end_to_end')
def main():
test = TestStack()
test.test_end_to_end()
if __name__ == '__main__':
main()

View File

@ -92,6 +92,7 @@
"\n",
"\n",
"class MyStack(Stack):\n",
"\n",
" def __init__(self, top=None):\n",
" # TODO: Implement me\n",
" pass\n",
@ -133,6 +134,7 @@
"\n",
"\n",
"class TestStackMin(object):\n",
"\n",
" def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n",
" stack = MyStack()\n",
@ -161,13 +163,15 @@
"\n",
" print('Test: Pop empty stack')\n",
" assert_equal(stack.pop(), None)\n",
" \n",
"\n",
" print('Success: test_stack_min')\n",
"\n",
"\n",
"def main():\n",
" test = TestStackMin()\n",
" test.test_stack_min()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -120,6 +120,7 @@
"\n",
"\n",
"class MyStack(Stack):\n",
"\n",
" def __init__(self, top=None):\n",
" self.min_vals = Stack()\n",
" super(MyStack, self).__init__(top)\n",
@ -171,6 +172,7 @@
"\n",
"\n",
"class TestStackMin(object):\n",
"\n",
" def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n",
" stack = MyStack()\n",
@ -199,13 +201,15 @@
"\n",
" print('Test: Pop empty stack')\n",
" assert_equal(stack.pop(), None)\n",
" \n",
"\n",
" print('Success: test_stack_min')\n",
"\n",
"\n",
"def main():\n",
" test = TestStackMin()\n",
" test.test_stack_min()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -2,6 +2,7 @@ from nose.tools import assert_equal
class TestStackMin(object):
def test_stack_min(self):
print('Test: Push on empty stack, non-empty stack')
stack = MyStack()
@ -30,12 +31,14 @@ class TestStackMin(object):
print('Test: Pop empty stack')
assert_equal(stack.pop(), None)
print('Success: test_stack_min')
def main():
test = TestStackMin()
test.test_stack_min()
if __name__ == '__main__':
main()