cs101 ยป

CS101 - Unit 5 Extra Practice

Problem 1 - Shift a letter

# Write a procedure, shift, which takes as its input a lowercase letter,
# a-z and returns the next letter in the alphabet after it, with 'a' 
# following 'z'.

def shift(letter):

print shift('a')
#>>> b
print shift('n')
#>>> o
print shift('z')
#>>> a

Problem 2 - Shift n letters

# Write a procedure, shift_n_letters which takes as its input a lowercase
# letter, a-z, and an integer n, and returns the letter n steps in the
# alphabet after it. If the input is z, the output should be a. Note that n
# can be positive, negative or zero.

def shift_n_letters(letter, n):

print shift_n_letters('s',1)
#>>> t
print shift_n_letters('s',2)
#>>> u
print shift_n_letters('s',10)
#>>> c
print shift_n_letters('s',-10)
#>>> i

Problem 3 - Rotate

# Write a procedure, rotate, which takes as its input a string of lower case
# letters, a-z and an integer n, and returns the string of lower case letters 
# where each letter is n steps after the corresponding letter in the input
# word. Note that 'z' follows 'a'. You can use an additional procedure if you
# choose to as long as rotate returns the correct string.
# Note that n can be positive, negative or zero.

def rotate():
    # Your code here

print rotate ('sarah', 13)
#>>> 'fnenu'
print rotate('fnenu',13)
#>>> 'sarah'
print rotate('dave',5)
#>>>'ifaj'
print rotate('ifaj',-5)
#>>>'dave'
print rotate(("zw pfli tfuv nfibj tfiivtkcp pfl jyflcu "
                "sv rscv kf ivru kyzj"),-17)
#>>> ???