From cf48629858e7c6bff93d3a205481aaefc620bcda Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 4 Feb 2017 19:07:06 -0500 Subject: [PATCH] Move xor challenge solution to a class (#143) --- .../maximizing_xor_challenge.ipynb | 21 +++++++------ .../maximizing_xor_solution.ipynb | 31 ++++++++++--------- .../maximizing_xor/test_maximizing_xor.py | 3 +- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/online_judges/maximizing_xor/maximizing_xor_challenge.ipynb b/online_judges/maximizing_xor/maximizing_xor_challenge.ipynb index f00d63a..1364e05 100644 --- a/online_judges/maximizing_xor/maximizing_xor_challenge.ipynb +++ b/online_judges/maximizing_xor/maximizing_xor_challenge.ipynb @@ -72,9 +72,11 @@ }, "outputs": [], "source": [ - "def max_xor(lower, upper):\n", - " # TODO: Implement me\n", - " pass" + "class Solution(object):\n", + "\n", + " def max_xor(self, lower, upper):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -101,7 +103,8 @@ "class TestMaximingXor(object):\n", "\n", " def test_maximizing_xor(self):\n", - " assert_equal(max_xor(10, 15), 7)\n", + " solution = Solution()\n", + " assert_equal(solution.max_xor(10, 15), 7)\n", " print('Success: test_maximizing_xor')\n", "\n", "\n", @@ -126,21 +129,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, diff --git a/online_judges/maximizing_xor/maximizing_xor_solution.ipynb b/online_judges/maximizing_xor/maximizing_xor_solution.ipynb index af3da14..c9487a3 100644 --- a/online_judges/maximizing_xor/maximizing_xor_solution.ipynb +++ b/online_judges/maximizing_xor/maximizing_xor_solution.ipynb @@ -82,14 +82,16 @@ }, "outputs": [], "source": [ - "def max_xor(lower, upper):\n", - " result = 0\n", - " for l in range(lower, upper + 1):\n", - " for u in range(lower, upper + 1):\n", - " curr = l ^ u\n", - " if result < curr:\n", - " result = curr\n", - " return result" + "class Solution(object):\n", + "\n", + " def max_xor(self, lower, upper):\n", + " result = 0\n", + " for l in range(lower, upper + 1):\n", + " for u in range(lower, upper + 1):\n", + " curr = l ^ u\n", + " if result < curr:\n", + " result = curr\n", + " return result" ] }, { @@ -123,7 +125,8 @@ "class TestMaximingXor(object):\n", "\n", " def test_maximizing_xor(self):\n", - " assert_equal(max_xor(10, 15), 7)\n", + " solution = Solution()\n", + " assert_equal(solution.max_xor(10, 15), 7)\n", " print('Success: test_maximizing_xor')\n", "\n", "\n", @@ -158,21 +161,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, diff --git a/online_judges/maximizing_xor/test_maximizing_xor.py b/online_judges/maximizing_xor/test_maximizing_xor.py index c165070..4c5abc1 100644 --- a/online_judges/maximizing_xor/test_maximizing_xor.py +++ b/online_judges/maximizing_xor/test_maximizing_xor.py @@ -4,7 +4,8 @@ from nose.tools import assert_equal class TestMaximingXor(object): def test_maximizing_xor(self): - assert_equal(max_xor(10, 15), 7) + solution = Solution() + assert_equal(solution.max_xor(10, 15), 7) print('Success: test_maximizing_xor')