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. PyGrub

    from:https://wiki.debian.org/PyGrub Using pyGRUB on Wheezy to boot a domU kernel Using pyGRUB from x ...

  2. 11-vector的使用

    C++ vector用法(详解!!函数,实现) 原创 2016年09月30日 01:13:40 7862 1,简述一下vector的基本操作,它的size,capacity(),clear,rever ...

  3. 数字图像处理实验(16):PROJECT 06-03,Color Image Enhancement by Histogram Processing 标签: 图像处理MATLAB 2017

    实验要求: Objective: To know how to implement image enhancement for color images by histogram processing ...

  4. 为什么有时候在Windbg中下断点下不了呢??

    1.今天我发现我下断点的地方是我声明的变量,变量还仅仅是声明,没有赋值.因此不能下断点. 2.当不能下断点时,如何解决呢? (1)重启Vmware虚拟机.(2)在虚拟机中恢复“快照”.

  5. 【linux命令】setterm控制终端属性命令(中英文)

    [linux命令]setterm控制终端属性命令(中英文) 2018年03月23日 17:13:44 阅读数:489 标签: linux 更多 个人分类: linux 摘自:https://blog. ...

  6. Thumbnail 图片帮助

    public class Thumbnail { private Image srcImage; private string srcFileName; /// <summary> /// ...

  7. HTML5移动开发即学即用(双色) 王志刚 pdf扫描版​

    HTML5已经广泛应用于各智能移动终端设备上,而且绝大部分技术已经被各种最新版本的测览器所支持:逐一剖析HTML5标准中包含的最新技术,详细介绍了HTML5新标准中提供的各种API,各种各样的应用实例 ...

  8. 【转】android中layout_weight的理解

    android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...

  9. sql server 简单语句整合

    1.去重distinct , group by select distinct userid,username from 表名 select userid,username from 表名 group ...

  10. [转]B+Tree图解

    一,    M阶B+树的定义(M阶是指一个节点最多能拥有的孩子数,M>2): 图1.1 3阶B+树 (1)根结点只有1个,分支数量范围[2,m]. (2)除根以外的非叶子结点,每个结点包含分支数 ...