2

I'm relatively new to python(3.3) and I'm just trying to do a binary search through a list of words, and cant figure out how to fix my operand types when it comes down to looping through the indices... I continue to get the TypeError. Cant figure out any way around it

def find(L, target):
    start = 0
    end = len(L) - 1

    while start <= end: 
        middle = (start + end)// 2 
        midpoint = L[middle]
        if midpoint > target:
            end = midpoint - 1
        elif midpoint < target:
            start = midpoint + 1
        else:
            return midpoint

I'm calling the function as so:

L = ["Brian", "Meg", "Peter", "Joe", "Stewie", "Lois"]

find(L, "Joe")

7
  • 6
    binary search only works on sorted lists Commented Dec 17, 2015 at 5:33
  • 3
    midpoint is a string. What should midpoint - 1 do? Commented Dec 17, 2015 at 5:33
  • @FernandoMatsumoto might be a typo. I think he meant middle - 1, middle + 1. Commented Dec 17, 2015 at 5:35
  • To do a successful binary search on an array, the data in the array must be in sorted order. The entries for all except Brian are out of position — the sequence should be Brian, Joe, Lois, Meg, Peter, Stewie. Commented Dec 17, 2015 at 5:35
  • @jianweichuah I think Fernando is pointing out the bug. Commented Dec 17, 2015 at 5:36

3 Answers 3

3

Your logic seems fine, except for the input and the bug with incrementing and decrementing midpoint instead of middle.

def find(L, target):
    start = 0
    end = len(L) - 1

    while start <= end:
        middle = (start + end)/ 2
        midpoint = L[middle]
        if midpoint > target:
            end = middle - 1
        elif midpoint < target:
            start = middle + 1
        else:
            return midpoint

L = ["Brian", "Joe", "Lois", "Meg", "Peter", "Stewie"] # Needs to be sorted.

print find(L, "Peter")
Sign up to request clarification or add additional context in comments.

3 Comments

What if you try to find a string tha doesn't exist? What is returned?
@cricket_007 None. Not sure what the OP's use case is
It should be int((start + end) / 2)
2
def find(L, target):
    start = 0
    end = len(L) - 1
    while start <= end:
        middle = (start + end)// 2
        midpoint = L[middle]
        if midpoint > target:
            end = middle - 1
        elif midpoint < target:
            start = middle + 1
        else:
            return midpoint

    L = ["Brian", "Joe", "Lois", "Meg", "Peter", "Stewie"]
    L = sorted(L)
    print(find(L, "Lois"))

As pointed out by others, use middle instead of midpoint

And to optimally use binary search, sort the list first

1 Comment

sorted(L) returns a new sorted list with the elements of L. Either use L = sorted(L) or L.sort().
0
def binarySearchOnString(arr, x):
        l = 0
        r = len(arr) - 1
        while (l <= r): 
            m = (l + r) // 2 
            if (arr[m] == x): 
                return m
            elif (arr[m] < x): 
                l = m + 1
            else: 
                r = m - 1
        return -1  #   If element is not found  then it will return -1
    
            

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.