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) that given an dictionary d with string
    keys and lists of strings as values, returns its inverse.

    For instance, if the original dictionary d 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)
    function to obtain the inverted dictionary, and prints the result.

Input

The input is a dictionary in the following format:

- Each line contains several strings separated by whitespaces,
  describing a single dictionary entry.

- The first string in the line is the key, and the remaining strings are
  the list of values for that key.

- Each line has at least two string (the key plus at least one element
  for the list value).

- All strings in the list value for a key are different.

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
