CMU 15-112: Fundamentals of Programming and Computer Science
Writing-Session3 Practice

  1. vowelCount(s)
    Write the function vowelCount(s), that takes a string s, and returns the number of vowels in s, ignoring case, so "A" and "a" are both vowels. The vowels are "a", "e", "i", "o", and "u". So, for example:
    assert(vowelCount("Abc def!!! a? yzyzyz!") == 3) # two a's and one e

  2. rotateStringLeft(s, n)
    Note: To receive credit, do not use loops on this problem.
    Write the function rotateStringLeft(s, n) that takes a string s and a possibly-negative integer n. If n is non-negative, the function returns the string s rotated n places to the left. If n is negative, the function returns the string s rotated |n| places to the right. So, for example:
    assert(rotateStringLeft('abcd', 1) == 'bcda') assert(rotateStringLeft('abcd', -1) == 'dabc')

  3. rotateStringRight(s, n)
    Note: Again, to receive credit, do not use loops on this problem.
    Write the function rotateStringRight(s, n) that works the same as rotateStringLeft(s, n) only in the opposite direction. So:
    assert(rotateStringRight('abcd', 1) == 'dabc') assert(rotateStringRight('abcd', -1) == 'bcda')

  4. isRotation(s, t)
    Write the function isRotation(s, t) that takes two possibly-empty strings and returns True if one is a rotation of the other. Note that a string is not considered a rotation of itself.

  5. applyCaesarCipher(message, shift)
    A Caesar Cipher is a simple cipher that works by shifting each letter in the given message by a certain number. For example, if we shift the message "We Attack At Dawn" by 1 letter, it becomes "Xf Buubdl Bu Ebxo".

    Write the function applyCaesarCipher(message, shift) which shifts the given message by shift letters. You are guaranteed that message is a string, and that shift is an integer between -25 and 25. Capital letters should stay capital and lowercase letters should stay lowercase, and non-letter characters should not be changed. Note that "Z" wraps around to "A". So, for example:
    assert(applyCaesarCipher("We Attack At Dawn", 1) == "Xf Buubdl Bu Ebxo") assert(applyCaesarCipher("zodiac", -2) == "xmbgya")

  6. collapseWhitespace(s)
    Without using the s.replace() method, write the function collapseWhitespace(s), that takes a string s and returns an equivalent string except that each occurrence of whitespace in the string is replaced by a single space. So, for example, collapseWhitespace("a\t\t\tb\n\nc") replaces the three tabs with a single space, and the two newlines with another single space , returning "a b c". Here are a few more test cases for you:
    assert(collapseWhitespace("a\nb") == "a b") assert(collapseWhitespace("a\n \t b") == "a b") assert(collapseWhitespace("a\n \t b \n\n \t\t\t c ") == "a b c ")
    Once again, do not use s.replace() in your solution.