Homework 2

advertisement
Homework 2
Due February 2, 2015
Submissions are due by 11:59PM on the specified due date. Submissions may be made
on the Blackboard course site under the Assignments tab. Late submissions will be accepted
up to one day late with a 10% penalty.
Make sure your name and FSUID are in a comment at the top of the file.
In this assignment, you will be writing two small Python programs to accomplish some useful
tasks. There is an optional extra credit task which I encourage you all to try. You may only
use the sys, strings, and time packages for this assignment.
1
mixed cipher.py (50 points)
Your task is to create a Python module called mixed cipher. You may have had some experience with writing programs that implement simple Caesar ciphers. In this assignment, we’ll
be implementing a small program which takes in a filename as a command-line argument
and then performs simple substitution on the file contents with a mixed alphabet. A mixed
alphabet can be created by prompting the user for the keyword, removing repeated letters
in the keyword, and then writing the remaining letters of the alphabet in the usual order.
For example, if my keyword is “computer”, then I have the following:
Plaintext: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Ciphertext: COMPUTERABDFGHIJKLNQSVWXYZ
Your program should output the “encrypted” text to the screen. Here’s an example execution. Let’s say I have a file called mixed test.txt which has the following contents:
This is a file. This file has some WORDS in it.
$ python mixed_cipher.py mixed_test.txt
Please enter a keyword for the mixed cipher: Motherboard
Plaintext:
abcdefghijklmnopqrstuvwxyz
Ciphertext: motherbadcfgijklnpqsuvwxyz
Sadq dq m rdge. Sadq rdge amq qkie WKPHQ dj ds.
2
weather analyzer.py (50 points)
Your task in this part of the assignment is to create a module called weather analyzer. The
weather analyzer module works by prompting the user for the name of a comma-separatedvalue (.csv) file which contains some weather data for a variable period of time. Each day is
recorded on a separate line. The known attributes are:
• TMAX: Maximum temperature in 1/10 of a degree Celsius for a given day.
• TMIN: Minimum temperature in 1/10 of a degree Celsius for a given day.
1
• AWND: Average daily wind speed in 1/10 meters per second for a given day.
• PRCP: Precipitation in 1/10 of a millimeter for a given day.
• DATE: Format is YYYYMMDD.
The position of the attributes is not known - some files may contain additional attributes.
Your module must prompt the user for the csv file which contains the weather data and
output a couple of statements about the file. These statements include:
• The maximum temperature reading over the time period in degrees Fahrenheit (up to
two decimal places) and the day it occurred.
• The minimum difference between the maximum and minimum temperature taken (in
degrees Fahrenheit) on a specific day and the day it occurred.
• The average wind speed in meters per second over the time period.
• The total precipitation amount over the time period in millimeters.
If there are, for example, multiple days that reach the maximum temperature, take the
earliest date. For example, let’s say I have a sample file called tallahassee weather.csv. It
contains some sample data gathered over a period of two months.
$ python weather_analyzer.py
File to be analyzed (.csv only): tallahassee_weather.csv
Maximum temperature (F): 80.96 on 11/12/2014
Minimum temperature difference (F): 2.88 on 12/21/2014
Average wind speed (m/s): 2.32
Total precipitation (mm): 441.4
3
Extra Credit (5 points)
Create a decorator which times (using the time package) the execution of an arbitrary
function which has no arguments and prints the results. You can simply save it in a file
called timer d.py and call the decorator function t decorator(). For example,
>>> import timer_d
>>> import time
>>> def myfunc():
...
time.sleep(5)
...
>>> mytimedfunc = timer_d.t_decorator(myfunc)
>>> mytimedfunc()
Time to execute function (s) : 5.00512385368
2
Download