A
A
Anton2021-09-30 01:42:55
Python
Anton, 2021-09-30 01:42:55

How to write a condition for counting identical adjacent characters in a string?

Greetings.
Tell me how to write a condition for counting identical adjacent characters in a string, for example, if there are more than 3 repeated letters or numbers in a string, then the condition is met. Upper or lower case is not important.
Examples:
Paaassword - the condition will be met
Paaaassword - the condition will be met
P111ssword - the condition will be met
Password - the condition will not be met

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
low molecular macro, 2021-09-30
@boypush

passw = 'PasssWword'

def ifTriple(word):
    for i in range(1, len(word)-1):
        if(word[i].lower() == word[i-1].lower() and word[i].lower() == word[i+1].lower() ):
            return True
    return False
        
print(ifTriple(passw))

A
aRegius, 2021-09-30
@aRegius

group by

any(len(list(group)) > num for _, group in groupby(word.lower()))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question