-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.py
45 lines (33 loc) · 852 Bytes
/
rotate.py
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
38
39
40
41
42
43
44
45
import string
def rotate_letter(letter, n):
"""Rotates a letter by n places. Does not change other chars.
letter: single-letter string
n: int
Returns: single-letter string
"""
if letter.isupper():
start = ord('A')
elif letter.islower():
start = ord('a')
else:
return letter
c = ord(letter) - start
i = (c + n) % 26 + start
return chr(i)
def rotate_word(word, n):
"""Rotates a word by n places.
word: string
n: integer
Returns: string
"""
res = ''
for letter in word:
res += rotate_letter(letter, n)
return res
if __name__ == '__main__':
word=input()
shift=(int)(input())
print(rotate_word(word, shift))
# print(rotate_word('cheer', 7))
# print(rotate_word('melon', -10))
# print(rotate_word('sleep', 9))