Home > other >  How to find overlapping reservations
How to find overlapping reservations

Time:02-27

I'm creating a reservation system and I want my function is_reserved to return a new reservation scheduled or a message saying 'there's already a reservation'

from datetime import datetime

def is_reserved(request):
    start_request = to_date(request['start'])
    stop_request = to_date(request['end'])
    flag = False
    for reservation in range(len(reservations)):
        start = to_date(reservations[reservation]['start'])
        stop = to_date(reservations[reservation]['end'])
        if start_request > start and stop_request < stop:
            print("There's already a reservation!")
            flag = True
            break
    if flag == False:
        reservations.append(request)
        print("There wasn't a reservation, new reservation made")
        

def to_date(request):
    return datetime.strptime(request, '%Y-%m-%dT%H:%M:%S' "Z")

def see(list):
    for reservation in range(len(reservations)):
        print(reservations[reservation])

reservations = []


res1 = {
    'id' :1,
    'start':datetime(2022,2,23,hour=14, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end':datetime(2022,2,23,hour=15, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

reservations.append(res1)

res2 = {
    'id' :2,
    'start':datetime(2022,2,23,hour=16, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end':datetime(2022,2,23,hour=18, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

res3 = {
    'id' : 3,
    'start' : datetime(2022,2,23,hour=18, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end': datetime(2022,2,23,hour=19, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

res4 = {
    'id' : 4,
    'start' : datetime(2022,2,23,hour=15, minute=30).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end': datetime(2022,2,23,hour=17, minute=50).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

see(reservations)

is_reserved(res2)

see(reservations)

is_reserved(res3)

see(reservations)

is_reserved(res4)

see(reservations)

I get these results:

{'id': 1, 'start': '2022-02-23T14:00:00Z', 'end': '2022-02-23T15:00:00Z'}
There wasn't a reservation, new reservation made
{'id': 1, 'start': '2022-02-23T14:00:00Z', 'end': '2022-02-23T15:00:00Z'}
{'id': 2, 'start': '2022-02-23T16:00:00Z', 'end': '2022-02-23T18:00:00Z'}
There wasn't a reservation, new reservation made
{'id': 1, 'start': '2022-02-23T14:00:00Z', 'end': '2022-02-23T15:00:00Z'}
{'id': 2, 'start': '2022-02-23T16:00:00Z', 'end': '2022-02-23T18:00:00Z'}
{'id': 3, 'start': '2022-02-23T18:00:00Z', 'end': '2022-02-23T19:00:00Z'}
There wasn't a reservation, new reservation made
{'id': 1, 'start': '2022-02-23T14:00:00Z', 'end': '2022-02-23T15:00:00Z'}
{'id': 2, 'start': '2022-02-23T16:00:00Z', 'end': '2022-02-23T18:00:00Z'}
{'id': 3, 'start': '2022-02-23T18:00:00Z', 'end': '2022-02-23T19:00:00Z'}
{'id': 4, 'start': '2022-02-23T15:30:00Z', 'end': '2022-02-23T17:50:00Z'}

The problem is that the fourth reservation normally can't be done because there's already the second reservation. In my for loop I compare the reservation request with every other reservation already made, but comparing one to one doesn't let the function see if there's reservations after or before and that's where I'm blocking.

If you have any ideas on how I can solve this please.

CodePudding user response:

The best way to accomplish that is using a number of "rooms" for example. In this case the rooms are represented with the constant CONN which for your case it is equal to 1.

 from datetime import datetime

arrivals = []
departures = []
CONN = 1

def to_date(request):
    return datetime.strptime(request, '%Y-%m-%dT%H:%M:%S' "Z")

def reserve(request):
    start_request = to_date(request['start'])
    stop_request = to_date(request['end'])

    arrivals.append(start_request)
    departures.append(stop_request)


    arrivals.sort()
    departures.sort()
    
    for i in range(len(arrivals)):
        if i CONN < len(arrivals) and arrivals[CONN i] < departures[i]:
            arrivals.pop(len(arrivals)-1)
            departures.pop(len(arrivals)-1)
            print("We can't make the reservation")
            return False
    print("Reservation made!")
    reservations.append(request)
    see(reservations)
    return True

def see(list):
    print("Reservations: ")
    for reservation in range(len(reservations)):
        print(reservations[reservation])

reservations = []


res1 = {
    'id' :1,
    'start':datetime(2022,2,23,hour=14, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end':datetime(2022,2,23,hour=15, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}



res2 = {
    'id' :2,
    'start':datetime(2022,2,23,hour=16, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end':datetime(2022,2,23,hour=18, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

res3 = {
    'id' : 3,
    'start' : datetime(2022,2,23,hour=18, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end': datetime(2022,2,23,hour=19, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

res4 = {
    'id' : 4,
    'start' : datetime(2022,2,23,hour=18, minute=30).strftime('%Y-%m-%dT%H:%M:%S')   "Z",
    'end': datetime(2022,2,23,hour=19, minute=0).strftime('%Y-%m-%dT%H:%M:%S')   "Z"
}

reserve(res1)
reserve(res2)
reserve(res3)
reserve(res4)
see(reservations)
  • Related