Neural Sync Active
Jan 2026 - Python - Week 8 - GrPA 1
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:
text100 public_1.txt
Expected Output:
textan:6 correct:2 ideal:7 inner:5 something:2
Actual Output:
textan:6 correct:2 ideal:7 inner:5 something:2
Case 2
Input:
text200 public_2.txt
Expected Output:
textgood:2 great:3 inner:2 precise:2 something:7
Actual Output:
textgood:2 great:3 inner:2 precise:2 something:7
Private Tests ( 5/5 )
Case 1
Input:
text300 private_1.txt
Expected Output:
textcorrect:1 divine:2 great:1 inner:6 something:7
Actual Output:
textcorrect:1 divine:2 great:1 inner:6 something:7
Case 2
Input:
text400 private_2.txt
Expected Output:
textan:7 good:2 ideal:4 inner:5 precise:3
Actual Output:
textan:7 good:2 ideal:4 inner:5 precise:3
Case 3
Input:
text500 private_3.txt
Expected Output:
textgood:4 outer:4 predict:8 something:6 the:3
Actual Output:
textgood:4 outer:4 predict:8 something:6 the:3
Case 4
Input:
text600 private_4.txt
Expected Output:
textan:2 great:3 outer:5 something:4 the:2
Actual Output:
textan:2 great:3 outer:5 something:4 the:2
Case 5
Input:
text700 private_5.txt
Expected Output:
textan:3 correct:4 great:1 precise:3 predict:4
Actual Output:
textan:3 correct:4 great:1 precise:3 predict:4
💻 IITM Official Solution
pythondef 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
pythondef 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