Skip to content

Commit 6e7e1f2

Browse files
Add another implementation (#802)
* Add tests for rotate.py * Add another implementation
1 parent d54f4a5 commit 6e7e1f2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

algorithms/strings/rotate.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ def rotate(s, k):
1717
return long_string[k:k + len(s)]
1818
else:
1919
return long_string[k-len(s):k]
20+
21+
def rotate_alt(string, k):
22+
k = k % len(string)
23+
return string[k:] + string[:k]

tests/test_strings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ def test_rotate(self):
225225
self.assertEqual("hello", rotate("hello", 5))
226226
self.assertEqual("elloh", rotate("hello", 6))
227227
self.assertEqual("llohe", rotate("hello", 7))
228+
229+
def test_rotate_alt(self):
230+
self.assertEqual("llohe", rotate_alt("hello", 2))
231+
self.assertEqual("hello", rotate_alt("hello", 5))
232+
self.assertEqual("elloh", rotate_alt("hello", 6))
233+
self.assertEqual("llohe", rotate_alt("hello", 7))
228234

229235

230236
class TestLicenseNumber(unittest.TestCase):

0 commit comments

Comments
 (0)