Hanwool Codes RSS Tag Admin Write Guestbook
2023-02-01 04:46:40

Finding the percentage | HackerRank

 

Finding the percentage | HackerRank

Store a list of students and marks in a dictionary, and find the average marks obtained by a student.

www.hackerrank.com

 

Goal

 

Print the average of the marks array for the given student name and showing 2 places after the decimal.

 

 

Solution

 

First, we need to get average score from the give student marks.

Secondly, we need to print out only 2 places after the decimal.

I use {:.2f} format to show 2 places after the decimal. But there are other ways too.

 

# Finding the percentage

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    query_scores = [x for x in student_marks[query_name]]
    
    print("{:.2f}".format(float(sum(query_scores)) / len(query_scores)))

 

Hanwool Codes. Designed by 코딩재개발.