I am trying to write a code which to compare each element of a list and give the index of the closet larger number in two direction: left or right. Using the divide and conquer method
For example:
Input: arr=[5,2,6,8,1,4,3,9]
Output:
Left=[None, 0, None, None, 3, 3, 5, None]
Right=[2, 2, 3, 7, 5, 7, 7, None]
Input: arr=[4,2,3,1,8,5,6,9]
Output:
L=[None, 0, 0, 2, None, 4, 4, None]
R=[4, 2, 4, 4, 7, 6, 7, None]
This is what I have now:
arr = [5,2,6,8,1,4,3,9]
def Left(arr):
L = []
for i in range(len(arr)):
flag = True
for j in range(i,-1,-1):
if (arr[i] < arr[j]):
L.append(j)
flag = False
break
if flag:
L.append(None)
return L
def Right(arr):
R = []
for i in range(len(arr)):
flag = True
for j in range(i, len(arr), 1):
if (arr[i] < arr[j]):
R.append(j)
flag = False
break
if flag:
R.append(None)
return R
print(*Left(arr), sep = ",")
print(*Right(arr), sep =",")
Am I doing it in a right way? Thank you.