These questions follow Dictionaries and Choosing a Data Structure. A dictionary is organised by key, which is how people ask questions — by name, not by position. Most of the work below is one of two patterns: tally, or look up safely.

Reading

  1. Predict all four printed lines.
    stock = {"pasta": 4, "soup": 6}
    stock["rice"] = 2
    stock["soup"] = stock["soup"] + 3
    print(len(stock))
    print("Soup" in stock)
    print(stock.get("beans", 0))
    print(stock)
  2. Find the fault. This is supposed to answer “should we ask for more of this?” for any item, including ones nobody has donated.
    def restock_needed(stock, item):
        return stock[item] < 3
  3. Read somebody else’s code. Say in one sentence what this returns, then predict it for [("Nadia", 2), ("Rowan", 1.5), ("Nadia", 2.5)].
    def mystery(pairs):
        result = {}
        for name, hours in pairs:
            if name not in result:
                result[name] = []
            result[name].append(hours)
        return result

Writing

  1. Write tally(items), which returns a dictionary counting how often each item appears. Test it on ["Nadia", "Rowan", "Nadia", "Bea", "Nadia", "Rowan"].
  2. Write most_frequent(counts), which returns the key with the highest value, and returns None for an empty dictionary. Say why the dictionary cannot answer this quickly.
  3. Write invert(lookup), which turns {"Nadia": 12, "Rowan": 11, "Ali": 12, "Bea": 10} into a dictionary from grade to a list of names. Explain why the values have to be lists.
  4. Write combine(first, second), which returns a new dictionary holding the totals of two weekly tallies without changing either input. Show that first is unchanged afterwards.
  5. Using this nested structure, print the total gym attendance across both days, then a per-day total for every activity:
    attendance = {
        "Monday": {"homework club": 12, "gym": 20},
        "Tuesday": {"homework club": 9},
    }
    attendance["Tuesday"]["gym"] = 15
  6. Judgement. The community centre has 400 members. It needs: the sign-in order for today, instant lookup of a member by card number, and a count of visits per member this month. Say what container each one needs, and why one of the three might sensibly be two containers.

Answers

Curriculum connection

A1.2

demonstrate an understanding of type conversion (e.g., string-to-integer, character-to-integer, integer-to-character, floating point-to-integer, casting in an inheritance hierarchy);

Link to original

A1.3

demonstrate the ability to use non-numeric comparisons (e.g., strings, comparable interface) in computer programs;

Link to original

A1.5

describe and use one-dimensional arrays of compound data types (e.g., objects, structures, records) in a computer program.

Link to original

A3.1

demonstrate the ability to read from, and write to, an external file (e.g., text file, binary file, database, XML file) from within a computer program;

Link to original

C1.1

decompose a problem into modules, classes, or abstract data types (e.g., stack, queue, dictionary) using an object-oriented design methodology (e.g., CRC [Class Responsibility Collaborator] or UML [Unified Modeling Language]);

Link to original

C2.1

demonstrate the ability to analyse a precondition (i.e., starting state) and a postcondition (i.e., ending state) in an algorithm;

Link to original