interactive-coding-challenges/bit_manipulation/bit/bit_challenge.ipynb

222 lines
5.9 KiB
Python

{
"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: Implement common bit manipulation operations: get_bit, set_bit, clear_bit, clear_bits_msb_to_index, clear_bits_msb_to_lsb, update_bit.\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 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 a number input -> Exception\n",
"* Negative index -> Exception\n",
"\n",
"### get_bit\n",
" number = 0b10001110, index = 3\n",
" expected = True\n",
"### set_bit\n",
" number = 0b10001110, index = 4\n",
" expected = 0b10011110\n",
"### clear_bit\n",
" number = 0b10001110, index = 3\n",
" expected = 0b10000110\n",
"### clear_bits_msb_to_index\n",
" number = 0b10001110, index = 3\n",
" expected = 0b00000110\n",
"### clear_bits_index_to_lsb\n",
" number = 0b10001110, index = 3\n",
" expected = 0b10000000\n",
"### update_bit\n",
" number = 0b10001110, index = 3, value = 1\n",
" expected = 0b10001110\n",
" number = 0b10001110, index = 3, value = 0\n",
" expected = 0b10000110\n",
" number = 0b10001110, index = 0, value = 1\n",
" expected = 0b10001111"
]
},
{
"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": {},
"outputs": [],
"source": [
"class Bit(object):\n",
"\n",
" def __init__(self, number):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def get_bit(self, index):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def set_bit(self, index):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def clear_bit(self, index):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def clear_bits_msb_to_index(self, index):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def clear_bits_index_to_lsb(self, index):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def update_bit(self, index, value):\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": {},
"outputs": [],
"source": [
"# %load test_bit.py\n",
"import unittest\n",
"\n",
"\n",
"class TestBit(unittest.TestCase):\n",
"\n",
" def test_bit(self):\n",
" number = int('10001110', base=2)\n",
" bit = Bit(number)\n",
" self.assertEqual(bit.get_bit(index=3), True)\n",
" expected = int('10011110', base=2)\n",
" self.assertEqual(bit.set_bit(index=4), expected)\n",
" bit = Bit(number)\n",
" expected = int('10000110', base=2)\n",
" self.assertEqual(bit.clear_bit(index=3), expected)\n",
" bit = Bit(number)\n",
" expected = int('00000110', base=2)\n",
" self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)\n",
" bit = Bit(number)\n",
" expected = int('10000000', base=2)\n",
" self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)\n",
" bit = Bit(number)\n",
" self.assertEqual(bit.update_bit(index=3, value=1), number)\n",
" bit = Bit(number)\n",
" expected = int('10000110', base=2)\n",
" self.assertEqual(bit.update_bit(index=3, value=0), expected)\n",
" bit = Bit(number)\n",
" expected = int('10001111', base=2)\n",
" self.assertEqual(bit.update_bit(index=0, value=1), expected)\n",
" print('Success: test_bit')\n",
"\n",
"\n",
"def main():\n",
" test = TestBit()\n",
" test.test_bit()\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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}