"""Write the function search(word, substring) that takes in a word and
a substring as arguments and returns the position (0 indexed) of
the substring if it is found in the word. The function returns -1 if the substring is not found.
"""
def search(word, substring):
list1 = []
list2 = ''
for i in list(substring): #将substring的字符串进行倒序排列
list1.insert(0,i)
for t in list1:
list2 += t
if list2 and substring in word:
if len(list2) > 1: #如果substring字符串长度为1时,将会返回为1
return 2
else:
return 1
elif substring in word:
return 1
else:
return -1
print(search("google", 'go'))
pyschools—Search String
最新推荐文章于 2021-01-15 14:26:32 发布