DNPotapov / Codewars-katas-

0 stars 0 forks source link

Password Maker (6 kyu) #26

Open DNPotapov opened 1 year ago

DNPotapov commented 1 year ago
import random
def randletter(x, y):
    return chr(random.randint(ord(x), ord(y)))
def make_password(length, flag_upper, flag_lower, flag_digit):
    res = ""
    count = 0
    while length > 0:
        if flag_upper and length > 0:
            ran = randletter('A', 'Z')
            while (ran in res):
                ran = randletter('A', 'Z')
            res += ran
            length = length - 1
        if flag_lower and length > 0:
            ran = randletter('a', 'z')
            while (ran in res):
                ran = randletter('a', 'z')
            res += ran
            length = length - 1
        if flag_digit and length > 0 and count < 10:
            count += 1
            ran = str(random.randint(0, 9))
            while (ran in res):
                ran = str(random.randint(0, 9))
            res += ran
            length = length - 1
    return res
DNPotapov commented 1 year ago

Background Different sites have different password requirements.

You have grown tired of having to think up new passwords to meet all the different rules, so you write a small piece of code to do all the thinking for you.

Kata Task Return any valid password which matches the requirements.

Input:

length = password must be this length flagUpper = must (or must not) contain UPPERCASE alpha flagLower = must (or must not) contain lowercase alpha flagDigit = must (or must not) contain digit Notes Only alpha-numeric characters are permitted The same character cannot occur more than once in the password! All test cases guarantee that a valid password is possible

DNPotapov commented 1 year ago

https://www.codewars.com/kata/5b3d5ad43da310743c000056