logo CodeStepByStep logo

collection_mystery4

Language/Type: Python collection mystery collections

Write the output that is printed when the given function below is passed each of the following dictionaries and lists as its parameters. Recall that dictionaries print in a {key1: value1, key2: value2, ..., keyN: valueN} format.

Though dictionaries usually have unpredictable ordering, for this problem, you should assume that when looping over the dictionary or printing a dictionary, it visits the keys in the order that they were added to the dictionary or the order they are declared below. If a dictionary sets the value for a key that already exists, it replaces that key and keeps it in its current position in the ordering.

def collection_mystery4(map, lst):
    result = {}
    for s in lst:
        if s in result:
            result[s] = result[s] + result[s]
        elif s in map:
            result[map[s]] = s
    print(result)
map = {'Marty': 'Stepp', 'Cynthia': 'Lee', 'Keith': 'Schwarz', 'Bruce': 'Lee', 'Mehran': 'Sahami'} lst = ['Cynthia', 'Bruce', 'Lee', 'Eric', 'Schwarz', 'Keith', 'Sahami']
map = {'dog': 'woof', 'cat': 'meow', 'horse': 'whinny', 'frog': 'ribbit', 'duck': 'dog'} list = ['dog', 'horse', 'dog', 'woof', 'meow', 'cat', 'meow', 'woof']

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.