Python Finding the Percentage HackerRank Solution
In this tutorial, we will solve a hacker rank-finding percentage problem in python!
Task
The provided code stub is read in a dictionary containing key/value pairs of name:[Marks] for a list of students. Print the average of the marks array for the student name is provided, showing 2 places after the decimal.
Example
marks key: value pairs are
‘alpha’:[20, 30, 40]
‘beta’:[30, 50, 70]
query_name = ‘beta’
The query_name is ‘beta’. beta’s average score is (30 + 50 + 70)/3 = 50.0.
Input Format
The first line contains the integer n, the number of student records. The next n lines contain the names and marks obtained by a student, each value separated by a space. The final line contains query_name, the name of a student to query.
Constraints
- 2 ≤ n ≤ 10
- 0 ≤ marks[i] ≤ 100
- length of the marks array = 3
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 of 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 0
26.50
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()
l1 = list(student_marks[query_name])
addition = sum(l1)
result = addition/len(l1)
print('%.2f'% result)
Steps Used in solving the problem -
Step 1: First, n will take input for a total number of students.
Step 2: then, we created a dictionary to store the name and marks of students.
Step 3: After this, we created a for loop.
Step 4: inside for loop, we have taken an input of the name.
Step 5: we have also taken the input of scores and stored them in a list.
Step 6: then we stored names and scores in the dictionary.
Step 7: in the next step we had taken the input of query_name i.e, the name of the student whose percentage we want to print. And, in the next step, we created the list of query_name marks.
Step 8: After this, we added all the scores to our list. Then we divided our total sum by the length of the list.
Step 9: in the last step we had printed the result.