Add insert m into n challenge

This commit is contained in:
Donne Martin 2017-03-30 05:48:26 -04:00
parent 470b38cdc4
commit b1ea49f0c8
4 changed files with 415 additions and 0 deletions

View File

@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Given two 16 bit numbers, n and m, and two indices i, j, insert m into n such that m starts at bit j and ends at bit i.\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",
"* Can we assume j > i?\n",
" * Yes\n",
"* Can we assume i through j have enough space for m?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None as an input -> Exception\n",
"* Negative index for i or j -> Exception\n",
"* General case\n",
"<pre>\n",
"i = 2\n",
"j = 6\n",
"n = 0000 0100 0000 0000\n",
"m = 0000 0000 0001 0011\n",
"result = 0000 0100 0100 1100\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](). 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": [
"class Bits(object):\n",
"\n",
" def insert_m_into_n(self, m, n, i, j):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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_insert_m_into_n.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestBit(object):\n",
"\n",
" def test_insert_m_into_n(self):\n",
" n = int('0000010000111101', base=2)\n",
" m = int('0000000000010011', base=2)\n",
" expected = int('0000010001001101', base=2)\n",
" bits = Bits()\n",
" assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
" print('Success: test_insert_m_into_n')\n",
"\n",
"\n",
"def main():\n",
" test = TestBit()\n",
" test.test_insert_m_into_n()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -0,0 +1,221 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Given two 16 bit numbers, n and m, and two indices i, j, insert m into n such that m starts at bit j and ends at bit i.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume j > i?\n",
" * Yes\n",
"* Can we assume i through j have enough space for m?\n",
" * Yes\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None as an input -> Exception\n",
"* Negative index for i or j -> Exception\n",
"* General case\n",
"\n",
"<pre>\n",
"i = 2, j = 6\n",
" j i\n",
"n = 0000 0100 0011 1101\n",
"m = 0000 0000 0001 0011\n",
"result = 0000 0100 0100 1101\n",
"</pre>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"<pre>\n",
" j i\n",
"n = 0000 0100 0011 1101\n",
"m = 0000 0000 0001 0011\n",
"\n",
"lmask = 1111 1111 1111 1111 -1\n",
"lmask = 1111 1111 1000 0000 -1 << (j + 1)\n",
"\n",
"rmask = 0000 0000 0000 0001 1\n",
"rmask = 0000 0000 0000 0100 1 << i\n",
"rmask = 0000 0000 0000 0011 (1 << i) -1\n",
"\n",
"mask = 1111 1111 1000 0011 lmask | rmask\n",
"\n",
"n = 0000 0100 0011 1101\n",
"mask = 1111 1111 1000 0011 n & mask \n",
"--------------------------------------------------\n",
"n2 = 0000 0100 0000 0001\n",
"\n",
"n2 = 0000 0100 0000 0001\n",
"mask2 = 0000 0000 0100 1100 m << i\n",
"--------------------------------------------------\n",
"result = 0000 0100 0100 1101 n2 | mask2\n",
"</pre>\n",
"\n",
"Complexity:\n",
"* Time: O(b), where b is the number of bits\n",
"* Space: O(b), where b is the number of bits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Bits(object):\n",
"\n",
" def insert_m_into_n(self, m, n, i, j):\n",
" if None in (m, n, i, j):\n",
" raise TypeError('Argument cannot be None')\n",
" if i < 0 or j < 0:\n",
" raise ValueError('Index cannot be negative')\n",
" left_mask = -1 << (j + 1)\n",
" right_mask = (1 << i) - 1\n",
" n_mask = left_mask | right_mask\n",
" # Clear bits from j to i, inclusive\n",
" n_cleared = n & n_mask\n",
" # Shift m into place before inserting it into n\n",
" m_mask = m << i\n",
" return n_cleared | m_mask"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_insert_m_into_n.py\n"
]
}
],
"source": [
"%%writefile test_insert_m_into_n.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class TestBit(object):\n",
"\n",
" def test_insert_m_into_n(self):\n",
" n = int('0000010000111101', base=2)\n",
" m = int('0000000000010011', base=2)\n",
" expected = int('0000010001001101', base=2)\n",
" bits = Bits()\n",
" assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
" print('Success: test_insert_m_into_n')\n",
"\n",
"\n",
"def main():\n",
" test = TestBit()\n",
" test.test_insert_m_into_n()\n",
"\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_insert_m_into_n\n"
]
}
],
"source": [
"%run -i test_insert_m_into_n.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -0,0 +1,21 @@
from nose.tools import assert_equal
class TestBit(object):
def test_insert_m_into_n(self):
n = int('0000010000111101', base=2)
m = int('0000000000010011', base=2)
expected = int('0000010001001101', base=2)
bits = Bits()
assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)
print('Success: test_insert_m_into_n')
def main():
test = TestBit()
test.test_insert_m_into_n()
if __name__ == '__main__':
main()