|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
2.
Write a function called findIt that is a refactor of the following code. Your function must not use recursion.
## Determine whether or not text is in string.
# @param text the text to search for
# @param string the string to search for text
# @return True if string is in text, False otherwise
#
def find(text, string) :
if text.startswith(string) :
return True
elif len(string) < len(text) :
return find(text[1 : ], string)
return False
3. Write a function called countIt. This function is an ammended version of findIt. It returns the number of times a substring is present in a string. For example
countIt(‘abaaba’,‘ab’) should equal 2
countIt(‘abaaba’,‘baa’) should equal 1
countIt(‘abaaba’,‘c’) should equal 0
4. Write a function called countRec. This is a refactored version of countIt. Your function must use recursion. |
|