Skip to content

Instantly share code, notes, and snippets.

@killjoy1221
Created December 4, 2019 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killjoy1221/192bcdf4696a6ca05fbe15246cac3631 to your computer and use it in GitHub Desktop.
Save killjoy1221/192bcdf4696a6ca05fbe15246cac3631 to your computer and use it in GitHub Desktop.
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