1

Here's my code, I'd like to get the number of combinations based on this pattern : x - y - x , example : "UBU" , "ANA", "INI"

    import re 

    pattern = r"(?P<name>[a-z]).(?P=name)"

    print(len(re.findall(pattern, "hello"))) # should return 0 : OK

    print(len(re.findall(pattern, "mirror"))) # should return 1 because there is "ror" : OK 

    print(len(re.findall(pattern, "irir"))) #should return 2 because there are "iri" and "rir" : return just 1

It's not working when part of the combinations is imbricated into another one like the first two letters of "rir" in "iri".

Would you know how to get the correct number of those combinations (for the third example) ? Thank you very much in advance

ncica
  • 7,015
  • 1
  • 15
  • 37
GG24
  • 183
  • 3
  • 17

1 Answers1

0
def pattern_counter(word):
    return sum([1 for index, letter in enumerate(word[:(len(word)-2)]) if letter == word[index+2]])

word1 = "hello"
word2 = "mirror"
word3 = "irir"

print(pattern_counter(word1)) # prints 0
print(pattern_counter(word2)) # prints 1
print(pattern_counter(word3)) # prints 2
Tyson
  • 592
  • 4
  • 13