Change language

Python | Find the longest sequence of letters and numbers

| |

Examples :

  Input:  geeks123available  Output:  (’available’, 123)  Input:  98apple658pine  Output:  (’apple’, 658) 

Approach # 1: brute force
This is naive or crude method to find the longest sequence of letters and numbers. We take two string variables of type longest_letter and longest_digit . We start the loop and check the consecutive substrings of letters and numbers. At each iteration, we check if the current alphabetic substring is greater than the longest alphabetic or numeric substring, respectively. If so, we assign the current letter and number substring to the longest letter and digit substring, respectively. Otherwise, do nothing.

# Python3 program to find the longest
# sequential substring of a specific type

import re

 

def longestSubstring (s):

 

longest_letterSeq = ’’

longest_digitSeq = ’’

i = 0

< p> while (i " len (s)):

 

curr_letterSeq = ’’

curr_digitSeq = ’’

 

# For literal substring

while (i " len (s) and s [i] .isalpha ()):

  curr_letterS eq + = s [i]

i + = 1

 

# For numeric substring

while (i " len (s) and s [i] .isdigit ()):

curr_digitSeq + = s [i]

  i + = 1

  

  Handle case if character

# is neither a letter nor digit

if (i & lt;  len (s) and not (s [i] .isdigit ()) 

  and not (s [i] .isalpha ())):

i + = 1

 

if ( len (curr_letterSeq)" len (longest_letterSeq)):

longest_letterSeq = curr_letterSeq

  

if ( len (curr_digitSeq)"  len (longest_digitSeq)):

longest_digitSeq = curr_digitSeq

 

return longest_letterSeq, longest_digitSeq

 
Driver code

str = ’3Geeksfor123geeks3’

print (longestSubstring ( str ))

Exit:

 (’Geeksfor’,’ 123’) 

Approach # 2: Using Python regular expressions
Python Regex is another method to solve this problem. Find a sequence of substrings of numbers and letters using a Python regex, and then find the longest substring length, respectively.

# Python3 program to find the longest
# sequential substring of a certain type

import re

 

def longestSubstring ( str ):

  letter = max (re.findall (r ’ D + ’ , str ), key = len )

digit = max (re.findall (r ’d +’ , str ), key = len )

 

return letter, digit

 
Driver code

str = ’geeks123pythonengineering1’

print (longestSubstring ( str ))

Output:

 (’pythonengineering’,’ 123’) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically