Find the best solutions to your questions at Westonci.ca, the premier Q&A platform with a community of knowledgeable experts. Our platform connects you with professionals ready to provide precise answers to all your questions in various areas of expertise. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

Can someone help me out with a code. Code's in python write a function that returns the longest common prefix of two strings. For example, the longest
common prefix of distance and disinfection is dis. The header of the method is:
def prefix(s1, s2)
If the two strings have no common prefix, the method returns an empty string.
Write a main function that prompts the user to enter two strings and displays their common
prefix.
here's what I have currently but its not working.
def longestCommonPrefix(self, arr):

arr.sort(reverse = False)
print(arr)
n= len(arr)
str1 = arr[0]
str2 = arr[n-1]

n1 = len(str1)
n2 = len(str2)
result = ""
j = 0
i = 0

while(i <= n1 - 1 and j <= n2 - 1):
if (str1[i] != str2[j]):
break
result += (str1[i])

i += 1
j += 1

return (result)


Sagot :

Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Thank you for choosing our platform. We're dedicated to providing the best answers for all your questions. Visit us again. Thank you for choosing Westonci.ca as your information source. We look forward to your next visit.