collections.namedtuple | HackerRank

admin
By -
0




the problems state that  we using namedtuple storing students records and finding the total average of all students marks  for this taking total number of record and each column names one by one and then taking values for each column to  till records and calculate .



Average = sum of total marks / total number of students



Here is not optimized solution because takes is to  finish it under the 4 lines 

and we doing operations twice times but it starting point.

one loop  collecting each columns values and other just unpacking and increment variable of total_marks and at the end we just  divide it with length of our all students named tuples from list.Althoguh time complexity still considered as Big (O)n.


Problem:https://www.hackerrank.com/challenges/py-collections-namedtuple/problem?isFullScreen=true

CODE:

# Naive 

# Enter your code here. Read input from STDIN. Print output to STDOUT
#Naive
from  collections import namedtuple
n = int(input())
column_names = input().split()
total_marks = 0
students = []
StudentsRecord = namedtuple('StudentsRecord',column_names)
for _ in range(n):
    p,q,m,n= input().split()
    students.append(StudentsRecord(p,q,m,n))

for student in students:
    total_marks += int(student.MARKS)
#avg
print(total_marks/len(students))

Code Solutions:

4 Lines only
#4 Lines strictly using list compressions
from  collections import namedtuple
n, column_names = int(input()) , input().split()
StudentRecord = namedtuple('StudentsRecord',column_names)
students = [StudentRecord(*input().split()) for _ in range(n)]
print(sum(int(student.MARKS) for student in students)/n)

For this you need knowledge or list compressions and multiple variable assignment a
taking multiple inputs and splitting inputs with spaces and data types conversion there are
some other ways as well you can do to solve this more better if then comment please

Time Complexity: Big(O)n
  1. Input Parsing:

    • n, column_names = int(input()), input().split():

      • First, the code reads an integer n, which represents the number of students.

      • Then, it reads the next line which contains column names (like NAME, CLASS, MARKS), and splits them into a list of strings column_names.

  2. NamedTuple Creation:

    • StudentRecord = namedtuple('StudentRecord', column_names):

      • This line defines a namedtuple class StudentRecord with fields specified by column_names. A namedtuple creates lightweight objects where each field can be accessed by name, like student.NAME, student.CLASS, etc.

  3. List Comprehension for Student Records:

    • students = [StudentRecord(*input().split()) for _ in range(n)]:

      • This is a list comprehension that loops n times, taking input for each student and splitting their data (e.g., John 10 90) into separate values.

      • It then unpacks the values into the StudentRecord named tuple, which gives us a list of StudentRecord objects where each object holds the student's data.

  4. Average Calculation:

    • print(sum(int(student.MARKS) for student in students) / n):

      • This line calculates the sum of the MARKS for all students. It uses a generator expression to extract the MARKS field, converts it to an integer, and sums them up.

      • Finally, it divides the total marks by n to compute the average and prints the result.

Time Complexity:

  • The time complexity is O(n) because the program loops over the student records once to collect the data and once more to compute the sum of the marks.

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*