Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Oneliner | re.compile( ) | re.search( ) solution in Clear category for Acceptable Password II by vinicius.rznd
import re
# https://stackoverflow.com/questions/19859282/check-if-a-string-contains-a-number
# Compiles Regex for better performance
RE_D = re.compile('\d')
def is_acceptable_password(password: str) -> bool:
return True if len(password) > 6 and RE_D.search(password) else False
# Using re.compile() and saving the resulting regular expression object for reuse
# is more efficient when the expression will be used several times in a single program.
# Note: The compiled versions of the most recent patterns passed to re.match(),
# re.search() or re.compile() are cached, so programs that use only a few regular
# expressions at a time needn’t worry about compiling regular expressions.
if __name__ == '__main__':
print("Example:")
print(is_acceptable_password('short'))
# These "asserts" are used for self-checking and not for an auto-testing
assert is_acceptable_password('short') == False
assert is_acceptable_password('muchlonger') == False
assert is_acceptable_password('ashort') == False
assert is_acceptable_password('muchlonger5') == True
assert is_acceptable_password('sh5') == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 11, 2021