Asksia AI LOGO

Sia

Question
Computer Science
Posted 5 months ago

etc.
'EEE',
(e) 'Imagine a dictionary with the structure student_id:[student_major, student_cgpa]. Assume majors are represented by strings like 'CoE',
for
all
students
Now define a function avg_cgpa_by_major that takes such a dictionary and a string for major as inputs and returns the average cgpa
with that major.
Sample input/Output:
In_1 = {41251:['CoE',2.5],42509:['EEE',3.1],4 41713:['CoE',1.9],41101:['CoE',2.7]}
print(avg_cgpa_by_major(In_1,"CoE"))
Sign in to unlock the answer
Answer from Sia
Posted 5 months ago
python
python
Explanation
The code defines a function "avg_cgpa_by_major" that calculates the average CGPA for a given major from a dictionary of student IDs to majors and CGPAs. It then tests this function with two use cases.
Step-by-step Instruction
Initialize variables to sum CGPAs and count students with the given major
Iterate over the dictionary, summing CGPAs and counting students if the major matches
Calculate the average CGPA by dividing the total CGPA by the count of students
Return the average CGPA, ensuring to handle division by zero if no students have the given major
Define the main function to test `avg_cgpa_by_major` with specific use cases
Use assertions to verify that the function returns the correct average CGPA for each test case
Time Complexity
The time complexity is O(n), where n is the number of entries in the dictionary, because the function iterates through each entry once.
Space Complexity
The space complexity is O(1), as the function uses a fixed amount of additional space regardless of the input size.

Not the question you are looking for? Ask here!

Enter question by text

Enter question by image

Upgrade to Asksia Pro

Join a AskSia's Pro Plan, and get 24/7 AI tutoring for your reviews, assignments, quizzes and exam preps.

Unlimited chat query usages
Strong algorithms that better know you
Early access to new release features
Study Other Question