0% found this document useful (0 votes)
12 views3 pages

PS - Files

The document describes two problems related to encrypting and decrypting text files. Problem 1 involves writing a function that takes input and output file names and encrypts the input file by alternating characters between pairs of lines and writing them to the output file. Problem 2 involves writing a reverse function that can decrypt an encrypted file produced by the first function.

Uploaded by

Michel Kordahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

PS - Files

The document describes two problems related to encrypting and decrypting text files. Problem 1 involves writing a function that takes input and output file names and encrypts the input file by alternating characters between pairs of lines and writing them to the output file. Problem 2 involves writing a reverse function that can decrypt an encrypted file produced by the first function.

Uploaded by

Michel Kordahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EECE 230 Introduction to Computation and Programming,

Section 1
Solving Session 5

Wed Oct 13, 2020

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

*
**
***
****
*****
******
*******
********
*********
**********

b) Function to append a string to a text file. Implement the function appendString(fileName,s),


which given a string fileName and a string s, appends s to the the file named fileName if it exists
(otherwise, a new file consisting of s should be created).
Use the following test program:

appendString("test10.txt","This was added in Part (b)")

This should modify ”test10.txt” to

*
**
***
****
*****
******
*******
********
*********
**********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.

Part I: (30 points)


Given two strings containing the names of an input text file and an output file, your
function is required to open the input file and store its content “encrypted” in the second
file. The method works as follows:

(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.

As an example, say that the original file contained


Ziad Rahbani is the son of the Lebanese famous composer Assi Rahbani
and Nouhad Haddad, the famous Lebanese female singer known as
Fairuz.

the output file would be

Zainadd NRoauhhbaadn iH aidsd atdh,e tshoen foafm otuhse LLeebba


anneessee ffeammaolues scionmgpeors ekrn oAwsns ia sRahbani
Fairuz.

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"

You might also like