15-112 Fall 2012 Homework
5a
Autolab submission due Sunday, 30-Sep, at
10pm
Read these
instructions first!
- This part of the hw is SOLO.
As explained in the course syllabus, you must not discuss these problems or
share your code with anyone else except for the course staff and the tutors at
Academic Development. This includes students from former semesters (who may
indeed have the answers to these problems!).
- This portion is worth 50 pts (4 non-bonus problems worth 12.5 pts each).
- Be sure to name your functions exactly as specified, and take exactly the same parameters as specified!
- There is no starter file nor any public autograder this week. Be sure to
write your own test functions!!!
- solvesCryptarithm
Background: a cryptarithm is a puzzle where we start with a simple arithmetic
statement but then we replace all the digits with letters (where the same digit
is replaced by the same letter each time). We will limit such puzzles to
strings the form “A+B=C” (no spaces), where A, B, and C are positive integers.
For example, “SEND+MORE=MONEY” is such a puzzle. The goal of the puzzle is to
find an assignment of digits to the letters to make the math work out properly.
For example, if we assign 0 to “O”, 1 to “M”, 2 to “Y”, 5 to “E”, 6 to “N”, 7 to
“D”, 8 to “R”, and 9 to “S” we get:
S E N D 9 5 6 7
+ M O R E + 1 0 8 5
--------- ---------
M O N E Y 1 0 6 5 2
And so we see that this assignment does in fact solve the problem! Now, we
need a way to encode a possible solution. For that, we will use a single string
where the index of the letter corresponds to the digit it represents. Thus, the
string “OMY--ENDRS" represents the assignments listed above (the dashes are for
unassigned digits).
With this in mind, write the function solvesCryptarithm(puzzle, solution)
that takes two strings, a puzzle (such as “SEND+MORE=MONEY”) and a proposed
solution (such as “OMY--ENDRS"). Your function should return True if
substituting the digits from the solution back into the puzzle results in a
mathematically correct addition problem, and False otherwise. You do not have
to check whether a letter occurs more than once in the proposed solution, but
you do have to verify that all the letters in the puzzle occur somewhere in the
solution (of course). You may not use the eval() function. Also, you almost
surely will want to write at least one well-chosen helper function.
- areaOfPolygon
Write the function areaOfPolygon that takes a list of (x,y) points that are
guaranteed to be in either clockwise or counter-clockwise order around a
polygon, and returns the area of that polygon, as described
here.
For example (taken from that text), areaOfPolygon([(4,10), (9,7), (11,2),
(2,2)]) returns 45.5 (at least the result is almostEqual to 45.5).
- linearRegression
Write the function linearRegression that takes a list of (x,y) points and finds
the line of best fit through those points. Specifically, your function should
return a triple of floats (a,b,r) such that y = ax+b is the line of best fit
through the given points and r is the correlation coefficient as explained
here (and yes, you must follow this exact approach). For example (taken
from the text), linearRegression([(1,3), (2,5), (4,8)]) should return a triple
of 3 values approximately equal to (1.6429, 1.5, 0.9972), indicating that
the line of best fit is y = 1.6429x + 1.5, with a correlation coefficient of
0.9972 (so it’s a really good fit, too). Note that the result is approximately
equal to these values. We'll use a large-ish epsilon as noted above. Also
note: the notes we refer you to do not discuss the meaning of the sign
of r, so just return the absolute value |r|. And: you may ignore the
case of a vertical line in your linearRegression code.
- bestScrabbleScore
Background: In a Scrabble-like game, players each have a hand, which is a
list of lowercase letters. There is also a dictionary, which is a list of
legal words (all in lowercase letters). And there is a list of
letterScores, which is length 26, where letterScores[i] contains the point value
for the ith character in the alphabet (so letterScores[0] contains the point
value for 'a'). Players can use some or all of the tiles in their hand and arrange them in
any order to form words. The point value for a word is 0 if it is not in
the dictionary, otherwise it is the sum of the point values of each letter in
the word, according to the letterScores list (pretty much as it works in actual
Scrabble).
In case you are interested, here is a list of the actual letterScores for
Scrabble:
letterScores = [
# a, b, c, d, e, f, g, h, i, j, k, l, m
1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
# n, o, p, q, r, s, t, u, v, w, x, y, z
1, 1, 3,10, 1, 1, 1, 1, 4, 4, 8, 4,10
]
Note that your function must work for any list of letterScores as is provided by
the caller.
With this in mind, write the function bestScrabbleScore(dictionary, letterScores,
hand) that takes 3 lists -- dictionary (a list of lowercase words), letterScores (a list
of 26 integers), and hand (a list of lowercase characters) -- and returns a tuple of the
highest-scoring word in the dictionary that can be formed by some
arrangement of some subset of letters in the hand, followed by its score.
In the case of a tie, the first element of the tuple should instead be a list of
all such words in the order they appear in the dictionary. If no such
words exist, return None.
Note: there is no fixed dictionary here. Each time we call the function,
we may provide a different dictionary! It may contain 100 words or perhaps
100,00 words.
- bonus/optional: nthLuckyPrime [2.5 pts]
Write the function nthLuckyPrime that takes a non-negative int n and returns
the nth number that is both lucky and prime, where a lucky number is as defined
here (or, identically,
here). Here
are the first several lucky primes: 3, 7, 13, 31, 37, 43, 67, 73, 79, 127,
151, 163, 193, 211...
Hint: to do this, you will need to use a list to implement a sieve, similar to
how we created the Sieve of Eratosthenes.
Bonus/Optional Graphics: Read this
carefully! The following problems are
bonus/optional. They are graphical, and you should be certain to place
them (and any helper functions they require) below the #ignore_rest line in your
hw5.py submission. Unlike the autograded problems, the bonus graphics problems will be graded only in person on
Monday or Tuesday night at office hours. To get your bonus graphics grade, go to
office hours where you should download your submitted code (and it must be just
as submitted, with no modifications), run it and show the CA the result.
The CA will grade your bonus at that time, mainly visually but also by looking
at the code as needed.
- Bonus/Optional: linearRegression
Visualization [2.5 pts]
Write a function that takes a list of (x,y) points, like the
linearRegression problem above, only here is generates a graphical
visualization showing the points, the coordinate axes, the line of best fit,
the actual squares of the errors (the vertical distances from each point to
the line), and the total sum of the squares of the errors, as in this
picture
here (which is part of
this web page). You don't have to match the picture perfectly, but
you should be reasonably close.
- Bonus/Optional: Static Motion AfterEffect
[2.5 pts]
Write a function that draws
this picture using only Tkinter primitives (rectangles, ovals, polygons,
arcs, etc).
carpe
diem - carpe
diem - carpe diem -
carpe diem - carpe diem
- carpe diem -
carpe diem - carpe diem
- carpe diem