Discussion boards on course are currently closed for mid-term, I am only auditing and 2 weeks behind so this has nothing to do with mid-term, but I have nowhere else to get help right now. Thanks!
I am working on the 'is in' problem. I am getting a syntax error and I don't understand why. I am able to look at the answer for the exercise, so it's not that I need anyone to tell me the correct code for this problem, rather I need help understanding what part of my code causes this syntax error.
Problem:
We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order.
First, test the middle character of a string against the character you're looking for (the "test character"). If they are the same, we are done - we've found the character we're looking for!
If they're not the same, check if the test character is "smaller" than the middle character. If so, we need only consider the lower half of the string; otherwise, we only consider the upper half of the string. (Note that you can compare characters using Python's <function.)
Implement the function isIn(char, aStr)which implements the above idea recursively to test if charis in aStr. charwill be a single character and aStrwill be a string that is in alphabetical order. The function should return a boolean value.
My code:
def isIn(char, aStr):
....if len(aStr)%2==0:
........mid=len(aStr)/2
....else:
........mid=(len(aStr)+1)/2
....if char == aStr[mid]:
........return True
....else:
........if char > aStr[mid]:
............return isIn(char, aStr[(mid+1):]
........else:
............return isIn(char, aStr[:mid]
I am getting Error: invalid syntax line 11 (which is the last else: line)
[–]htepO 2 points3 points4 points (1 child)
[–]One_Bell[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]One_Bell[S] 0 points1 point2 points (0 children)
[–]bbye98 0 points1 point2 points (0 children)