python100例经典python100例用途python100例测试python100例下载
下载地址 https://share.weiyun.com/Fz43Qnru
资料目录 Python练习集100题 100道Python面试题 Python100经典练习题 Python经典题目100道题 Python题库(已收录100道真题) Python100例视频讲解课程 菜鸟教程Python教程100例 130道python练习题,涵盖基础内容的方方面面
举例 find def my_find(source, target, start=0): """ 返回字符串source中 子串target开始的位置, 从start索引开始搜索 如果可以找到多个,返回第一个 :param source: :param target: :param start: :return: """ if not source or not target: return -1
# 不合理的搜索起始位置 if start < 0 or start >= len(source): return -1
if len(target) > len(source): return -1
for index in range(start, len(source) - len(target)+1): t_index = 0 while t_index < len(target): if target[t_index] == source[t_index+index]: t_index += 1 else: break
if t_index == len(target): return index
return -1
if __name__ == '__main__': print(my_find('this is a book', 'this')) print(my_find('this is a book', 'this', start=1)) print(my_find('this is a book', 'book')) print(my_find('this is a book', 'k', start=10)) print(my_find('this is a book', 'book', start=10)) print(my_find('this is a book', 'a', start=3))
|