Finding the percentage in python - HackerRank solution

Solution for hackerRank problem Finding the percentage in python

Problem :

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

The query_name is 'beta'. beta's average score is .

Input Format

The first line contains the integer , the number of students' records. The next 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 arrays = 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

Explanation 0

Marks for Malika are  whose average is 

Sample Input 1

2

Harsh 25 26.5 28

Anurag 26 28 30

Harsh

Sample Output 1

26.50

Procedure :

1. Start.

2. Get the number of students as input from user.

3. Create an empty dictionary.

4. Get the name and marks of students as input from user.

5. Store the name as key and the corresponding list of marks as value in the created dictionary.

6. Get the name of the student for whom the average mark is to be calculated.

7. Access the value of the key (student name given by the user).

8. Find the sum of the marks of the student and divide it by the length of the list, so that the average mark can be received.

9. Format the average into decimal values.

10. Print the value.

11. End

Code :

#Get the number of students as input from the user

no_of_stu=int(input())

#Create an empty dictionary

stu={}

#Store the name as keys and the corresponding list of marks as values in the dictionary

for i in range(no_of_stu):

    name,x,y,z=input().split()

    mark=list(map(float, [x,y,z]))

    stu[name]=mark

query_name=str(input())

#Calculating the average mark

avg=sum(stu[query_name])/len(stu[query_name])

#Formatting the output to two decimal values

print("%.2f"%avg)   


Explanation of the python functions used :

1. map()- used to apply the given function to each item of a given iterable(list,tuple).

2. split()- splits the input based on the given delimiter and returns a list. It
defaultly takes space as the delimiter.

3. sum()- returns the sum of elements in the list.

4. len()- returns the length of the list.

5. %2f()- formats the integer to two decimal values.




Comments

Popular posts from this blog

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution