Collections.namedtuple() - Python HackerRank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Collections.namedtuple() Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Python Collections.namedtuple() Solution - Problem Statement
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 {codeBox}
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 {codeBox}
Task
Dr. John Wesley has a spreadsheet containing a list of student's IDs, marks, classs and name.
Your task is to help Dr. Wesley calculate the average marks of the students.
Average = Sum of all marks/Total Students
Note:
1. Columns can be in any order. IDs, marks, class and name can be written in any order in the spreadsheet.
2. 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 contains 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
Test Case 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 {codeBox}
Test Case 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 {codeBox}
Sample Output 0
Test Case 01
78.00 {codeBox}
Test Case 02
81.00 {codeBox}
Explanation:
TESTCASE 01
Average = (97 + 50 + 91 + 72 + 80)/5
Can you solve this challenge in 4 lines of code or less?
NOTE: There is no penalty for solutions that are correct but have more than 4 lines.
Python Collections.namedtuple() - Hacker Rank Solution
Approach I: Collections.namedtuple() HackerRank Python Solution
# ========================
# Information
# ========================
# Name: Collections.namedtuple() in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/py-collections-namedtuple/problem
# Difficulty: Easy
# Max Score: 20
# Language: Pypy 3
# ========================
# Solution Start
# ========================
#Collections.namedtuple() in Python - Hacker Rank Solution
from collections import namedtuple
n = int(input())
fields = input().split()
total_marks = 0
for _ in range(n):
students = namedtuple('student', fields)
MARKS, CLASS, NAME, ID = input().split()
student = students(MARKS, CLASS, NAME, ID)
total_marks += int(student.MARKS)
print('{:.2f}'.format(total_marks / n))
#Collections.namedtuple() in Python - Hacker Rank Solution END
# MyEduWaves
Disclaimer: The above Problem ( Python Collections.namedtuple() ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Collections.namedtuple() Hackerrank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Python