Suppose a courses score of a class is saved in a dictionarydata = {'Tom': [84, 96, 90], 'Bob':[ 67, 82, 76], 'Jack': [62, 90, 67], 'Linda': [78, 66,90]}Write a function to calculate the average score for each student, round the float value to integer, and save the average score in a dictionary, and then sort the dictionary based on the average score in reverse order and save in a list. return the dictionary and list.output:({'Tom': 90, 'Bob': 75, 'Jack': 73, 'Linda': 78},[('Tom', 90), ('Linda', 78), ('Bob', 75), ('Jack', 73)])
Question
Suppose a courses score of a class is saved in a dictionarydata = {'Tom': [84, 96, 90], 'Bob':[ 67, 82, 76], 'Jack': [62, 90, 67], 'Linda': [78, 66,90]}Write a function to calculate the average score for each student, round the float value to integer, and save the average score in a dictionary, and then sort the dictionary based on the average score in reverse order and save in a list. return the dictionary and list.output:({'Tom': 90, 'Bob': 75, 'Jack': 73, 'Linda': 78},[('Tom', 90), ('Linda', 78), ('Bob', 75), ('Jack', 73)])
Solution
Here is a Python solution for your problem:
def calculate_average(data):
average_scores = {}
for student, scores in data.items():
average_scores[student] = round(sum(scores) / len(scores))
sorted_scores = sorted(average_scores.items(), key=lambda x: x[1], reverse=True)
return average_scores, sorted_scores
data = {'Tom': [84, 96, 90], 'Bob':[ 67, 82, 76], 'Jack': [62, 90, 67], 'Linda': [78, 66,90]}
print(calculate_average(data))
This function works by iterating over each student in the data dictionary. For each student, it calculates the average score by summing all scores and dividing by the number of scores. The result is rounded to the nearest integer and stored in the average_scores dictionary.
Then, it sorts the average_scores dictionary by value in reverse order (i.e., from highest to lowest average score) and stores the result in the sorted_scores list.
Finally, it returns both the average_scores dictionary and the sorted_scores list.
Similar Questions
Define a function sort_grades(tup1). The argument tup1 is a tuple each composed of (student_name, score). The function returns a tuple with same elements of tup1 but sorted by their scores in descending order.Exampletup1 = (('Bob', 47), ('Claire', 61), ('Dominic', 79))Expected output:(('Dominic', 79), ('Claire', 61), ('Bob', 47))Hintfirst convert tup1 to a list. Since each element in this list is a tuple with two items (student_name, score), to sort the list according to the second item score, you can use Python built-in sorted function:sorted(iterable, key=None, reverse=False) function returns a sorted list from the iterable object(refer to similar examples in Chapter 4.1).convert the above list to a tuple again, and return the tuple
The marks of a student on 6 subjects are stored in a list, list1=[80,66,94,87,99,95]. How can the student’s average mark be calculated?print(avg(list1))print(sum(list1)/len(list1))print(sum(list1)/sizeof(list1))print(total(list1)/len(list1))
Write a Python program to sort a dictionary – Based on the ValuesConstraints:Input Format:DictionaryOutput Format:After Sorting - Key and Value needs to be printed one by one with a space separating each<Key> <Value>Example:Input:{2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}Output:1 126 184 242 565 1223 323 Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 {2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}1 126 184 242 565 1223 323
ou are given a list marks that has the marks scored by a class of students in a Mathematics test. Find the median marks and store it in a float variable named median. You can assume that marks is a list of float values.Procedure to find the median(1) Sort the marks in ascending order. Do not try to use built-in methods. Look at the lecture 4.5 of week-4 to get a better idea.(2) If the number of students is odd, then the median is the middle value in the sorted sequence. If the number of students is even, then the median is the arithmetic mean of the two middle values in the sorted sequence.You do not have to accept input from the console as it has already been provided to you. You do not have to print the output to the console. Input-Output is the responsibility of the autograder for this problem. Refer to PPA-11 if you are not sure how this works.FILL THE MISSING CODEdef solution(marks): ### Enter your solution below this line ### Indent your entire code by one unit (4 spaces) to the right # FILL THE MISSING CODE (...) sorted_marks = [ ] while marks != [ ]: min_mark = ... for mark in marks: if mark < min_mark: ... marks.remove(min_mark) ... n = len(sorted_marks) if n % 2 != 0: median = ... else: median = ... ### Enter your solution above this line return medianSample Test CasesDownload All Test Case 1InputExpected OutputActual Output30,50,40,10,2030.0Test Case 2InputExpected OutputActual Output60,10,30,40,20,5035.0
Question 2: Given a list of student names and their corresponding scores:student_scores = ["John: 85", "Emma: 92", "Michael: 78", "Sophia: 95", "William: 88"]Create a dictionary using a dictionary comprehension where the student name is the key and the score is the value. Only include students whose scores are greater than or equal to 90.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.