Strong Password

Sort by

recency

|

920 Discussions

|

  • + 0 comments
    public static int minimumNumber(int n, string password)
    {
    // Return the minimum number of characters to make the password strong
        List<char> distinctChars = password.Distinct().ToList<char>();
    
        int charsToBeAdded = 0;
    
        if(!distinctChars.Exists(x => Char.IsAsciiDigit(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterLower(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterUpper(x) == true))
        {
            charsToBeAdded++;
        }
    
        if(!distinctChars.Exists(x => Char.IsAsciiLetterOrDigit(x) == false && Char.IsWhiteSpace(x) == false))
        {
            charsToBeAdded++;
        }
    
        const int minPasswordLength = 6;
    
        if(password.Length > minPasswordLength)
        {
            return charsToBeAdded;
        }
        else
        {
            int difference = minPasswordLength - password.Length;
    
            if(difference > charsToBeAdded)
            {
                return difference;
            }
            else
            {
                return charsToBeAdded;
            }
        }
    
    }
    
  • + 0 comments
    def minimumNumber(n, password):
        # Return the minimum number of characters to make the password strong
        special_characters = "!@#$%^&*()-+"
    
        d = lower = upper = sc = 0
        for ch in password:
            if ch.isdigit() and d == 0:
                d = 1
            elif ch.islower() and lower == 0:
                lower = 1
            elif ch.isupper() and upper == 0:
                upper = 1
            elif ch in special_characters and sc == 0:
                sc = 1
    
        total = d + lower + upper + sc
    
        return max(6-n, 4-total)
    
  • + 0 comments

    c++

    int minimumNumber(int n, string password) {
        // Return the minimum number of characters to make the password strong
        bool UP =0, down =0, digit =0, special =0;
        unsigned int result1 =4, res2 =0; 
        
        for (int i=0;i<n;i++) {
            if (password.at(i) <= 'z' and password.at(i) >= 'a') {
                down = 1;
            }
            if (password.at(i) <= '9' and password.at(i) >= '0') {
                digit = 1;
            }
            if (password.at(i) <= 'Z' and password.at(i) >= 'A') {
                UP = 1;
            }
            if ((password.at(i) <= '+' and
                password.at(i) >= '!')or(password.at(i)=='^')or(password.at(i)=='-')
                or (password.at(i)=='@')){
                special = 1;
            }
        }
        res2 = UP+down+digit+special;
        if (n>=6) {
        result1 -= res2;
        }
        if(n == 5 and res2 ==0) {
            result1 = res2+1;
        }
        if(n == 5 and res2 >=1) {
            result1 = result1 - res2;
        }
        if(n == 5 and res2 == 4) {
            result1 = 6-n;
        }
        if (n==4) {
            result1 = res2+2;
        }
        if (n==4 and res2 >=2) {
            result1 = res2;         
        }
        if (n==4 and res2 >=3) {
            result1 = 6-n;
        }
        if (n<=3) {
            result1 = 6-n;
        }
        return result1;
    }
    
  • + 0 comments
    def minimumNumber(n, password):
        digits, lower_case, upper_case, special_ch = 0, 0 ,0, 0
    
        for sign in password:
            if sign.isnumeric():
                digits = 1
            elif sign.islower():
                lower_case = 1
            elif sign.isupper():
                upper_case = 1
            elif sign in "!@#$%^&*()-+":
                special_ch = 1
        result = [digits, lower_case, upper_case, special_ch]
        missing = 4 - sum(result)
        password_length = len(password)
    
        if password_length >= 6:
            if all(result):
                return 0
            return missing
        else:
            if password_length + missing >= 6:
                return missing
            return 6 - (password_length + missing) + missing
    
  • + 0 comments
        digits, lower_case, upper_case, special_ch = 0, 0 ,0, 0
    
        for sign in password:
            if sign.isnumeric():
                digits = 1
            elif sign.islower():
                lower_case = 1
            elif sign.isupper():
                upper_case = 1
            elif sign in "!@#$%^&*()-+":
                special_ch = 1
        result = [digits, lower_case, upper_case, special_ch]
        missing = 4 - sum(result)
        password_length = len(password)
    
        if password_length >= 6:
            if all(result):
                return 0
            return missing
        else:
            if password_length + missing >= 6:
                return missing
            return 6 - (password_length + missing) + missing