Difference between revisions of "CSC111 Homework 9 2011"
(→Requirements) |
|||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 12:28, 13 November 2011 (EST) | --[[User:Thiebaut|D. Thiebaut]] 12:28, 13 November 2011 (EST) | ||
---- | ---- | ||
− | + | __TOC__ | |
+ | <br /> | ||
<center> | <center> | ||
<font size="+2">Page under construction!</font> | <font size="+2">Page under construction!</font> | ||
Line 50: | Line 51: | ||
:if you worked on beowulf, or submit your program using this page: http://cs.smith.edu/~111a/submit9.htm | :if you worked on beowulf, or submit your program using this page: http://cs.smith.edu/~111a/submit9.htm | ||
+ | |||
+ | =Problem #2= | ||
+ | |||
+ | |||
+ | |||
+ | <center>[[Image:elevatorOutOfOrder.jpg]]</center> | ||
+ | |||
+ | <P> | ||
+ | <code><PRE> | ||
+ | JT 150 | ||
+ | TJ 80 | ||
+ | TT 120 | ||
+ | BT 170 | ||
+ | Mike 12 | ||
+ | AF 27 | ||
+ | Lea 78 | ||
+ | Leo 180 | ||
+ | AI 55 | ||
+ | GK 110 | ||
+ | </pre></code> | ||
+ | |||
+ | * We have a text file called '''people.txt''', and this file contains lines of text. | ||
+ | * Each line represents a person. The first string is that person's name (or initials), followed by a space, followed by a number representing that person's weight, in pounds. | ||
+ | |||
+ | * This list is the list of the first people who have signed up to use Google's first elevator to the moon when it becomes available (Check this [http://www.nytimes.com/2011/11/14/technology/at-google-x-a-top-secret-lab-dreaming-up-the-future.html NYT article] to see for yourself!) | ||
+ | |||
+ | * The only problem is that the total amount of weight allowed is limited, and that we may have to prevent some people from boarding. The maximum weight allowed is input by the user. | ||
+ | |||
+ | * Our only criterion for boarding the elevator will be their weight. | ||
+ | |||
+ | Write a program called '''hw9b.py''' that will compute and output the answers to the following questions: | ||
+ | |||
+ | ;Question 1 | ||
+ | :What is the maximum number of people that can fit in the elevator? | ||
+ | |||
+ | ;Question 2 | ||
+ | :What is the total weight of the people who fit in the elevator? | ||
+ | |||
+ | ;Question 3 | ||
+ | :What are the names of the people in the elevator? | ||
+ | |||
+ | ;Question 4 | ||
+ | :What are the names of the people who will have to wait? | ||
+ | |||
+ | * Use while loops! | ||
+ | |||
+ | * Make sure your program is robust! (It could be that the file does not exist, of that the person's name is several words long. It is also possible that the weight is a real number, e.g. 180.5) | ||
+ | |||
+ | * Once your program works, modify it and make it input the total weight allowed in the elevator from the user. The allowed range allowed by your program should be any number between 0 and 1000. If the number entered by the user is less than 0 or greater than 1000, your program should prompt the user to input that number again. | ||
+ | |||
+ | * Make sure your program works if the maximum weight allowed is 0, or when it is set to 1000. |
Revision as of 13:44, 15 November 2011
--D. Thiebaut 12:28, 13 November 2011 (EST)
Problem #1
- Write a program called hw9a.py that outputs the average length of the presidency of US presidents, categorized by party. Your program should get the information from a file called presidents.txt, whose contents is available here.
- If you are working on beowulf, you can get a copy of the file into your account this way:
getcopy presidents.txt
- Note that this file does not have triple double-quotes around the list of presidents, and that it does have a header, i.e. the first line defines what the different fields are, but is not a valid president line.
- You will get the contents of the file using a statement of the form
file = open( "presidents.txt", "r" )
Requirements
- Your program should use functions. Study Homework 7 a second time to get inspiration for your program organization.
- Your program will assume that if a president is incumbent, his time is the current year (2011) minus the time he came to office. So, for Obama, this would be 2011-2009 = 2 years. Don't use the number 2. Make your program compute the current year minus whatever the year in office is. This way your program can work for future presidents.
- To get the current year in Python, you need to import the datetime module, as illustrated below. now.year is an integer.
import datetime now = datetime.datetime.now() currentYear = now.year print( "The current year is", currentYear )
- The output of your program should be only a few lines long. We do not want to see the list of presidents, but just something like this:
Average length of presidency. File presidents.txt contains 44 presidents. Democratic presidents: x.xx years Republican presidents: x.xx years Others: x.xx years
- The number of presidents (44) should be the number found by your program in the text file. When I test your program, I may provide a presidents.txt file with fewer than 44 presidents. Or a file that has just a header and no presidents at all! Or even a file with nothing in it, just empty. Be sure you test your program well so that it doesn't crash on any of these potential input files.
Submission
- Submit your program as follows:
rsubmit hw9 hw9a.py
- if you worked on beowulf, or submit your program using this page: http://cs.smith.edu/~111a/submit9.htm
Problem #2
JT 150
TJ 80
TT 120
BT 170
Mike 12
AF 27
Lea 78
Leo 180
AI 55
GK 110
- We have a text file called people.txt, and this file contains lines of text.
- Each line represents a person. The first string is that person's name (or initials), followed by a space, followed by a number representing that person's weight, in pounds.
- This list is the list of the first people who have signed up to use Google's first elevator to the moon when it becomes available (Check this NYT article to see for yourself!)
- The only problem is that the total amount of weight allowed is limited, and that we may have to prevent some people from boarding. The maximum weight allowed is input by the user.
- Our only criterion for boarding the elevator will be their weight.
Write a program called hw9b.py that will compute and output the answers to the following questions:
- Question 1
- What is the maximum number of people that can fit in the elevator?
- Question 2
- What is the total weight of the people who fit in the elevator?
- Question 3
- What are the names of the people in the elevator?
- Question 4
- What are the names of the people who will have to wait?
- Use while loops!
- Make sure your program is robust! (It could be that the file does not exist, of that the person's name is several words long. It is also possible that the weight is a real number, e.g. 180.5)
- Once your program works, modify it and make it input the total weight allowed in the elevator from the user. The allowed range allowed by your program should be any number between 0 and 1000. If the number entered by the user is less than 0 or greater than 1000, your program should prompt the user to input that number again.
- Make sure your program works if the maximum weight allowed is 0, or when it is set to 1000.