Created
December 4, 2019 14:53
-
-
Save killjoy1221/192bcdf4696a6ca05fbe15246cac3631 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def NewType(name, base=object): | |
class _NewType(base): | |
pass | |
_NewType.__name__ = name | |
return _NewType | |
PswdExc = NewType("PswdExc", Exception) | |
NoDouble = NewType("NoDouble", PswdExc) | |
TooManyDigits = NewType("TooManyDigits", PswdExc) | |
DecrDigit = NewType("DecrDigit", PswdExc) | |
def main(): | |
print(len(list(find_passwords(168630, 718098)))) | |
def test(): | |
_check_password("111111") | |
_check_password("112233") | |
_check_password("223450") | |
_check_password("123789") | |
def _check_password(pswd): | |
try: | |
check_password(pswd) | |
print(True) | |
except PswdExc as e: | |
print(type(e).__name__) | |
def find_passwords(r_min, r_max): | |
for i in map(str, range(r_min, r_max + 1)): | |
try: | |
check_password(i) | |
except PswdExc: | |
pass | |
else: | |
yield i | |
def check_password(pswd): | |
m = re.findall(r'((\d)\2+)', pswd) | |
if not m: | |
raise NoDouble() | |
for i in m: | |
if len(i[0]) == 2: | |
break | |
else: | |
raise TooManyDigits() | |
min_digit = 0 | |
for digit in map(int, pswd): | |
if digit < min_digit: | |
raise DecrDigit() | |
min_digit = digit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment