2015-05-21 17:58:08 +08:00
{
" cells " : [
2015-06-18 04:38:39 +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-02 17:57:29 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
2015-06-18 04:38:39 +08:00
]
} ,
2015-05-21 17:58:08 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Problem: Utopian Tree \n " ,
" \n " ,
2015-07-02 17:57:29 +08:00
" See the [HackerRank problem page](https://www.hackerrank.com/challenges/utopian-tree). \n " ,
2015-05-21 17:58:08 +08:00
" \n " ,
2015-07-02 17:57:29 +08:00
" * [Constraints](#Constraints) \n " ,
" * [Test Cases](#Test-Cases) \n " ,
2015-05-21 17:58:08 +08:00
" * [Algorithm](#Algorithm) \n " ,
2015-07-02 17:57:29 +08:00
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Constraints \n " ,
" \n " ,
" See the [HackerRank problem page](https://www.hackerrank.com/challenges/utopian-tree). "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" See the [HackerRank problem page](https://www.hackerrank.com/challenges/utopian-tree). "
2015-05-21 17:58:08 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
" * If cycles is 0, return height of 1 \n " ,
" * For 1 to cycles: \n " ,
" * If cycle is odd, double height \n " ,
" * Else, increment height \n " ,
" * Return height \n " ,
" \n " ,
" Complexity: \n " ,
2015-07-06 17:45:44 +08:00
" * Time: O(n) \n " ,
2015-05-21 17:58:08 +08:00
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
2015-07-02 17:57:29 +08:00
" execution_count " : 1 ,
2015-05-21 17:58:08 +08:00
" metadata " : {
" collapsed " : false
} ,
" outputs " : [ ] ,
" source " : [
2017-02-06 01:15:53 +08:00
" class Solution(object): \n " ,
" \n " ,
" def calc_utopian_tree_height(self, cycles): \n " ,
" height = 1 \n " ,
" if cycles == 0: \n " ,
" return height \n " ,
" for i in range(1, cycles+1): \n " ,
" if i % 2 == 1: \n " ,
" height *= 2 \n " ,
" else: \n " ,
" height += 1 \n " ,
" return height "
2015-07-02 17:57:29 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test \n " ,
" \n "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 2 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Overwriting test_utopian_tree.py \n "
]
}
] ,
" source " : [
" %% writefile test_utopian_tree.py \n " ,
" from nose.tools import assert_equal \n " ,
" \n " ,
2015-05-21 17:58:08 +08:00
" \n " ,
2015-07-02 17:57:29 +08:00
" class TestUtopianTree(object): \n " ,
" \n " ,
" def test_utopian_tree(self): \n " ,
2017-02-06 01:15:53 +08:00
" solution = Solution() \n " ,
" assert_equal(solution.calc_utopian_tree_height(0), 1) \n " ,
" assert_equal(solution.calc_utopian_tree_height(1), 2) \n " ,
" assert_equal(solution.calc_utopian_tree_height(4), 7) \n " ,
2015-07-02 17:57:29 +08:00
" print( ' Success: test_utopian_tree ' ) \n " ,
" \n " ,
2015-07-12 03:35:22 +08:00
" \n " ,
2015-07-02 17:57:29 +08:00
" def main(): \n " ,
" test = TestUtopianTree() \n " ,
" test.test_utopian_tree() \n " ,
" \n " ,
2015-07-12 03:35:22 +08:00
" \n " ,
2015-07-02 17:57:29 +08:00
" if __name__ == ' __main__ ' : \n " ,
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 3 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Success: test_utopian_tree \n "
]
}
] ,
" source " : [
" run -i test_utopian_tree.py "
2015-05-21 17:58:08 +08:00
]
}
] ,
" metadata " : {
" kernelspec " : {
2017-02-06 01:15:53 +08:00
" display_name " : " Python 3 " ,
2015-05-21 17:58:08 +08:00
" language " : " python " ,
2017-02-06 01:15:53 +08:00
" name " : " python3 "
2015-05-21 17:58:08 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2017-02-06 01:15:53 +08:00
" version " : 3
2015-05-21 17:58:08 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2017-02-06 01:15:53 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
2015-05-21 17:58:08 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}