我应该得到那些结果,前两个字符串是s1和s2,第三个是我应该得到的结果。 第一个代码是我写的那个,第二个代码是老师的。
“Maria” e “Norma”: Mnao |
“Mai” e “Nor”: Mnaoir |
“Maria” e “Noa”:False |
“Mar” e “Noa”: MNaora |
“Mar” e “Noar”: False
def strin(s1,s2):
count_n(s1,s2)
if len(s1)==len(s2) or s1[n]==s2[n]:
if s1[0]==s2[0]:
return ""
else:
return s1[0]+s2[0]+strin(s1[1:],s2[1:])
else:
return False
def count_n(s1,s2):
global n
n=0
if len(s1)==len(s2) and (len(s1)>0 or len(s2)>0):
if s1[0]==s2[0]:
return n
else:
return (n+1) and count_n(s1[1:],s2[1:])
else:
return n
老师的回答
def intercala(s1,s2):
if s1=='' and s2 == '':
return ''
if s1=='' or s2 =='':
return False
if s1[0] == s2[0]:
return ''
resp= intercala(s1[1:], s2[1:])
if resp != False:
return s1[0]+s2[0]+resp
else:
return resp
def intercala2(s1,s2):
if s1=='' and s2 == '':
return ''
if s1=='' or s2 =='':
return False
if s1[-1] == s2[-1]:
return intercala2(s1[0:-1],s2[0:-1])
resp= intercala(s1[0:-1], s2[0:-1])
if resp != False:
return resp + s1[-1]+s2[-1]
else:
return resp
答案 0 :(得分:0)
def recur(s1,s2):
if len(s1)==len(s2):
if s1[0]==s2[0]:
return ""
if len(s1)==1 or len(s2)==1:
return s1[0]+s2[0]
else:
return s1[0]+s2[0]+recur(s1[1:],s2[1:])
else:
return False