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
4566d1a803
commit
3712839cc9
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -116,7 +115,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHanoi(object):\n",
|
"class TestHanoi(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_hanoi(self):\n",
|
" def test_hanoi(self):\n",
|
||||||
" num_disks = 3\n",
|
" num_disks = 3\n",
|
||||||
" src = Stack()\n",
|
" src = Stack()\n",
|
||||||
|
@ -144,10 +143,12 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_hanoi')\n",
|
" print('Success: test_hanoi')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestHanoi()\n",
|
" test = TestHanoi()\n",
|
||||||
" test.test_hanoi()\n",
|
" test.test_hanoi()\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",
|
||||||
|
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -133,7 +132,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestHanoi(object):\n",
|
"class TestHanoi(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_hanoi(self):\n",
|
" def test_hanoi(self):\n",
|
||||||
" num_disks = 3\n",
|
" num_disks = 3\n",
|
||||||
" src = Stack()\n",
|
" src = Stack()\n",
|
||||||
|
@ -161,10 +160,12 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_hanoi')\n",
|
" print('Success: test_hanoi')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestHanoi()\n",
|
" test = TestHanoi()\n",
|
||||||
" test.test_hanoi()\n",
|
" test.test_hanoi()\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -30,9 +30,11 @@ class TestHanoi(object):
|
||||||
|
|
||||||
print('Success: test_hanoi')
|
print('Success: test_hanoi')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestHanoi()
|
test = TestHanoi()
|
||||||
test.test_hanoi()
|
test.test_hanoi()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* Are the stacks and array a fixed size?\n",
|
"* Are the stacks and array a fixed size?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -77,7 +76,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Stacks(object):\n",
|
"class Stacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, num_stacks, stack_size):\n",
|
" def __init__(self, num_stacks, stack_size):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -120,7 +119,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStacks(object):\n",
|
"class TestStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
" @raises(Exception)\n",
|
||||||
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||||
" print('Test: Pop on empty stack')\n",
|
" print('Test: Pop on empty stack')\n",
|
||||||
|
@ -151,14 +150,16 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_stacks\\n')\n",
|
" print('Success: test_stacks\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" num_stacks = 3\n",
|
" num_stacks = 3\n",
|
||||||
" stack_size = 100 \n",
|
" stack_size = 100\n",
|
||||||
" test = TestStacks()\n",
|
" test = TestStacks()\n",
|
||||||
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
||||||
" test.test_push_on_full(num_stacks, stack_size)\n",
|
" test.test_push_on_full(num_stacks, stack_size)\n",
|
||||||
" test.test_stacks(num_stacks, stack_size)\n",
|
" test.test_stacks(num_stacks, stack_size)\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",
|
||||||
|
|
||||||
"* Are the stacks and array a fixed size?\n",
|
"* Are the stacks and array a fixed size?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -107,7 +106,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Stacks(object):\n",
|
"class Stacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, num_stacks, stack_size):\n",
|
" def __init__(self, num_stacks, stack_size):\n",
|
||||||
" self.num_stacks = num_stacks\n",
|
" self.num_stacks = num_stacks\n",
|
||||||
" self.stack_size = stack_size\n",
|
" self.stack_size = stack_size\n",
|
||||||
|
@ -129,7 +128,7 @@
|
||||||
" if self.stack_pointers[stack_index] == -1:\n",
|
" if self.stack_pointers[stack_index] == -1:\n",
|
||||||
" raise Exception('Stack is empty')\n",
|
" raise Exception('Stack is empty')\n",
|
||||||
" else:\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",
|
" data = self.stack_array[array_index]\n",
|
||||||
" self.stack_array[array_index] = None\n",
|
" self.stack_array[array_index] = None\n",
|
||||||
" self.stack_pointers[stack_index] -= 1\n",
|
" self.stack_pointers[stack_index] -= 1\n",
|
||||||
|
@ -166,7 +165,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStacks(object):\n",
|
"class TestStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" @raises(Exception)\n",
|
" @raises(Exception)\n",
|
||||||
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
" def test_pop_on_empty(self, num_stacks, stack_size):\n",
|
||||||
" print('Test: Pop on empty stack')\n",
|
" print('Test: Pop on empty stack')\n",
|
||||||
|
@ -197,14 +196,16 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_stacks\\n')\n",
|
" print('Success: test_stacks\\n')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" num_stacks = 3\n",
|
" num_stacks = 3\n",
|
||||||
" stack_size = 100 \n",
|
" stack_size = 100\n",
|
||||||
" test = TestStacks()\n",
|
" test = TestStacks()\n",
|
||||||
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
" test.test_pop_on_empty(num_stacks, stack_size)\n",
|
||||||
" test.test_push_on_full(num_stacks, stack_size)\n",
|
" test.test_push_on_full(num_stacks, stack_size)\n",
|
||||||
" test.test_stacks(num_stacks, stack_size)\n",
|
" test.test_stacks(num_stacks, stack_size)\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -34,6 +34,7 @@ class TestStacks(object):
|
||||||
|
|
||||||
print('Success: test_stacks\n')
|
print('Success: test_stacks\n')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
num_stacks = 3
|
num_stacks = 3
|
||||||
stack_size = 100
|
stack_size = 100
|
||||||
|
@ -42,5 +43,6 @@ def main():
|
||||||
test.test_push_on_full(num_stacks, stack_size)
|
test.test_push_on_full(num_stacks, stack_size)
|
||||||
test.test_stacks(num_stacks, stack_size)
|
test.test_stacks(num_stacks, stack_size)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* Do you expect the methods to be enqueue and dequeue?\n",
|
"* Do you expect the methods to be enqueue and dequeue?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
|
@ -92,7 +91,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class QueueFromStacks(object):\n",
|
"class QueueFromStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -131,7 +130,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestQueueFromStacks(object):\n",
|
"class TestQueueFromStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_queue_from_stacks(self):\n",
|
" def test_queue_from_stacks(self):\n",
|
||||||
" print('Test: Dequeue on empty stack')\n",
|
" print('Test: Dequeue on empty stack')\n",
|
||||||
" queue = QueueFromStacks()\n",
|
" queue = QueueFromStacks()\n",
|
||||||
|
@ -141,7 +140,7 @@
|
||||||
" print('Test: Enqueue on non-empty stack')\n",
|
" print('Test: Enqueue on non-empty stack')\n",
|
||||||
" print('Test: Multiple enqueue in a row')\n",
|
" print('Test: Multiple enqueue in a row')\n",
|
||||||
" num_items = 3\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",
|
" queue.enqueue(i)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Dequeue on non-empty stack')\n",
|
" print('Test: Dequeue on non-empty stack')\n",
|
||||||
|
@ -158,10 +157,12 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_queue_from_stacks')\n",
|
" print('Success: test_queue_from_stacks')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQueueFromStacks()\n",
|
" test = TestQueueFromStacks()\n",
|
||||||
" test.test_queue_from_stacks()\n",
|
" test.test_queue_from_stacks()\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",
|
||||||
|
|
||||||
"* Do you expect the methods to be enqueue and dequeue?\n",
|
"* Do you expect the methods to be enqueue and dequeue?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
|
@ -121,7 +120,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class QueueFromStacks(object):\n",
|
"class QueueFromStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" self.left_stack = Stack()\n",
|
" self.left_stack = Stack()\n",
|
||||||
" self.right_stack = Stack()\n",
|
" self.right_stack = Stack()\n",
|
||||||
|
@ -170,7 +169,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestQueueFromStacks(object):\n",
|
"class TestQueueFromStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_queue_from_stacks(self):\n",
|
" def test_queue_from_stacks(self):\n",
|
||||||
" print('Test: Dequeue on empty stack')\n",
|
" print('Test: Dequeue on empty stack')\n",
|
||||||
" queue = QueueFromStacks()\n",
|
" queue = QueueFromStacks()\n",
|
||||||
|
@ -180,7 +179,7 @@
|
||||||
" print('Test: Enqueue on non-empty stack')\n",
|
" print('Test: Enqueue on non-empty stack')\n",
|
||||||
" print('Test: Multiple enqueue in a row')\n",
|
" print('Test: Multiple enqueue in a row')\n",
|
||||||
" num_items = 3\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",
|
" queue.enqueue(i)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Dequeue on non-empty stack')\n",
|
" print('Test: Dequeue on non-empty stack')\n",
|
||||||
|
@ -197,10 +196,12 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_queue_from_stacks')\n",
|
" print('Success: test_queue_from_stacks')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQueueFromStacks()\n",
|
" test = TestQueueFromStacks()\n",
|
||||||
" test.test_queue_from_stacks()\n",
|
" test.test_queue_from_stacks()\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 TestQueueFromStacks(object):
|
class TestQueueFromStacks(object):
|
||||||
|
|
||||||
def test_queue_from_stacks(self):
|
def test_queue_from_stacks(self):
|
||||||
print('Test: Dequeue on empty stack')
|
print('Test: Dequeue on empty stack')
|
||||||
queue = QueueFromStacks()
|
queue = QueueFromStacks()
|
||||||
|
@ -12,7 +12,7 @@ class TestQueueFromStacks(object):
|
||||||
print('Test: Enqueue on non-empty stack')
|
print('Test: Enqueue on non-empty stack')
|
||||||
print('Test: Multiple enqueue in a row')
|
print('Test: Multiple enqueue in a row')
|
||||||
num_items = 3
|
num_items = 3
|
||||||
for i in range (0, num_items):
|
for i in range(0, num_items):
|
||||||
queue.enqueue(i)
|
queue.enqueue(i)
|
||||||
|
|
||||||
print('Test: Dequeue on non-empty stack')
|
print('Test: Dequeue on non-empty stack')
|
||||||
|
@ -29,9 +29,11 @@ class TestQueueFromStacks(object):
|
||||||
|
|
||||||
print('Success: test_queue_from_stacks')
|
print('Success: test_queue_from_stacks')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestQueueFromStacks()
|
test = TestQueueFromStacks()
|
||||||
test.test_queue_from_stacks()
|
test.test_queue_from_stacks()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -1,11 +1,12 @@
|
||||||
class Node(object):
|
class Node(object):
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.next = None
|
self.next = None
|
||||||
|
|
||||||
|
|
||||||
class Queue(object):
|
class Queue(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.first = None
|
self.first = None
|
||||||
self.last = None
|
self.last = None
|
||||||
|
@ -23,7 +24,7 @@ class Queue(object):
|
||||||
# Empty list
|
# Empty list
|
||||||
if self.first is None and self.last is None:
|
if self.first is None and self.last is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Remove only element from a one element list
|
# Remove only element from a one element list
|
||||||
elif self.first == self.last:
|
elif self.first == self.last:
|
||||||
data = self.first.data
|
data = self.first.data
|
||||||
|
|
|
@ -35,7 +35,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\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",
|
" * Yes\n",
|
||||||
"* If there are no items on the list, do you expect the first and last pointers to be None?\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": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class Queue(object):\n",
|
"class Queue(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -131,7 +131,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestQueue(object):\n",
|
"class TestQueue(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # TODO: It would be better if we had unit tests for each\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",
|
" # method in addition to the following end-to-end test\n",
|
||||||
" def test_end_to_end(self):\n",
|
" def test_end_to_end(self):\n",
|
||||||
|
@ -154,13 +154,15 @@
|
||||||
" assert_equal(queue.dequeue(), 2)\n",
|
" assert_equal(queue.dequeue(), 2)\n",
|
||||||
" assert_equal(queue.dequeue(), 3)\n",
|
" assert_equal(queue.dequeue(), 3)\n",
|
||||||
" assert_equal(queue.dequeue(), 4)\n",
|
" assert_equal(queue.dequeue(), 4)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_end_to_end')\n",
|
" print('Success: test_end_to_end')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQueue()\n",
|
" test = TestQueue()\n",
|
||||||
" test.test_end_to_end()\n",
|
" test.test_end_to_end()\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\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",
|
" * Yes\n",
|
||||||
"* If there are no items on the list, do you expect the first and last pointers to be None?\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": [
|
"source": [
|
||||||
"%%writefile queue_list.py\n",
|
"%%writefile queue_list.py\n",
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
" self.data = data\n",
|
" self.data = data\n",
|
||||||
" self.next = None\n",
|
" self.next = None\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class Queue(object):\n",
|
"class Queue(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" self.first = None\n",
|
" self.first = None\n",
|
||||||
" self.last = None\n",
|
" self.last = None\n",
|
||||||
|
@ -142,7 +142,7 @@
|
||||||
" # Empty list\n",
|
" # Empty list\n",
|
||||||
" if self.first is None and self.last is None:\n",
|
" if self.first is None and self.last is None:\n",
|
||||||
" return None\n",
|
" return None\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # Remove only element from a one element list\n",
|
" # Remove only element from a one element list\n",
|
||||||
" elif self.first == self.last:\n",
|
" elif self.first == self.last:\n",
|
||||||
" data = self.first.data\n",
|
" data = self.first.data\n",
|
||||||
|
@ -195,7 +195,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestQueue(object):\n",
|
"class TestQueue(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # TODO: It would be better if we had unit tests for each\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",
|
" # method in addition to the following end-to-end test\n",
|
||||||
" def test_end_to_end(self):\n",
|
" def test_end_to_end(self):\n",
|
||||||
|
@ -218,13 +218,15 @@
|
||||||
" assert_equal(queue.dequeue(), 2)\n",
|
" assert_equal(queue.dequeue(), 2)\n",
|
||||||
" assert_equal(queue.dequeue(), 3)\n",
|
" assert_equal(queue.dequeue(), 3)\n",
|
||||||
" assert_equal(queue.dequeue(), 4)\n",
|
" assert_equal(queue.dequeue(), 4)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_end_to_end')\n",
|
" print('Success: test_end_to_end')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestQueue()\n",
|
" test = TestQueue()\n",
|
||||||
" test.test_end_to_end()\n",
|
" test.test_end_to_end()\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 TestQueue(object):
|
class TestQueue(object):
|
||||||
|
|
||||||
# TODO: It would be better if we had unit tests for each
|
# TODO: It would be better if we had unit tests for each
|
||||||
# method in addition to the following end-to-end test
|
# method in addition to the following end-to-end test
|
||||||
def test_end_to_end(self):
|
def test_end_to_end(self):
|
||||||
|
@ -25,12 +25,14 @@ class TestQueue(object):
|
||||||
assert_equal(queue.dequeue(), 2)
|
assert_equal(queue.dequeue(), 2)
|
||||||
assert_equal(queue.dequeue(), 3)
|
assert_equal(queue.dequeue(), 3)
|
||||||
assert_equal(queue.dequeue(), 4)
|
assert_equal(queue.dequeue(), 4)
|
||||||
|
|
||||||
print('Success: test_end_to_end')
|
print('Success: test_end_to_end')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestQueue()
|
test = TestQueue()
|
||||||
test.test_end_to_end()
|
test.test_end_to_end()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If a stack becomes full, we should automatically create one?\n",
|
"* If a stack becomes full, we should automatically create one?\n",
|
||||||
|
@ -92,7 +91,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class StackWithCapacity(Stack):\n",
|
"class StackWithCapacity(Stack):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, top=None, capacity=10):\n",
|
" def __init__(self, top=None, capacity=10):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -109,8 +108,9 @@
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class SetOfStacks(object):\n",
|
"class SetOfStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, capacity):\n",
|
" def __init__(self, capacity):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -148,7 +148,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSetOfStacks(object):\n",
|
"class TestSetOfStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_set_of_stacks(self):\n",
|
" def test_set_of_stacks(self):\n",
|
||||||
" print('Test: Push on an empty stack')\n",
|
" print('Test: Push on an empty stack')\n",
|
||||||
" capacity = 2\n",
|
" capacity = 2\n",
|
||||||
|
@ -170,13 +170,15 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Pop on no elements')\n",
|
" print('Test: Pop on no elements')\n",
|
||||||
" assert_equal(stacks.pop(), None)\n",
|
" assert_equal(stacks.pop(), None)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_set_of_stacks')\n",
|
" print('Success: test_set_of_stacks')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSetOfStacks()\n",
|
" test = TestSetOfStacks()\n",
|
||||||
" test.test_set_of_stacks()\n",
|
" test.test_set_of_stacks()\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",
|
||||||
|
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* If a stack becomes full, we should automatically create one?\n",
|
"* If a stack becomes full, we should automatically create one?\n",
|
||||||
|
@ -112,7 +111,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class StackWithCapacity(Stack):\n",
|
"class StackWithCapacity(Stack):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, top=None, capacity=10):\n",
|
" def __init__(self, top=None, capacity=10):\n",
|
||||||
" self.capacity = capacity\n",
|
" self.capacity = capacity\n",
|
||||||
" self.num_items = 0\n",
|
" self.num_items = 0\n",
|
||||||
|
@ -133,8 +132,9 @@
|
||||||
" def is_full(self):\n",
|
" def is_full(self):\n",
|
||||||
" return self.num_items == self.capacity\n",
|
" return self.num_items == self.capacity\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class SetOfStacks(object):\n",
|
"class SetOfStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, capacity):\n",
|
" def __init__(self, capacity):\n",
|
||||||
" self.capacity = capacity\n",
|
" self.capacity = capacity\n",
|
||||||
" self.stacks = []\n",
|
" self.stacks = []\n",
|
||||||
|
@ -191,7 +191,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSetOfStacks(object):\n",
|
"class TestSetOfStacks(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def test_set_of_stacks(self):\n",
|
" def test_set_of_stacks(self):\n",
|
||||||
" print('Test: Push on an empty stack')\n",
|
" print('Test: Push on an empty stack')\n",
|
||||||
" capacity = 2\n",
|
" capacity = 2\n",
|
||||||
|
@ -213,13 +213,15 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Pop on no elements')\n",
|
" print('Test: Pop on no elements')\n",
|
||||||
" assert_equal(stacks.pop(), None)\n",
|
" assert_equal(stacks.pop(), None)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_set_of_stacks')\n",
|
" print('Success: test_set_of_stacks')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSetOfStacks()\n",
|
" test = TestSetOfStacks()\n",
|
||||||
" test.test_set_of_stacks()\n",
|
" test.test_set_of_stacks()\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 TestSetOfStacks(object):
|
class TestSetOfStacks(object):
|
||||||
|
|
||||||
def test_set_of_stacks(self):
|
def test_set_of_stacks(self):
|
||||||
print('Test: Push on an empty stack')
|
print('Test: Push on an empty stack')
|
||||||
capacity = 2
|
capacity = 2
|
||||||
|
@ -24,12 +24,14 @@ class TestSetOfStacks(object):
|
||||||
|
|
||||||
print('Test: Pop on no elements')
|
print('Test: Pop on no elements')
|
||||||
assert_equal(stacks.pop(), None)
|
assert_equal(stacks.pop(), None)
|
||||||
|
|
||||||
print('Success: test_set_of_stacks')
|
print('Success: test_set_of_stacks')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestSetOfStacks()
|
test = TestSetOfStacks()
|
||||||
test.test_set_of_stacks()
|
test.test_set_of_stacks()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* When sorted, should the largest element be at the top or bottom?\n",
|
"* When sorted, should the largest element be at the top or bottom?\n",
|
||||||
" * Top\n",
|
" * Top\n",
|
||||||
"* Can you have duplicate values like 5, 5?\n",
|
"* Can you have duplicate values like 5, 5?\n",
|
||||||
|
@ -92,7 +91,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MyStack(Stack):\n",
|
"class MyStack(Stack):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def sort(self):\n",
|
" def sort(self):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass"
|
" pass"
|
||||||
|
@ -123,7 +122,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSortStack(object):\n",
|
"class TestSortStack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def get_sorted_stack(self, numbers):\n",
|
" def get_sorted_stack(self, numbers):\n",
|
||||||
" stack = MyStack()\n",
|
" stack = MyStack()\n",
|
||||||
" for x in numbers:\n",
|
" for x in numbers:\n",
|
||||||
|
@ -148,13 +147,15 @@
|
||||||
" for _ in range(num_items):\n",
|
" for _ in range(num_items):\n",
|
||||||
" sorted_numbers.append(sorted_stack.pop())\n",
|
" sorted_numbers.append(sorted_stack.pop())\n",
|
||||||
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_sort_stack')\n",
|
" print('Success: test_sort_stack')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSortStack()\n",
|
" test = TestSortStack()\n",
|
||||||
" test.test_sort_stack()\n",
|
" test.test_sort_stack()\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",
|
||||||
|
|
||||||
"* When sorted, should the largest element be at the top or bottom?\n",
|
"* When sorted, should the largest element be at the top or bottom?\n",
|
||||||
" * Top\n",
|
" * Top\n",
|
||||||
"* Can you have duplicate values like 5, 5?\n",
|
"* Can you have duplicate values like 5, 5?\n",
|
||||||
|
@ -101,7 +100,7 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MyStack(Stack):\n",
|
"class MyStack(Stack):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def sort(self):\n",
|
" def sort(self):\n",
|
||||||
" buff = MyStack()\n",
|
" buff = MyStack()\n",
|
||||||
" while not self.is_empty():\n",
|
" while not self.is_empty():\n",
|
||||||
|
@ -142,7 +141,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestSortStack(object):\n",
|
"class TestSortStack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def get_sorted_stack(self, numbers):\n",
|
" def get_sorted_stack(self, numbers):\n",
|
||||||
" stack = MyStack()\n",
|
" stack = MyStack()\n",
|
||||||
" for x in numbers:\n",
|
" for x in numbers:\n",
|
||||||
|
@ -167,13 +166,15 @@
|
||||||
" for _ in range(num_items):\n",
|
" for _ in range(num_items):\n",
|
||||||
" sorted_numbers.append(sorted_stack.pop())\n",
|
" sorted_numbers.append(sorted_stack.pop())\n",
|
||||||
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_sort_stack')\n",
|
" print('Success: test_sort_stack')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestSortStack()\n",
|
" test = TestSortStack()\n",
|
||||||
" test.test_sort_stack()\n",
|
" test.test_sort_stack()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -31,9 +31,11 @@ class TestSortStack(object):
|
||||||
|
|
||||||
print('Success: test_sort_stack')
|
print('Success: test_sort_stack')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestSortStack()
|
test = TestSortStack()
|
||||||
test.test_sort_stack()
|
test.test_sort_stack()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -1,11 +1,12 @@
|
||||||
class Node(object):
|
class Node(object):
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
self.next = None
|
self.next = None
|
||||||
|
|
||||||
|
|
||||||
class Stack(object):
|
class Stack(object):
|
||||||
|
|
||||||
def __init__(self, top=None):
|
def __init__(self, top=None):
|
||||||
self.top = top
|
self.top = top
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* None"
|
"* None"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -92,13 +91,14 @@
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class Stack(object):\n",
|
"class Stack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -144,7 +144,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStack(object):\n",
|
"class TestStack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # TODO: It would be better if we had unit tests for each\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",
|
" # method in addition to the following end-to-end test\n",
|
||||||
" def test_end_to_end(self):\n",
|
" def test_end_to_end(self):\n",
|
||||||
|
@ -172,13 +172,15 @@
|
||||||
" assert_equal(stack.pop(), 1)\n",
|
" assert_equal(stack.pop(), 1)\n",
|
||||||
" assert_equal(stack.peek(), None)\n",
|
" assert_equal(stack.peek(), None)\n",
|
||||||
" assert_equal(stack.is_empty(), True)\n",
|
" assert_equal(stack.is_empty(), True)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_end_to_end')\n",
|
" print('Success: test_end_to_end')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestStack()\n",
|
" test = TestStack()\n",
|
||||||
" test.test_end_to_end()\n",
|
" test.test_end_to_end()\n",
|
||||||
" \n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -34,7 +34,6 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
||||||
"* None"
|
"* None"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -137,13 +136,14 @@
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile stack.py\n",
|
"%%writefile stack.py\n",
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
" self.data = data\n",
|
" self.data = data\n",
|
||||||
" self.next = None\n",
|
" self.next = None\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"class Stack(object):\n",
|
"class Stack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
" self.top = top\n",
|
" self.top = top\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -208,7 +208,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStack(object):\n",
|
"class TestStack(object):\n",
|
||||||
" \n",
|
"\n",
|
||||||
" # TODO: It would be better if we had unit tests for each\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",
|
" # method in addition to the following end-to-end test\n",
|
||||||
" def test_end_to_end(self):\n",
|
" def test_end_to_end(self):\n",
|
||||||
|
@ -236,13 +236,15 @@
|
||||||
" assert_equal(stack.pop(), 1)\n",
|
" assert_equal(stack.pop(), 1)\n",
|
||||||
" assert_equal(stack.peek(), None)\n",
|
" assert_equal(stack.peek(), None)\n",
|
||||||
" assert_equal(stack.is_empty(), True)\n",
|
" assert_equal(stack.is_empty(), True)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_end_to_end')\n",
|
" print('Success: test_end_to_end')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestStack()\n",
|
" test = TestStack()\n",
|
||||||
" test.test_end_to_end()\n",
|
" test.test_end_to_end()\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 TestStack(object):
|
class TestStack(object):
|
||||||
|
|
||||||
# TODO: It would be better if we had unit tests for each
|
# TODO: It would be better if we had unit tests for each
|
||||||
# method in addition to the following end-to-end test
|
# method in addition to the following end-to-end test
|
||||||
def test_end_to_end(self):
|
def test_end_to_end(self):
|
||||||
|
@ -30,12 +30,14 @@ class TestStack(object):
|
||||||
assert_equal(stack.pop(), 1)
|
assert_equal(stack.pop(), 1)
|
||||||
assert_equal(stack.peek(), None)
|
assert_equal(stack.peek(), None)
|
||||||
assert_equal(stack.is_empty(), True)
|
assert_equal(stack.is_empty(), True)
|
||||||
|
|
||||||
print('Success: test_end_to_end')
|
print('Success: test_end_to_end')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestStack()
|
test = TestStack()
|
||||||
test.test_end_to_end()
|
test.test_end_to_end()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
|
@ -92,6 +92,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class MyStack(Stack):\n",
|
"class MyStack(Stack):\n",
|
||||||
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" pass\n",
|
" pass\n",
|
||||||
|
@ -133,6 +134,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStackMin(object):\n",
|
"class TestStackMin(object):\n",
|
||||||
|
"\n",
|
||||||
" def test_stack_min(self):\n",
|
" def test_stack_min(self):\n",
|
||||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||||
" stack = MyStack()\n",
|
" stack = MyStack()\n",
|
||||||
|
@ -161,13 +163,15 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Pop empty stack')\n",
|
" print('Test: Pop empty stack')\n",
|
||||||
" assert_equal(stack.pop(), None)\n",
|
" assert_equal(stack.pop(), None)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_stack_min')\n",
|
" print('Success: test_stack_min')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestStackMin()\n",
|
" test = TestStackMin()\n",
|
||||||
" test.test_stack_min()\n",
|
" test.test_stack_min()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -120,6 +120,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class MyStack(Stack):\n",
|
"class MyStack(Stack):\n",
|
||||||
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
" self.min_vals = Stack()\n",
|
" self.min_vals = Stack()\n",
|
||||||
" super(MyStack, self).__init__(top)\n",
|
" super(MyStack, self).__init__(top)\n",
|
||||||
|
@ -171,6 +172,7 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class TestStackMin(object):\n",
|
"class TestStackMin(object):\n",
|
||||||
|
"\n",
|
||||||
" def test_stack_min(self):\n",
|
" def test_stack_min(self):\n",
|
||||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||||
" stack = MyStack()\n",
|
" stack = MyStack()\n",
|
||||||
|
@ -199,13 +201,15 @@
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Pop empty stack')\n",
|
" print('Test: Pop empty stack')\n",
|
||||||
" assert_equal(stack.pop(), None)\n",
|
" assert_equal(stack.pop(), None)\n",
|
||||||
" \n",
|
"\n",
|
||||||
" print('Success: test_stack_min')\n",
|
" print('Success: test_stack_min')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def main():\n",
|
"def main():\n",
|
||||||
" test = TestStackMin()\n",
|
" test = TestStackMin()\n",
|
||||||
" test.test_stack_min()\n",
|
" test.test_stack_min()\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" main()"
|
" main()"
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,6 +2,7 @@ from nose.tools import assert_equal
|
||||||
|
|
||||||
|
|
||||||
class TestStackMin(object):
|
class TestStackMin(object):
|
||||||
|
|
||||||
def test_stack_min(self):
|
def test_stack_min(self):
|
||||||
print('Test: Push on empty stack, non-empty stack')
|
print('Test: Push on empty stack, non-empty stack')
|
||||||
stack = MyStack()
|
stack = MyStack()
|
||||||
|
@ -30,12 +31,14 @@ class TestStackMin(object):
|
||||||
|
|
||||||
print('Test: Pop empty stack')
|
print('Test: Pop empty stack')
|
||||||
assert_equal(stack.pop(), None)
|
assert_equal(stack.pop(), None)
|
||||||
|
|
||||||
print('Success: test_stack_min')
|
print('Success: test_stack_min')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test = TestStackMin()
|
test = TestStackMin()
|
||||||
test.test_stack_min()
|
test.test_stack_min()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user