Question: Python Find the Percentage [Basic Data Types]
The provided code stub will 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 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 students’ 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 marks array = 3
Output Format:
Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.
Possible solutions
Now let us move toward the possible solutions. The following code is already given in the editor of the hacker rank:
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()
Let us solve the problem using various methods:
Solution-1: Using for loop
Let us solve the problem using the for loop:
if __name__ == '__main__':
n = int(input())
x = [input().split() for i in range(n)]
c = input()
d = {}
for i in range(n):
d[x[i][0]] = list(map(float,x[i][1:]))
print(f'{sum(d[c])/len(d[c]):.2f}')
This code takes an input integer “n” from the user, then creates a
list “x” of n number of inputs each consisting of multiple values
separated by a space. Then it takes another input “c” from the user.
It then creates a dictionary “d” where the first value of each element
of “x” is used as the key and the rest of the values are used as a
list as the value for that key. Then it calculates the average of the
values of the key “c” in the dictionary and prints the result in a
formatted manner with 2 decimal places.
Solution-2: Alternative way of using for loop
Now let us modify the above method and solve the problem using for loop:
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()
na = student_marks[query_name]
avg = sum(na)/len(na)
print("%.2f" % avg)
This code also takes an input integer “n” from the user, and then
creates an empty dictionary “student_marks”. It then uses a for loop
to take “n” number of inputs, where each input consists of a name
followed by multiple values separated by a space. The name is used as
the key in the dictionary “student_marks” and the rest of the values
are used as a list of scores as the value for that key. Then it takes
another input “query_name” from the user. Using this input it accesses
the value of the key in the dictionary “student_marks” and assigns it
to variable “na” . Then it calculates the average of the values in
“na” and prints the result in a formatted manner with 2 decimal
places. This code is different from the previous one in the way it uses
the unpacking operator “*” to unpack the list of scores from the
input, also it uses “query_name” input to get the average of the
student’s marks as compared to the previous code where it used “c”
input to get the average of the marks of any student.
Solution-3: Using if statement with for loop
Now let us use the if statement with for loop to solve the problem:
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()
if query_name in student_marks.keys():
l=student_marks[query_name]
aver=sum(l)/len(l)
print(format(aver,".2f"))
This code takes an input integer “n” from the user, and then creates
an empty dictionary “student_marks”. It then uses a for loop to take
“n” number of inputs, where each input consists of a name followed by
multiple values separated by a space. The name is used as the key in the
dictionary “student_marks” and the rest of the values are used as a
list of scores as the value for that key. Then it takes another input
“query_name” from the user. Using this input it checks whether the
entered student name is available in the keys of the student_marks
dictionary using an if condition. If yes, it assigns the value of the
key to a variable “l” and calculates the average of the values in
“l” and prints the result in a formatted manner with 2 decimal places.
If the student name is not available in the keys of the dictionary, the
code will not give any output. This code is different from the previous
one in the way that it uses an if-else condition to check the
availability of the student name in the keys of the student_marks
dictionary and only gives the average if the name is available
Summary
In this short article, we discussed three different methods to solve the Find percentage question on Hacker Rank. We explained each method deeply.
Further reading
Question on Hacker Rank website: Finding the percentage [Basic Data Types]

![HackerRank Solution: Finding the Percentage [3 Methods]](/finding-the-percentage-hackerrank-solution/python_find_the_percentage.jpg)
