This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Examine the following test case:
We see that if we can use the given is_substring method if we take compare s2 with s1 + s1:
Complexity:
class Rotation(object):
def is_substring(self, s1, s2):
return s1 in s2
def is_rotation(self, s1, s2):
if s1 is None or s2 is None:
return False
if len(s1) != len(s2):
return False
return self.is_substring(s1, s2 + s2)
%%writefile test_rotation.py
import unittest
class TestRotation(unittest.TestCase):
def test_rotation(self):
rotation = Rotation()
self.assertEqual(rotation.is_rotation('o', 'oo'), False)
self.assertEqual(rotation.is_rotation(None, 'foo'), False)
self.assertEqual(rotation.is_rotation('', 'foo'), False)
self.assertEqual(rotation.is_rotation('', ''), True)
self.assertEqual(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)
print('Success: test_rotation')
def main():
test = TestRotation()
test.test_rotation()
if __name__ == '__main__':
main()
Overwriting test_rotation.py
%run -i test_rotation.py
Success: test_rotation