11_password.py 915 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. from itertools import dropwhile
  3. illegal = tuple(map(ord, 'iol'))
  4. def invalid(pw):
  5. pairs = 0
  6. prevpair = -1
  7. sequence = False
  8. for i in range(len(pw) - 2):
  9. a, b, c = pw[i:i + 3]
  10. if a in illegal:
  11. return True
  12. elif a == b and i >= prevpair + 2:
  13. pairs += 1
  14. prevpair = i
  15. elif a == b - 1 and b == c - 1:
  16. sequence = True
  17. return not sequence or pairs < 2
  18. def convert(pw):
  19. return ''.join(chr(i + ord('a')) for i in pw[:-2])
  20. def increment(pw):
  21. pw = [ord(c) - ord('a') for c in pw] + [-1, -1]
  22. while True:
  23. i = len(pw) - 3
  24. pw[i] += 1
  25. while pw[i] == 26:
  26. pw[i] = 0
  27. i -= 1
  28. pw[i] += 1
  29. yield pw
  30. options = increment('cqjxjnds')
  31. print(convert(next(dropwhile(invalid, options))))
  32. print(convert(next(dropwhile(invalid, options))))