Difference between revisions of "CSC111 Homework 3 2011"
(→Your assignment) |
(→Problem #2) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 98: | Line 98: | ||
* The forms are slightly different from the ones we generated in class. | * 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. | + | * <font color="magenta">(This paragraph modified 9/30/11 at 4:20p.m.)</font> 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 first piece of information entered by the user should be the width of the box | ||
− | ** The | + | ** The following piece of information should be the name of the string to display in the box under the date box |
+ | ** The third piece of information should be the number of candidate names | ||
** The next pieces of information should be the names of the candidates | ** The next pieces of information should be the names of the candidates | ||
− | |||
*Below is an example of the exchange between the user and your program: | *Below is an example of the exchange between the user and your program: | ||
Line 235: | Line 235: | ||
Welcome to the SMITH Virtual Cash Machine | Welcome to the SMITH Virtual Cash Machine | ||
+ | |||
How much money do you need today? <u>57</u> | How much money do you need today? <u>57</u> | ||
Line 246: | Line 247: | ||
* Feel free to use different greetings in your program! What is important is for your program to output the correct number of bills. | * Feel free to use different greetings in your program! What is important is for your program to output the correct number of bills. | ||
+ | |||
+ | ==Submission== | ||
+ | |||
+ | |||
+ | rsubmit hw3 hw3d.py | ||
+ | |||
+ | |||
+ | ==Optional and Extra-Credit== | ||
+ | |||
+ | * Make your program ask the user for the number of different bill denominations she wants, the denomination, and the amount to withdraw. | ||
+ | |||
+ | * Your program will then print the output in a nicely formatted box, similar to the one shown below. | ||
+ | |||
+ | * Submit as follows: | ||
+ | |||
+ | rsubmit hw3 hw3x.py | ||
+-----------------------------------------------------+ | +-----------------------------------------------------+ | ||
− | | Welcome to the SMITH Virtual Cash Machine | + | | Welcome to the SMITH Virtual Cash Machine | |
+-----------------------------------------------------+ | +-----------------------------------------------------+ | ||
Line 263: | Line 280: | ||
| | | | | | ||
+------------------------------------- Good Bye! -----+ | +------------------------------------- Good Bye! -----+ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Troubleshooting= | =Troubleshooting= |
Latest revision as of 15:20, 30 September 2011
--D. Thiebaut 15:46, 27 September 2011 (EDT)
This assignment is due on Tuesday evening, Oct. 4th, at midnight. You can work on this assignment individually or in pairs.
Contents
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 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
# yourFirstName yourLastName
# 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", "President Christ", "Our Ninja TA", "The apprentice Ninja TA", "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 behave/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
Misc. Information
- About randomness, and random words, you may find this story interesting: A few million virtual monkeys randomly recreate Shakespeare.
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 in the response to the input.
- The forms are slightly different from the ones we generated in class.
- (This paragraph modified 9/30/11 at 4:20p.m.) 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 following piece of information should be the name of the string to display in the box under the date box
- The third piece of information should be the number of candidate names
- The next pieces of information should be the names of the candidates
- 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 hw3b.py < hw3test1.txt
- Verify that you get the output for two candidates
python3 hw3b.py < 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!)
Problem #3
- This problem is the same as Problem #1, but the poems are centered, with the longest poem starting at the leftmost column. In other words, the longest poem does not have any spaces in front of its first word.
- You will need to use the max() function, which returns the maximum of a list of numbers.
list = [ 1, 10, 3, 4, -10, 300, 2 ] print( max( list ) )
- will print 300
Submission
- Store your program in a file called hw3c.py and submit it as follows:
rsubmit hw3 hw3c.py
Problem #4
- Python offers 3 operators to perform divisions: /, //, and %.
- At the Python shell (where you see >>>), try these different operators with a variety of numbers:
>>> 3 / 2 >>> 11 // 4 >>> 11 % 2 etc...
- Once you understand how the % operator works with integers, imagine that I want to write a program for a teller machine (cash machine), and the program has to figure out the number of bills it has to give the customer depending on the amount she wants.
- For example, if she wants $56, you can ask python to compute 56 // 20 and you will get the number of $20-bills to give her. If you ask Python to compute 56 % 20, python will compute what is left over once you have given out all the $20-bills. You can apply this rule again to figure out how many $10 bills in what remains, and so on until you have figured out the quantities of $20, $10, $5 and $1.
Your assignment
- Write a program called hw3d.py that will ask the user how much money she wants to withdraw from the cash machine, and will output how many bills she will get in return.
- Here is an example of how your program should behave when one runs it:
Welcome to the SMITH Virtual Cash Machine
How much money do you need today? 57 Please lift your keyboard and find: 2 $20-bill(s) 1 $10-bill(s) 1 $5-bill(s) 2 $1-bill(s) Don't buy too much chocolate with that! Good bye!
- Feel free to use different greetings in your program! What is important is for your program to output the correct number of bills.
Submission
rsubmit hw3 hw3d.py
Optional and Extra-Credit
- Make your program ask the user for the number of different bill denominations she wants, the denomination, and the amount to withdraw.
- Your program will then print the output in a nicely formatted box, similar to the one shown below.
- Submit as follows:
rsubmit hw3 hw3x.py
+-----------------------------------------------------+ | Welcome to the SMITH Virtual Cash Machine | +-----------------------------------------------------+ How much money do you need today? 57 +-----------------------------------------------------+ | Please lift your keyboard and find: 2 $20-bill(s) | | 1 $10-bill(s) | | 1 $5-bill(s) | | 2 $1-bill(s) | | | | Don't buy too much chocolate with that! | | | +------------------------------------- Good Bye! -----+
Troubleshooting
- If you cannot submit using the rsubmit command, try the Web submission page: submit3.htm