Introduction to Computer Science I
Final exam practice questions
Implement in finalPractice.py the
functions described below.
1. Write
a function innerProduct() that
that takes as input two lists (of same length) of integers and then
computes and returns the inner
product of the two lists of integers. The inner product of
two lists x = [x1, x2, ..., xn] and
y = [y1, y2, ..., yn] is the value
x1 y1 + x2 y2
+
... + xn yn .
Usage:
>>>
innerProduct([2,3,4], [1,0,2])
10
2. Write a function digitsum()
that takes a positive integer n as input and returns the sum
of the digits of n.
Usage:
>>> digitsum(34)
7
>>> digitsum(934)
16
>>> digitsum(9314)
17
3. Write a function wordcount()
that takes the name of a text file as input and prints the
number of occurrences of every word in the file. You function should
be case-insensitive so 'Hello' and 'hello' are treated as the same
word. You should ignore words of length 2 or less. Test your
implementation on file frankenstein.txt.
Usage:
>>> wordcount('frankenstein.txt')
artifice
1
resting
2
compact
1
service
3
sixth
1
lulling
1
radiance
2
offices
1
tree
5
incline
1
treasures
3
unravel
1
oblivion
2
god
20
malignity
7
promontory
4
acknowledged 1
refinement
1
proceedings
1
surviving
1
inroads
1
arguments
7
pursuits
4
quit
17
...
...
4. Write a function called frequencies()
that takes a table (i.e., a two dimensional list) of numbers as
input and returns a dictionary that maps each number that
appears in the table to its frequency (i.e., number of times it
appears in the table)
>>> t = [[2,0,4,1], [3,1,3,3], [0,4,5,1]]
>>> frequencies(t)
{0: 2, 1: 3, 2: 1, 3: 3, 4: 2, 5: 1}
5. Write a function named lottery()
that takes an integer n as input and that prints 6 different
numbers in the range from 1 to 60, chosen uniformly at random,
as shown below:
>>> lottery(60)
27
10
58
28
60
2
>>> lottery(60)
6
33
21
55
16
1
6. Function fileLength(),
given to you, takes the name of a file as input and returns the
length of the file:
>>> fileLength('finalPractice.py')
125
>>> fileLength('finl.py')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in
<module>
fileLength('finl.py')
File "/Users/me/final.py", line 2, in fileLength
infile = open(filename)
FileNotFoundError: [Errno 2] No such file or directory:
'finl.py'
As shown above, if the file cannot be found by the interpreter or if
it cannot be read as a text file, an exception will be raised.
Modify function fileLength() so that a friendly message is
printed instead:
>>>
fileLength('finalPractice.py')
224
>>> fileLength('finl.py')
File finl.py not found.
7. The
constant Pi is an irrational number with value approximately
3.1415928... The precise value of Pi is equal to this infinite
sum:
Pi = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11+ ...
We can get a good approximation of Pi by computing the sum of the
first few terms. Write a function approxPi() that takes as
input a float-value error and approximates constant Pi
within error by computing the above sum, term by term,
until the difference between the current sum and the previous sum
(with one less term) is no greater than error. The function should
return the new sum.
>>> approxPi(0.01)
3.1465677471829556
>>> approxPi(0.0000001)
3.1415927035898146