Skip to content
Advertisement

Find for the maximum occurrences of a particular pattern appearing consecutively within a substring [Python] [closed]

Suppose I want to find a pattern AAGTC in a string AGTCAAGTCAAGTCAAGTCXYZSDAAGTCAAGTC and return the maximum times AAGTC occurs consecutively (3).

I’m trying to do the following – Find all occurrences using re-find all define a variable m = sum of all occurrences

Then I’m trying to iterate from m till match found for AAGTC{m} using re.search. But it’s not working? How should I go about this?

Advertisement

Answer

There is 5 “AAGTC” in your string but they are not consecutive. You can try to start from m and decrease it till you don’t find match.

>>> "AGTCAAGTCAAGTCAAGTCXYZSDAAGTCAAGTC".count("AAGTC")
5
>>> ("AAGTC" * 3) in "AGTCAAGTCAAGTCAAGTCXYZSDAAGTCAAGTC"
True
>>> ("AAGTC" * 4) in "AGTCAAGTCAAGTCAAGTCXYZSDAAGTCAAGTC"
False
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement