|
First Summer 2006 |
n and prints a pattern like the following, with n
stars in the longest line.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
a) Write a function namedmake_setthat takes a list and returns a set. Hint: copy the list, sort the copy, and remove duplicates from it before returning it.
b) Write a predicate namedset_equalsthat takes two sets and indicates if they are equivalent as sets (have the same values, but order may be different).
c) Write a function namedset_differencethat takes two sets and returns a set of the values in the first set that are not in the second set. (This is related to the difference function described earlier, which was also named after the mathematical set difference operation, but is not the same in several respects.)
d) Write a function namedset_unionthat takes two sets and returns a set of the values that are in either one.
e) Write a function namedset_intersectionthat takes two sets and returns a set of the values that are in both sets.
lengths that takes a list of strings and returns a list whose values are the lengths of the corresponding
value in the given list.
i consists of those elements of each row with index i. (A matrix is basically a
table of numbers.) In the following examples, assume m == [[1,2], [4,5]]
Write a functionrowSumthat takes a matrix and returns a list of the sums of the numbers in the corresponding rows.
ThusrowSum(m) == [1+2, 4+5]Write a function
columnSumthat takes a matrix and returns a list of the sums of the numbers in the corresponding column.
ThuscolumnSum(m) == [1+4, 2+5]A matrix is square if it has the same number of rows and columns. The diagonal of a square matrix is the sequence whose
i-th element is at the intersection of rowiand columni.Write a functiondiagonalthat takes a square matrix and returns a list representing its diagonal. Thusdiagonal(m) == [1, 5]. In the transpose,mt, of a matrixmthe rows and columns ofmexchange places. Hence for valid indicesiandj,mt[i][j] == m[j][i]. The transposing of a square matrix looks like flipping it around its diagonal. Write a functiontransposethat takes a matrix and returns its transpose.Write a function
matrixString(m, n)that returns a multi-line string in which lines represent the rows of matrix m with elements in columns of widthn. (Hint: write a functionrjustwhich pads with spaces to the left and use it. Maybe strings already have such a function). As an example:matrixString(m, 3) == ' 1 2\n 4 5\n'
word_frequency that takes a string, s, and returns a list of tuples associating each word in s
with the frequency of its occurence. Consider words to be sequences of characters separated by whitespace, making the string method split very handy. Word comparison
should be case-insensitive, so for example Cat and cat would both contribute to the count the dictionary associates with key cat. Also ignore all characters that are
not letters or numbers, so for example end; (including a semicolon) would contribute to the count for the word end. Can you solve this problem without the use of a dictionary?
box(width, height)
that prints an "ASCII art" box of the given dimensions,
which are assumed to be greater than or equal to 2. The
box is "hollow", having one or more spaces inside if the
width and height are both greater than 2.
Next, define a>>> box(5, 4) ***** * * * * *****
Box type of object that has the width and the height as instance variables.
Write a function incrementLetters(s) that returns a string whose characters are obtained by substituting for each corresponding letter in string s the next letter in the alphabet, with Z becoming A. Maintain the same letter case (upper or lower), and leave non-letter characters unchanged. Hints: The built-in functions ord and chr convert a character to its corresponding integer and visa-versa, and the string method isalpha is a predicate indicating if its argument is a letter. You may also wish to use the string method islower that indiates if its argument is a lower-case letter.
Write a function rotateLetters(n, s) that has the effect of applying incrementLetters n times (that is, replacing each letter by the one n charactesr above it in the alphabet, rotating from Z back to A. Try writing this both the easiest way, actually calling incrementLetters n times, and more efficiently by iterating over the string only once. Hints for the efficent approach: use the remainder operator, the string method islower that indicates the case of a letter, recall that there are 26 letters in the ASCII alphabet, and at first simplify the problem by assuming all characters are upper case.