Hotel Booking System using Python

def hotel_booking(arr, dept, room):
    event = [(t, 'RED') for t in arr] + [(t, "BLUE") for t in dept]
    event = sorted(event)
    
    guest = 0
    for e in event:
        if e[1] == 'RED':
            guest+=1
        else:
            guest-=1
        
        if guest > room:
            return 'Sorry the rooms are full'
    return 'Rooms available'

arr = [1, 3, 5]
dept = [2, 6, 8]
k = 1

print(hotel_booking(arr, dept, k))

Rain Water Trapping program in Python

def rain_water_trap(arr):
    size = len(arr)
    left = size * [0]
    right = size * [0]
    left[0] = arr[0]
    max_so_far_left = arr[0]
    water = 0

    for index in range(size):
        if max_so_far_left < arr[index]:
            max_so_far_left = arr[index]
            left[index] = max_so_far_left
        else:
            left[index] = max_so_far_left
     
    max_so_far_right = arr[-1]

    for index in range(size-1, -1, -1):
        if max_so_far_right < arr[index]:
            max_so_far_right = arr[index]
            right[index] = max_so_far_right
        else:
            right[index] = max_so_far_right

    for index in range(size):
        water = water + min(left[index],right[index]) - arr[index]

    return water

list1 = [1, 0, 2, 0, 1, 0, 3, 1, 0, 2]
print(rain_water_trap(list1))

Evaluate a Postfix expression using Python

def postfix(arr):
    stack = []
    operators = ['+', '-', '*', '/', '%']
    for item in arr:
        if item not in operators:
            stack.append(item)
        else:
            first = int(stack.pop())
            second = int(stack.pop())

            if(item == '+'):
                stack.append(second + first)
            elif(item == '-'):
                stack.append(second - first)
            elif(item == '*'):
                stack.append(second * first)
            elif(item == '/'):
                stack.append(second / first)
            elif(item == '%'):
                stack.append(second % first)
    return stack[-1]

arr = ['2', '1', '+', '3', '5', '+', '*']
print(postfix(arr))