logo CodeStepByStep logo

CollectionMystery2

Language/Type: C# collection mystery collections

Write the output that is printed when the given method below is passed each of the following pairs of maps as its parameter. 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 in the order they are declared below. If a dictionary adds a key that already exists, it retains its current position in the ordering.

public static Dictionary<string, string> CollectionMystery2(Dictionary<string, int> dict1, Dictionary<int, string> dict2)
{
    Dictionary<string, string> result = new Dictionary<string, string>();
    foreach (string s1 in dict1.Keys)
    {
        if (dict2.ContainsKey(dict1[s1]))
        {
            result[s1] = dict2[dict1[s1]];
        }
    }
    return result;
}
dict1 = {{"bar", 1}, {"baz", 2}, {"foo", 3}, {"mumble", 4}}, dict2 = {{1, "earth"}, {2, "wind"}, {3, "air"}, {4, "fire"}}
dict1 = {{"five", 105}, {"four", 104}, {"one", 101}, {"six", 106}, {"three", 103}, {"two", 102}}, dict2 = {{99, "uno"}, {101, "dos"}, {103, "tres"}, {105, "cuatro"}}
dict1 = {{"a", 42}, {"b", 9}, {"c", 7}, {"d", 15}, {"e", 11}, {"f", 24}, {"g", 7}}, dict2 = {{1, "four"}, {3, "score"}, {5, "and"}, {7, "seven"}, {9, "years"}, {11, "ago"}}

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.