I am writing a function where I can take a string as input, and then find the even number of substrings. The final result is the first longest even length string is returned by the function.
For example:
Input -> It is a pleasant day today
Output -> pleasant
For example:
Input -> It is a day today
Output-> It
(Since there are two sub-strings of the same even length, returning the first longest even length string is enough.)
If there are no such even length strings then, the function should return 00.
import sys
def longestEvenWord(sentence):
list1 = sentence.split()
for x in list1[:]:
# just removing all the odd length words from
# the list. Then the list will contain only
# even words
if (len(x) % 2 != 0):
list1.remove(x)
#after removing the odd length numbers, we will have only even and find the
#longest word and return that.
return max(list1, key=len)
if __name__ == '__main__':
result = longestEvenWord(" qqqqqqqqq It is a day today qqqqqqqqq")
print(result)