Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I return False if a character in a string appears three times in a row

I have asked this question before, and although the response from the kind StackOverflow users were right, it kinda fails to answer my question, or it’s kinda complicated for a beginner like me, so I’m gonna ask again :).

So here is a code which checks whether a string fits certain criterias or not….

def passwordlength(password: str):
    upper = digit = special = False
    for char in password:
        if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" :
            upper = True
        elif char in "1234567890":
            digit = True
        elif char in "$#@%!":
            special = True
    return upper and digit and special

def password(password: str):
    if passwordlength(password):
        if not (6 < len(password) < 12):
            return False
    else:     
        return True

So this part works fine. no problem with my initial code. However, I’m trying to edit my code so that if a number, character or letter appears 3 times in a row.. for example, AAA or ccc or 111 or ###, the output would be false

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my attempt at it, which didn’t work at all…

def passwordlength(password: str):
    upper = digit = special = False
    for char in password:
        if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" :
            upper = True
        elif char in "1234567890":
            digit = True
        elif char in "$#@%!":
            special = True
    return upper and digit and special

def password(password: str):
    if passwordlength(password):
        if not (6 < len(password) < 12):
            return False
        for char in password:
            if password.count(char) > 3:
                return False
        return True
    return False

What should I change in my code to make sure this works??

Please use the simplest form of coding possible since I’m a beginner and I would like to learn along as I do programming :). Also pls dont use import or collections.iter because I haven’t learnt that yet… I’m learning python online and I haven’t learnt that far yet.. Thank you 🙂

>Solution :

A trivial implementation would be:

def check_repeating_chars(password):
    for i in range(2, len(password)):
        if password[i] == password[i-1] == password[i-2]:
            return False
    return True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading