Difference between revisions of "CSC111 Homework 4 2014"
(→The Output) |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
<bluebox> | <bluebox> | ||
− | This assignment can be | + | This assignment can be done individually or in Programming-Pair mode. You are not restricted in your selection of a partner if you are working in pair. The assignment is due on Thursday evening, February 27th, 2014, at midnight. |
</bluebox> | </bluebox> | ||
Line 14: | Line 14: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
− | =Problem #1= | + | =Problem #1: Rock-Paper-Scissors (50%)= |
<br /> | <br /> | ||
Rewrite the Rock-Scissors-Paper game using '''while''' loops. Call your program '''hw4a.py'''. | Rewrite the Rock-Scissors-Paper game using '''while''' loops. Call your program '''hw4a.py'''. | ||
Line 24: | Line 24: | ||
* The program will keep on prompting the user if the input is not one of 'P', 'R', or 'S'. The program will accept valid letters in lower or upper case, and with extra spaces before or after the letter. | * The program will keep on prompting the user if the input is not one of 'P', 'R', or 'S'. The program will accept valid letters in lower or upper case, and with extra spaces before or after the letter. | ||
* The program will indicate the winner of each round. | * The program will indicate the winner of each round. | ||
− | * The program will output a final | + | * The program will stop and output a final message at the end, when the difference in score is 3, pointing out who the winner is. |
<br /> | <br /> | ||
+ | ==Comments== | ||
<br /> | <br /> | ||
− | =Problem #2: Magic Lily= | + | * Make sure your program contains a header at the top, with the name of your program, your name and 2-letter Id number (and those of your partner if you are programming in pair), and a description of the program. |
+ | * Add comments before the main logical sections of your program. | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | =Problem #2: Magic Lily (50%)= | ||
<br /> | <br /> | ||
Assume that we have a put a magic lily in Paradise Pond. The magic lily grows in surface every 24 hours. When we first put it in the pond, it is only 1 square inch in area. We approximate the size of the pond to be 31,416 square feet. | Assume that we have a put a magic lily in Paradise Pond. The magic lily grows in surface every 24 hours. When we first put it in the pond, it is only 1 square inch in area. We approximate the size of the pond to be 31,416 square feet. | ||
Line 34: | Line 40: | ||
We assume that the lily is magic is that it can cover areas that are as irregular as the pond is. It's magic, remember? | We assume that the lily is magic is that it can cover areas that are as irregular as the pond is. It's magic, remember? | ||
− | Write a program that uses a '''while''' loop and that outputs the number of days it will take for the lily to cover the whole pond. Make your program output all the known information and the final result (the actual | + | Write a program that uses a '''while''' loop and that outputs the number of days it will take for the lily to cover the whole pond. Make your program output all the known information and the final result (the actual number of days shown here is incorrect, on purpose). |
Line 43: | Line 49: | ||
Minimum number of days required to cover the whole pond (or more): 16 days | Minimum number of days required to cover the whole pond (or more): 16 days | ||
+ | Make sure you document your program well. Put a header at the top and add comments in the code. Look at the Homework 3 solutions for examples of good documentation. | ||
+ | |||
Call your program '''hw4b.py'''. | Call your program '''hw4b.py'''. | ||
<br /> | <br /> | ||
Line 60: | Line 68: | ||
===Approximating the Area of the Pond=== | ===Approximating the Area of the Pond=== | ||
<br /> | <br /> | ||
− | I simply tried to cover a map of the pond with disks that had a diameter of 200 ft. Some disks overlap, some | + | I simply tried to cover a map of the pond with disks that had a diameter of 200 ft. Some disks overlap, some parts of the pond are not covered, and I tried to make both the overlap and the non-covered areas about the same size. I counted 16 disks. |
<br /> | <br /> | ||
<center>[[Image:ParadisePondArea.png|650px]]</center> | <center>[[Image:ParadisePondArea.png|650px]]</center> | ||
Line 67: | Line 75: | ||
<br /> | <br /> | ||
− | =Problem 3 (Optional and Extra Credits)= | + | =Problem 3 (Optional and Extra Credits: 10%)= |
<br /> | <br /> | ||
− | Write a program called '''hw4c.py''' that gets input from the keyboard and outputs some statistics about the input. TAs are allowed to help out with this assignment. | + | Write a program called '''hw4c.py''' that gets input from the keyboard and outputs some statistics about the input. TAs are allowed to help out with this assignment. |
<br /> | <br /> | ||
==The Input== | ==The Input== | ||
<br /> | <br /> | ||
− | * The input provided by the user will be lines containing the name of a city followed by a temperature. The name of the cities will be either Boston, New York, or Montreal. The city names will never be misspelled. | + | * The input provided by the user will be lines containing the name of a city followed by a temperature. The name of the cities will be either Boston, New York, or Montreal. The city names will never be misspelled. |
− | * The temperature will be an integer number. | + | * The city name will always be the first word on a line |
+ | * A line will never contain anything except a city name and an integer number, or the word "end" by itself. There may be many spaces at the front or end of the line, as well as in between the word and the integer. | ||
+ | * The temperature will always be an integer number. The number may be negative. | ||
* There will be at least one space between the city name and the temperature. | * There will be at least one space between the city name and the temperature. | ||
* When the user does not have any more information to provide, she will enter the single word '''end''' on the last line. The program will use this word as a sentinel and will stop getting input from the user. | * When the user does not have any more information to provide, she will enter the single word '''end''' on the last line. The program will use this word as a sentinel and will stop getting input from the user. | ||
<br /> | <br /> | ||
+ | |||
==The Output== | ==The Output== | ||
<br /> | <br /> | ||
Line 86: | Line 97: | ||
Please enter temperature recording for Boston, New York or Montreal. Enter the word "end" to stop. | Please enter temperature recording for Boston, New York or Montreal. Enter the word "end" to stop. | ||
> '''Boston 20''' | > '''Boston 20''' | ||
+ | > '''New York 10''' | ||
+ | > ''' Boston 40''' | ||
> '''Boston 33''' | > '''Boston 33''' | ||
− | |||
− | |||
> '''end''' | > '''end''' | ||
Line 101: | Line 112: | ||
* You do not need any new constructs. All the Python constructs we have seen so far are sufficient to solve this problem. | * You do not need any new constructs. All the Python constructs we have seen so far are sufficient to solve this problem. | ||
* the word '''end''' will act as a sentinel | * the word '''end''' will act as a sentinel | ||
− | * | + | * Remember: it is easier to solve a simpler version of the problem first. |
+ | ** '''You should first solve a simpler problem where you ask the user to enter a collection of numbers followed by a sentinel, and then output the average of all the numbers entered'''. | ||
+ | ** Once you can solve this first simple problem, write a program that expects only one city on the input; say Boston. If you can solve this second simpler problem, then solving Problem 3 with 3 cities will be a short hop away! | ||
* You do not need anything fancy to get the temperature from the string that will contain the city name and the temperature. If you can remove Boston from "Boston 31" you get " 31" and you should be able to easily transform this into the integer '''31'''. | * You do not need anything fancy to get the temperature from the string that will contain the city name and the temperature. If you can remove Boston from "Boston 31" you get " 31" and you should be able to easily transform this into the integer '''31'''. | ||
+ | * Print everything that takes place in your loop to make sure your logic is right. Remove the debugging print statements before submitting your program, though! | ||
+ | |||
+ | <br /><br /> | ||
=Submission= | =Submission= |
Latest revision as of 17:18, 21 February 2014
--D. Thiebaut (talk) 12:26, 18 February 2014 (EST)
This assignment can be done individually or in Programming-Pair mode. You are not restricted in your selection of a partner if you are working in pair. The assignment is due on Thursday evening, February 27th, 2014, at midnight.
Contents
Problem #1: Rock-Paper-Scissors (50%)
Rewrite the Rock-Scissors-Paper game using while loops. Call your program hw4a.py.
Requirements
- The program will stop only once the difference in score between computer and human players is 3.
- You should not include the mug of Homework 3 in this program/game.
- The program will keep on prompting the user if the input is not one of 'P', 'R', or 'S'. The program will accept valid letters in lower or upper case, and with extra spaces before or after the letter.
- The program will indicate the winner of each round.
- The program will stop and output a final message at the end, when the difference in score is 3, pointing out who the winner is.
Comments
- Make sure your program contains a header at the top, with the name of your program, your name and 2-letter Id number (and those of your partner if you are programming in pair), and a description of the program.
- Add comments before the main logical sections of your program.
Problem #2: Magic Lily (50%)
Assume that we have a put a magic lily in Paradise Pond. The magic lily grows in surface every 24 hours. When we first put it in the pond, it is only 1 square inch in area. We approximate the size of the pond to be 31,416 square feet.
We assume that the lily is magic is that it can cover areas that are as irregular as the pond is. It's magic, remember?
Write a program that uses a while loop and that outputs the number of days it will take for the lily to cover the whole pond. Make your program output all the known information and the final result (the actual number of days shown here is incorrect, on purpose).
Magic Lily Initial size: 1 square inch Growth rate: 2 times its size in 1 day Pond size: 31416 square feet Minimum number of days required to cover the whole pond (or more): 16 days
Make sure you document your program well. Put a header at the top and add comments in the code. Look at the Homework 3 solutions for examples of good documentation.
Call your program hw4b.py.
Additional Information
Computation
Those of you who are math inclined can probably figure out an equation giving the size of the lily after some number of days. You are welcome to code this equation to verify the output of your while loop, but you need to also compute the solution using a while loop that doubles the size of the lily every iteration.
Conversion
Use the Web to convert square feet into square inches. Wolfram Alpha is a great resource for every type of translation from one unit into another.
Where did Paradise Pond get its name?
Could it be the Swedish Nightingale Jenny Lind? You can read all about it here!
Approximating the Area of the Pond
I simply tried to cover a map of the pond with disks that had a diameter of 200 ft. Some disks overlap, some parts of the pond are not covered, and I tried to make both the overlap and the non-covered areas about the same size. I counted 16 disks.
Problem 3 (Optional and Extra Credits: 10%)
Write a program called hw4c.py that gets input from the keyboard and outputs some statistics about the input. TAs are allowed to help out with this assignment.
The Input
- The input provided by the user will be lines containing the name of a city followed by a temperature. The name of the cities will be either Boston, New York, or Montreal. The city names will never be misspelled.
- The city name will always be the first word on a line
- A line will never contain anything except a city name and an integer number, or the word "end" by itself. There may be many spaces at the front or end of the line, as well as in between the word and the integer.
- The temperature will always be an integer number. The number may be negative.
- There will be at least one space between the city name and the temperature.
- When the user does not have any more information to provide, she will enter the single word end on the last line. The program will use this word as a sentinel and will stop getting input from the user.
The Output
The program will output the average temperature (as a float or as an integer) in each of the 3 cities.
Here is an example of an interaction with the program. The user input is in boldface.
Please enter temperature recording for Boston, New York or Montreal. Enter the word "end" to stop. > Boston 20 > New York 10 > Boston 40 > Boston 33 > end Average temperature for Boston: 31 Average temperature for Montreal: None entered Average temperature for New York: 10
Hints
- You do not need any new constructs. All the Python constructs we have seen so far are sufficient to solve this problem.
- the word end will act as a sentinel
- Remember: it is easier to solve a simpler version of the problem first.
- You should first solve a simpler problem where you ask the user to enter a collection of numbers followed by a sentinel, and then output the average of all the numbers entered.
- Once you can solve this first simple problem, write a program that expects only one city on the input; say Boston. If you can solve this second simpler problem, then solving Problem 3 with 3 cities will be a short hop away!
- You do not need anything fancy to get the temperature from the string that will contain the city name and the temperature. If you can remove Boston from "Boston 31" you get " 31" and you should be able to easily transform this into the integer 31.
- Print everything that takes place in your loop to make sure your logic is right. Remove the debugging print statements before submitting your program, though!
Submission
Submit your programs to cs.smith.edu/~thiebaut/111b/submit4.php.