human
Write me a code to ceiling in a sorted array and the other steps required given the following:
Ceiling in a sorted array | Function to get index of ceiling of x in arr [ low . . high ].
If x is smaller than or equal to the first element , then return the first element.
If x is greater than the last element , then return - 1.
get the index of middle element of arr [ low . . high ] low + ( high - low ) / 2.
If x is same as middle element , then return mid.
If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ].
If x is smaller than arr [ mid ] , then either arr [ mid ] is ceiling of x or ceiling lies in arr [ low ... mid - 1 ].
Driver program to check above functions"
How can the above be solved in Python?
assistant
#Here is code to ceiling in a sorted array and the other steps required.
 def ceilSearch ( arr , low , high , x ) :
	if x <= arr [ low ] :
		return low
	if x > arr [ high ] :
		return - 1
	mid = ( low + high ) / 2 ;
	if arr [ mid ] == x :
		return mid
	elif arr [ mid ] < x :
		if mid + 1 <= high and x <= arr [ mid + 1 ] :
			return mid + 1
		else :
			return ceilSearch ( arr , mid + 1 , high , x )
	else :
		if mid - 1 >= low and x > arr [ mid - 1 ] :
			return mid
		else :
			return ceilSearch ( arr , low , mid - 1 , x )
arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ]
n = len ( arr )
x = 20
index = ceilSearch ( arr , 0 , n - 1 , x ) ;
if index == - 1 :
	print ( " Ceiling ▁ of ▁ % d ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " % x )
else :
	print ( " ceiling ▁ of ▁ % d ▁ is ▁ % d " % ( x , arr [ index ] ) )