-
Do these instructions for installing PyDev and Eclipse.
- PyDev is an integrated development environment (IDE) that runs inside the general-purpose IDE called Eclipse.
- We will begin using PyDev in your next session (Session 6).
- Start early enough on the above installation
so that you can get help from someone
(e.g. lab assistants from 7 p.m. to 11 p.m.)
if you have trouble.
-
(30 pts) Decoding messages: the accumulator loop pattern, chr and ord. Suppose you are a security
agent who encrypts messages that you send to other agents. Of
course, the receiving agents need to be able to decode the
encoded messages they receive from you.
Assuming that both you
and the agents receiving your messages share a common key that
you use to encode and decode messages, write a decode module in
a file named decode.py that:
- Prompts the user for the list of numbers that encodes the message.
- Prompts the user for a key (an integer between -30 and 100).
- Reverses the calculation of this encode module
and prints the string that results.
Here is a sample run, assuming that the encoding was done with key = 42. Note the format of the input and output,
where red shows what the user types.
Enter the list of numbers that encodes the message: [126, 153, 154, 74, 157, 143, 141, 156, 143, 158]
Enter the key (an integer between -30 and 100): 42
The decoded message is: Top secret
- The input is entered as a list, so should you use input or raw_input?
- Recall that ord and chr are inverses: ord takes a charcter and returns its numeric code,
while chr takes a numeric code and returns the corresponding character.
- The encode.py module uses the accumulator pattern, building up the list that forms the encoded message.
Your decode.py module should also use the accumulator pattern,
but building up the string that forms the decoded message.
- Alternatively, you could build up a list of characters that forms the decoded message and then use the join function to join the list into a string.
- Submit your Python source file decode.py to the Decode drop box in the Homework 5 folder on ANGEL.
-
(15 pts)
Loops and string formatting: Storing
information in files is an important exercise for
engineers and scientists running experiments that generate a large quantity of useful
data. The stored data can later be analyzed,
categorized, and manipulated to allow engineers/scientists to draw
useful conclusions.
In this problem, you will write the data to the screen; in the next problem,
you will actually write it to a file.
Often, the generated data is formatted so that another
program can automate the process of analyzing, categorizing,
and manipulating the data. In this problem you are to write a program called
functionToConsole.py that implements the following design:
Here is a sample run, where red shows what the user types.
How many data points do you want? 20
0 300.000
1 296.962
2 287.939
3 273.205
4 253.209
5 228.558
6 200.000
7 168.404
8 134.730
9 100.000
10 65.270
11 31.596
12 0.000
13 -28.558
14 -53.209
15 -73.205
16 -87.939
17 -96.962
18 -100.000
19 -96.962
Submit your Python source file functionToConsole.py to the FunctionToConsole drop box in the Homework 5 folder on ANGEL.
-
(15 points)
Repeat the previous problem, but this time name your program functionToFile
and have the program write the output to a file named dataPoints.txt instead of to the screen.
- You continue to get input from the console exactly as in the previous problem; the only difference is that the output goes to a file.
-
(15 points)
Repeat problem 3 above, but this time:
- In addition to asking the user how many data points, also ask for the function whose values you will print .
The program should use the given (inputted) function instead of
100 + 200 cos(nπ/18)
The given (inputted) function should be a single-variable function of n .
-
Name this program anyFunctionToConsole.
-
Print to the console, as in problem 3 above. You do NOT have to have the formatting line up nicely;
just use the same formatting string that you used in problem 3. Print num function values,
just as in problem 3, where the user inputs num.
Here are two sample run, where red shows what the user types.
As the examples suggest, your program should work for ANY single-valued function of n.
Sample run 1:
What function do you want to use? 4.815162342**n + 42
How many data points do you want? 10
0 43.000
1 46.815
2 65.186
3 153.643
4 579.581
5 2630.539
6 12506.234
7 60059.311
8 289035.098
9 1391590.683
Sample run 2:
What function do you want to use? 4 + 8*n + 15*n**2 + 16*n**3 + 23*n**4 + 42*n**5
How many data points do you want? 6
0 4.000
1 108.000
2 1920.000
3 12664.000
4 50196.000
5 148044.000
This problem is easy once you see the “trick” to it;
don't spend more than 15 minutes on this problem without seeking help!
The key ideas for solving this problem are:
- Have the user enter the function as a string. (So which is better: input or raw_input?)
- Use eval, with the input string as its argument, to evaluate the function and get the number to print.
To repeat: if you don't see how to do this problem, ask for help!
(Bring your questions to class, you'll have time in class to complete the problem then if you need to.)
Submit your Python source file anyFunctionToConsole.py to the AnyFunctionToConsole drop box in the Homework 5 folder on ANGEL.
- Web links, bacon, eggs, and spam: The spam.py module that you will use to test your Subclipse installation is a reference to the
Penguin sketch. We hope you enjoy this four minute cultural experience!