2015-05-16 20:46:58 +08:00
{
" cells " : [
2015-06-18 04:39:43 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2016-07-31 20:11:18 +08:00
" This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). "
2015-07-03 11:04:15 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
2015-06-18 04:39:43 +08:00
]
} ,
2015-05-16 20:46:58 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Problem: Implement n stacks using a single array. \n " ,
" \n " ,
2015-06-30 17:55:58 +08:00
" * [Constraints](#Constraints) \n " ,
2015-05-16 20:46:58 +08:00
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
2015-06-27 18:32:01 +08:00
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) "
2015-05-16 20:46:58 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-06-28 19:13:27 +08:00
" ## Constraints \n " ,
2015-06-27 18:32:01 +08:00
" \n " ,
2015-05-16 20:46:58 +08:00
" * Are the stacks and array a fixed size? \n " ,
2016-06-20 09:00:44 +08:00
" * Yes \n " ,
" * Are the stacks equally sized? \n " ,
" * Yes \n " ,
" * Does pushing to a full stack result in an exception? \n " ,
" * Yes \n " ,
" * Does popping from an empty stack result in an exception? \n " ,
" * Yes \n " ,
" * Can we assume the user passed in stack index is valid? \n " ,
2016-10-29 21:13:02 +08:00
" * Yes \n " ,
" * Can we assume this fits memory? \n " ,
2015-05-16 20:46:58 +08:00
" * Yes "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" * Test the following on the three stacks: \n " ,
2015-07-03 11:04:15 +08:00
" * Push to full stack -> Exception \n " ,
2015-05-16 20:46:58 +08:00
" * Push to non-full stack \n " ,
2015-07-03 11:04:15 +08:00
" * Pop on empty stack -> Exception \n " ,
2015-05-16 20:46:58 +08:00
" * Pop on non-empty stack "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
2015-06-27 18:32:01 +08:00
" ### Absolute Index \n " ,
" \n " ,
" * return stack size * stack index + stack pointer \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
2015-05-16 20:46:58 +08:00
" \n " ,
" ### Push \n " ,
" \n " ,
" * If stack is full, throw exception \n " ,
" * Else \n " ,
" * Increment stack pointer \n " ,
" * Get the absolute array index \n " ,
" * Insert the value to this index \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
" ### Pop \n " ,
" \n " ,
" * If stack is empty, throw exception \n " ,
" * Else \n " ,
" * Store the value contained in the absolute array index \n " ,
" * Set the value in the absolute array index to None \n " ,
" * Decrement stack pointer \n " ,
" * return value \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
2015-06-27 18:32:01 +08:00
" execution_count " : 1 ,
2020-07-09 07:57:55 +08:00
" metadata " : { } ,
2015-05-16 20:46:58 +08:00
" outputs " : [ ] ,
" source " : [
" class Stacks(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-05-16 20:46:58 +08:00
" def __init__(self, num_stacks, stack_size): \n " ,
" self.num_stacks = num_stacks \n " ,
" self.stack_size = stack_size \n " ,
2016-10-29 21:13:02 +08:00
" self.stack_pointers = [-1] * self.num_stacks \n " ,
" self.stack_array = [None] * self.num_stacks * self.stack_size \n " ,
2015-05-16 20:46:58 +08:00
" \n " ,
" def abs_index(self, stack_index): \n " ,
" return stack_index * self.stack_size + self.stack_pointers[stack_index] \n " ,
" \n " ,
" def push(self, stack_index, data): \n " ,
" if self.stack_pointers[stack_index] == self.stack_size - 1: \n " ,
" raise Exception( ' Stack is full ' ) \n " ,
2016-02-22 05:20:29 +08:00
" self.stack_pointers[stack_index] += 1 \n " ,
" array_index = self.abs_index(stack_index) \n " ,
" self.stack_array[array_index] = data \n " ,
2015-05-16 20:46:58 +08:00
" \n " ,
" def pop(self, stack_index): \n " ,
" if self.stack_pointers[stack_index] == -1: \n " ,
" raise Exception( ' Stack is empty ' ) \n " ,
2016-02-22 05:20:29 +08:00
" 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 " ,
" return data "
2015-05-16 20:46:58 +08:00
]
} ,
2015-06-27 18:32:01 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test \n " ,
2015-07-03 11:04:15 +08:00
" \n "
2015-06-27 18:32:01 +08:00
]
} ,
2015-05-16 20:46:58 +08:00
{
" cell_type " : " code " ,
2015-06-27 18:32:01 +08:00
" execution_count " : 2 ,
2020-07-09 07:57:55 +08:00
" metadata " : { } ,
2015-06-27 18:32:01 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2015-07-03 11:04:15 +08:00
" Overwriting test_n_stacks.py \n "
2015-06-27 18:32:01 +08:00
]
}
] ,
2015-05-16 20:46:58 +08:00
" source " : [
2015-07-03 11:04:15 +08:00
" %% writefile test_n_stacks.py \n " ,
2020-07-09 07:57:55 +08:00
" import unittest \n " ,
2015-06-27 18:32:01 +08:00
" \n " ,
2015-07-03 11:04:15 +08:00
" \n " ,
2020-07-09 07:57:55 +08:00
" class TestStacks(unittest.TestCase): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:32:01 +08:00
" def test_pop_on_empty(self, num_stacks, stack_size): \n " ,
" print( ' Test: Pop on empty stack ' ) \n " ,
" stacks = Stacks(num_stacks, stack_size) \n " ,
" stacks.pop(0) \n " ,
" \n " ,
" def test_push_on_full(self, num_stacks, stack_size): \n " ,
" print( ' Test: Push to full stack ' ) \n " ,
" stacks = Stacks(num_stacks, stack_size) \n " ,
2015-07-06 18:15:53 +08:00
" for i in range(0, stack_size): \n " ,
2015-06-27 18:32:01 +08:00
" stacks.push(2, i) \n " ,
" stacks.push(2, stack_size) \n " ,
" \n " ,
" def test_stacks(self, num_stacks, stack_size): \n " ,
" print( ' Test: Push to non-full stack ' ) \n " ,
" stacks = Stacks(num_stacks, stack_size) \n " ,
" stacks.push(0, 1) \n " ,
" stacks.push(0, 2) \n " ,
" stacks.push(1, 3) \n " ,
" stacks.push(2, 4) \n " ,
" \n " ,
" print( ' Test: Pop on non-empty stack ' ) \n " ,
2020-07-09 07:57:55 +08:00
" self.assertEqual(stacks.pop(0), 2) \n " ,
" self.assertEqual(stacks.pop(0), 1) \n " ,
" self.assertEqual(stacks.pop(1), 3) \n " ,
" self.assertEqual(stacks.pop(2), 4) \n " ,
2015-06-27 18:32:01 +08:00
" \n " ,
" print( ' Success: test_stacks \\ n ' ) \n " ,
" \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-07-03 11:04:15 +08:00
" def main(): \n " ,
2015-06-27 18:32:01 +08:00
" num_stacks = 3 \n " ,
2015-07-12 03:39:59 +08:00
" stack_size = 100 \n " ,
2015-07-03 11:04:15 +08:00
" test = TestStacks() \n " ,
2020-07-09 07:57:55 +08:00
" test.assertRaises(Exception, test.test_pop_on_empty, num_stacks, \n " ,
" stack_size) \n " ,
" test.assertRaises(Exception, test.test_push_on_full, num_stacks, \n " ,
" stack_size) \n " ,
2015-07-03 11:04:15 +08:00
" test.test_stacks(num_stacks, stack_size) \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
" \n " ,
2015-07-03 11:04:15 +08:00
" if __name__ == ' __main__ ' : \n " ,
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
2020-07-09 07:57:55 +08:00
" metadata " : { } ,
2015-07-03 11:04:15 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Test: Pop on empty stack \n " ,
" Test: Push to full stack \n " ,
" Test: Push to non-full stack \n " ,
" Test: Pop on non-empty stack \n " ,
" Success: test_stacks \n " ,
" \n "
]
}
] ,
" source " : [
" run -i test_n_stacks.py "
2015-05-16 20:46:58 +08:00
]
2020-07-09 07:57:55 +08:00
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
" metadata " : { } ,
" outputs " : [ ] ,
" source " : [ ]
2015-05-16 20:46:58 +08:00
}
] ,
" metadata " : {
" kernelspec " : {
2016-02-22 05:20:29 +08:00
" display_name " : " Python 3 " ,
2015-05-16 20:46:58 +08:00
" language " : " python " ,
2016-02-22 05:20:29 +08:00
" name " : " python3 "
2015-05-16 20:46:58 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-02-22 05:20:29 +08:00
" version " : 3
2015-05-16 20:46:58 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-02-22 05:20:29 +08:00
" pygments_lexer " : " ipython3 " ,
2020-07-09 07:57:55 +08:00
" version " : " 3.7.2 "
2015-05-16 20:46:58 +08:00
}
} ,
" nbformat " : 4 ,
2020-07-09 07:57:55 +08:00
" nbformat_minor " : 1
2015-05-16 20:46:58 +08:00
}