This notebook was prepared by Donne Martin. Source and license info is on GitHub.
input = 1001 1111 0110 result = 0110 1111 1001
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.
class Bits(object):
def pairwise_swap(self, num):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_pairwise_swap.py
import unittest
class TestBits(unittest.TestCase):
def test_pairwise_swap(self):
bits = Bits()
self.assertEqual(bits.pairwise_swap(0), 0)
self.assertEqual(bits.pairwise_swap(1), 1)
num = int('0000100111110110', base=2)
expected = int('0000011011111001', base=2)
self.assertEqual(bits.pairwise_swap(num), expected)
print('Success: test_pairwise_swap')
def main():
test = TestBits()
test.test_pairwise_swap()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.