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): class TestUtopianTree(object):
def test_utopian_tree(self): def test_utopian_tree(self):
assert_equal(calc_utopian_tree_height(0), 1) solution = Solution()
assert_equal(calc_utopian_tree_height(1), 2) assert_equal(solution.calc_utopian_tree_height(0), 1)
assert_equal(calc_utopian_tree_height(4), 7) assert_equal(solution.calc_utopian_tree_height(1), 2)
assert_equal(solution.calc_utopian_tree_height(4), 7)
print('Success: test_utopian_tree') print('Success: test_utopian_tree')

View File

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

View File

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