This notebook was prepared by Donne Martin. Source and license info is on GitHub.

Challenge Notebook

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.

Constraints

  • Can we assume j > i?
    • Yes
  • Can we assume i through j have enough space for m?
    • Yes
  • Can we assume the inputs are valid?
    • No
  • Can we assume this fits memory?
    • Yes

Test Cases

  • None as an input -> Exception
  • Negative index for i or j -> Exception
  • General case
    i      = 2
    j      = 6
    n      = 0000 0100 0000 0000
    m      = 0000 0000 0001 0011
    result = 0000 0100 0100 1100
    

Algorithm

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.

Code

In [ ]:
class Bits(object):

    def insert_m_into_n(self, m, n, i, j):
        # TODO: Implement me
        pass

Unit Test

The following unit test is expected to fail until you solve the challenge.

In [ ]:
# %load test_insert_m_into_n.py
import unittest


class TestBit(unittest.TestCase):

    def test_insert_m_into_n(self):
        n = int('0000010000111101', base=2)
        m = int('0000000000010011', base=2)
        expected = int('0000010001001101', base=2)
        bits = Bits()
        self.assertEqual(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()

Solution Notebook

Review the Solution Notebook for a discussion on algorithms and code solutions.