<p>我的代码将iMid作为一个float读取,并给出一个TypeError,即使在将它包装到integer函数中之后也是如此。还有,有没有其他方法可以找到中间值的索引,比我在这里尝试的更简单</p>
<pre><code>def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string
returns: True if char is in aStr; False otherwise
'''
# Your code here
import numpy as np
def iMid(x):
'''
x : a string
returns: index of the middle value of the string
'''
if len(x) % 2 == 0:
return int(np.mean(len(x)/2, (len(x)+2)/2)) #wrapped the
# answer for iMid
#in the integer function
else:
return int((len(x)+1)/2)
if char == aStr[iMid] or char == aStr: #iMid is not being interpreted as an integer
return True
elif char < aStr[iMid]:
return isIn(char, aStr[0:aStr[iMid]])
else:
return isIn(char, aStr[aStr[iMid]:])
print(isIn('c', "abcd"))
</code></pre>