-1

Getting None from bs function which is binary search as a recursion it should return the minimum number with rotating arrays. But I am getting None

def bs(a, tempArray, L, R):
    mid = (L + R) // 2
    print(mid, L, R)
    if tempArray[mid + 1] == "T":
        L = mid + 1
    elif tempArray[mid - 1] == "F":
        R = mid - 1
    elif tempArray[mid] == "F":
        return a[mid]
    bs(a, tempArray, L, R)


a = [3, 4, 5, 6, 7, 1, 2]
# T, T, T, T, T, F,F
end = a[len(a) - 1]
tempArray = []
for i in a:
    if i <= end:
        tempArray.append("F")
    else:
        tempArray.append("T")

L, R = 0, len(a) - 1
smallest = bs(a, tempArray, L, R)
print(smallest)
sarvesh kumar
  • 183
  • 11
  • You have a print statement in `bs`, what is the output? Also there is only one return statement, print out a right before it is called. – peer Oct 09 '19 at 14:40
  • When you run bs(a, tempArray, L, R) you get the following : 3 0 6 // 5 4 6 . What would be your expected value for "smallest"?? – spYder Oct 09 '19 at 14:44

1 Answers1

0

You need to return the recursive call to bs.

def bs(a, tempArray, L, R):
    mid = (L + R) // 2
    print(mid, L, R)
    if tempArray[mid + 1] == "T":
        L = mid + 1
    elif tempArray[mid - 1] == "F":
        R = mid - 1
    elif tempArray[mid] == "F":
        return a[mid]
    return bs(a, tempArray, L, R)  # HERE<<<<<<
Jim Wright
  • 5,905
  • 1
  • 15
  • 34