-
Notifications
You must be signed in to change notification settings - Fork 32
/
lec5prob8-isIn.py
48 lines (41 loc) · 1.25 KB
/
lec5prob8-isIn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# lec5prob8-IsIn.py
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Lecture 5, problem 8
def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string
returns: True if char is in aStr; False otherwise
'''
# Your code here
low = 0
high = len(aStr)
mid = (low + high) / 2
i = 0
# Set limits on how many times loop will run
while i < 50:
i += 1
# If string is empty return False
if len(aStr) <= 0:
return False
# if char = middle character return True
if char == aStr[mid]:
return True
# if we've gone through entire string and not found match
# return False
if (low == mid or high == mid) and (char != aStr[mid]):
return False
else:
# if char is greater than the midpoint, move selection
# of string up
if char > aStr[mid]:
low = mid
return isIn(char, aStr[low:high])
# if char is greater than the midpoint, move selection
# of string down
else:
high = mid
return isIn(char, aStr[low:high])
theResult = isIn('a', 'abcdef')
print (str(theResult))