Monday, June 22, 2020

Finding the percentage-HackerRank Solution


Finding the percentage-HackerRank Solution

You have a record of  students. Each record contains the student's name, and their percent marks in Maths, Physics and Chemistry. The marks can be floating values. The user enters some integer  followed by the names and marks for  students. You are required to save the record in a dictionary data type. The user then enters a student's name. Output the average percentage of marks obtained by that student, correct to two decimal places.




Input Format

The first line contains the integer , the number of students. The next  lines contains the name and marks obtained by that student separated by a space. The final line contains the name of a particular student previously listed.

Constraints

  • 2<=N<=10
  • 0<=Marks<=100

Output Format

Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.

Sample Input 0

3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Malika

Sample Output 0

56.00

Explanation 0

Marks for Malika are  {52,56,60} whose average is (52+46+60)/3 => 56

Sample Input 1

2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh

Sample Output 1

26.50
Finding the percentage solution
import statistics
if __name__ == '__main__':
    a=[]
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores #store scores in key "name" inside dictionary
    query_name = input()
    for i in student_marks:
        if query_name==i: #match query_name with key of student_marks
            a=statistics.mean(student_marks[i]) #apply statistics module to find mean which is actually avg of input items.
            print("{:.2f}".format(a) ) #print the result till two decimal places

 

To read on statistics follow this link- https://docs.python.org/3/library/statistics.html

0 comments:

Post a Comment

Feel free to share your thoughts and suggestion...