First Summer 2006


Lecture Notes Eighteen: The End of the Beginning. Starting the Review for the Practical.
Here are some problems to keep us warm for the practical:

  1. Write a method that calculates the greatest common divisor of two integers.

  2. Write a program that prints a tip table with whole dollar amounts from 1 to 50 in the first column and 15% and 20% in the second and third columns. Just separate the columns with a single space to begin with, and add justification later if you like.

  3. Write a program that prompts for a series of floating point values, terminated by the empty string, and prints a message indicating if the values were entered in increasing sorted order. Extra challenge: indicate if they were in increasing or decreasing sorted order.

  4. An approximate value for the mathematical constant pi can be obtained by computing the value of some initial part of the series 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 and so on. Write a program that prompts for a positive integer n and prints the sum of the first terms in the pi series, and the difference between that approximation and the value of the constant math.pi (variable pi in the package math).

  5. Write a program that prompts for a positive integer n and prints a pattern like the following, with n stars in the longest line.
    *
    * *
    * * *
    * * * *
    * * * * *
    * * * * * *
    * * * * * * *
    * * * * * * * *
    * * * * * * * * *
    * * * * * * * * * *
    * * * * * * * * * 
    * * * * * * * *
    * * * * * * *
    * * * * * *
    * * * * *
    * * * *
    * * *
    * *
    *

  6. Write a program that simulates a fortune cookie. The program should display one of five unique fortunes, at random, each time it's run.

  7. Write a program that flips a coin 100 times and then tells you the number of heads and tails.

  8. Consider the guess my number game of chapter 3. Write a program in which the player and the computer trade places in the number guessing game. That is, the player picks a random number between 1 and 100 that the computer has to guess. Before you start think about how you guess. That's what the computer program should do.

  9. Write a function named difference that takes a list, lst, and a sequence, seq, and removes from lst all elements whose value is equal to an element of seq.

  10. Write a function named perfect_shuffle that takes two lists, lst1 and lst2, and returns a list that alternates the values in lst1 and lst2, with the elements of the longer list at the end if lst1 and lst2 are not the same length. Thus assuming both lists have at least two elements, the returned list would be of the form [lst1[0], lst2[0], lst1[1], lst2[1], ...]. Be sure your solution works even if one or both of the given lists is empty.

  11. Write a function commaForm that takes a string and returns a similar string, but with a comma between each three characters, counting from the right, as is common in representing large numbers.
  12. Write a program that counts for the user. Let the user enter the starting number, the ending number, and the amount by which to count.

  13. Create a program that gets a message from the user and then prints it out backwards.

  14. In mathematics, a set is a collection of values in which no value occurs more than once and in which the order of values is not significant. In Python it is natural to represent sets as lists for which the order of elements is not significant and no two elements have equal values.
    a) Write a function named make_set that 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 named set_equals that takes two sets and indicates if they are equivalent as sets (have the same values, but order may be different).


    c) Write a function named set_difference that 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 named set_union that takes two sets and returns a set of the values that are in either one.


    e) Write a function named set_intersection that takes two sets and returns a set of the values that are in both sets.

  15. Write a function named lengths that takes a list of strings and returns a list whose values are the lengths of the corresponding value in the given list.

  16. A matrix of numbers is usually represented in Python as a list of equal-length rows, each of which is also a list. Matrix column 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 function rowSum that takes a matrix and returns a list of the sums of the numbers in the corresponding rows.
    Thus rowSum(m) == [1+2, 4+5]

    Write a function columnSum that takes a matrix and returns a list of the sums of the numbers in the corresponding column.
    Thus columnSum(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 row i and column i.Write a function diagonal that takes a square matrix and returns a list representing its diagonal. Thus diagonal(m) == [1, 5]. In the transpose, mt, of a matrix m the rows and columns of m exchange places. Hence for valid indices i and j, mt[i][j] == m[j][i]. The transposing of a square matrix looks like flipping it around its diagonal. Write a function transpose that 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 width n. (Hint: write a function rjust which 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'

  17. Write a function named 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?

  18. Write a function 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.
    >>> box(5, 4)
    *****
    *   *
    *   *
    *****
    Next, define a Box type of object that has the width and the height as instance variables.

  19. Write a function reverse(lst) that takes a sequence and returns a list of its values in reverse order.

  20. Write a function indices(value, lst) that returns a list of the index numbers of each occurence of value in lst.

  21. Write a function powersOfTwo(lst) that returns a list whose values are two raised to the power indicated by the number in the corresponding position in lst.

  22. Write a function to compute the factorial of an integer.

  23. Use the last three functions to write a function binaryValue(s) that takes a string of 0 and 1 characters representing a binary number and returns its corresponding unsigned integer value.

  24. Write a function largestPower(n) that returns the largest power of two that is less than or equal to the integer n.

  25. 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.


Last updated: June 5, 2006 by Adrian German for A201/A597