Finding the percentage in Python - Hacker Rank Solution


Finding the percentage in Python - Hacker Rank Solution


Problem


Tutorial :

A dictionary is a data type which stores values in pairs. For each element in the dictionary, there is a unique key that points to a value. A dictionary is mutable. It can be changed.
For example:
a_dict = {'one': 1} # Here 'one' is the key. 

Note: The key of a dictionary is immutable. We cannot use a list as a key because a list is mutable. But we can make a tuple of list and use as key.

a_dict['two'] = 2 # Adds key 'two' which points to 2
print a_dict['one']
# prints 1  
if 'three' in a_dict:
    # To check whether a certain string exist as a key in the dictionary  
    print a_dict['three']
else:  
    print "Three not there"
# prints Three not there
del a_dict['one']
# Deletes index 'one' and the value associated with it  
print a_dict
# prints {'two': 2}

Note: A dictionary is unordered. So, only use the keys to navigate through the dictionary.

You have a record of N 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 N followed by the names and marks for N 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 marks obtained by that student, correct to two decimal places.



Input Format :

The first line contains the integer N, the number of students. The next N 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+56+60/3 = 56

Sample Input 1:

2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh

Sample Output 1:

26.50



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Finding the percentage in Python - Hacker Rank Solution
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()

    # Finding the percentage in Python - Hacker Rank Solution START
    output = list(student_marks[query_name])
    per = sum(output)/len(output);
    print("%.2f" % per);
    # Finding the percentage in Python - Hacker Rank Solution END
   





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
4 Comments
  • omesh
    omesh Saturday, September 26, 2020

    print("{:.2f}".format(per))

    • jamson
      jamson Saturday, December 12, 2020

      f indicates float datatype and 2 means .2 decimal point

  • pythonneir
    pythonneir Tuesday, January 12, 2021

    hey brother i also have a blog give me few suggestion please

  • Unknown
    Unknown Monday, June 21, 2021

    what *line means in line 6

Add Comment
comment url