forked from reemanaqvi/HW04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW04_ex08_12.py
45 lines (29 loc) · 991 Bytes
/
HW04_ex08_12.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
# Structure this script entirely on your own.
# See Chapter 8: Strings Exercise 12 for guidance
# Please do provide function calls that test/demonstrate your function
# import string
def rotate_word(text, num):
result = ""
for letter in text:
result = result + calculate_char(letter,num)
print result
#calculates the next char according to the given integer
def calculate_char(letter,num):
if letter.isupper():
int_starting_alphabet = ord('A')
elif letter.islower():
int_starting_alphabet = ord('a')
else:
return letter
#
int_val = ord(letter) - int_starting_alphabet
int_val = (int_val + num)%26 + int_starting_alphabet
char_val = chr(int_val)
return char_val
def main():
rotate_word("abc",1)
rotate_word("xyz",10)
rotate_word("ehu",13)
rotate_word("ABC",-1)
if __name__ == '__main__':
main()