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!"
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