0

I have a list and a function that is built for the list. how to make the function run? How to properly understand the meaning of self in this function?

strs = ["flower","flow","flight"]

def longestCommonPrefix(self, strs):
    longest_pre = ""
    if not strs: return longest_pre
    shortest_str = min(strs, key=len)
    for i in range(len(shortest_str)):
        if all([x.startswith(shortest_str[:i+1]) for x in strs]):
            longest_pre = shortest_str[:i+1]
        else:
            break
    return longest_pre

if I run the code as follow:

longestCommonPrefix(strs)

it shows:

TypeError: longestCommonPrefix() missing 1 required positional argument: 'strs'
Yumeng Xu
  • 135
  • 1
  • 10
  • 2
    Is this function part of a class? If not, then you don't need the `self` 1st parameter. It is only used to define instance methods of a class. – Gino Mempin Jan 08 '22 at 01:59

0 Answers0