CPSC120
Fundamentals of Computer Science

In-class Activity 16

  1. What would each of the following snippets of Python code print to the command line if executed:

    a.

        spam = "this is spam"
        print(spam[2:4] + spam[5:7])

    b.

        spam = "this is spam"
        print(spam[-4:len(spam) - 1])

    c.

        spam = "this is spam"
        i = spam.find(" ")
        print(spam[i + 1:i + 3])
  2. What is the the binary number 10101010 in decimal?

  3. What is the decimal number 72 in binary?

  4. Create a function called rotate_thirteen(text) that returns a string that has been modified according to the rot13 encryption algorithm. The name rot13 is short for rotate thirteen. The algorithm rotates all alphabetic characters in a string by 13 places. That is, a letter is replaced with the letter that is 13th later in the alphabet. For example, the letter “A”, the first in the alphabet, would be replaced with the fourteenth letter in the alphabet, “N”. For letters in the second half of the alphabet, the alphabet is wrapped. For example the letter “Z”, the twenty sixth letter in the alphabet, would be replaced with the thirteenth letter in the alphabet, “M”. Characters that are not letters are removed and characters that are lowercase letters are converted to uppercase.

  5. Write a program that prompts the user for text and it uses the rot13 algorithm to print the text enciphered.