程序填空题:返回两个字符串从头开始的最长公共前缀
下面程序返回两个字符串从头开始的最长公共前缀。如distance和disinfection的最长公共前缀为dis。
def main():
s1 = input().strip()
s2 = input().strip()
s3 = prefix(s1, s2)
if s3 None:
print("The common prefix is " + s3)
else:
print("No common prefix")
def prefix(s1, s2):
result = ""
for i in range(len(s1)):
if s1[i] == s2[i]:
result s1[i]
else:
break
if len(result) == 0:
return None
else:
return
main()
答案:
第1空:!=
第2空:+=
第3空:result
def main():
s1 = input().strip()
s2 = input().strip()
s3 = prefix(s1, s2)
if s3 None:
print("The common prefix is " + s3)
else:
print("No common prefix")
def prefix(s1, s2):
result = ""
for i in range(len(s1)):
if s1[i] == s2[i]:
result s1[i]
else:
break
if len(result) == 0:
return None
else:
return
main()
答案:
第1空:!=
第2空:+=
第3空:result