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:])

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

MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions的更多相关文章

  1. edX MITx: 6.00.1x Introduction to Computer Science and Programming Using Python 课程 Week 1: Python Basics Problem Set 1 Problem 3

    Assume s is a string of lower case characters. Write a program that prints the longest substring of  ...

  2. MIT Introduction to Computer Science and Programming (Lesson one )

    MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Les ...

  3. Introduction to Computer Science and Programming in Python--MIT

    学习总结--(Introduction to Computer Science and Programming in Python--MIT) 导论 主题 重新利用数据结构来表达知识 理解算法的复杂性 ...

  4. 6.00.1x Introduction to computation

    6.00 Introduction to Computer Science                  and  Programming • Goal: –Become skillful at ...

  5. Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>

    Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> ...

  6. Note 1 for <Pratical Programming : An Introduction to Computer Science Using Python 3>

    Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> ...

  7. Discovering the Computer Science Behind Postgres Indexes

    This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...

  8. Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)

    I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...

  9. Computer Science: the Big Picture

    1.课程PPTMIT OpenCourseWarehttp://ocw.mit.edu/courses/; Courses  Stanfordhttp://cs.stanford.edu/course ...

随机推荐

  1. 通过递归遍历n位2进制数的所有情况

    题目要求: 输入一个正整数m,输出m位2进制的所有取值情况,从小到大输出,每个输出结果用换行符分割. 解题思路: 通过递归调用,从第1个到第m个数组元素分别置0和置1,然后当从1到m所有的元素都置0或 ...

  2. Use SFTP in Linux (转)

    From http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html sftp 是一个交互式文件传输程式.它类似于 ftp, ...

  3. 使用mail架包发送邮件javax.mail.AuthenticationFailedException: failed to connect at javax.mail.Service.connec

    这个错误是因为连接不上邮箱服务器导致的,可能有以下几个原因(以网易邮箱为例) 1.当使用第三方登录邮箱时需要有邮箱的授权码,且要开启POP3/SMTP/IMAP:服务 2.在代码中要调用网易邮箱的密码 ...

  4. ThreadStart方式实现多线程

    以ThreadStart方式实现多线程 3.1 使用ThreadStart委托 这 里先以一个例子体现一下多线程带来的好处,首先在Message类中建立一个方法ShowMessage(),里面显示了当 ...

  5. 在Struts2的Action中取得请求参数值的几种方法

    先看GetRequestParameterAction类代码: public class GetRequestParameterAction extends ActionSupport { priva ...

  6. oracle数据库列的操作

    本章和大家分享一下如何在数据库中进行列的一些相关操作. 1.增加列名 (我们先来看一个原始版本) 下面我们增加一个列名tel  记住,增加列时需要把列对应的数据类型要说明,不然会报错. alter t ...

  7. Tomcat与Web.xml配置

    1.编码配置 <Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableL ...

  8. 15分XX秒后订单自动关闭(倒计时)

    //订单记录 function get_order(){ //请求订单ajax方法 XX.send_api("method",{data},function(){ var date ...

  9. .Net Core 项目引用本地类库方式(一)

    最近了解到.NET Core 项目,引用本地类库DLL的方式有三种 1.非同解决方案下的引用,直接引用,浏览,找到对应的DLL,然后确定引用. 这种方式有个不好的地方就是,如果引用的DLL文件里面,也 ...

  10. JMeter的使用——ApacheJMeterTemporaryRootCA.crt的用法

    在使用JMeter的时候,启动HTTP代理服务器弹出的那个提示框一直不知道是什么意思,刚刚弄明白了,在JMeter2.1之后,通过JMeter的代理服务器来访问https安全连接的网页的时候,浏览器会 ...