CSC111 Homework 3 2011

From dftwiki3
Revision as of 14:07, 27 September 2011 by Thiebaut (talk | contribs) (Testing)
Jump to: navigation, search

Page under construction!
UnderConstruction.jpg

This assignment is due on Tuesday evening, Oct. 4th, at midnight. You can work on this assignment in pairs.


Problem #1: Random poems

It is sometimes necessary to select random items in a list. Generating random numbers is an easy task, and is can be done by using the randrange() function that is available in a standard Python program called random.py. Because this special program is part of the language, and available with all versions of python (for Linux, Windows, or the Mac), you do not need to physically get a copy of it as you did in the previous assignment. It is always there on the system, installed as part of the python language. All you have to do in your program is state that you want to use the function randrange() that is part of the random program.

Here is an example of how this works:

.

# hw3a.py
# 111a-xx
# firstName lastName
# this program prints a random word every time it is run
#
from random import randrange

def main():
   # create a list of animals
   farm = [ "pig", "cat", "dog", "horse", "donkey", "monkey", "snake", "fly" ]

   # figure out how many animals are on the farm
   animalCount = len( farm )

   # pick a number at random that is between 0 (included) and
   # the number of animals (excluded)
   randomIndex = randrange( animalCount )

   # select the animal at that index and print its name
   print( "the", farm[ randomIndex ], "lives on the farm" )

main()

.

Create this program in your 111a-xx account on Beowulf (or cut and paste it from this Web page). Observe that every time you run it, the output is unpredictable.

Using this new possibility, write a program that creates random poems. In our case, a poem will be just one line long, and will contain a subject, an adverb, a verb, a location, and a time. An example will clarify this idea.

Assume that we have a list of subjects, a list of adverbs, a list of verbs, a list of locations, and a list of times, as follows:

.

     subjects  = [ "The cat", "Mrs Christ", "Our CS prof", "Paris Hilton" ]
     adverbs   = [ "never", "sometimes", "usually", "amazingly", "voraciously" ]
     verbs     = [ "likes to sleep", "eats mice", "runs" ]
     locations = [ "around the pond", "on the sofa", "in Duckett House" ]
     times     = [ "at night", "early in the morning", "every Sunday afernoon", "whenever possible" ]

.

If we pick a random word from each list and "glue" all the words together, we end up with a one-line poem. For example Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon. Your assignment: write a program that declares a different series of lists (lists should not have the same number of words), and asks the user how many one-line poems she would like to see. In response the program will print that many different poems.

Here is an example of how your program should work:


Welcome to the Random Poems of the Day! 
How many poems would you like me to generate for you? 3

Here are your 3 poems:

Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon
The cat never runs on the sofa early in the morning
Mrs Christ sometimes runs on the sofa at night

Use your imagination when you create the lists of words, please!

Store your program in a file called hw3a.py and submit it as follows:


        rsubmit  hw3  hw3a.py

Problem #2

  • Using the top-down, step-by-step method in class on Tuesday 9/27 to generate forms (see this page for more info), create a program called hw3b.py that will ask the user for several pieces of information, and that will print several "forms" on the screen at the end.
  • The forms are slightly different from the ones we generated in class.
  • The order in which your program gets the information from the user must be the same as indicated here, otherwise your program will not work correctly when it is tested by my grading script.
    • The first piece of information entered by the user should be the width of the box
    • The second piece of information should be the number of candidate names
    • The next pieces of information should be the names of the candidates
    • The following piece of information should be the name of the string to display in the box under the date box
  • Below is an example of the exchange between the user and your program:
What is the width of the form? 60
What is the label of the box under the date? time:

How many candidates do you have? 3
Candidate 1: Albert
Candidate 2: Roberta
Candidate 3: Alexandra
+-------------------------------------------+--------------+
|Albert                                     | date:        |
|                                           +--------------+
|                                           | time:        |
|                                           +--------------+
|                                                          |
+----------------------------------------------------------+
| Comments:                                                |
|                                                          |
|                                                          |
|                                                          |
+----------------------------------------------------------+


+-------------------------------------------+--------------+
|Roberta                                    | date:        |
|                                           +--------------+
|                                           | time:        |
|                                           +--------------+
|                                                          |
+----------------------------------------------------------+
| Comments:                                                |
|                                                          |
|                                                          |
|                                                          |
+----------------------------------------------------------+


+-------------------------------------------+--------------+
|Alexandra                                  | date:        |
|                                           +--------------+
|                                           | time:        |
|                                           +--------------+
|                                                          |
+----------------------------------------------------------+
| Comments:                                                |
|                                                          |
|                                                          |
|                                                          |
+----------------------------------------------------------+



  • You can play with the solution program to better understand how it works (but you won't be able to look at its code :-) by login to your 111a-xx account, and by typing the following commands at the prompt:
  getcopy hw3bsol.pyc
  python3 hw3bsol.pyc


Testing

  • Your program will be tested by a script. I will not test your program by hand. The script will feed a file of information to your program, in the order indicated above. You can check whether your program will pass some of my tests by downloading two of the test files:
  getcopy hw3test1.txt
  getcopy hw3test2.txt
  • Then test your programs with the test files. At the Linux prompt, type:
  python3 hw3bsol.pyc < hw3test1.txt
Verify that you get the output for two candidates
 python3 hw3bsol.pyc < hw3test2.txt
Verify that you get no output, since the test file contains 0 for the number of candidates (a special case that your program should be able to handle).

Documentation

  • Make sure your program contains a header, with your name(s), account number(s), the name of the program, and a description of what it does.
  • Indicate what the limitations of the program are. In particular, if the program is not going to work well for some values of the width, or some candidate number, or candidate names, indicate that as well.
  • Similarly for the string to be stored in the box under the date.
  • Add comments throughout the program to enhance its logic, and its readability. Look at solution programs for past homework assignments for inspiration.

Submission

  • Submit your program using the rsubmit command from your 111a-xx account:
  rsubmit hw3 hw3b.py
(do not submit the solution program!)