CMU 15-112 Fall 2015 Quiz 3 Practice
(Due never)



  1. Code Tracing
  2. vowelCount(s)
  3. interleave(s1, s2)
  4. mostFrequentLetters(s)
  5. hasBalancedParentheses(s)
  6. areAnagrams(s1, s2)
  7. rotateStringLeft(s, k)
  8. rotateStringRight(s, k)
  9. collapseWhitespace(s)

  1. Code Tracing
    See here.

  2. 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:
       vowelCount("Abc def!!! a? yzyzyz!")
    returns 3 (two a's and one e).
    import string def vowelCount(s): return 42 # replace with your answer! def testVowelCount(): print("Testing vowelCount()...", end="") assert(vowelCount("abcdefg") == 2) assert(vowelCount("ABCDEFG") == 2) assert(vowelCount("") == 0) assert(vowelCount("This is a test. 12345.") == 4) assert(vowelCount(string.ascii_lowercase) == 5) assert(vowelCount(string.ascii_lowercase*100) == 500) assert(vowelCount(string.ascii_uppercase) == 5) assert(vowelCount(string.punctuation) == 0) assert(vowelCount(string.whitespace) == 0) print("Passed!") testVowelCount()

  3. interleave(s1, s2)
    Write the function interleave(s1, s2) that takes two strings, s1 and s2, and interleaves their characters starting with the first character in s1. For example, interleave('pto', 'yhn') would return the string "python". If one string is longer than the other, concatenate the rest of the remaining string onto the end of the new string. For example ('a#', 'cD!f2') would return the string "ac#D!f2". Assume that both s1 and s2 will always be strings.
    def interleave(s1, s2): return "Z" # replace with your answer! def testInterleave(): print("Testing interleave()...", end="") assert(interleave("abcdefg", "abcdefg") == "aabbccddeeffgg") assert(interleave("abcde", "abcdefgh") == "aabbccddeefgh") assert(interleave("abcdefgh","abcde") == "aabbccddeefgh") assert(interleave("Smlksgeneg n a!", "a ie re gsadhm") == "Sam likes green eggs and ham!") assert(interleave("","") == "") print("Passed!") testInterleave()

  4. mostFrequentLetters(s)
    Write the function mostFrequentLetter(s) that takes a string s and returns the letter that occurs the most frequently in it. Your test should be case-insensitive, to "A" and "a" are the same, though your return value should always be uppercase. And if there is a tie, you should return a string with all the most frequent letters in alphabetic order. You should ignore non-letters. And if there are no letters, you should return the empty string.
    import string def mostFrequentLetters(s): return 'Z' # replace with your answer! def testMostFrequentLetters(): print("Testing mostFrequentLetters()...", end="") assert(mostFrequentLetters("Cat") == 'ACT') assert(mostFrequentLetters("A cat") == 'A') assert(mostFrequentLetters("A cat in the hat") == 'AT') assert(mostFrequentLetters("This is a test") == 'ST') assert(mostFrequentLetters("This is an I test?") == 'IST') assert(mostFrequentLetters("") == "") print("Passed!") testMostFrequentLetters()

  5. hasBalancedParentheses(s)
    Write the function hasBalancedParentheses, which takes a string and returns True if that code has balanced parentheses and False otherwise (ignoring all non-parentheses in the string). We say that parentheses are balanced if each right parenthesis closes (matches) an open (unmatched) left parenthesis, and no left parentheses are left unclosed (unmatched) at the end of the text. So, for example, "( ( ( ) ( ) ) ( ) )" is balanced, but "( ) )" is not balanced, and "( ) ) (" is also not balanced. Hint: keep track of how many right parentheses remain unmatched as you iterate over the string.
    def hasBalancedParentheses(s): return False # replace with your answer! def testHasBalancedParentheses(): print("Testing hasBalancedParentheses()...", end="") assert(hasBalancedParentheses("()") == True) assert(hasBalancedParentheses("") == True) assert(hasBalancedParentheses("())") == False) assert(hasBalancedParentheses("()(") == False) assert(hasBalancedParentheses(")(") == False) assert(hasBalancedParentheses("(()())") == True) assert(hasBalancedParentheses("((()())(()(()())))") == True) assert(hasBalancedParentheses("((()())(()((()())))") == False) assert(hasBalancedParentheses("((()())(((()())))") == False) print("Passed!") testHasBalancedParentheses()

  6. areAnagrams(s1, s2)
    Write the function areAnagrams(s1, s2) that takes two strings, s1 and s2, that you may assume contain only upper and/or lower case letters, and returns True if the strings are anagrams, and False otherwise. Two strings are anagrams if each can be reordered into the other. Treat "a" and "A" as the same letters (so "Aba" and "BAA" are anagrams). You may not use sort() or sorted() or any other list-based functions or approaches. Hint: you may use s.count(), which could be quite handy here.
    import string def areAnagrams(s1, s2): return False # replace with your answer! def testAreAnagrams(): print("Testing areAnagrams()...", end="") assert(areAnagrams("", "") == True) assert(areAnagrams("abCdabCd", "abcdabcd") == True) assert(areAnagrams("abcdaBcD", "AAbbcddc") == True) assert(areAnagrams("abcdaabcd", "aabbcddcb") == False) print("Passed!") testAreAnagrams()

  7. rotateStringLeft(s)
    Write the function rotateStringLeft that takes a string s and a non-negative integer k, and returns the string s rotated k places to the left.
    def rotateStringLeft(s, k): return 42 # you write this! def testRotateStringLeft(): print("Testing rotateStringLeft()...", end="") assert(rotateStringLeft("abcde", 0) == "abcde") assert(rotateStringLeft("abcde", 1) == "bcdea") assert(rotateStringLeft("abcde", 2) == "cdeab") assert(rotateStringLeft("abcde", 3) == "deabc") assert(rotateStringLeft("abcde", 4) == "eabcd") assert(rotateStringLeft("abcde", 5) == "abcde") assert(rotateStringLeft("abcde", 25) == "abcde") assert(rotateStringLeft("abcde", 28) == "deabc") print("Passed!") testRotateStringLeft()

  8. rotateStringRight(s, k)
    Write the function rotateStringRight that takes a string s and a non-negative integer k, and returns the string s rotated k places to the right.
    def rotateStringRight(s, k): return 42 # you write this! def testRotateStringRight(): print("Testing rotateStringRight()...", end="") assert(rotateStringRight("abcde", 0) == "abcde") assert(rotateStringRight("abcde", 1) == "eabcd") assert(rotateStringRight("abcde", 2) == "deabc") assert(rotateStringRight("abcde", 3) == "cdeab") assert(rotateStringRight("abcde", 4) == "bcdea") assert(rotateStringRight("abcde", 5) == "abcde") assert(rotateStringRight("abcde", 25) == "abcde") assert(rotateStringRight("abcde", 28) == "cdeab") print("Passed!") testRotateStringRight()

  9. 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(cw("a\nb") == "a b")
        assert(cw("a\n   \t    b") == "a b")
        assert(cw("a\n   \t    b  \n\n  \t\t\t c   ") == "a b c ")
    
    Once again, do not use s.replace() in your solution.
    def collapseWhitespace(s): return "Z" # replace with your answer! def testCollapseWhitespace(): print("Testing collapseWhitespace()...", end="") assert(collapseWhitespace("a\n\n\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 ") print("Passed!") testCollapseWhitespace()