mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-08 10:28:03 +00:00
Renamed top-level folders to use underscores instead of dashes.
This commit is contained in:
0
hackerrank_topcoder/__init__.py
Normal file
0
hackerrank_topcoder/__init__.py
Normal file
92
hackerrank_topcoder/maximizing-xor.ipynb
Normal file
92
hackerrank_topcoder/maximizing-xor.ipynb
Normal file
@@ -0,0 +1,92 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
0
hackerrank_topcoder/tests/__init__.py
Normal file
0
hackerrank_topcoder/tests/__init__.py
Normal file
98
hackerrank_topcoder/utopian-tree.ipynb
Normal file
98
hackerrank_topcoder/utopian-tree.ipynb
Normal file
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Utopian Tree\n",
|
||||
"\n",
|
||||
"From HackerRank: https://www.hackerrank.com/challenges/utopian-tree\n",
|
||||
"\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"* If cycles is 0, return height of 1\n",
|
||||
"* For 1 to cycles:\n",
|
||||
" * If cycle is odd, double height\n",
|
||||
" * Else, increment height\n",
|
||||
"* Return height\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n), where n is the number of cycles, for each cycle we perform a calculation\n",
|
||||
"* Space: O(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# cycles = 0, print 1: base case\n",
|
||||
"# cycles = 1, print 2: i = 1: 1 * 2\n",
|
||||
"# cycles = 4, print 7: i = 1: 1 * 2, i = 2: 2 + 1, i = 3: 3 * 2, i = 4: 6 + 1\n",
|
||||
"def calc_utopian_tree_height(cycles):\n",
|
||||
" height = 1\n",
|
||||
" if cycles == 0:\n",
|
||||
" return height\n",
|
||||
" for i in xrange(1, cycles+1):\n",
|
||||
" if i % 2 == 1:\n",
|
||||
" height *= 2\n",
|
||||
" else:\n",
|
||||
" height += 1\n",
|
||||
" return height\n",
|
||||
"\n",
|
||||
"num_lines = int(raw_input())\n",
|
||||
"for i in range(0, num_lines):\n",
|
||||
" cycles = raw_input()\n",
|
||||
" cycles = int(cycles)\n",
|
||||
" res = calc_utopian_tree_height(cycles)\n",
|
||||
" print res"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user