def wave(arr):
size = len(arr)
for index in range(0, size, 2):
if index > 0 and arr[index-1] > arr[index]:
arr[index-1], arr[index] = arr[index], arr[index-1]
elif index < size - 1 and arr[index] < arr[index+1]:
arr[index], arr[index+1] = arr[index+1], arr[index]
return arr
arr = [3, 5, 12, 2, 6, 10, 7, 9, 8]
print(wave(arr))
Category: Uncategorized
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))
Maximum difference between 2 array elements in Python
def max_diff(arr):
arr = sorted(arr, reverse = True)
x = len(arr)
max_diff = 0
for i in range(x-1):
if(arr[i]-arr[i+1] > max_diff):
max_diff = arr[i] - arr[i+1]
return max_diff
arr = [1, 6, 8, 4, 5, 3, 98, 65]
print(max_diff(arr))
Minimum difference between 2 array elements in Python
def min_diff(arr):
arr=sorted(arr)
x = len(arr)
min_diff = 9999*9999
for i in range(x-1):
if(arr[i+1]-arr[i]<min_diff):
min_diff = arr[i+1] - arr[i]
return min_diff
arr = [1, 6, 8, 4, 5, 3, 98, 65]
print(min_diff(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))
Return the maximum 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+ldepth
return 1+rdepth
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))
Program to convert dictionary to a tuple in Python.
def dict_to_tuple():
x = {1: 'one', 2: 'two', 3: 'three'}
for item in x.items():
print(item)
dict_to_tuple()
Program to convert two list into dictionary in Python.
def list_to_dict():
list1 = [1, 2, 3]
list2 = ['one', 'two', 'three']
result = dict(zip(list1, list2))
print(result)
list_to_dict()
Program to print same alphabets in two strings
def common_letters():
str1 = input("Enter the first string : ")
str2 = input("Enter the second string : ")
set1 = set(str1)
set2 = set(str2)
common = set1 & set2
print(common)
common_letters()