Registry Synced

Jan 2026 - Python - Week 8 - Graded Assignment 8

1207 words
6 min read

Week 8 - Graded Assignment 8

Course: Jan 2026 - Python
Week 8 - Graded Assignment 8
Last Submitted: You have last submitted on: 2026-04-08, 05:45 IST

Introduction

**Note to Students **
All the files that we will work with are non-empty. That is, each file will have at least one line of non-empty text in it. The last line in any given file will not have a '\n' at the end. That is, the last character of the last line of the file will not be a '\n'. We request learners to follow this convention while writing to files as well.
Common Data for questions (1), (2), (3) and (4)
scores.csv is a CSV file that has the following contents:
python
SeqNo,Name,Gender,CT,Python,PDSA
0,Ram,M,80,90,100
1,Sahana,F,90,80,70
2,Ritvik,M,80,100,80
3,Suchitra,F,100,100,100
4,Praful,M,60,70,85
5,Aisha,F,85,95,90
6,Andrew,M,100,65,75
7,Soundarya,F,85,75,65
8,Krishnan,M,95,85,90
9,Brinda,F,65,95,85
Answer questions (1) to (4) based on this data. Points to know.
If f represents a file object opened in read-mode, then f.readline() will read one line of the file and then move to the next line. Many times, we may want to ignore the header, so we just call f.readline() without assigning this line to any variable. This is as good as skipping the header and starting from the second line of the file.

Question 1

What is the output of the following snippet of code? (NAT)
python
def do_something(filename, sub):
    f = open(filename, 'r')
    # Ignore the header
    f.readline()
    # we are now at the beginning of the second line of the file
    val, count = 0, 0

    for line in f:
        sno, name, gender, ct, python, pdsa = line.strip().split(',')
        sno, ct, python, pdsa = int(sno), int(ct), int(python), int(pdsa)
        if sub == 'CT':
            val = val + ct
        elif sub == 'Python':
            val = val + python
        elif sub == 'PDSA':
            val = val + pdsa
        count = count + 1

    f.close()
    return val / count

# Hint: int(1.5) is 1
print(int(do_something('scores.csv', 'Python')))
Your Answer: (Not answered)
Status: Yes, the answer is correct. Score: Score: 3
Feedback: do_something('scores.csv', 'Python') computes the average marks of students in Python. It returns a float value, but to make things simple, it is converted into an integer in line-23 of the code.
Accepted Answers:
(Type: Numeric) 85

Question 2

What is the output of the following snippet of code?
python
def get_scores(filename, index):
    f = open(filename, 'r')
    # Ignore the header
    f.readline()
    # we are now at the beginning of the second line of the file

    scores = [ ]
    line = f.readline()
    while line != '':
        L = line.strip().split(',')
        scores.append(int(L[index]))
        line = f.readline()

    f.close()
    return scores
For what value of x does get_scores('scores.csv', x) return a list of the marks scored by the students in CT? (NAT)
Your Answer: (Not answered)
Status: Yes, the answer is correct. Score: Score: 2
Feedback: get_scores('scores.csv', 3) returns the list of scores of students in the subject CT.
Accepted Answers:
(Type: Numeric) 3

Question 3

Let us reuse the function get_scores from question-2. What is the output of the following snippet of code? (NAT)
python
def do_something(L):
    L.sort()
    mid = len(L) // 2
    if len(L) % 2 == 0:
        return (L[mid] + L[mid - 1]) // 2
    else:
        return L[mid]

print(do_something(get_scores('scores.csv', 4)))
Your Answer: (Not answered)
Status: Yes, the answer is correct. Score: Score: 2
Feedback: do_something(L) returns the median score (in Python) of students in the class.
Accepted Answers:
(Type: Numeric) 87

Question 4

Consider the following snippet of code:
python
def do_something(infile, gender, outfile):
    f = open(infile, 'r')
    g = open(outfile, 'w')
    header = f.readline()
    # we are now at the beginning of the second line of the file
    # 'good,M,great'.replace(',M,', ',')
    # returns 'good,great'
    header = header.replace(',Gender,', ',')
    g.write(header)

    for line in f.readlines():
        fields = line.strip().split(',')
        gender_in_file = fields[2]
        if gender_in_file == gender:
            out_line = line.replace(f',{gender},', ',')
            g.write(out_line)

    f.close()
    g.close()

do_something('scores.csv', 'F', 'out.csv')
Which of the following represents the contents of the file out.csv?
  • ```python SeqNo,Name,Gender,CT,Python,PDSA 0,Ram,M,80,90,100 1,Sahana,F,90,80,70 2,Ritvik,M,80,100,80 3,Suchitra,F,100,100,100 4,Praful,M,60,70,85 5,Aisha,F,85,95,90 6,Andrew,M,100,65,75 7,Soundarya,F,85,75,65 8,Krishnan,M,95,85,90 9,Brinda,F,65,95,85
  • ```python SeqNo,Name,Gender,CT,Python,PDSA 0,Ram,M,80,90,100 1,Sahana,F,90,80,70 2,Ritvik,M,80,100,80 3,Suchitra,F,100,100,100 4,Praful,M,60,70,85
  • ```python SeqNo,Name,Gender,CT,Python,PDSA 1,Sahana,F,90,80,70 3,Suchitra,F,100,100,100 5,Aisha,F,85,95,90 7,Soundarya,F,85,75,65 9,Brinda,F,65,95,85
  • ```python SeqNo,Name,CT,Python,PDSA 1,Sahana,90,80,70 3,Suchitra,100,100,100 5,Aisha,85,95,90 7,Soundarya,85,75,65 9,Brinda,65,95,85
  • ```python 1,Sahana,90,80,70 3,Suchitra,100,100,100 5,Aisha,85,95,90 7,Soundarya,85,75,65 9,Brinda,65,95,85
Status: Yes, the answer is correct. Score: Score: 3
Feedback: We are copying the details of all female students from the current file to 'out.cst' with one catch: we are ignoring the column Gender.
Option-(a) is just a copy of the original file.
Option-(b) has a mix of genders
Option-(c) is wrong because it has the Gender field. Note that line-15 removes the gender field.
Option-(e) is wrong because it don't have the header.
Accepted Answers:
python
SeqNo,Name,CT,Python,PDSA
1,Sahana,90,80,70
3,Suchitra,100,100,100
5,Aisha,85,95,90
7,Soundarya,85,75,65
9,Brinda,65,95,85

Question 5

words.txt is a text file with the following contents:
python
no
on
one
two
a
an
the
six
fin
out
What is the output of the following snippet of code? (NAT)
python
def do_something(filename):
    f = open(filename, 'r')
    maxword = f.readline().strip()
    count = 1
    # we are now at the beginning of the second line
    for line in f:
        word = line.strip()
        if len(word) > len(maxword):
            maxword = word
            count = 1
        elif len(word) == len(maxword):
            count += 1

    f.close()
    return count

print(do_something('words.txt'))
Your Answer: (Not answered)
Status: Yes, the answer is correct. Score: Score: 2
Feedback: The function reads the file and returns the number of words which have the maximum length in the file.
Accepted Answers:
(Type: Numeric) 6

Question 6

Select the correct implementation of a program that creates a text file named pattern.txt with the following contents:
python
az
by
cx
dw
ev
fu
gt
hs
ir
jq
kp
lo
mn
  • ```python f = open('pattern.txt', 'r') letters = 'abcdefghijklmnopqrstuvwxyz' n = len(letters) // 2 for i in range(n): line = letters[i] + letters[-1 - i] if i != n - 1: line = line + '\n' f.write(line) f.close()
  • ```python f = open('pattern.txt', 'w') letters = 'abcdefghijklmnopqrstuvwxyz' n = len(letters) // 2 for i in range(n): line = letters[i] + letters[-1 - i] f.write(line) f.close()
  • ```python f = open('pattern.txt', 'w') letters = 'abcdefghijklmnopqrstuvwxyz' n = len(letters) // 2 for i in range(n): line = letters[i] + letters[-1 - i] if i != n - 1: line = line + '\n' f.write(line) f.close()
  • ```python f = open('pattern.txt', 'w') letters = 'abcdefghijklmnopqrstuvwxyz' n = len(letters) for i in range(n): line = letters[i] + letters[-1 - i] if i != n - 1: line = line + '\n' f.write(line) f.close()
Status: Yes, the answer is correct. Score: Score: 3
Feedback: Option-(a) is wrong as the file has been opened in read mode
Option-(b) is wrong as we are not moving to the next line
Option-(d) is wrong because it will write more lines to the file
Accepted Answers:
python
f = open('pattern.txt', 'w')
letters = 'abcdefghijklmnopqrstuvwxyz'
n = len(letters) // 2
for i in range(n):
    line = letters[i] + letters[-1 - i]
    if i != n - 1:
        line = line + '\n'
    f.write(line)
f.close()

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.