Skip to content

Commit 339a0d2

Browse files
authored
fix_rotate.py (keon#778)
* fix_rotate.py The way it was, you couldn't pass in a rotation higher than the length of the string doubled without getting a blank string in return. This way allows you to pass in any positive integer and get a result. Also, The last line of the description said that it took 2 strings and returned a boolean. This is not correct. The first line of the comment is correct. * Update rotate.py After submitting my last change, I realized it would have been better to not keep concatenating the string. * Updated Comments rotate.py
1 parent 5995c05 commit 339a0d2

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

algorithms/strings/rotate.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
"""
22
Given a strings s and int k, return a string that rotates k times
33
4+
k can be any positive integer.
5+
46
For example,
57
rotate("hello", 2) return "llohe"
68
rotate("hello", 5) return "hello"
79
rotate("hello", 6) return "elloh"
810
rotate("hello", 7) return "llohe"
11+
rotate("hello", 102) return "lohel"
912
10-
accepts two strings
11-
returns bool
1213
"""
1314
def rotate(s, k):
14-
double_s = s + s
15+
long_string = s * (k // len(s) + 2)
1516
if k <= len(s):
16-
return double_s[k:k + len(s)]
17+
return long_string[k:k + len(s)]
1718
else:
18-
return double_s[k-len(s):k]
19+
return long_string[k-len(s):k]

0 commit comments

Comments
 (0)