logo CodeStepByStep logo

CollectionMystery3

Language/Type: C# collection mystery collections

Write the output that is printed when the given method below is passed each of the following dictionaries 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 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 void CollectionMystery3(Dictionary<string, string> dict)
{
    List<string> list = new List<string>();
    foreach (string key in dict.Keys)
    {
        if (dict[key].Length > key.Length)
        {
            list.Add(dict[key]);
        }
        else
        {
            list.Insert(0, key);
            list.Remove(dict[key]);
        }
    }
    Console.WriteLine(list);
}
{{"horse", "cow"}, {"cow", "horse"}, {"dog", "cat"}, {"ok", "yo"}}
{{"bye", "hello"}, {"bird", "dog"}, {"hi", "hello"}, {"hyena", "apple"}, {"fruit", "meat"}}
{{"a", "b"}, {"c", "d"}, {"e", "a"}, {"ff", "a"}, {"gg", "c"}, {"hhh", "ff"}}

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.