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

Challenge Notebook

Problem: Determine the number of bits to flip to convert int a to int b'.

Constraints

  • Can we assume A and B are always ints?
    • Yes
  • Is the output an int?
    • Yes
  • Can we assume A and B are always the same number of bits?
    • Yes
  • Can we assume the inputs are valid (not None)?
    • No
  • Can we assume this fits memory?
    • Yes

Test Cases

  • A or B is None -> Exception
  • General case
      A = 11101
      B = 01111
      Result: 2

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 bits_to_flip(self, a, b):
        # TODO: Implement me
        pass

Unit Test

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

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


class TestBits(unittest.TestCase):

    def test_bits_to_flip(self):
        bits = Bits()
        a = int('11101', base=2)
        b = int('01111', base=2)
        expected = 2
        self.assertEqual(bits.bits_to_flip(a, b), expected)
        print('Success: test_bits_to_flip')


def main():
    test = TestBits()
    test.test_bits_to_flip()


if __name__ == '__main__':
    main()

Solution Notebook

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