USING PYTHON 1. Work with Census Data: Dowload the following file: http://www.ce
ID: 663173 • Letter: U
Question
USING PYTHON
1. Work with Census Data: Dowload the following file:
http://www.census.gov/population/www/censusdata/files/urpop0090.txt
The census _le is a text _le with data for the 10-year census from 1900 to 1990 (e.g.,
1900, 1910, 1920, . . . ). It has population data for each state as well as regional
and overall data. Each state is on its own line, but the data are grouped so that
only three decades of data are on each line complicating the task of extracting
the data. In addition, the data are further broken down into total, urban, rural,
and percentages. Write a program that for any census year input (e.g., 1970)
the program will print the state and its total population with the minimum and
maximum. For example:
Enter census year 1900 to 1990: 1970
Minimum: (302853, 'Alaska')
Maximum: (19971069, 'California')
The output is displayed as a tuple as a hint to assist with solving the problem
rather than illustrating readable output. Some points to consider:
(a) Begin by generating clean data: there are footnotes that need to be elim-
inated, numbers contain commas, some rows (lines) have data you are not
interested in (e.g., region data), you are not interested in all columns (e.g.,
percentages), and so on. Simply printing the lines with extraneous data re-
moved is a good start.
(b) You will likely want to combine multiple state name strings into one, e.g.,
New" York" becomes New York."
(c) A tuple (population, state) provides a way to tag population data with a state
in a way that allows a list of tuples to be sorted (remember that by default,
sorting uses the first value).
2. Write a class for linear equations. A generic linear equation is of the form y =
mx + b where m and b are constants. Include the following methods:
(a) init , str , repr .
(b) value(x), which returns the value of the equation given x.
(c) compose(LinearEquation) that composes two linear equations. That is, if y =
x+1 and z = 2a+5, then y(z) = 2x+6 and will be called as y.compose(z).
Note that the compose operation is not commutative.
(d) add returns the sum of two linear equations. That is, if y = ax + b and
z = cx + d, then y + z = (a + c)x + (b + d).
Include sample code that uses your class and demonstrates the use of all methods
as well as error handling.
3. Design a class Message that models an e-mail message. A message has a recipient,
a sender, and a message text. Support the following methods:
_ A constructor that takes the sender and recipient
_ A method append that appends a line of text to the message body
_ A method toString that makes the message into one long string like this:
"From: Harry Morgan
nTo: Rudolf Reindeer
n . . ."
4. Design a class Mailbox that stores e-mail messages, using the Message class of
Exercise 3. Implement the following methods:
_ def addMessage(self, message)
_ def getMessage(self, index)
_ def removeMessage(self, index)
5. Write a program that uses the Message and Mailbox classes from
problems 3 and 4 to make a message, print it and store it in a Mailbox.
Ineed this by today so i will be really greatful..thank u in advance
Explanation / Answer
3.Python Code:
return output
4,5
del self._mails[index]
class Message(): def __init__(self, sender, recipient): self._sender = sender self._recipient = recipient self._text = [] def get_sender(self): return self._sender def get_recipient(self): return self._recipient def add_message(self, message): self._text.append(message) def to_string(self): output = "From: {} To: {} ".format(self._sender, self._recipient) output += " ".join(self._text)return output
4,5
class Mailbox(): def __init__(self): self._mails = [] def list_messages(self): output = [] for i, message in enumerate(self._mails): output.append("[{}] From: {}, To: {}".format(i, message.get_sender(), message.get_recipient())) return " ".join(output) def add_message(self, message_object): self._mails.append(message_object) def get_message(self, index): return self._mails[index].to_string() def remove_message(self, index):del self._mails[index]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.