Registry Synced

Jan 2026 - Python - Week 8 - GrPA 1

580 words
3 min read

GrPA 1

Course: Jan 2026 - Python
GrPA 1
Submission deadline has passed for this assignment
Due Apr 08, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
2/2 Passed
Submitted on Apr 08, 2026 at 5:45 AM IST
Private Tests
5/5 Passed
Submitted on Apr 08, 2026 at 5:45 AM IST

**Change in eligibility criteria to write oppe2 exam: A5>=40/100 AND A6>=40/100 AND A7>=40/100 AND A8>=40/100. and becoming eligible to give the end term exam. ** filename is a text file that contains a collection of words in lower case, one word on each line. Write a function named get_freq that accepts filename as argument. It should return a dictionary where the keys are distinct words in the file, the values are the frequencies of these words in the file.
For example, given the following file:
good
great
good
work
work
The dictionary returned should be:
{'good': 2, 'great': 1, 'work': 2}
The order in which the words are added to the dictionary doesn't matter.

(1) filename is a string variable that holds the name of the file. For example, in the first test case, it is filename = 'public_1.txt'.
(2) You do not have to accept input from the console or print the output to the console. You just have to write the function definition.

Public Tests ( 2/2 )

Case 1

Input:
text
100
public_1.txt
Expected Output:
text
an:6

correct:2

ideal:7

inner:5

something:2
Actual Output:
text
an:6

correct:2

ideal:7

inner:5

something:2

Case 2

Input:
text
200
public_2.txt
Expected Output:
text
good:2

great:3

inner:2

precise:2

something:7
Actual Output:
text
good:2

great:3

inner:2

precise:2

something:7

Private Tests ( 5/5 )

Case 1

Input:
text
300
private_1.txt
Expected Output:
text
correct:1

divine:2

great:1

inner:6

something:7
Actual Output:
text
correct:1

divine:2

great:1

inner:6

something:7

Case 2

Input:
text
400
private_2.txt
Expected Output:
text
an:7

good:2

ideal:4

inner:5

precise:3
Actual Output:
text
an:7

good:2

ideal:4

inner:5

precise:3

Case 3

Input:
text
500
private_3.txt
Expected Output:
text
good:4

outer:4

predict:8

something:6

the:3
Actual Output:
text
good:4

outer:4

predict:8

something:6

the:3

Case 4

Input:
text
600
private_4.txt
Expected Output:
text
an:2

great:3

outer:5

something:4

the:2
Actual Output:
text
an:2

great:3

outer:5

something:4

the:2

Case 5

Input:
text
700
private_5.txt
Expected Output:
text
an:3

correct:4

great:1

precise:3

predict:4
Actual Output:
text
an:3

correct:4

great:1

precise:3

predict:4

💻 IITM Official Solution

python
def get_freq(filename):
    f = open(filename, 'r')
    freq = dict()
    for line in f:
        word = line.strip()
        if word not in freq:
            freq[word] = 0
        freq[word] += 1
    f.close()
    return freq


💻 My Submitted Code

python
def get_freq(filename):
    f = open(filename, 'r')
    # Use a dictionary to track counts
    result = {}

    # Efficiently iterate through the file line by line
    for line in f:
        # Remove whitespace/newlines and skip empty lines if any
        word = line.strip()
        if word:
            # The .get(key, default) pattern is the pro way to count
            result[word] = result.get(word, 0) + 1

    f.close()
    return result

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.