Your users passwords were all stole in the Yahoo! hack, and it turns out they have been lax in creating secure passwords.  Create a function that checks their new password (passed as a string) to make sure it meets the following requirements:


Between 8 - 20 characters

Contains only the following characters: (and at least one character from each category): uppercase letters, lowercase letters, digits, and the special characters !@#$%^&*?




Return "valid" if passed or else "not valid"

Difficulty: introductory

Starter code:

def check_password(s):
	

Solutions:
import re;
def check_password(s):
    if re.search('^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[!@#$%^&*?])[a-zA-Z\d!@#$%^&*?]{8,20}$', s) :
        return 'valid'
    else:
        return 'not valid'

import re

def check_password(s):
    m = re.match(r'(?=.{8,20}$)(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*?])(?=^[a-zA-Z\d!@#$%^&*?]*$)', s)
    return 'valid' if m else 'not valid'

import re

PATTERN = re.compile(
'^'                   # begin string
'(?=.*?[A-Z])'        # at least one uppercase letter
'(?=.*?[a-z])'        # at least one lowercase letter
'(?=.*?\d)'           # at least one digit
'(?=.*?[!@#$%^&*?])'  # at least one special character
'[A-Za-z\d!@#$%^&*?]' # only the given characters
'{8,20}'              # between 8 and 20 characters long
'$'                   # end string
)

def check_password(s):
    return "valid" if PATTERN.match(s) else "not valid"

from re import sub

def check_password(string):
    reduced = sub("[a-z]", "a", sub("[A-Z]", "A", sub("[0-9]", "0", sub("[!@#$%^&*?]", "!", string))))
    return "valid" if 8 <= len(string) <= 20 and set(reduced) == set("aA0!") else "not valid"





## without re
#from string import ascii_lowercase as LOW, ascii_uppercase as UPP, digits as DIG

## without import
#LOW = "abcdefghijklmnopqrstuvwxyz"
#UPP = LOW.upper()
#DIG = "0123456789"

#SPE = "!@#$%^&*?"
#ALLOWED = (LOW, UPP, DIG, SPE)

#def check_password(string):
#    reduced = string.translate(str.maketrans("".join(ALLOWED), "".join(s[0]*len(s) for s in ALLOWED)))
#    return "valid" if 8 <= len(string) <= 20 and set(reduced) == set(s[0] for s in ALLOWED) else "not valid"


def check_password(s):
    c1 = 8 <= len(s) <=20
    c2 = any([i.isupper() for i in s])
    c3 = any([i.islower() for i in s])
    c4 = any([i.isdigit() for i in s])
    c5 = any([i for i in s if i in '!@#$%^&*?'])
    c6 = not any([i for i in s if i not in '!@#$%^&*?' and not i.isupper() and not i.islower() and not i.isdigit()])
    return 'valid' if c1 and c2 and c3 and c4 and c5 and c6 else 'not valid'

from re import search

def check_password(s):
    return 'valid' if bool(search('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*?])[\w!@#$%^&*?]{8,20}$', s)) else 'not valid'

import re
def check_password(s):
    if len(s) > 20 or len(s) < 8: return "not valid"
    a1 = bool(re.search(r'[A-Z]', s))
    a2 = bool(re.search(r'[a-z]', s))
    a3 = bool(re.search(r'\d', s))
    a4 = bool(re.search(r'[!@#$%^&*?]', s))
    a5 = bool(re.search(r'^[A-Za-z!@#$%^&*?\d]*$', s))
    return "valid" if a1*a2*a3*a4*a5 else "not valid"

import re

def check_password(s):
    return 'valid' if re.search(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*?])[A-Za-z0-9!@#$%^&*?]{8,20}$', s) else 'not valid'

from string import digits as D, ascii_lowercase as L, ascii_uppercase as U
S = "!@#$%^&*?"


def check_password(s):
    return "valid" if 8 <= len(s) <= 20 \
                and all(c in D+L+U+S for c in s) \
                and any(c in D for c in s) \
                and any(c in L for c in s) \
                and any(c in U for c in s) \
                and any(c in S for c in s) \
                else "not valid"

def check_password(s):
    import re
    chars = "!@#$%^&*?1234567890qazwsxedcrfvtgbyhnujmiklopQAZWSXEDCRFVTGBYHNUJMIKLOP"
    sp = "[!@#$%^&*?]"
    a="[a-z]"
    if len(s) in range(8,21):
        for c in s:
            if c not in chars:
                return "not valid"            
    else:
        return "not valid"
    if all((re.search(sp,s), re.search("\d",s), re.search(a,s), re.search(a.upper(),s))):
          return "valid" 
    else:
        return "not valid"