Created
August 26, 2016 05:14
-
-
Save dudepare/af7865419d0dde698381201e068d0439 to your computer and use it in GitHub Desktop.
String Rotation: Assumeyou have a method isSubstringwhich checks ifoneword isa substring of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat").
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rotation: | |
@staticmethod | |
def is_substring(s1, s2): | |
"""Checks if s2 is a substring of s1 | |
Args: | |
s1 (string) string input | |
s2 (string) rotation of s1 | |
Returns: | |
True if s2 is a substring of s1 | |
False otherwise | |
""" | |
# we should check somewhere here if s1 is indeed a rotation of s2 | |
if len(s1) != len(s2): | |
return False | |
s = s2 + s2 | |
return s1 in s | |
import unittest | |
class TestRotation(unittest.TestCase): | |
def test_example(self): | |
self.assertTrue(Rotation.is_substring("waterbottle", "erbottlewat")) | |
def test_empty(self): | |
self.assertTrue(Rotation.is_substring("","")) | |
def test_not_rotation_diff_length(self): | |
self.assertFalse(Rotation.is_substring("a", "ba")) | |
def test_not_rotation_same_length(self): | |
self.assertFalse(Rotation.is_substring("aa", "ba")) | |
def test_rotation_same_string(self): | |
self.assertTrue(Rotation.is_substring("aa", "aa")) | |
def test_rotation_stuff(self): | |
self.assertTrue(Rotation.is_substring("andrew", "rewand")) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment