HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank Collections.namedtuple() solution in python | python question solution

 

collections.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  and .

Your task is to help Dr. Wesley calculate the average marks of the 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 IDMARKSCLASS and NAME. (The spelling and case type of these names won’t change.)

Input Format

The first line contains an integer , the total number of students.
The second line contains the names of the columns in any order.
The next  lines contains the  and , under their respective column names.

Constraints

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 = 

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.

Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import namedtuple
N = int(raw_input())
headers = raw_input()
student = namedtuple('Student',headers)
students = []
for i in range(N):
students.append(student(*raw_input().split()))
print sum(list(map(lambda x: float(x.MARKS),students)))/len(students)

Problem solution in Python 3 programming.

N, headers, total = int(input()), list(input().split()), 0
for _ in range(N):
total += int(list(input().split())[headers.index('MARKS')])
print(total/N)

Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import namedtuple
n, Student = input(), namedtuple('Student', raw_input())
print "%.2f" %( sum([float(stud.MARKS) for stud in [Student(*raw_input().split()) for _ in xrange(n)]]) / n )

Problem solution in pypy3 programming.

num_students = int(input())
relevant_pos = None
total = 0
for num in range(num_students + 1):
if num == 0:
titles = list(filter(lambda el: el != '', input().split(' ')))
relevant_pos = titles.index('MARKS')
continue
total += int(list(filter(lambda el: el != '', input().split(' ')))[relevant_pos])

print(round(float(total) / num_students, 2))

Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science