diff --git a/hackerrank_topcoder/maximizing_xor/__init__.py b/hackerrank_topcoder/maximizing_xor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hackerrank_topcoder/maximizing_xor/maximizing_xor.ipynb b/hackerrank_topcoder/maximizing_xor/maximizing_xor.ipynb index 58e6e1f..7fd4b71 100644 --- a/hackerrank_topcoder/maximizing_xor/maximizing_xor.ipynb +++ b/hackerrank_topcoder/maximizing_xor/maximizing_xor.ipynb @@ -4,7 +4,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes)." + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" ] }, { @@ -13,10 +20,33 @@ "source": [ "## Problem: Maximizing XOR\n", "\n", - "From HackerRank: https://www.hackerrank.com/challenges/maximizing-xor\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor).\n", "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", - "* [Code](#Code)" + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", + "\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor)." ] }, { @@ -45,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -58,13 +88,68 @@ " curr = l ^ u\n", " if result < curr:\n", " result = curr\n", - " return result\n", + " return result" + ] + }, + { + "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_maximizing_xor.py\n" + ] + } + ], + "source": [ + "%%writefile test_maximizing_xor.py\n", + "from nose.tools import assert_equal\n", "\n", - "lower = int(raw_input());\n", - "upper = int(raw_input());\n", "\n", - "result = max_xor(lower, upper);\n", - "print(result)" + "class TestMaximingXor(object):\n", + "\n", + " def test_maximizing_xor(self):\n", + " assert_equal(max_xor(10, 15), 7)\n", + " print('Success: test_maximizing_xor')\n", + "\n", + "def main():\n", + " test = TestMaximingXor()\n", + " test.test_maximizing_xor()\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_maximizing_xor\n" + ] + } + ], + "source": [ + "%run -i test_maximizing_xor.py" ] } ], diff --git a/hackerrank_topcoder/maximizing_xor/maximizing_xor_challenge.ipynb b/hackerrank_topcoder/maximizing_xor/maximizing_xor_challenge.ipynb new file mode 100644 index 0000000..9fd56f4 --- /dev/null +++ b/hackerrank_topcoder/maximizing_xor/maximizing_xor_challenge.ipynb @@ -0,0 +1,148 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Maximizing XOR\n", + "\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor).\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", + "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", + "\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "See the [HackerRank problem page](https://www.hackerrank.com/challenges/maximizing-xor)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/hackerrank_topcoder/utopian_tree/maximizing_xor_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def max_xor(lower, upper):\n", + " # TODO: Implement me\n", + " pass" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\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": [ + "# %load test_maximizing_xor.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestMaximingXor(object):\n", + "\n", + " def test_maximizing_xor(self):\n", + " assert_equal(max_xor(10, 15), 7)\n", + " print('Success: test_maximizing_xor')\n", + "\n", + "def main():\n", + " test = TestMaximingXor()\n", + " test.test_maximizing_xor()\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/coding-challenges/blob/master/hackerrank_topcoder/utopian_tree/maximizing_xor_solution.ipynb) for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/hackerrank_topcoder/maximizing_xor/test_maximizing_xor.py b/hackerrank_topcoder/maximizing_xor/test_maximizing_xor.py new file mode 100644 index 0000000..5be8601 --- /dev/null +++ b/hackerrank_topcoder/maximizing_xor/test_maximizing_xor.py @@ -0,0 +1,15 @@ +from nose.tools import assert_equal + + +class TestMaximingXor(object): + + def test_maximizing_xor(self): + assert_equal(max_xor(10, 15), 7) + print('Success: test_maximizing_xor') + +def main(): + test = TestMaximingXor() + test.test_maximizing_xor() + +if __name__ == '__main__': + main() \ No newline at end of file