Python Practice Exam 2
Python Practice Exam 2
print(frequencyAnalysis('606', 1))
a) 2
b) 4
c) -4
d) 6
e) none of the above
Question 2
def substring(a, b):
s = ''
while True:
if a in b:
continue
elif b in a:
return b
else:
return a
return s
print(substring('ark', 'snark'))
a) []
b) ['I', 'want']
c) ['I', 'want', 'I', 'want']
d) ['I', 'want', 'I', 'want', 'I', 'want']
e) none of the above
Question 4
word = 'alakazam'
length = len(word)
accum = ''
for i in range(length):
j = (i+2)%length
if word[i] == word[j]:
break
elif word[i] < word[j]:
accum += word[i]
else:
accum += word[j]
print(accum)
Question 5
d = {1:{'three':3}, 0:'zero', '2':0}
print(d[1][0])
a) 3
b) t
c) zero
d) KeyError: 0
e) none of the above
Question 6
bools = [True, not not True, 7//2 == 1, True and False]
output = 1
for bool in bools:
if bool:
output *= 2
else:
output *= -3
print(output)
a) 1
b) -24
c) 36
d) -54
e) none of the above
Question 7
language = 'Python'
output = ''
for w in language[:1]:
output += w
for x in language[-2:]:
output += x
print(output)
a) PoPn
b) Pon
c) Ponyon
d) PoPnyoyn
e) none of the above
Question 8
tom = "it is error only that shrinks from inquiry"
def tailDict(t):
words = t.split()
d = {}
for word in words:
tail = word[-1]
if tail not in d:
d[tail] = [word]
else:
d[tail].append(word)
return len(d)
print(tailDict(tom))
a) 5
b) 6
c) 8
d) KeyError: ' '
e) none of the above
Question 9
For this question assume that a file named edgar.txt exists and has the following content:
keeping time time time
in a sort of runic rhyme
bells bells bells
bells bells bells
def stat(inFile):
inF = open(inFile)
fileContent = inF.read()
things = fileContent.split()
unique = []
for thing in things:
if things.count(thing) == 1 and thing not in unique:
unique.append(thing)
inF.close()
return len(unique)
print(stat('edgar.txt'))
a) 2
b) 3
c) 7
d) 9
e) none of the above
Question 10
will = ['to', 'be', 'or', 'not', 'to', 'be', 'that', 'is', 'the', 'question']
extreme = 0
for word in will:
if len(word) >= extreme:
extreme = len(word)
print(extreme)
a) 0
b) 2
c) 3
d) 4
e) none of the above
Question 11A (8 points)
Write a function named pentagon that uses turtle graphics to draw a regular pentagon of specified size. A
regular pentagon has five equal sides and five equal internal angles of 108 degrees.
The function pentagon takes two parameters:
1. t, a turtle that is used for drawing
2. side, a number that is the length of a side
The function pentagon should draw a regular pentagon at the initial position and orientation of t. It
should leave t at the same position and with the same orientation on exit. Turtle t is initially at one of the
vertices of the pentagon and is oriented in the direction of one of the sides. Rotate the turtle clockwise to
draw successive sides. Do not make any assumptions about the initial up/down state of the turtle. For full
credit you must use a loop for repeated operations.
The following is correct sample input and output:
import turtle
logy = turtle.Turtle()
pentagon(logy, 40)
The function filter returns a dictionary containing all key-value pairs in d in which the key is less than
kFilter and the value is less than vFilter.
• if a word starts with a consonant (any letter that is not a vowel), move the consonant to the end of
the word and then append the suffix 'ay'.
Examples:
Write a function named pigLatin that reads the English words on each line of an input file and writes the
Pig Latin translation of each word, followed by a space ( ' '), to a corresponding line of an output file.
Each line of the input file contains one or more English words, each followed by a space. Each line of the
input file is terminated by a newline ('\n'). Important: The input file contains only lower case letters,
spaces and newline characters.
The function pigLatin takes two parameters:
1. inFile, a string, the name of an input file that exists before pigLatin is called
2. outFile, a string, the name of an output file that pigLatin creates and writes to