Invert-a-dict (2)

A dictionary where values are lists can be inverted obtaining another dictionary where the keys are the elements in value lists of the original dictionary, and values are lists of the original keys.

  1. Program a function inverse(d)inverse(d) that given an dictionary dd with string keys and lists of strings as values, returns its inverse.

    For instance, if the original dictionary dd is:

    { 'John' : ['AP1', 'CAOS', 'POC', 'MB'],
      'Mary' :  ['CAOS', 'MB', 'ALG'],
      'Sheila' : ['CAL']
      'Peter' :  ['AP1', 'ALG', 'POC', 'CB', 'MB'] 
    }

    the function will return

    { 'AP1': ['John', 'Peter'],
      'CAOS': ['John', 'Mary'],
      'POC': ['John', 'Peter'],
      'MB': ['John', 'Mary', 'Peter'],
      'ALG': ['Mary', 'Peter'],
      'CAL': ['Sheila']
      'CB': ['Peter']
    }
  2. Write a main program that reads a dictionary, uses the inverse(d)inverse(d) function to obtain the inverted dictionary, and prints the result.

Input

The input is a dictionary in the following format:

Output

The output is the inverted dictionary. Keys are printed in lexicographical order, and so are their corresponding lists of values. Follow the format of the examples.

Problem information

Author: Lluís Padró

Generation: 2026-01-25T12:52:36.001Z

© Jutge.org, 2006–2026.
https://jutge.org