How can I get the longest common prefix? Python 3 | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

How can I get the longest common prefix? Python 3

I want to fin the longest common prefix in a list with n elements, I'm not sure about how to do that, I'm new in python 3, how can I find the longest prefix?

25th Apr 2018, 12:17 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
6 Réponses
25th Apr 2018, 5:04 AM
Emma
+ 1
Xan that's right, I'm trying so hard to do it, but I have almost nothing knowledge about lists...
25th Apr 2018, 1:46 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Xan, it will return both in that example, the problem for me is I don't know how to do it :(
25th Apr 2018, 2:28 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 1
Xan thx! I understand now, it works pretty good
25th Apr 2018, 4:48 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
Can you expand on this, and give some examples of what you mean? Do you mean that ["hello", "goat", "help"] would return "hel" as being the longest prefix?
25th Apr 2018, 1:08 AM
Emma
0
The problem would be to decide what to do when there are more than one longest common prefixes amongst multiple words in the list. Can you expand on this, and give some examples of what you mean? Do you mean that ["hello", "goat", "help", "goal"] could return "hel" or "goa". There is no common longest prefix in that case. Maybe return both in that example?
25th Apr 2018, 1:47 AM
Emma