Move utopian tree challenge solution to a class (#144)

This commit is contained in:
Donne Martin
2017-02-05 12:15:53 -05:00
committed by GitHub
parent cf48629858
commit 2f2e25c33d
3 changed files with 39 additions and 32 deletions

View File

@@ -4,9 +4,10 @@ from nose.tools import assert_equal
class TestUtopianTree(object):
def test_utopian_tree(self):
assert_equal(calc_utopian_tree_height(0), 1)
assert_equal(calc_utopian_tree_height(1), 2)
assert_equal(calc_utopian_tree_height(4), 7)
solution = Solution()
assert_equal(solution.calc_utopian_tree_height(0), 1)
assert_equal(solution.calc_utopian_tree_height(1), 2)
assert_equal(solution.calc_utopian_tree_height(4), 7)
print('Success: test_utopian_tree')

View File

@@ -72,9 +72,11 @@
},
"outputs": [],
"source": [
"def calc_utopian_tree_height(cycles):\n",
" # TODO: Implement me\n",
" pass"
"class Solution(object):\n",
"\n",
" def calc_utopian_tree_height(self, cycles):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
@@ -101,9 +103,10 @@
"class TestUtopianTree(object):\n",
"\n",
" def test_utopian_tree(self):\n",
" assert_equal(calc_utopian_tree_height(0), 1)\n",
" assert_equal(calc_utopian_tree_height(1), 2)\n",
" assert_equal(calc_utopian_tree_height(4), 7)\n",
" 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",
" print('Success: test_utopian_tree')\n",
"\n",
"\n",
@@ -128,21 +131,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@@ -79,16 +79,18 @@
},
"outputs": [],
"source": [
"def calc_utopian_tree_height(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"
"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"
]
},
{
@@ -122,9 +124,10 @@
"class TestUtopianTree(object):\n",
"\n",
" def test_utopian_tree(self):\n",
" assert_equal(calc_utopian_tree_height(0), 1)\n",
" assert_equal(calc_utopian_tree_height(1), 2)\n",
" assert_equal(calc_utopian_tree_height(4), 7)\n",
" 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",
" print('Success: test_utopian_tree')\n",
"\n",
"\n",
@@ -159,21 +162,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,