Late Library Books

Data with PythonMedium+10 XP+10

Late Library Books

A library lends books for 14 days. Its records give, for each loan, the book's title, the date it was borrowed and the date it came back. A book that hasn't come back yet has None as its return date.

Write days_late(titles, borrowed, returned, today). The three lists line up position by position. All dates are text in the form "YYYY-MM-DD", and so is today.

  • A loan ends on its return date. If the book is still out (None), count it as ending on the date given by today.
  • Days late = (days from borrowing to the end of the loan) − 14.

Return a dictionary mapping each title that is more than 0 days late to how many days late it is, as a whole number, in the same order as the input. Titles are all different. If nothing is late, return an empty dictionary.

Examples

titles = ["Dune", "Emma", "Ulysses"]
borrowed = ["2024-03-01", "2024-03-05", "2024-03-10"]
returned = ["2024-03-20", "2024-03-12", None]
today = "2024-04-01"
returns {'Dune': 5, 'Ulysses': 8}
titles = ["Holes", "Matilda"]
borrowed = ["2024-06-01", "2024-06-03"]
returned = ["2024-06-15", None]
today = "2024-06-10"
returns {}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run