Ask Question
10 August, 08:30

Write the function longest that, given a nonempty list of strings, finds the longest even-length string that ends with ing. It takes a list of strings L as input and returns a string. If there are many longest strings, return the first. If there are no such strings, return the empty string (which clearly indicates that no string was found, since a valid string would need to be at least length 3).

+4
Answers (1)
  1. 10 August, 09:13
    0
    def longest (L) : for x in L: if (len (x) % 2 = = 0) : if (x. endswith ("ing")) : return x return "" print (longest (["Playing", "Gaming", "Studying"]))

    Explanation:

    The solution is written in Python 3.

    Firstly, create a function longest that takes one parameter L as required by question (Line 1).

    In the function, create a for loop to traverse through each string in L and check if the current string length is even (Line 2 - 3). If so, use string endwiths method to check is the current string ended with "ing". If so return the string (Line 4-5).

    At last, test the function by passing a list of string and we shall get the output "Gaming".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write the function longest that, given a nonempty list of strings, finds the longest even-length string that ends with ing. It takes a list ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers