1/42
Part 1 of 8 · The problemLearn

Split stays and group stays

This lesson teaches intervals, stretches of time with a start and an end, and you'll use them to build a stays search. Airbnb candidates report two questions about listings and the nights they're free. One is a split stay, two listings that together cover a trip (3 sources). The other is a group stay, listings chosen so their capacities fit a group (2 reports). Here they're one problem in levels, and level 1 starts with a statement like this one:

listings maps each listing's name to a dict of its details: free, the nights it's free, capacity, how many guests it sleeps, and neighborhood. For example, {"loft": {"free": [1, 2, 3], "capacity": 2, "neighborhood": "Mission"}}. A trip runs from night start through night end, both included.

Write split_stays(listings, start, end). It returns every pair (first, second) of different listings where a guest can sleep at first from night start through some night k, then at second from night k + 1 through end, with at least one night at each.

  • A pair appears once, however many nights k would work. (first, second) and (second, first) are different trips, and the list is sorted.
  • A listing that's free for the whole trip is in no pair: the guest would just book it.
  • A one-night trip has no splits.
ListingFree nights
loft1, 2, 3
barn3, 4, 5
cabin1, 2
dome4, 5
yurt1, 2, 3, 4, 5

split_stays(listings, 1, 5) returns these three pairs, in this order:

firstsecondLast night at first
cabinbarn2
loftbarn2 or 3
loftdome3

split_stays(listings, 2, 2) returns [].

cabin and dome don't pair, because nobody covers night 3. yurt is in no pair, because it's free for the whole trip.