Question

Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

Python 3

Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled.

The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course.

For example, the following would be correct input and output.

>>> student_courses = {'Elise':[],'Nelson':['CS100','MATH111'],'Justin':['CS100']}

>>> print(inverse(student_courses)) {'CS100': ['Nelson', 'Justin'], 'MATH111': ['Nelson']}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#code.py
def inverse(d):
    res = dict()
    for key in d.keys():
        for x in d[key]:
            if not res.get(x):
                res[x] = [key]
            else:
                res[x].append(key)
    return res

#Testing
student_courses = {'Elise':[],'Nelson':['CS100','MATH111'],'Justin':['CS100']}
print(inverse(student_courses))

{'CS100': ['Nelson', 'Justin'], 'MATH111': ['Nelson']}

Add a comment
Know the answer?
Add Answer to:
Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • Python: Write a function named "sort_by_average_rating" that takes a list/array of key-value stores as a parameter...

    Python: Write a function named "sort_by_average_rating" that takes a list/array of key-value stores as a parameter where each key-value store has keys "ratings", "budget", and "box_office" where budget and box_office are integers and ratings is a list of integers. Sort the input based on the average of the values in "ratings"

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • project-8a Write a function named count_letters that takes as a parameter a string and returns a...

    project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...

  • write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

    write a function firstLetterWords(words) that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

  • Write a function named "find_key" that takes a key-value store as a parameter with strings as...

    Write a function named "find_key" that takes a key-value store as a parameter with strings as keys and integers as values. The function returns a boolean representing true if the string "focus" is in the input as a key, false otherwise(javascript)

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • Write a Python function named print_nums that takes a single parameter, a list of float values,...

    Write a Python function named print_nums that takes a single parameter, a list of float values, and prints out the list on a single line.enclosed in brackets, each value with three places after the decimal point, the values separated by two spaces. You do not have to provide comments for this code. Example 1: print_nums([3/3, 4/3, 573, 6/3]) prints: [1.000 1.333 1.667 2.000] Example 2: print_nums([3]) prints: [3.000] Example 3: print_nums([]) prints: []

  • Python code define a function car that takes 'swift' as a parameter function should count how...

    Python code define a function car that takes 'swift' as a parameter function should count how many letters in the string function should return a dictionary where key is lower case alpha caracter and value is the number of times that character appears in sentence. ex- input- that big car is so expensive output- 't':1, 'h':1, 'a':2, 'b':1, 'i':3,......

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT