Name: Score:____/ 10
Time: 120 mins No partial credit.
No calculator or any electronic device. You may only use your pen, pencil or blank
paper. Anything is outside the answer box will not be graded
This content belongs to following universities:
Massachusetts Institute of Technology
Carnegie Mellon University
University of California, Berkeley
Stanford University
For any wrong input = - 1 A = 10 D = 0.5 - 2.5
For any blank answer = 0 B = 6.5 - 7.5 F = (- 4) - 0
For any right input = + according score C=3-5
Massachusetts Institute of Technology: MITx 6.00.1x
1/ Filling in the blank.
[2.5 points]
Write a recursive Python function, given any integer n, to count and return the number
of occurrences of the digit 7 in n.
For example:
count7(717) -> 2
count7(1237123) -> 1
count7(8989) -> 0
This function has to be recursive; you may not use any other loops other than the given
one! This function takes in one integer and returns one integer.
def count7(n):
# Fill in the given sketeleon without adding any line.
count = 0
while____________:
_________________________________
_____________
Carnegie Mellon University: CMU 15-112
2/ Code Tracing / Environment Diagram
[2.5 points]
Indicate what the following code prints. Place your answers (and nothing else) in the
box below.
def ct4(L, depth=0):
if (L == [ ]):
return [(42, depth)]
if (len(L) < 2):
return [(L[0], depth)]
else:
i = len(L)//2
return [(L[i], depth)] + ct4(L[:i], depth+1) + ct4(L[i+1:], depth+1)
print(ct4([2,4,6,8]))
University of California, Berkeley: CS 61A
3/ What Would Python Display? (WWPD?)
[2.5 points]
For each of the expressions in the table below, write the output displayed by the
interactive Python interpreter when the expression is evaluated. The output may have
multiple lines. If an error occurs, write “Error”, but include all output displayed before the
error. If evaluation would run forever, write “Forever”. To display a function value, write
“Function”.
The interactive interpreter displays the value of a successfully evaluated expression,
unless it is None. Assume that you have first started python3 and executed the
statements below.
alpha = lambda f: lambda x: f(f(x))
alpha = alpha(alpha)
sigma = 1
gamma = lambda y: y + sigma
sigma = -1
>>> alpha(gamma)(5)
Stanford University: CS106AP
4/ Counting Word Frequency
[2.5 points]
We’re conducting an analysis on a PYTimes article and want to see which words occur
most frequently within the article. We don’t want to count common words (like “a”, “and”,
“the”, etc.) because they don’t tell us very much about what the article is discussing.
GOAL: Write a function get_word_frequencies()that reads each word in a file
indicated by filenameand returns a counts dictionary that maps from a word to the
number of times that that word appeared in the file. You should ignore words that are
contained in a given list of common_words, and you should handle words
case-insensitively (so that ‘Science’ and ‘science’ are treated as the same word).
Input: filename (string), common_words (list[string])
Returns: word_counts (dict: string -> int)
Input: filename (string), common_words (list[string]) Returns: word_counts (dict: string
-> int)
Assumptions:
● The file has multiple lines, which are separated by the newline character (‘\n’).
● Each line has no punctuation, and all words are separated by spaces (‘ ’).
● All words in common_wordsare completely lowercase.
● Note that in the example below, 'Science' and 'science' are counted as the
same
(case-insensitive) word.
Example with a sample file:
File contents:
1 Computer Science
2 is a fun science
common_words = [‘is’, ‘a’, ‘the’]
Given the above file contents and common_wordslist, your function should have the
following return value:
{'computer': 1, 'science': 2, 'fun': 1}
def get_word_frequencies(filename, common_words):
"""
Reads each word in a file and returns a dictionary of the
number of times each word occurs in a file, ignoring all
words contained in the list common_words. This function
should be case-insensitive.
Input:
filename (string): name of file to be processed
common_words (list of strings): list of words to be ignored
Returns:
word_counts (dict: string -> int): dict containing the # of
"""
# YOUR CODE HERE
—----------------------------End of the test—----------------------------