PS - Files
PS - Files
Section 1
Solving Session 5
Problem 1. Files
a) Create file of stars. Implement the function createFileOfStars(fileName, n), which given a
string fileName and an integer n, creates a file named fileName consisting of n lines: one star on the
first line, 2 on the second, . . ., and n stars on the n’th line.
Use the following test program:
createFileOfStars("test0.txt", 0)
createFileOfStars("test10.txt", 10)
It should create an empty file named ”test0.txt” and a file ”test10.txt” consisting of
*
**
***
****
*****
******
*******
********
*********
**********
*
**
***
****
*****
******
*******
********
*********
**********This was added in Part (b)
1
Problem 1 (45 points). Read file and plot: EECE 230 is fun and easy
Implement the function readFileAndPlot(fileName), which takes as input argument a string fileName,
opens the file named fileName and plots its content as detailed below. We assume that the file contains
data of two graphs formatted as follows:
• The first line is the figure title.
• The second line is the x-axis label.
• The third line contains, say, n numbers separated by spaces representing the x-coordinates of both
graphs.
• The fourth line is the label of the first graph
• The fifth line contains n numbers separated by spaces representing the y-coordinates of the first
graph.
• The sixth and seventh lines are similar to the fourth and fifth, but correspond to the second graph.
You are not asked to check if the file format is as above: assume that this is the case. Your function is
supposed to plot the two graphs in two different colors on the same figure, while showing the title, the
x-axis label, and the legend.
If nameHandle is a file handle, we know from class and the assignments how to read the lines one by
one using a for loop: for line in nameHandle. An alternative method, which is more suitable for this
problem, is via the readline method. After opening the file, invoking nameHandle.readline() returns
the first line of the file. Invoking nameHandle.readline() again returns the second line, and so on.
Any correct solution is worth full grade.
Test it on the file named “data.txt” consisting of:
In EECE 230, computational thinking is fun and Python syntax is easy
Number of weeks
1 2 3 4 5 6 7 8 9 10 11
Computational thinking level
1 1 1 1 1 3 3 3 3 3 1
New syntax level
2 2 2 2 2 0 0 1 0 0 2
You should get the following figure:
Problem 2. (40 points)
Your task in this problem is to write a function that implements a rather rudimentary
method to encrypt a text file.
(i) It works on pairs of lines; starts by lines 1 and 2, then proceeds to lines 3 and 4,
etc ... until the last line. If the number of lines in the input file is odd, only one
line will be left in which case that line will be unchanged.
(ii) For a pair of lines, the output file will also have two lines, each with the same
length as the original ones, except that the letters will be scrambled, alternating
one character from line one, then one character from line two.
If one of the lines is shorter than the other, then the remaining part of line is written
as is.
Test your function on the above example where the input file name is “input.txt” and
the output is named “output.txt”.
Part II:
Write a function that reverses the operation and decrypts the encrypted file
Test it on your encrypted file "output.txt"