From 73d265525bd4fea84a353a3ebbeb5d919d8d61bf Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Thu, 21 May 2015 06:05:32 -0400 Subject: [PATCH] Added notebook solving the following: HackerRank Maximizing XOR problem.. --- README.md | 1 + hacker-rank/maximizing-xor.ipynb | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 hacker-rank/maximizing-xor.ipynb diff --git a/README.md b/README.md index 94fd5d8..76bad2f 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Continually updated IPython Notebooks containing coding problems and solutions ( ## Hacker Rank * [Utopian Tree](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/hacker-rank/utopian-tree.ipynb) +* [Maximizing XOR](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/hacker-rank/maximizing-xor.ipynb) ## License diff --git a/hacker-rank/maximizing-xor.ipynb b/hacker-rank/maximizing-xor.ipynb new file mode 100644 index 0000000..2ef336a --- /dev/null +++ b/hacker-rank/maximizing-xor.ipynb @@ -0,0 +1,85 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Maximizing XOR\n", + "\n", + "From HackerRank: https://www.hackerrank.com/challenges/maximizing-xor\n", + "\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "* Set max to 0\n", + "* For i in the range (lower, upper) inclusive\n", + " * For j in the range (lower, upper) inclusive\n", + " * Compare i ^ j with max, update max if needed\n", + "* return max\n", + "\n", + "Complexity:\n", + "* Time: O(n^2)\n", + "* Space: O(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def max_xor(lower, upper):\n", + " result = 0\n", + " for l in xrange(lower, upper + 1):\n", + " for u in xrange(lower, upper + 1):\n", + " curr = l ^ u\n", + " if result < curr:\n", + " result = curr\n", + " return result\n", + "\n", + "lower = int(raw_input());\n", + "upper = int(raw_input());\n", + "\n", + "result = max_xor(lower, upper);\n", + "print(result)" + ] + } + ], + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}