Skip to content

Commit 1f2bb52

Browse files
committed
add rotate algorithm
1 parent d7a74e3 commit 1f2bb52

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

strings/rotate.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
rotate("hello", 2) => "llohe"
3+
rotate("hello", 5) => "hello"
4+
rotate("hello", 6) => "elloh"
5+
rotate("hello", 7) => "llohe"
6+
complexity: O(1)
7+
"""
8+
9+
10+
def rotate(string: str, degree: int) -> str:
11+
degree %= len(string)
12+
double_string = string + string
13+
return double_string[degree:degree+len(string)]
14+
15+
16+
if __name__ == "__main__":
17+
print(rotate("hello", 2) == "llohe")
18+
print(rotate("hello", 5) == "hello")
19+
print(rotate("hello", 6) == "elloh")
20+
print(rotate("hello", 7) == "llohe")
21+
print(rotate("hello", 102) == "llohe")

0 commit comments

Comments
 (0)