-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path1.9-String_Rotation.py
More file actions
37 lines (29 loc) · 880 Bytes
/
1.9-String_Rotation.py
File metadata and controls
37 lines (29 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# CTCI 1.9
# String Rotation
import unittest
# Is Substring Function
def is_substring(string, sub):
return string.find(sub) != -1
# My Solution
def string_rotation(s1, s2):
if len(s1) == len(s2):
return is_substring(s2+s2, s1)
return False
#-------------------------------------------------------------------------------
# CTCI Solution
# Had the same solution
#-------------------------------------------------------------------------------
#Testing
class Test(unittest.TestCase):
'''Test Cases'''
data = [
('waterbottle', 'erbottlewat', True),
('foo', 'bar', False),
('foo', 'foofoo', False)
]
def test_string_rotation(self):
for [s1, s2, expected] in self.data:
actual = string_rotation(s1, s2)
self.assertEqual(actual, expected)
if __name__ == "__main__":
unittest.main()