HackerRank Python Solution - Collections Topic - namedtuple()

  • Basically, namedtuples are easy to create, lightweight object types.
  • They turn tuples into convenient containers for simple tasks.
  • With namedtuples, you don’t have to use integer indices for accessing members of a tuple.
Example:

Code 01:

>>> from collections import namedtuple
>>> Point = namedtuple('Point','x,y')
>>> pt1 = Point(1,2)
>>> pt2 = Point(3,4)
>>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y )
>>> print dot_product
11
Code 02:

>>> from collections import namedtuple
>>> Car = namedtuple('Car','Price Mileage Colour Class')
>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')
>>> print xyz
Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y')
>>> print xyz.Class
Y
Task:
  • Dr. John Wesley has a spreadsheet containing a list of student's IDs, marks, class, and name.
  • Your task is to help Dr. Wesley calculate the average marks of the students.
    • Average = Sum of all marks / Total Students
  • Note:
    • Columns can be in any order. IDs, marks, class, and name can be written in any order in the spreadsheet.
    • Column names are ID, MARKS, CLASS, and NAME. (The spelling and case type of these names won't change.)
Input Format:
  • The first line contains an integer N, the total number of students.
  • The second line contains the names of the columns in any order.
  • The next N lines contain the marks, IDs, name, and class, under their respective column names.
Constraints:
  • 0 < N <= 100
Output Format:

Print the average marks of the list corrected to 2 decimal places.

Sample Input:

TESTCASE 01:

5
ID         MARKS      NAME       CLASS     
1          97         Raymond    7         
2          50         Steven     4         
3          91         Adrian     9         
4          72         Stewart    5         
5          80         Peter      6   
TESTCASE 02:

5
MARKS      CLASS      NAME       ID        
92         2          Calum      1         
82         5          Scott      2         
94         2          Jason      3         
55         8          Glenn      4         
82         2          Fergus     5
Sample Output:

TESTCASE 01:

78.00
TESTCASE 02:

81.00
Explanation:

TESTCASE 01:
  • Average = (97 + 50 + 91 + 72 + 80)/5
Solution:

from collections import namedtuple

N = int(input())

Std_lst = []

for i in range(N+1):
    
    if i == 0:
        header = input()
        Students = namedtuple('Students',header)
    else:
        student = input().split()
        Std_lst.append(Students(student[0],student[1],student[2], student[3]))

std_sum = 0
for std in Std_lst:
    std_sum += int(std.MARKS)

print(std_sum/N)
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...