0

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?

3 Answers3

2

One option is using re.findall with max:

import re
max(re.findall('[a-zA-Z]+',  'ab24[AaBbCDExy0longest]'), key=len)
# 'AaBbCDExy'

max(re.findall('[a-zA-Z]+', 'a a a1234b|c|d '), key=len)
# 'a'

Small hack to contemplate cases where there are no matches:

max(re.findall('[a-zA-Z]+', '12345 ') or [''], key=len)
# ''

Though I'd suggest you to go with the more readable approach:

r = re.findall('[a-zA-Z]+', '12345 ') 
if r:
    out = max(r, key=len)
else:
    out = ''

Or as @deepstop suggests with the conditional expression:

out = max(r, key=len) if r else ''
yatu
  • 86,083
  • 12
  • 84
  • 139
0

like yatu, my first though on such a problem would be regex. However I have offered a solution based on your approach. the problem in your code is that you only increment i when the character is an alpha. So for the string abc123 you will increment i 3 times. but as the next char is not alpha you dont incerase i. which means i is now stuck with a value of 3 and thats less than the length of the string 6. so your function is stuck in an infinite loop since you stop increasing i.

A simplified version of your code can be written as below. essentially there is no need for a second while loop. Infact there is no need for a while loop at all, you can just use a for loop to iterate over each character in the string

def longestSubstring(string):
    longest_letterSeq = ''
    curr_letterSeq = ''
    for char in string:
        if char.isalpha():
            curr_letterSeq += char
        else:
            if len(curr_letterSeq) > len(longest_letterSeq):
                longest_letterSeq = curr_letterSeq
            curr_letterSeq = ''
    return longest_letterSeq

my_strings = ['ab24[AaBbCDExy0longest]', 'a a a1234b|c|d ', '12345']
for string in my_strings:
    longest = longestSubstring(string)
    print(f'the longest string in "{string}" is "{longest}"')

OUTPUT

the longest string in "ab24[AaBbCDExy0longest]" is "AaBbCDExy"
the longest string in "a a a1234b|c|d " is "a"
the longest string in "12345" is ""
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
0

An alternative approach is to replace all non-letters with a space, split, and then choose the longest string.

import re

def func(s) :
    l = re.sub('[^a-zA-Z]+', ' ', s).split()
    l.append('')  # Append an empty string so the list is bound not to be empty.
    return max(l, key=len)

func('ab24[AaBbCDExy0longest]')
func('foo2bar')
func('')