15-112 Fall 2012 Quiz 5a (Autograded) [see Quiz5b below]
45 minutes


Read these instructions first!
  1. leastFrequentLetters [40 pts]
  2. getCaesarShift [40 pts]

  1. leastFrequentLetters [40 pts]
    Write the function leastFrequentLetters(s) that takes a string s which may contain arbitrary characters and returns a lowercase string containing all the alphabetic letters that occur least frequently in s, in alphabetic order.  The test is case insensitive.  If s contains no letters, return the empty string.  To further help you understand the task, here is a sample test function (also in quiz5.py):
    def testLeastFrequentLetters():
        print "Testing leastFrequentLetters()...",
        assert(leastFrequentLetters("abc") == "abc")
        assert(leastFrequentLetters("cba") == "abc")
        assert(leastFrequentLetters("abcab") == "c")
        assert(leastFrequentLetters("abca") == "bc")
        assert(leastFrequentLetters("aBcA") == "bc")
        assert(leastFrequentLetters("") == "")
        assert(leastFrequentLetters("^%#$*&!@#*&#!@#") == "")
        print "Passed!"
  2. getCaesarShift [40 pts]
    Background:  in a Caesar cipher, we start with a "shift", which here we will assume is an integer between 0 and 25 inclusive, and each letter in a word is shifted by that amount.  For example, if the shift is 2, then "abc" is shifted to become "cde" ("a" is shifted to "c", "b" is shifted to "d", and "c" is shifted to "e").  The shift wraps around as needed, so if the shift is 2, then "xyz" becomes "zab".

    With this in mind, write the function getCaesarShift(word1, word2) that takes two words, both guaranteed to only contain lowercase letters, and returns the shift required to change word1 into word2, or -1 if this is not possible.  If the words are the same, return 0.  To further help you understand the task, here is a sample test function (also in quiz5.py):
    def testGetCaesarShift():
        print "Testing getCaesarShift()...",
        assert(getCaesarShift("abc", "abc") == 0)
        assert(getCaesarShift("abc", "bcd") == 1)
        assert(getCaesarShift("abc", "zab") == 25)
        assert(getCaesarShift("zab", "abc") == 1)
        assert(getCaesarShift("zab", "abd") == -1)
        assert(getCaesarShift("zab", "abcd") == -1)
        assert(getCaesarShift("", "") == 0)
        print "Passed!"


15-112 Fall 2012 Quiz 5b (non-autograded)

* 10 Minutes.  No calculators, no notes, no books, no computers.
* 20 pts (4 problems, 5 pts each; 5 pts bonus).  (The other 80 points are on the autograded quiz5a)

# What will each of the following print?
def f1(s):
    t = s.replace("a","")
    for c in s:
        if (c in t):
            t += c.upper()
    return t
print f1("abAB")

def f2(x):
    s = ""
    for n in xrange(15,15+x):
        s += str(n)
    return s[3:6]*2
print f2(5)

def f3(s, t):
    u = ""
    for i in xrange(min(len(s), len(t))):
        if (s[i] == t[len(t)-1-i]):
            u += s[i]
    return u
print f3("a1bc2de", "ad2ab3e")

def f4(m, n):
    s = ""
    while (m > 0):
        for x in xrange(n-m):
            s += "."
        s += "\n"
        m -= 1
    return s
print f4(3, 5)

def f5bonus():
    x = count = 0
    while (count < 7):
        (c,d,e) = (0,x*x**2*x,str(x*x**2*x))
        while (d): (c,d) = (c+1,d/10)
        (x,count) = (x+1,count+(c == 5))
    while (str(c)*len(e) != e): e = str(int(e)-1)
    return e
print f5bonus()
 


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem