Hackerrank Division Solution – Python

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": "beta"

The query_name is ‘beta’. beta’s average score is (30 + 50 + 70)/3 = 50.0

Inputr 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
  • lenght 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

3Krishna 67 68 69Arjun 70 98 63Malika 52 56 60Malika

Sample Output 1

26.50

Solution

The first line of the code creates a key:value pair dictionary that gets the conbinations of the names and the scores by the students.

store = dict()

Since the number of students record will be given, create a for loop to get each record was considered.

for _ in range(int(input())):    lis = input().split()

The next two lines brings out the score of each student, create a list with it and also the student name from the firstly created list.

score = list(map(int, lis[1:]))name = lis[0]

Then assigned each student to his score in the dictionary object created above.

store[name] = score

The next line receives the query value (i.e the name of the student whose average result is be calculated).

query = input()

The average of a set of numbers is always gotten from the sum of the numbers divided by the total number. The below code does that and prints out the required average score in two decimal places.

print(round(float(sum(store[query])/len(store[query])), 2))

Full Code

store = dict() for _ in range(int(input())):     lis = input().split()     score = list(map(int, lis[1:]))     name = lis[0]     store[name] = scorequery = input()print(round(float(sum(store[query])/len(store[query])), 2))

Comments