Write a block of code to print the longest subsequence of letters out of an input string. - First, ask user to input any string. - You code should print the longest sub-string of the input string, which contains only letters of the English alphabet, including both uppercase and lowercase letters.
If there are multiple subsequences of the same longest length, the code returns the first one.
If input_str doesn’t contain any letter, the function returns an empty string.
For example,
for input string 'ab24[AaBbCDExy0longest]', it should print 'AaBbCDExy'.
for input string 'a a a1234b|c|d ', it should print 'a'.
for input string '12345 ', it should print "" (empty string).
Tried the following code but in vain:
# Your code here
#longest_letterSeq = ''
def longestSubstring(s):
longest_letterSeq = ''
i = 0
while(i<len(s)):
curr_letterSeq = ''
# For letter substring
while(i<len(s) and s[i].isalpha()):
curr_letterSeq += s[i]
i+= 1
# Case handling if the character is not letter
if(i< len(s) and not(s[i].isalpha())) :
i+= 1
if(len(curr_letterSeq) > len(longest_letterSeq) ):
longest_letterSeq = curr_letterSeq
return longest_letterSeq
str = input("Please input your string here: ")
print(longestSubstring(str))
Can someone help with the edited or correct code?