4
#Recursive BinaryChop
def recursiveBinaryChop( value, elementList, min, max ):
    if len( elementList ) == 0:
        return -1
    if max <= min:
        if ( max == min and elementList[min] == value ):
            return min
        else:
            return -1
    else:
        midPointOfList = ( min + max ) / 2

        if elementList[midPointOfList] > value:
            max = --midPointOfList
            return recursiveBinaryChop( value, elementList, min, max )
        elif elementList[midPointOfList] < value:
            min = ++midPointOfList
            return recursiveBinaryChop( value, elementList, min, max )
        else:
            return midPointOfList

#Recursive BinaryChop Test Cases
assert recursiveBinaryChop(3, [], 0, 0) == -1
assert recursiveBinaryChop(3, [1], 0, 0) == -1
assert recursiveBinaryChop(1, [1], 0, 0) == 0
assert recursiveBinaryChop(1, [1, 3, 5], 0, 2) == 0
assert recursiveBinaryChop(3, [1, 3, 5], 0, 2) == 1
assert recursiveBinaryChop(5, [1, 3, 5], 0, 2) == 2
assert recursiveBinaryChop(0, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(2, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(4, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(6, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(1, [1, 3, 5, 7], 0, 3) == 0
assert recursiveBinaryChop(3, [1, 3, 5, 7], 0, 3) == 1
assert recursiveBinaryChop(5, [1, 3, 5, 7], 0, 3) == 2
assert recursiveBinaryChop(7, [1, 3, 5, 7], 0, 3) == 3
assert recursiveBinaryChop(0, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(2, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(4, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(6, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(8, [1, 3, 5, 7], 0, 3) == -1

I am getting the run time error for this simple code, I have tried searching but all answer seems to suggest setting the recursion limit but I don't see that happening with my test input. I am not sure whether my algorithm is wrong or has some logical error. I have the same algorithm working for me in C++.

Please help.

Krishna Oza
  • 1,390
  • 2
  • 25
  • 50
  • Same with `++`. `+x` is just `+x` unchanged, so `++x` = `+(+x)` = `x`. – Andrew Janke May 04 '15 at 14:59
  • @Kevin unfortunately it didn't throw any error and hence a possible source of logical errors such as this since I come from `C/C++` background. – Krishna Oza May 04 '15 at 15:01
  • 4
    Incidentally, great job providing all those test case asserts. I wish every SO post was that thorough :-) – Kevin May 04 '15 at 15:02
  • @Kevin How can we edit the question title to reflect the real problem the OP is encountering and help others find it? Something like "Why don't ++ and -- work in Python?" – kdopen May 04 '15 at 15:09
  • @kdopen there are questions with similar title, it was just the ignorance on my part to not know this. – Krishna Oza May 04 '15 at 15:12
  • Yep, the obvious one is http://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python – kdopen May 04 '15 at 15:19

2 Answers2

6
    if elementList[midPointOfList] > value:
        max = --midPointOfList
        return recursiveBinaryChop( value, elementList, min, max )
    elif elementList[midPointOfList] < value:
        min = ++midPointOfList
        return recursiveBinaryChop( value, elementList, min, max )

Python doesn't have -- or ++ operators. If you're trying to decrement and increment, try "-1" and "+1".

    if elementList[midPointOfList] > value:
        max = midPointOfList - 1
        return recursiveBinaryChop( value, elementList, min, max )
    elif elementList[midPointOfList] < value:
        min = midPointOfList + 1
        return recursiveBinaryChop( value, elementList, min, max )

(This isn't exactly the same behavior as C++'s -- and ++, since midPointOfList's value remains unchanged, but that doesn't seem to matter in this particular circumstance; midPointOfList doesn't get referred to after those lines anyway)

Kevin
  • 74,910
  • 12
  • 133
  • 166
6

These two lines don't do what you think:

max = --midPointOfList
min = ++midPointOfList

Python doesn't have this type of increment operators, but they do parse and execute successfully.

++i parses as +(+i) and --i as -(-i). Both leave i unchanged and are effectively

max = midPointOfList
min = midPointOfList
kdopen
  • 8,032
  • 7
  • 44
  • 52
  • I don't understand the real use case of this operators for `int` like variables. – Krishna Oza May 04 '15 at 15:04
  • 4
    The point is that `++` and `--` **aren't** operators in Python. Therefore, Python parses them according to its actual grammar. The `+(+i)` form is the only way it can make sense of `++`. Personally, I think it should be a syntax error. – kdopen May 04 '15 at 15:07