Registry Synced

Jan 2026 - Python - Week 8 - GrPA 3

631 words
3 min read

GrPA 3

Course: Jan 2026 - Python
GrPA 3
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
3/3 Passed
Submitted on Apr 08, 2026 at 5:46 AM IST
Private Tests
7/7 Passed
Submitted on Apr 08, 2026 at 5:46 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 CSV file that has the following header:
Name,Country,Goals
The first five lines of a sample file are given below:
Name,Country,Goals
P1,Brazil,20
P2,Argentina,30
P3,Brazil,50
P4,Germany,30
Write a function named get_goals that accepts filename and the name of a country as arguments. It should return a tuple having two elements: (num_players, num_goals). num_players is the number of players from this country that appear in this file, num_goals is the total number of goals scored by all the players who belong to this country. If the country is not present in the file, then return the tuple (-1, -1).

(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.csv'.
(2) Each player who represents a country has scored at least one goal. That is, the last column in the file will have only positive integers.
(3) You do not have to accept input from the user or print the output to the console. You just have to write the function definition.

Public Tests ( 3/3 )

Case 1

Input:
text
100
public_1.csv
Brazil
Expected Output:
text
4,105
Actual Output:
text
4,105

Case 2

Input:
text
200
public_2.csv
Spain
Expected Output:
text
8,161
Actual Output:
text
8,161

Case 3

Input:
text
300
public_3.csv
Random
Expected Output:
text
-1,-1
Actual Output:
text
-1,-1

Private Tests ( 7/7 )

Case 1

Input:
text
400
private_1.csv
Argentina
Expected Output:
text
1,11
Actual Output:
text
1,11

Case 2

Input:
text
500
private_2.csv
France
Expected Output:
text
3,59
Actual Output:
text
3,59

Case 3

Input:
text
700
private_3.csv
Portugal
Expected Output:
text
5,113
Actual Output:
text
5,113

Case 4

Input:
text
800
private_4.csv
Italy
Expected Output:
text
9,187
Actual Output:
text
9,187

Case 5

Input:
text
1000
private_5.csv
Nigeria
Expected Output:
text
8,175
Actual Output:
text
8,175

Case 6

Input:
text
1200
private_6.csv
SomeCountry

Case 7

Input:
text
1300
private_7.csv
AnotherCountry

💻 IITM Official Solution

python
def get_goals(filename, country):
    f = open(filename, 'r')
    # Ignore the header
    f.readline()
    nplayers, ngoals = 0, 0
    for line in f:
        # Unpacking a list into three variables
        player, country_file, goals = line.split(',')
        if country_file == country:
            nplayers += 1
            ngoals += int(goals)
    f.close()
    if nplayers > 0:
        return (nplayers, ngoals)
    return (-1, -1)


💻 My Submitted Code

python
def get_goals(filename, country):
    f = open(filename, 'r')
    # Skip the header: Name,Country,Goals
    f.readline()

    num_players = 0
    num_goals = 0
    country_found = False

    for line in f:
        # Standard CSV split
        row = line.strip().split(',')
        if len(row) == 3:
            file_country = row[1]
            file_goals = int(row[2])

            if file_country == country:
                country_found = True
                num_players += 1
                num_goals += file_goals

    f.close()

    if country_found:
        return (num_players, num_goals)
    else:
        return (-1, -1)

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.