2015-07-03 11:04:15 +08:00
{
" cells " : [
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-07-05 19:59:00 +08:00
" <small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small> "
2015-07-03 11:04:15 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Challenge Notebook "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Problem: Implement n stacks using a single array. \n " ,
" \n " ,
" * [Constraints](#Constraints) \n " ,
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) \n " ,
" * [Solution Notebook](#Solution-Notebook) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Constraints \n " ,
" \n " ,
" * 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 " ,
2015-07-03 11:04:15 +08:00
" * Yes "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" * Test the following on the three stacks: \n " ,
" * Push to full stack -> Exception \n " ,
" * Push to non-full stack \n " ,
" * Pop on empty stack -> Exception \n " ,
" * Pop on non-empty stack "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
2015-07-05 19:59:00 +08:00
" Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/stacks_queues/n_stacks/n_stacks_solution.ipynb). If you are stuck and need a hint, the solution notebook ' s algorithm discussion might be a good place to start. "
2015-07-03 11:04:15 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" class Stacks(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-07-03 11:04:15 +08:00
" def __init__(self, num_stacks, stack_size): \n " ,
" # TODO: Implement me \n " ,
" pass \n " ,
" \n " ,
" def abs_index(self, stack_index): \n " ,
" # TODO: Implement me \n " ,
" pass \n " ,
" \n " ,
" def push(self, stack_index, data): \n " ,
" # TODO: Implement me \n " ,
" pass \n " ,
" \n " ,
" def pop(self, stack_index): \n " ,
" # TODO: Implement me \n " ,
" pass "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test \n " ,
" \n " ,
" \n " ,
" \n " ,
" **The following unit test is expected to fail until you solve the challenge.** "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : null ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
" # %lo ad test_n_stacks.py \n " ,
" from nose.tools import assert_equal \n " ,
" from nose.tools import raises \n " ,
" \n " ,
" \n " ,
" class TestStacks(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-07-03 11:04:15 +08:00
" @raises(Exception) \n " ,
" 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 " ,
" @raises(Exception) \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-07-03 11:04:15 +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 " ,
" assert_equal(stacks.pop(0), 2) \n " ,
" assert_equal(stacks.pop(0), 1) \n " ,
" assert_equal(stacks.pop(1), 3) \n " ,
" assert_equal(stacks.pop(2), 4) \n " ,
" \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 " ,
" 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 " ,
" 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 " ,
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 " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Solution Notebook \n " ,
" \n " ,
2015-07-05 19:59:00 +08:00
" Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/stacks_queues/n_stacks/n_stacks_solution.ipynb) for a discussion on algorithms and code solutions. "
2015-07-03 11:04:15 +08:00
]
}
] ,
" metadata " : {
" kernelspec " : {
2016-06-20 09:00:44 +08:00
" display_name " : " Python 3 " ,
2015-07-03 11:04:15 +08:00
" language " : " python " ,
2016-06-20 09:00:44 +08:00
" name " : " python3 "
2015-07-03 11:04:15 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-06-20 09:00:44 +08:00
" version " : 3
2015-07-03 11:04:15 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-06-20 09:00:44 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
2015-07-03 11:04:15 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}