18.2k views
2 votes
arglongest (L) Write the function argLongest that, given a nonempty list of strings, finds the index of the longest even-length string that ends with ing. It takes a list of strings L as input and returns an integer, the index of the longest string in L with a suffix ing of even length. If there are many longest strings, return the index of the first one. If there are no such strings, return the index -1. Finding the index is more valuable than finding the element: you can quickly retrieve the element using the index, but you cannot quickly retrieve the index using the element. So we want to get in the habit of computing indices of elements, not elements.

User Ishrat
by
4.0k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

def argLongest(L):

index = -1

for i in range(len(L)):

s = L[i]

if len(s) % 2 == 0 and s.endswith('ing'):

if index == -1 or len(s) > len(L[index]):

index = i

return index

print(argLongest(['living', 'checking', 'hello how are you?']))

print(argLongest(['living', 'hello how are you?', 'flying']))

print(argLongest(['hello how are you?']))

The output can be seen in the attached image below

1 Process finished with exit code 0

arglongest (L) Write the function argLongest that, given a nonempty list of strings-example-1
User Salmonstrikes
by
4.9k points