采用递归思想实现了四个函数,功能分别是①contrains_element(my_list,elem),查找列表中是否含有某一元素;②longest_prefix(word1,word2)查找两个字符串相同的最长前缀并返回;③longest_word(my_list)查找字符串列表中长度最长的字符串(递归法1);④longest_word2(my_list)查找字符串列表中长度最长的字符串(递归法2)。
目录
问题1:Write a recursive function contains_element(my_list,elem)that takes a list of items as input, and returns True if my_listcontains the element elem.
编写一个递归函数contrains_element(my_list,elem),该函数将列表和待查元素作为输入,如果my_list中含有elem,则返回true。
代码实现:
#question 1
def contains_element( my_list , elem ):
if( len( my_list ) == 0):
return False
else:
if(my_list.pop() == elem):
return True
else:
return contains_element( my_list , elem )
#test
print(contains_element([1, 2, 3, 4, 5], 3))
print(contains_element([1, 2, 3, 4, 5], 7))
print(contains_element(['python', 'is', 'life'], 'python'))
测试输出:
True False True
问题2: Write a recursive function longest_prefix(word1,word2) that takes two words as input and returns the longest common prefixbetween them. The longest common prefix between two words isthe longest string that is a prefix of both words.
编写递归函数longest_prefix(word1,word2)将两个单词视为输入,并返回它们之间最长的前缀。两个单词之间的最长前缀是两个单词的前缀最长的字符串。
代码实现:
#question 2
def longest_prefix( word1 , word2 ):
if(len(word1) < len(word2)):
reducelento = len(word1)
else:
reducelento = len(word2)
word1 , word2 = word1[0:reducelento] , word2[0:reducelento]
if(word1==word2):
return word1
elif(len(word1) == 0):
return ''
else:
return longest_prefix(word1[0:-1],word2[0:-1])
#test
print(longest_prefix("python", "pythagoras"))
print(longest_prefix("hello", "world"))
print(longest_prefix("hello", "helloworld"))
测试输出:
pyth hello
问题3:Write a recursive function longest_word(my_list) that takes a listof words (each in the form of a string) and returns the longest wordin the list.On an empty list, your function should return None.
编写一个递归函数longest_word(my_list),该函数在列表中返回最长的单词列表(字符串的形式)。在一个空列表中,您的函数应返回None。(单侧递归)
代码实现:
#question 3
def longest_word(my_list):
if(len(my_list) == 0):
return None
if(len(my_list) == 1):
return my_list[0]
else:
temp = my_list[0]
my_list = my_list[1:]
return temp if len(temp)>len(longest_word(my_list)) else longest_word(my_list)
#test
print(longest_word(['recursion', 'is', 'outrageously', 'fun']))
print(longest_word([]))
print(longest_word(['recursion', 'is', 'outrageously', 'fun','123','123456','and have less to offer, each time we start with someone new']))
测试输出:
outrageously None and have less to offer, each time we start with someone new
问题4:Now see if you can rewrite the previous function longest_word(my_list) using two recursive function callsinstead of one.(双侧递归)
代码实现:
#question 4
def longest_word2(my_list):
if(len(my_list) == 0):
return None
if(len(my_list) == 1):
return my_list[0]
else:
half = int(len(my_list)/2)
return longest_word2(my_list[0:half]) if len(longest_word2(my_list[0:half]))>len(longest_word2(my_list[half:])) else longest_word2(my_list[half:])
# if(len(longest_word2(my_list[0:half]))>len(longest_word2(my_list[half:]))):
# return longest_word2(my_list[0:half])
# else:
# return longest_word2(my_list[half:])
#test
print(longest_word2(['recursion', 'is', 'incredibly', 'fun']))
print(longest_word([]))
print(longest_word(['python <3']))
测试输出:
incredibly None python <3