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:
listingsmaps each listing's name to a dict of its details:free, the nights it's free,capacity, how many guests it sleeps, andneighborhood. For example,{"loft": {"free": [1, 2, 3], "capacity": 2, "neighborhood": "Mission"}}. A trip runs from nightstartthrough nightend, both included.Write
split_stays(listings, start, end). It returns every pair(first, second)of different listings where a guest can sleep atfirstfrom nightstartthrough some nightk, then atsecondfrom nightk + 1throughend, with at least one night at each.
- A pair appears once, however many nights
kwould 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.
Listing Free nights loft 1, 2, 3 barn 3, 4, 5 cabin 1, 2 dome 4, 5 yurt 1, 2, 3, 4, 5
split_stays(listings, 1, 5)returns these three pairs, in this order:
first second Last night at first cabin barn 2 loft barn 2 or 3 loft dome 3
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.