CS 30 Homework 4

Due by class time Wednesday, February 22

If you wish, you may work with a partner on this assignment.

Reading

Practice Exercises (Optional)

Problems to Turn In

  1. Write a program to draw an 8 × 8 red and white chessboard on the screen, like the one shown below (the top left square should be white), using chessboard.py as your starting point. Your job is to fill in the code for the three functions drawRow, drawGrid, and drawChessboard. The drawChessboard function should call drawGrid, which should in turn call drawRow. You should not change the parameters of the functions.

  2. Write definitions for the following two functions:

    Then use these functions in a program called sums() that prompts the user for n and prints out the sum of the first n natural numbers and the sum of the cubes of the first n natural numbers. (See Homework 1 problems 6 and 7.)

  3. Write a function called triangleArea that returns the area of a triangle given the lengths of its three sides as input parameters. Then use the function in a program called triangle() that prompts the user for the sides and prints out the area. (See Homework 1 problem 5.)

  4. Rewrite the nameValue program from Homework 2 as a function. That is, nameValue should take a name as input, represented as a string, and return the corresponding numeric value. Example:

    >>> print "The name Zelle has value", nameValue("Zelle")
    The name Zelle has value 60
    >>>
    

    Next, write a function called fullnameValue that takes a full name as input and returns its total value. Hint: use string.split to break up the full name into pieces and then call your nameValue function on each of the pieces. Examples:

    >>> print fullnameValue("John Marvin Zelle")
    184
    >>> print fullnameValue("Hillary Rodham Clinton") + fullnameValue("Laura Bush")
    334
    >>>
    
  5. Rewrite the acronym program from week 3 as a function that takes a phrase as input and returns the corresponding acronym. Example:

    >>> print "Do this", acronym("as soon as possible")
    Do this ASAP
    >>>
    
  6. Do problem 15 on page 196. Call your main program faces(), which should call your drawFace function several times to draw faces of varying sizes on the screen.

Turning in Your Homework

Put your code for part 1 in a file called chessboard.py and your code for parts 2-6 in a separate file called assign4.py. Include your name at the top of each file as a comment, and submit them both using /common/cs/submit/cs30-submit. Be sure to run this command from the directory containing your files.

If you have questions about anything, don't hesitate to ask!