Strong Password

Sort by

recency

|

97 Discussions

|

  • + 0 comments

    Python

    special_characters = "!@#\$%\^&*()\-+"
    patterns = [r'\d', r'[a-z]', r'[A-Z]', rf'[{special_characters}]']
    
    def minimumNumber(n, password):
        res = len([1 for pattern in patterns if not re.search(pattern, password)])
        res += max(0, 6 - (len(password) + res))
        return res
    
  • + 0 comments

    Python 3:

    numbers = {*"0123456789"}
    lower_case = {*"abcdefghijklmnopqrstuvwxyz"}
    upper_case = {*"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
    special_characters = {*"!@#$%^&*()-+"}
    
    
    def minimumNumber(n: int, password: str) -> int:
        # Return the minimum number of characters to make the password strong
        if n < 3:
            return 6 - n
        characters = {*password}
        return max(
            sum(
                not (characters & char_class)
                for char_class in (numbers, lower_case, upper_case, special_characters)
            ),
            6 - n,
        )
    
  • + 0 comments

    My rust solution:

    const SPECIAL_CHARS: &'static str = "!@#$%^&*()-+";
    fn minimumNumber(n: i32, password: &str) -> i32 {
        // Return the minimum number of characters to make the password strong
        let missing_char = if n < 6 { 6 - n } else { 0 };
        let mut criteria = [0; 4];
        
        for c in password.chars() {
            if SPECIAL_CHARS.contains(c) {
                criteria[0] += 1;
            } else if c.is_digit(10) {
                criteria[1] += 1;
            } else if c.is_lowercase() {
                criteria[2] += 1;
            } else if c.is_uppercase() { 
                criteria[3] += 1; 
            }
        }
        
        let missing_criteria = criteria.iter().map(|&x| if x > 0 { 0 } else { 1 }).sum::<i32>();
        if missing_char >= missing_criteria { missing_char } else { missing_criteria }     
    }
    
  • + 0 comments

    import string

    def minimumNumber(n, password):

    d = {'L': 0, 'U': 0, 'N': 0, 'S': 0}
    l, u, t, s = 1, 1, 1, 1
    for i in password:
        if i in string.ascii_lowercase:
            d['L'] = l
            l += 1
        if i in string.ascii_uppercase:
            d['U'] = u
            u += 1
        if i in string.digits:
            d['N'] = t
            t += 1
        if i in string.punctuation:
            d['S'] = s
            s += 1
    chars = len([v for k, v in d.items() if v == 0])
    if (n + chars) < 6:
        return 6 - n
    else:
        return chars
    
  • + 0 comments

    c++

    int minimumNumber(int n, string password) {
        // Return the minimum number of characters to make the password strong
        int d = 0, l = 0, u = 0, s = 0;
        
        for (const char& c : password)
        {
            if (c >= '0' && c <= '9')
                ++d;
            else if (c >= 'a' && c <= 'z')
                ++l;
            else if (c >= 'A' && c <= 'Z')
                ++u;
            else
                ++s;
        }
        
        int r = 0;
        
        if (d == 0)
            ++r;
            
        if (l == 0)
            ++r;
            
        if (u == 0)
            ++r;
            
        if (s == 0)
            ++r;
            
        return max(6 - n, r);
    }