This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).

Challenge Notebook

Problem: Determine if a string s1 is a rotation of another string s2, by calling (only once) a function is_substring

Constraints

  • Can you assume the string is ASCII?
    • Yes
    • Note: Unicode strings could require special handling depending on your language
  • Can you use additional data structures?
    • Yes
  • Is this case sensitive?
    • Yes

Test Cases

  • Any strings that differ in size -> False
  • None, 'foo' -> False (any None results in False)
  • ' ', 'foo' -> False
  • ' ', ' ' -> True
  • 'foobarbaz', 'barbazfoo' -> True

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 [ ]:
def is_substring(s1, s2):
    # TODO: Implement me
    pass


def is_rotation(s1, s2):
    # TODO: Implement me
    # Call is_substring only once
    pass

Unit Test

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


In [ ]:
# %load test_rotation.py
from nose.tools import assert_equal


class TestRotation(object):

    def test_rotation(self):
        assert_equal(is_rotation('o', 'oo'), False)
        assert_equal(is_rotation(None, 'foo'), False)
        assert_equal(is_rotation('', 'foo'), False)
        assert_equal(is_rotation('', ''), True)
        assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)
        print('Success: test_rotation')


def main():
    test = TestRotation()
    test.test_rotation()


if __name__ == '__main__':
    main()

Solution Notebook

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