Collections.namedtuple() Problem

The script is designed to solve a problem where we need to calculate the average of student marks. It uses Python's collections.namedtuple to store student data, which makes the code more readable and self-explanatory. The script reads the number of students and their data from the standard input, calculates the average of their marks, and prints the result.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Collections.namedtuple() Problem HackerRank solution using CodeRankGPT:

The script uses Python's collections.namedtuple to create a 'Student' data type, which has fields defined by the input. It then reads student data from the standard input, creates a 'Student' instance for each student, and stores these instances in a list. The script calculates the average of student marks by summing up the 'MARKS' field of all 'Student' instances and dividing the sum by the number of students. The result is printed with two decimal places.


from collections import namedtuple

n = int(input())
columns = ",".join(input().split())
Student = namedtuple("Student", columns)
data = []
for i in range(n):
    row = input().split()
    s = Student._make(row)
    data.append(s)
avg = sum(float(s.MARKS) for s in data) / n
print(f"{avg:.2f}")

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform