Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

A library contains books. Books have a writer, identified by last name and first name. Books also have a title. Books also have a location number that identifies where they can be found in the library. Librarians want to be able to locate a specific book if they know writer and title, and they want to be able to list all the books that they have of a specific writer. What data structure would you use to store the books?

Answer

Use a dictionary (dict) that maps each writer onto another dictionary (dict), holding the titles of that writer’s books and their location numbers. Represent the writer itself as a tuple with the last name and the first name: a tuple is immutable, so it can be used as a key of a dictionary, which a list cannot.

That covers both things the librarians want. To locate a single book, you look up the writer and then the title. To list everything the library has of a writer, you take the keys of that writer’s inner dictionary.

Assignment

Data about the books of a library is represented as a list of books, with each book represented as a tuple containing four elements: i) the last name of the writer (str), ii) the first name of the writer (str), iii) the title of the book (str) and iv) the location number of the book (int). Your task:

Example

>>> books = [
...     ('Adams', 'Douglas', "The Hitchhiker's Guide to the Galaxy", 42),
...     ('Adams', 'Douglas', 'The Restaurant at the End of the Universe', 43),
...     ('Adams', 'Douglas', 'Life, the Universe and Everything', 44),
...     ('Rowling', 'Joanne', "Harry Potter and the Philosopher's Stone", 271),
...     ('Rowling', 'Joanne', 'Harry Potter and the Chamber of Secrets', 272),
...     ('Tolkien', 'John', 'The Hobbit', 137),
...     ('Spronck', 'Pieter', "The Coder's Apprentice", 512),
... ]
>>> library = list2dict(books)
>>> library[('Tolkien', 'John')]
{'The Hobbit': 137}
>>> library[('Rowling', 'Joanne')]
{"Harry Potter and the Philosopher's Stone": 271, 'Harry Potter and the Chamber of Secrets': 272}
>>> location(library, 'Adams', 'Douglas', 'Life, the Universe and Everything')
44
>>> location(library, 'Adams', 'Douglas', 'The Long Dark Tea-Time of the Soul') is None
True
>>> titles(library, 'Adams', 'Douglas')
['Life, the Universe and Everything', "The Hitchhiker's Guide to the Galaxy", 'The Restaurant at the End of the Universe']
>>> titles(library, 'Pratchett', 'Terry')
[]