I am currently working in edx programming, and my instructions are as follows: Using the idea of searching in half, write a recursive algorithm that checks if a character is included in the string if the string is in alphabetical order. My code (python 2.7) is here:
def isitIn(char, aStr):
m = aStr[len(aStr)
if aStr == '' or len(aStr) == 1 or char == m:
return False
else:
if char < m:
return isitIn(char, aStr[:-1])
elif char > m:
return isitIn(char, aStr[1:])
return isitIn(char, aStr)
My explanation: First, I start by looking for the middle character of the string. If it is equal to a character, it returns False. If it does not match the character, it continues to check whether the character is below the middle character, and then uses the recursive function to create stacks and ultimately returns the logical value True. Now I used the index -1 and 1, since I do not want to include the middle character.
, , . !
Error message:
Test: isIn('a', '')
Your output:
Traceback (most recent call last):
File "submission.py", line 10, in isIn
m = aStr[len(aStr) // 2]
IndexError: string index out of range
Correct output:
False