Question

USING PYTHON. Avoid using built in functions if possible. I'm a beginner so keep it simple...

USING PYTHON. Avoid using built in functions if possible. I'm a beginner so keep it simple please.

''' Q5. Design a function that takes a dictionary where for each entry,
the key is a course as a string and the value is a list of related courses
as a list of strings. The function should create and return a new dictionary from
the given dictionary where each of the course names in the lists of values
is a key in the new dictionary and the value for each key in the new dictionary is
a list of the courses that are related to that key in sorted order.
  
Example.
if data is {'CSC 111': ['MATH 12'], 'CSC 116': ['CSC 110', 'CSC 111'],'CSC 115': ['CSC 110', 'CSC 111'], 'CSC 106': []}
then the function should return: {'MATH 12': [ 'CSC 111'], 'CSC 110': [ 'CSC 115', 'CSC 116'],
'CSC 111': [ 'CSC 115', 'CSC 116']}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.


def process(dic):
   newdic = {}
   for k in dic:
       values = dic[k]
       for v in values:
           if v not in newdic:
               newdic[v] = [k]
           else:
               newdic[v].append(k)
  
   #now sort the values list
  
   for k in newdic:
       newdic[k].sort()
  
   return newdic


#testing the function
data = {'CSC 111': ['MATH 12'], 'CSC 116': ['CSC 110', 'CSC 111'],'CSC 115': ['CSC 110', 'CSC 111'], 'CSC 106': []}
print(process(data))

output
======
{'MATH 12': ['CSC 111'], 'CSC 110': ['CSC 115', 'CSC 116'], 'CSC 111': ['CSC 115', 'CSC 116']}

Add a comment
Know the answer?
Add Answer to:
USING PYTHON. Avoid using built in functions if possible. I'm a beginner so keep it simple...
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
  • 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...

  • 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...

  • PYTHON Hello can someone create the following functions? I'm not sure how to start on them....

    PYTHON Hello can someone create the following functions? I'm not sure how to start on them. makeDictionary Define a function named makeDictionary with two parameters. The first argument passed the function must be a list of strings to be used as keys, while the second is a list of the same length containing values (of some type). This function must return a dictionary consisting of the keys paired with the corresponding values. For example, makeDictionary(['apno','value'],['REP-12','450']) must return this dictionary: {'apno':...

  • Using Python. Please help! The output should looks like below. Please follow the emphasis. I think...

    Using Python. Please help! The output should looks like below. Please follow the emphasis. I think it just prompt user to input course name. Write a modular program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key- value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Python 3 Coding Functions: Here is any code required to help code what is below: def...

    Python 3 Coding Functions: Here is any code required to help code what is below: def pokemon_by_types(db, types): new_db = {} for pokemon_type in types: for key in db: # iterate through all the type in types list if db[key][1] == pokemon_type or db[key][2] == pokemon_type: if key not in new_db: new_db[key] = db[key] return new_db I need help coding the functions listed below in the image: Thank you get types(db): Given a database db, this function determines all the...

  • For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements...

    For Python [25 pts] Write the method divisorList(aList, divisor) that takes in a list with elements of any data type and a divisor which is in the form of an integer. The output is a sorted list of only the integers that are divisible by the divisor. Example: divisorList([1, 1, 12, 'a', 90, 34], 2) will return [12, 34, 90] since 12, 90, and 34 are all divisible by two. Note that the returned list is sorted from smallest to...

  • ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a...

    ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a value and print a result to the console, to test you must call the function, run it and visually inspect the result for correctness. With functions that return a value, use the print_test function to provide feedback of the test results at the command line. The print_test function is implemented for you at the bottom of this file. RECALL: floating point arithmetic can lose...

  • In python Create a function that will take in an argument ( a 2D list of...

    In python Create a function that will take in an argument ( a 2D list of strings). The function will return a dictionary where the key is the first element of each row, and the key is the remaining elements. Example listOne ['aa', 'bb', 'cc', 'dd' ], ['ee', ff, 'gg', 'hh', 'ii', 'j' l ['kk', 'll', 'mm', 'nn' ] = Should return {'aa' : [ 'bb', 'cc', 'dd' ], 'ee' ff, 'gg', 'hh', 'ii, j' ] 'kk' : [ '',...

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