Python Wrapup
This is a revision of problem set 7 which previously was due
on Thursday November 15. As you will see, this problem set absorbs
the material in what had been posted as the extra credit problem
set 8. NB that the new due date is Wednesday November 21.
Like some of the earlier problem sets, this problem set allows
you to choose which problems you would like to work on. This is
a 15 point problem set but the problems listed here can total up
to 36 points. As before, you can do more of the problems for
extra credit.
I encourage you to carefully review the python code
that has been posted on the resources page and as solutions to problem
sets 5 and 6.
- (3 Points) Write a python function factors that accepts
an integer n and which returns a list of all integer factors
of n. For example, the expression factors(18) could
evaluate to the list [1, 18, 2, 9, 3, 6].
- (1 Point) Write a python function isPrime that accepts
an integer n and which returns True if n is a
prime number and False otherwise. For example,
isPrime(13) should evaluate to True because the
only factors of 13 are 1 and 13.
- (2 Points) Integers m and n are relatively
prime if the only factor that they share is 1. Write a function
areRelativelyPrime which accepts two integers and returns
True if they are relatively prime. Otherwise it should return
False.
- (3 Points) Write a function gcd which accepts two integers
m and n and which computes the
greatest common divisor of m and n. For example,
gcd(15,20) should evaluate to 5 and
gcd(15,17) should evaluate to 1.
- (1 Point) Write a python function nth which
accepts an integer n and a list l and which
returns the nth element of l. For example,
the expression nth(3,[10,20,30,40]) should evaluate
to 30. NB that for the purposes of this problem, the
first element of the list is considered at position 1.
If n is greater than the length of l your
nth function should return 0.
- (2 Points) Write a python function match which
accepts two lists a and b and two integers
ai and bi. match should return True
if the list a matches list b starting at positions
ai and bi (resp.). For example, the expression
match([2,3,4,3], 1, [3,4,3,5], 0) should evaluate to
True.
- (3 Points) Write a python function isSublist which
accepts two lists a and b and returns True
if a is a sublist of b. For example,
isSublist([2,3,4], [2,3,2,3,4,5]) should return True
while isSublist([2,3,4], [2,3,2,3,5]) should return
False.
- (3 Points) Write a python function rotateRight which
accepts an integer n and a list lst. The function
should return the list obtained by rotating the lst to
the right n times. For example,
rotateRight(3,['A', 'B', 'C', 'D', 'E']) should return
the list ['C', 'D', 'E', 'B', 'A'].
- (3 Points) Write a python function merge which
accepts two lists of integers lst1 and lst2 in
non-decreasing order and which returns a single non-decreasing
list of integers combining the two. For example, the expression
merge([2,3,3,8], [1,8,9]) should evaluate to the list
[1,2,3,3,8,8,9].
- (3 Points) Write a python function uniquify that accepts
a list and returns the list obtained by removing all copies. For
example, uniquify([1,2,3,2,3,4]) should return the list
[1,2,3,4].
- (1 Point) Write a python function bar that accepts
two integers m and n and which uses the turtle graphics
module to draw a verticle bar depicting the ratio m/n. For
example, the call of the function bar(1,10) should draw a bar
extending 1/10 of the distance from the bottom edge of the window to
the top. The call bar(10,20) should cause the bar to extend
halfway up and, e.g., bar(33,33) should cause the bar to extend
all the way to the top. The width and color of the bar are up to you.
- (3 Points) Recall that the expected frequency of the totals
2, 3, 4, 5, 6, 7, 8, 9, 10, 11 or 12 obtained by rolling 2 dice
are easily computed by counting the various combinations that result
in each value. There is only one way to roll a 2, there are 2 ways
to roll a 3 and so forth.
Write a function diceExpectations which uses the turtle
graphics module to draw a
histogram
depicting the expected frequencies of rolling two dice. In particular,
the diceExpectations function should draw 11 verticle bars
depicting the expected frequencies. The width of each bar should be 1/11
the window width and the height of each bar should be determined by
the expected frequency. The height of the bar for 7 (i.e., the middle
bar) should be 75% of the height of the window and the height of the other
bars should be calibrated relative to that. For example, your histogram
might look as follows:
- (8 Points) Recall that the random module contains a
random function which returns a number greater than or equal to
0 and less than 1. The expression:
int(random.random() * m)
will then evaluate to an integer in the range from 0 up to m - 1. Thus,
the expression int(random.random() * 6) + 1
can be used to simulate the roll of a die.
Write a python function manyRolls which accepts an integer
n and which simulates n rolls of two dice. The function
should compute the total of the two dice and count up the number of
times each outcome occurs. (I.e., how many times did 2 appear, how
many times did 3 appear and so forth.) After completing the n
rolls, the manyRolls function should draw two histograms
depicting the outcome of the rolls. In particular, the
manyRolls function should use the diceExpectations
function to draw the histogram for the expected outcome and it should
then overlay a new histogram over the old one showing the outcome of
the simulation. For example, the histogram on the left reflects 10 rolls
while the one on the right reflects 100,000 rolls.
Your histogram doesn't have to look exactly like these, but it should
allow one to see how well the n simulations matches the
expected outcome.
|