Homework 3

advertisement
Homework 3
Due February 19, 2016
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 two days late with a 10% penalty for each day (24 hours).
Make sure your name and FSUID are in a comment at the top of the file.
In this assignment, you may only use the sys, strings, and time packages.
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.
A sample run of mixed cipher.py with this test file would produce the following output. Note
that capitalization and punctuation are kept as-is in the encrypted text.
$ 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.
Assignment 3 continues on the next page.
1
2
timer_d.py (25 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(). The time.time() function will be of
use in this part of the assignment. 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
3
fibonacci.py (25 points)
Create a module named fibonacci, which defines the Fibonacci class. An instance of the
Fibonacci class is instantiated with a single integer argument which determines the number of
Fibonacci sequence numbers that should be stored in the list attribute nums. The Fibonacci
sequence includes the numbers 0 and 1. Every subsequent term in the sequence is defined as
the sum of the two previous terms. For example, the first ten terms of the Fibonacci sequence
are:
0, 1, 1, 2, 3, 5 , 8, 13, 21, 34
The Fibonacci class must define a get_nums() method which returns the list of Fibonacci
numbers. The Fibonacci class must also be defined such that it can be used in the context of a
for-loop, as shown below, as well as with the print statement. Your output should match mine
exactly.
>>> from fibonacci import Fibonacci
>>> f = Fibonacci(10)
>>> print f
The first 10 Fibonacci numbers are [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> f.get_nums()
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> for item in Fibonacci(8):
...
print item,
...
0 1 1 2 3 5 8 13
Now, in the same module, define the generator fibonacci_gen() which accepts a single integer
argument which determines the number of Fibonacci sequence numbers that should be
generated. Your generator should exhibit the following behavior:
>>> from fibonacci import fibonacci_gen
>>> for i in fibonacci_gen(12):
...
print i, ...
0 1 1 2 3 5 8 13 21 34 55 89
Note that you will receive 0 points for this part if your fibonacci_gen call returns a list object. It
must generate the elements on the fly as they are needed in the for-loop.
Download