Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Last active February 15, 2024 19:56
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 JamesTheAwesomeDude/d505bb33ef382e99945250e1e9a09484 to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/d505bb33ef382e99945250e1e9a09484 to your computer and use it in GitHub Desktop.
Perfectly random Android lockscreen pattern
from itertools import combinations, permutations
import re
MIN_LENGTH = 4
ILLEGAL_PATTERN_RE = re.compile('(%s)' % '|'.join(
# Simulate negative lookbehind via negative lookahead
# h/t https://learnbyexample.github.io/py_regular_expressions/lookarounds.html#negated-groups
f'^(?!.*{b}.*{a}{c}).*{a}{c}' for a,b,c, in
['123', '159', '147', '258', '369', '357', '321', '456', '654', '789', '741', '753', '852', '987', '951', '963']
))
PATTERNS = sorted(filter(
lambda seq: not ILLEGAL_PATTERN_RE.match(seq),
(seq for l in range(MIN_LENGTH, 10) for nodes in combinations('123456789', l)
for seq in (
''.join(node for node in _seq)
for _seq in permutations(nodes)
)
)
))
def repr_pattern(pattern):
return '{0} {1} {2}\n{3} {4} {5}\n{6} {7} {8}'.format(*(
('123456789'[pattern.index(node)] if node in pattern else '-')
for node in '123456789'
))
if __name__ == '__main__':
from random import SystemRandom
_rand = SystemRandom()
print(repr_pattern(_rand.choice(PATTERNS)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment