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

Challenge Notebook

Problem: Flip one bit from 0 to 1 to maximize the longest sequence of 1s.

Constraints

  • Is the input an int, base 2?
    • Yes
  • Can we assume the input is a 32 bit number?
    • Yes
  • Do we have to validate the length of the input?
    • No
  • Is the output an int?
    • Yes
  • Can we assume the inputs are valid?
    • No
  • Can we assume we are using a positive number since Python doesn't have an >>> operator?
    • Yes
  • Can we assume this fits memory?
    • Yes

Test Cases

  • None -> Exception
  • All 1's -> Count of 1s
  • All 0's -> 1
  • General case
    • 0000 1111 1101 1101 1111 0011 1111 0000 -> 10 (ten)

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 flip_bit(self, num):
        # TODO: Implement me
        pass

Unit Test

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

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


class TestBits(unittest.TestCase):

    def test_flip_bit(self):
        bits = Bits()
        self.assertRaises(TypeError, bits.flip_bit, None)
        self.assertEqual(bits.flip_bit(0), 1)
        self.assertEqual(bits.flip_bit(-1), bits.MAX_BITS)
        num = int('00001111110111011110001111110000', base=2)
        expected = 10
        self.assertEqual(bits.flip_bit(num), expected)
        num = int('00000100111011101111100011111011', base=2)
        expected = 9
        self.assertEqual(bits.flip_bit(num), expected)
        num = int('00010011101110111110001111101111', base=2)
        expected = 10
        self.assertEqual(bits.flip_bit(num), expected)
        print('Success: test_print_binary')


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


if __name__ == '__main__':
    main()

Solution Notebook

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