Lecture 11 Finger Exercise

The questions below are due on Friday May 08, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Question 1. Implement a function group_anagrams(words) that satisfies the docstring given below.

def group_anagrams(words):
    """
    Identify and label anagrams among a collection of words.
    Two words are anagrams of each other if they contain exactly the same letters,
    including the same count for each letter.

    Parameters:
        words (list): A collection of three-letter strs.
            These strings may contain repeated letters (e.g., "abb").
            You may assume that these strings are all unique are unique.

    Return a dict that maps tuples of three letters to their corresponding
    anagrams in words. Specifically:
        + Each key is a tuple containing the three single-character strings of the
        original word, sorted in alphabetical order.
        + Each value is a list containining all strs in words that can be formed
        using the tuple's characters. Each list should be in alphabetical
        order.
    """

For example:

  • When words = ["bat", "tab", "cat", "act", "dog", "god", "odg"],
    group_anagrams(words) should evaluate to
    {
        ("a", "b", "t"): ["bat", "tab"],
        ("a", "c", "t"): ["act", "cat"],
        ("d", "g", "o"): ["dog", "god", "odg"],
    }
    

  • When words = ["abb", "bab", "dec"],
    group_anagrams(words) should evaluate to
    {
        ("a", "b", "b"): ["abb", "bab"],
        ("c", "d", "e"): ["dec"],
    }
    

Question 2. Implement the function filter_graph(graph, blocked_roads) that removes all blocked roads from a given graph.

def filter_graph(graph, blocked_roads):
    """
    Remove blocked roads from a directed graph.

    Parameters:
        graph (dict): A dict where each key is a starting city (str) and
            the value is a list of destination cities.
        blocked_roads (list): A list of tuples (city1, city2) representing
            roads that are currently closed and should be removed if they
            are in the graph.

    Return a dict representing the filtered graph. Specifically:
        + Each list of destination cities must be sorted lexicographically.
        + The returned dict should only include keys (cities) that have
        at least one outgoing road remaining after filtering.
    """