Help me to fix the problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Help me to fix the problem

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string My code is: a = ["flower","flight"] if len(a) == 0: print(" ") result = " " lens = [len(str) for str in a] mim = min(lens) for i in range(1,mim+1): pre = a[0][:i] for q in a: if q[:i] == pre: result = result+pre print(result)

18th Aug 2021, 11:28 AM
Shahir
Shahir - avatar
4 Answers
+ 4
Some little logical bugs....still not valid for more than 2 strings a = ["flower","flight"] if len(a) == 0: print(" ") result = " " lens = [len(str) for str in a] mim = min(lens) for i in range(1,mim+1): pre = a[0][:i] for q in a[1:]: if q[:i] == pre: result = pre print(result)
18th Aug 2021, 11:45 AM
Oma Falk
Oma Falk - avatar
+ 4
✩✮★✮✩ yes....and some more problems. But my idea was step by step. Shahir has problems with logic. Better we don't improve all in one
18th Aug 2021, 12:25 PM
Oma Falk
Oma Falk - avatar
+ 1
Why did you use the a[1:] why not the all the elements in the list??
18th Aug 2021, 12:00 PM
Shahir
Shahir - avatar
+ 1
pre=a[0][:i] First element is used
18th Aug 2021, 12:02 PM
Oma Falk
Oma Falk - avatar