You are on page 1of 2

CS1101 2023 Worksheet 7

Worksheet 7
General Instructions: Do not copy-paste from this file to terminal. If you have doubts, contact the
instructors or TAs. And do not panic!

• The first two tasks in this worksheet require you to use Python3 shell. You need to copy paste
your work (commands and outputs) to a file using gedit or nano.

• The last few problems in this worksheet will require you to write a program.

• You should keep all your files in CS1101/ws07 folder.

• Use gedit or nano to type your programs.

• The name of the programs should be prob-n.py for nth problem.

• Save the output of your program in a text file prob-n-output.txt.

• After you finish, create an archive of the folder ws07 with name ws07-idnumber.tgz and
upload in WeLearn.

1. Write a program that takes an integer n as input and produce a file, named fibonacci.txt
containing the first n numbers in a Fibonacci sequence.

2. Write a program that takes an integer n as input and produce a file, named factorial.txt
containing the first n real numbers together with their factorial. For example, if n is given as 5,
then the content of the file factorial.txt should be:

1 1
2 2
3 6
4 24
5 120

3. (a) Write a program that takes 5 names from the user as input and writes the names in a file
called: names.txt.
(b) Extend the above program to only write the names containing at least one vowel in either
capital or small case.

4. (a) Read the file roll-marks.txt containing the roll and marks of 50 students. Store the
roll numbers and marks in two separate lists roll and marks such that the i-th index
of marks contains the marks of the student having roll number stored in the i-th index of
roll (roll[i]).
(b) Write a program to give a grace marks of 5 to all the students who scored < 50. Your pro-
gram should store the roll number and the new marks in another file called new-marks.txt.
(c) Make another file called failed.txt to store only the roll numbers of the students who
have failed (considering 50 as the pass marks) after the grace marks.
(d) Write a program that will return the highest marks stored in the file: new-marks.txt.

IISER Kolkata 1
CS1101 2023 Worksheet 7

5. (a) Read only the marks stored in roll-marks.txt in a list marks.


(b) Sort the list in descending order.
(c) Make another file, called sorted-marks.txt and store the sorted marks in that file.

6. You have used a text editing software to type some text. After saving the article as, say
words.txt, you realised that you have wrongly typed character $ in place of alphabet s
everywhere in the article.
Write a function rectify() that takes a filename as parameter and makes another file with
the corrected version (correct.txt) of entire content of the file.
Test your program with the provided story.txt with all the characters "$" to be displayed as
an alphabet "s" on screen.
BONUS: Remove all the non-alpha numeric characters except fullstop from story.txt and
store it in a file named: processed.txt.

IISER Kolkata 2

You might also like