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

來源:互聯網
上載者:User

標籤:com   span   middle   標準   should   programs   har   ica   ref   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.