Sum of Maximum Sub Array elements in Python

def max_sum_SubArray(arr):
    current_sum = 0
    max_so_far = arr[0]
    for i in range(len(arr)):
        current_sum = current_sum + arr[i]
        if max_so_far < current_sum:
            max_so_far = current_sum
        elif current_sum < 0:
            current_sum = 0
    return max_so_far

arr = [4, -3, -2, 2, 3, 1, -2, -3, 6, -6, -4, 2, 1]
print(max_sum_SubArray(arr))

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))

Return the minimum height of a Binary Tree using Python

class Binary:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def height(A):
    if A == None:
        return 0
    else:
        ldepth = height(A.left)
        rdepth = height(A.right)

        if(ldepth > rdepth):
            return 1+rdepth
        return 1+ldepth

root = Binary(1)
root.left = Binary(2)
root.right = Binary(3)
root.left.left = Binary(4)
root.left.right = Binary(5)
root.left.left.left = Binary(7)
root.right.right = Binary(8)

print(height(root))