How to get number of specific words from a string

Question:

Please I need to write a program in python3 that return the number of word in a string that has letter that repeat only n time successive.

  • Expl if n=2 "first loop ddd" the code must return 1 [Loop contains 2 o] [d is repeated 3 times in ddd so it wan’t be counted].

I wrote a long code but i did not get a result.

    words=st.split(" ")
    for word in words:   
            for i in range(1,len(word)-nb+1):          
               k=word[i:i+nb]       
                    if(  k==word[i]*nb and kelma[0]!=word[i-1] and k[-1]!=word[i+nb]   ):                
                        nbr=nbr+1
                        print(word)
                        break
          
    return nbr
Asked By: carlaa

||

Answers:

You could create a words list by splitting the sentence by whitespace, and then searching each word (after removing any punctuation etc..) for occurrences of repeated letters. I’ve kept a set of words found, so that the same word isn’t counted multiple times if it has repeats of more than one letter, but if you did want to count these you could just use a counter instead of the set.

def find_repeats(search_string, r):
    found = set()
    words = search_string.lower().split()
    for word in words:
        letters = ''.join(filter(lambda d: d.isalpha, word)) # remove non-alphabetical chars
        for l in set(letters):
            if l*r in letters and l*(r+1) not in letters:
                found.add(letters)
    return len(found)

print(find_repeats('first loop ddd',2))
# output 1

Edit:
To handle the case where multiple occurrences of the same letter happen, using a greedy regular expression is useful. In this case you match occurrences of each letter repeated as many times as possible (hence the term greedy), and then check the length of the repeats. re also allows for an alternative construction of the word list to the earlier example.

import re
def find_repeats(search_string, r):
    found = set()
    for word in re.findall('[a-z]+',search_string.lower()): #words list (or 'w+' possible)
        for letter in set(word):
            for repeats in re.findall(letter + '+', word): # letter+ is one or more of letter
                if len(repeats) == r:
                    found.add(word)
    return len(found)
print(find_repeats('first, loop; ddd caaabaaaa',3))
# output 2
Answered By: bn_ln
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.