MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs ...

ESTIMATED TIME TO COMPLETE: 18 minutes

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 char is in aStrchar will be a single character and aStr will be a string that is in alphabetical order. The function should return a boolean value. 

As you design the function, think very carefully about what the base cases should be.

 

def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string

returns: True if char is in aStr; False otherwise
''' 这是我写的答案:
# Your code here
if len(aStr) == 0:
  return False
elif len(aStr) == 1:
  if aStr == char:
    return True
  else:
    return False
else:
  if char == aStr[len(aStr)//2]:
    return True
  elif char < aStr[len(aStr)//2]:
    return isIn(char, aStr[:len(aStr)//2])
  else :
    return isIn(char, aStr[len(aStr)//2+1:])

def isIn(char, aStr):
   ''' 这是标准答案:
   char: a single character
   aStr: an alphabetized string
   
   returns: True if char is in aStr; False otherwise
   '''
   # Base case: If aStr is empty, we did not find the char.
   if aStr == '':
      return False

   # Base case: if aStr is of length 1, just see if the chars are equal
   if len(aStr) == 1:
      return aStr == char

   # Base case: See if the character in the middle of aStr equals the 
   #   test character 
   midIndex = len(aStr)//2
   midChar = aStr[midIndex]
   if char == midChar:
      # We found the character!
      return True
   
   # Recursive case: If the test character is smaller than the middle 
   #  character, recursively search on the first half of aStr
   elif char < midChar:
      return isIn(char, aStr[:midIndex])

   # Otherwise the test character is larger than the middle character,
   #  so recursively search on the last half of aStr
   else:
      return isIn(char, aStr[midIndex+1:])

虽然第一次看这道题时,我是懵逼的,不太懂题目的意思。但是我再看第二遍的时候,我突然理解了题意:用二分法和递归在从小到大排列的字符串中找出要求的字符。最后我的答案是正确的,可以看出:我和答案的思想是一样的,但是它写的比我简练,这点需要学习。

转载于:https://www.cnblogs.com/Bella2017/p/7944465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值