viveksyngh / MIT-6.00.1x-Introduction-to-Computer-Science-and-Programming-Using-Python

Code from MIT 6.00.1x: Introduction to Computer Science and Programming Using Python Course on EDX
1 stars 0 forks source link

IsIn #1

Open prabhat010187 opened 2 years ago

prabhat010187 commented 2 years ago

the solution is wrong as it did not consider if the len of given string is <=2. correct solution : lower=0 upper=len(aStr)-1 bisec=(lower+upper)//2

if len(aStr)==0:
    return False
elif len(aStr)==1 or len(aStr)==2:
    if aStr[bisec]==char or aStr[bisec:]==char :
         return True
    else:
        return False
elif aStr[bisec]==char:
        return True
elif char>aStr[bisec]:
    return isIn(char, aStr[bisec+1:])
elif char<aStr[bisec]:
        return isIn(char, aStr[0:bisec])
else:
        return False

isIn('n', 'acfhijnpptuvw')

prabhat010187 commented 2 years ago

small correction: upper =len(aStr)