Program of the Day #14

In this program, you will implement what is known as a rotation cipher. The idea is to “encode” or obfuscate a string by taking each letter of the alphabet and replacing it with a letter that is some offset number of letters later in the alphabet. If the offset would put you past the end of the alphabet, you’d wrap back around to the beginning.

For example, rotating the string “AB” by an offset of 2 would produce “CD”, because “C” is two letters later than “A” and “D” is two letters later than “B”. Rotating “XY” by an offset of 2 would yield “ZA”, because “Z” is two letters later than “Z”, and “A” is two letters later than “Y” after wrapping back to the beginning of the alphabet.

A popular example of a rotation cipher is called rot13; this one is cool because applying it twice gets the you back to the original string (because 13*2 = 26, the number of letters in the alphabet).

Your program should implement the following function:

def rotate(offset, in_string):
    """Apply a rotation cipher to in_string with the given offset, and
    return the result. Lower-case characters are converted to upper-case
    before rotation, and non-alphabetic characters are left unchanged. 
    Precondition: offset is a positive integer."""

The main program should take two command-line arguments, an offset and a string, and print the result of rotating the string by that offset. Note that you can pass a string that includes spaces as a single command line argument by enclosing it in quotes - otherwise it would be separated into multiple arguments:

>>> %Run P14_rotcipher.py 13 "THE QUICK BROWN FOX!"
GUR DHVPX OEBJA SBK!

>>> %Run P14_rotcipher.py 13 "GUR DHVPX OEBJA SBK!"
THE QUICK BROWN FOX!

Other Practice Problems

  1. Implement the following function, but this time use string methods to do it without a loop:

    def remove_vowels(string):
        """ Return string, but with all vowels removed. Don't count y as a vowel. """
  2. Implement the following function, but this time use string methods to do it without a loop:

    def remove_comments(string):
        """ Return a copy of string, but with all characters starting with and following
            the first instance of ‘#’ removed. If there is no # in the string, return
            the input unchanged."""
  3. Implement the following function:

    def uun_letters(first_name, last_name):
        """ Return the letters in a student's WWU Universal
        Username given the student's first_name and last_name.
        The username begins with the first 6 characters of
        the last name, followed by the first letter of the
        first name. Return the username in all lower case.
        Example: uun_letters("Scott", "Wehrwein") => "wehrwes" """
  4. Implement the following function:

    def capitalize_words(string):
        """ Return a copy of string, but with the first letter of each word
        capitalized. Pre: string contains words made of only letters, each separated
        by a single space."""
  5. Read up on the format string method. Write a program that implements a “Mad Libs” style word game (see here if you’re not familiar with Mad Libs). First, prompt the user for a series of words, each a specific part of speech. Then fill the words into a story of your choice and print the story. Implement this program with only one call to the print function with only one argument. You may find it helpful to use triple-quoted strings so you can write your story on multiple lines.

  6. Write a program to print a tidy, well-aligned table of characters and their corresponding character codes, starting with 32 and ending at 126. Make the character codes count by one going down each column. For extra fun, take a single command line argument that specifies how many columns your table should have. Example outputs:

    >>> %Run asciitable.py 6
     32     48 0   64 @   80 P   96 `  112 p  
     33 !   49 1   65 A   81 Q   97 a  113 q  
     34 "   50 2   66 B   82 R   98 b  114 r  
     35 #   51 3   67 C   83 S   99 c  115 s  
     36 $   52 4   68 D   84 T  100 d  116 t  
     37 %   53 5   69 E   85 U  101 e  117 u  
     38 &   54 6   70 F   86 V  102 f  118 v  
     39 '   55 7   71 G   87 W  103 g  119 w  
     40 (   56 8   72 H   88 X  104 h  120 x  
     41 )   57 9   73 I   89 Y  105 i  121 y  
     42 *   58 :   74 J   90 Z  106 j  122 z  
     43 +   59 ;   75 K   91 [  107 k  123 {  
     44 ,   60 <   76 L   92 \  108 l  124 |  
     45 -   61 =   77 M   93 ]  109 m  125 }  
     46 .   62 >   78 N   94 ^  110 n  126 ~  
     47 /   63 ?   79 O   95 _  111 o  
    >>> %Run asciitable.py 9
     32     43 +   54 6   65 A   76 L   87 W   98 b  109 m  120 x  
     33 !   44 ,   55 7   66 B   77 M   88 X   99 c  110 n  121 y  
     34 "   45 -   56 8   67 C   78 N   89 Y  100 d  111 o  122 z  
     35 #   46 .   57 9   68 D   79 O   90 Z  101 e  112 p  123 {  
     36 $   47 /   58 :   69 E   80 P   91 [  102 f  113 q  124 |  
     37 %   48 0   59 ;   70 F   81 Q   92 \  103 g  114 r  125 }  
     38 &   49 1   60 <   71 G   82 R   93 ]  104 h  115 s  126 ~  
     39 '   50 2   61 =   72 H   83 S   94 ^  105 i  116 t  
     40 (   51 3   62 >   73 I   84 T   95 _  106 j  117 u  
     41 )   52 4   63 ?   74 J   85 U   96 `  107 k  118 v  
     42 *   53 5   64 @   75 K   86 V   97 a  108 l  119 w