Find the minimum number of characters to add to make password strong
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This is the criteria:
Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+
My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.
import math
import os
import random
import re
import sys
# Complete the minimumNumber function below.
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
flag = 0
if len(password) >= 6:
passRegex = re.compile(r"^(?=.*[d])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[A-Z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[a-z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r'[!@#$%^&*()-+]')
mo = passRegex.search(password)
if mo is None:
flag+=1
return flag
else:
if any(x in "0123456789" for x in password) is False:
flag+=1
if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
flag+=1
if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
flag+=1
if any(x in "!@#$%^&*()-+" for x in password) is False:
flag+=1
if (flag+len(password)<6):
return 6-len(password)
if (flag+len(password)>=6):
return flag
if __name__ == '__main__':
n = int(input())
password = input()
answer = minimumNumber(n, password)
Any advice is appreciated.
python regex
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
This is the criteria:
Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+
My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.
import math
import os
import random
import re
import sys
# Complete the minimumNumber function below.
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
flag = 0
if len(password) >= 6:
passRegex = re.compile(r"^(?=.*[d])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[A-Z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[a-z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r'[!@#$%^&*()-+]')
mo = passRegex.search(password)
if mo is None:
flag+=1
return flag
else:
if any(x in "0123456789" for x in password) is False:
flag+=1
if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
flag+=1
if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
flag+=1
if any(x in "!@#$%^&*()-+" for x in password) is False:
flag+=1
if (flag+len(password)<6):
return 6-len(password)
if (flag+len(password)>=6):
return flag
if __name__ == '__main__':
n = int(input())
password = input()
answer = minimumNumber(n, password)
Any advice is appreciated.
python regex
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is the criteria:
Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+
My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.
import math
import os
import random
import re
import sys
# Complete the minimumNumber function below.
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
flag = 0
if len(password) >= 6:
passRegex = re.compile(r"^(?=.*[d])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[A-Z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[a-z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r'[!@#$%^&*()-+]')
mo = passRegex.search(password)
if mo is None:
flag+=1
return flag
else:
if any(x in "0123456789" for x in password) is False:
flag+=1
if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
flag+=1
if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
flag+=1
if any(x in "!@#$%^&*()-+" for x in password) is False:
flag+=1
if (flag+len(password)<6):
return 6-len(password)
if (flag+len(password)>=6):
return flag
if __name__ == '__main__':
n = int(input())
password = input()
answer = minimumNumber(n, password)
Any advice is appreciated.
python regex
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This is the criteria:
Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+
My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.
import math
import os
import random
import re
import sys
# Complete the minimumNumber function below.
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
flag = 0
if len(password) >= 6:
passRegex = re.compile(r"^(?=.*[d])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[A-Z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[a-z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r'[!@#$%^&*()-+]')
mo = passRegex.search(password)
if mo is None:
flag+=1
return flag
else:
if any(x in "0123456789" for x in password) is False:
flag+=1
if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
flag+=1
if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
flag+=1
if any(x in "!@#$%^&*()-+" for x in password) is False:
flag+=1
if (flag+len(password)<6):
return 6-len(password)
if (flag+len(password)>=6):
return flag
if __name__ == '__main__':
n = int(input())
password = input()
answer = minimumNumber(n, password)
Any advice is appreciated.
python regex
python regex
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago


db18
132
132
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.
It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.
- Length of at least 6
- One lowercase English character
- One uppercase English character
- One digit
- One special character
What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.
def needs_lower(text):
pass
def needs_upper(text):
pass
def needs_digit(text):
pass
def needs_extra_chars(text):
pass
def needs_special(text):
pass
Now, Python has a string
module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase
, string.ascii_uppercase
, and string.digit
. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.
import string
def needs_lower(text):
for char in text:
if char in string.ascii_lowercase:
return 0
return 1
def needs_upper(text):
for char in text:
if char in string.ascii_uppercase:
return 0
return 1
def needs_digit(text):
for char in text:
if char in string.digits:
return True
return False
There's quite a bit of repetition here that we can factor out.
def needs_any(text, values):
for char in text:
if char in values:
return True
return False
Then our previous functions can just call this:
def needs_lower(text):
return needs_any(text, string.ascii_lower)
And similarly for the needs_upper
and needs_special
.
In fact, we can do this with our special characters as well:
SPECIAL_CHARS = '!@#$%^&*()-+'
def needs_special(text):
return needs_any(text, SPECIAL_CHARS)
The last function we need to implement is needs_extra_chars
:
MINIMUM_LENGTH = 6
def needs_extra_chars(text):
if len(text) >= MINIMUM_LENGTH:
return 0
return MINIMUM_LENGTH - len(text)
Now we can stitch these all together within minimumNumber
:
def minimumNumber(n, password):
# I'm not really sure why there's a n passed in as well?
test_functions = [needs_lower, needs_upper,
needs_digit, needs_extra_chars,
contains_special]
extra_required = sum([test_fn(password) for test_fn in test_functions])
return extra_required
The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.
Just to drive home the point about size vs readability, here is a (slightly) code golfed version:
import string
def minimumRequired(password):
if len(password) < 6:
return 6 - len(password)
unique_pw = frozenset(password)
char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
frozenset(string.digits), frozenset('!@#$%^&*()-+'))
required_extra = 0
for char_set in char_sets:
if not char_set & unique_pw:
required_extra += 1
return required_extra
This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.
It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.
- Length of at least 6
- One lowercase English character
- One uppercase English character
- One digit
- One special character
What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.
def needs_lower(text):
pass
def needs_upper(text):
pass
def needs_digit(text):
pass
def needs_extra_chars(text):
pass
def needs_special(text):
pass
Now, Python has a string
module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase
, string.ascii_uppercase
, and string.digit
. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.
import string
def needs_lower(text):
for char in text:
if char in string.ascii_lowercase:
return 0
return 1
def needs_upper(text):
for char in text:
if char in string.ascii_uppercase:
return 0
return 1
def needs_digit(text):
for char in text:
if char in string.digits:
return True
return False
There's quite a bit of repetition here that we can factor out.
def needs_any(text, values):
for char in text:
if char in values:
return True
return False
Then our previous functions can just call this:
def needs_lower(text):
return needs_any(text, string.ascii_lower)
And similarly for the needs_upper
and needs_special
.
In fact, we can do this with our special characters as well:
SPECIAL_CHARS = '!@#$%^&*()-+'
def needs_special(text):
return needs_any(text, SPECIAL_CHARS)
The last function we need to implement is needs_extra_chars
:
MINIMUM_LENGTH = 6
def needs_extra_chars(text):
if len(text) >= MINIMUM_LENGTH:
return 0
return MINIMUM_LENGTH - len(text)
Now we can stitch these all together within minimumNumber
:
def minimumNumber(n, password):
# I'm not really sure why there's a n passed in as well?
test_functions = [needs_lower, needs_upper,
needs_digit, needs_extra_chars,
contains_special]
extra_required = sum([test_fn(password) for test_fn in test_functions])
return extra_required
The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.
Just to drive home the point about size vs readability, here is a (slightly) code golfed version:
import string
def minimumRequired(password):
if len(password) < 6:
return 6 - len(password)
unique_pw = frozenset(password)
char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
frozenset(string.digits), frozenset('!@#$%^&*()-+'))
required_extra = 0
for char_set in char_sets:
if not char_set & unique_pw:
required_extra += 1
return required_extra
This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).
add a comment |Â
up vote
2
down vote
accepted
Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.
It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.
- Length of at least 6
- One lowercase English character
- One uppercase English character
- One digit
- One special character
What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.
def needs_lower(text):
pass
def needs_upper(text):
pass
def needs_digit(text):
pass
def needs_extra_chars(text):
pass
def needs_special(text):
pass
Now, Python has a string
module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase
, string.ascii_uppercase
, and string.digit
. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.
import string
def needs_lower(text):
for char in text:
if char in string.ascii_lowercase:
return 0
return 1
def needs_upper(text):
for char in text:
if char in string.ascii_uppercase:
return 0
return 1
def needs_digit(text):
for char in text:
if char in string.digits:
return True
return False
There's quite a bit of repetition here that we can factor out.
def needs_any(text, values):
for char in text:
if char in values:
return True
return False
Then our previous functions can just call this:
def needs_lower(text):
return needs_any(text, string.ascii_lower)
And similarly for the needs_upper
and needs_special
.
In fact, we can do this with our special characters as well:
SPECIAL_CHARS = '!@#$%^&*()-+'
def needs_special(text):
return needs_any(text, SPECIAL_CHARS)
The last function we need to implement is needs_extra_chars
:
MINIMUM_LENGTH = 6
def needs_extra_chars(text):
if len(text) >= MINIMUM_LENGTH:
return 0
return MINIMUM_LENGTH - len(text)
Now we can stitch these all together within minimumNumber
:
def minimumNumber(n, password):
# I'm not really sure why there's a n passed in as well?
test_functions = [needs_lower, needs_upper,
needs_digit, needs_extra_chars,
contains_special]
extra_required = sum([test_fn(password) for test_fn in test_functions])
return extra_required
The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.
Just to drive home the point about size vs readability, here is a (slightly) code golfed version:
import string
def minimumRequired(password):
if len(password) < 6:
return 6 - len(password)
unique_pw = frozenset(password)
char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
frozenset(string.digits), frozenset('!@#$%^&*()-+'))
required_extra = 0
for char_set in char_sets:
if not char_set & unique_pw:
required_extra += 1
return required_extra
This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.
It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.
- Length of at least 6
- One lowercase English character
- One uppercase English character
- One digit
- One special character
What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.
def needs_lower(text):
pass
def needs_upper(text):
pass
def needs_digit(text):
pass
def needs_extra_chars(text):
pass
def needs_special(text):
pass
Now, Python has a string
module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase
, string.ascii_uppercase
, and string.digit
. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.
import string
def needs_lower(text):
for char in text:
if char in string.ascii_lowercase:
return 0
return 1
def needs_upper(text):
for char in text:
if char in string.ascii_uppercase:
return 0
return 1
def needs_digit(text):
for char in text:
if char in string.digits:
return True
return False
There's quite a bit of repetition here that we can factor out.
def needs_any(text, values):
for char in text:
if char in values:
return True
return False
Then our previous functions can just call this:
def needs_lower(text):
return needs_any(text, string.ascii_lower)
And similarly for the needs_upper
and needs_special
.
In fact, we can do this with our special characters as well:
SPECIAL_CHARS = '!@#$%^&*()-+'
def needs_special(text):
return needs_any(text, SPECIAL_CHARS)
The last function we need to implement is needs_extra_chars
:
MINIMUM_LENGTH = 6
def needs_extra_chars(text):
if len(text) >= MINIMUM_LENGTH:
return 0
return MINIMUM_LENGTH - len(text)
Now we can stitch these all together within minimumNumber
:
def minimumNumber(n, password):
# I'm not really sure why there's a n passed in as well?
test_functions = [needs_lower, needs_upper,
needs_digit, needs_extra_chars,
contains_special]
extra_required = sum([test_fn(password) for test_fn in test_functions])
return extra_required
The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.
Just to drive home the point about size vs readability, here is a (slightly) code golfed version:
import string
def minimumRequired(password):
if len(password) < 6:
return 6 - len(password)
unique_pw = frozenset(password)
char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
frozenset(string.digits), frozenset('!@#$%^&*()-+'))
required_extra = 0
for char_set in char_sets:
if not char_set & unique_pw:
required_extra += 1
return required_extra
This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).
Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.
It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.
- Length of at least 6
- One lowercase English character
- One uppercase English character
- One digit
- One special character
What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.
def needs_lower(text):
pass
def needs_upper(text):
pass
def needs_digit(text):
pass
def needs_extra_chars(text):
pass
def needs_special(text):
pass
Now, Python has a string
module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase
, string.ascii_uppercase
, and string.digit
. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.
import string
def needs_lower(text):
for char in text:
if char in string.ascii_lowercase:
return 0
return 1
def needs_upper(text):
for char in text:
if char in string.ascii_uppercase:
return 0
return 1
def needs_digit(text):
for char in text:
if char in string.digits:
return True
return False
There's quite a bit of repetition here that we can factor out.
def needs_any(text, values):
for char in text:
if char in values:
return True
return False
Then our previous functions can just call this:
def needs_lower(text):
return needs_any(text, string.ascii_lower)
And similarly for the needs_upper
and needs_special
.
In fact, we can do this with our special characters as well:
SPECIAL_CHARS = '!@#$%^&*()-+'
def needs_special(text):
return needs_any(text, SPECIAL_CHARS)
The last function we need to implement is needs_extra_chars
:
MINIMUM_LENGTH = 6
def needs_extra_chars(text):
if len(text) >= MINIMUM_LENGTH:
return 0
return MINIMUM_LENGTH - len(text)
Now we can stitch these all together within minimumNumber
:
def minimumNumber(n, password):
# I'm not really sure why there's a n passed in as well?
test_functions = [needs_lower, needs_upper,
needs_digit, needs_extra_chars,
contains_special]
extra_required = sum([test_fn(password) for test_fn in test_functions])
return extra_required
The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.
Just to drive home the point about size vs readability, here is a (slightly) code golfed version:
import string
def minimumRequired(password):
if len(password) < 6:
return 6 - len(password)
unique_pw = frozenset(password)
char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
frozenset(string.digits), frozenset('!@#$%^&*()-+'))
required_extra = 0
for char_set in char_sets:
if not char_set & unique_pw:
required_extra += 1
return required_extra
This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).
answered 2 hours ago


Yuushi
9,79422259
9,79422259
add a comment |Â
add a comment |Â
db18 is a new contributor. Be nice, and check out our Code of Conduct.
db18 is a new contributor. Be nice, and check out our Code of Conduct.
db18 is a new contributor. Be nice, and check out our Code of Conduct.
db18 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f203839%2ffind-the-minimum-number-of-characters-to-add-to-make-password-strong%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password