Created
September 25, 2015 11:09
-
-
Save netheril96/1d22d388f53bcd8ff35a to your computer and use it in GitHub Desktop.
Algorithm to count the number of Android patterns
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
#!/usr/bin/env python | |
# The Android dots are labelled as | |
# 0 1 2 | |
# 3 4 5 | |
# 6 7 8 | |
# The following is a handcoded table where table[i][j] is the number crossed by path i -> j. | |
table = [ | |
[None, None, 1, None, None, None, 3, None, 4], | |
[None, None, None, None, None, None, None, 4, None], | |
[1, None, None, None, None, None, 4, None, 5], | |
[None, None, None, None, None, 4, None, None, None], | |
[None, None, None, None, None, None, None, None, None], | |
[None, None, None, 4, None, None, None, None, None], | |
[3, None, 4, None, None, None, None, None, 7], | |
[None, 4, None, None, None, None, None, None, None], | |
[4, None, 5, None, None, None, 7, None, None] | |
] | |
def generateAndroidPattern(prefix): | |
if not prefix: # Special cases for empty prefix | |
for i in xrange(9): | |
for n in generateAndroidPattern((i,)): | |
yield n | |
else: | |
if len(prefix) >= 4: | |
yield prefix | |
for i in xrange(9): | |
if i in prefix: # Android patterns must have distinct dots | |
continue | |
crossNumber = table[prefix[-1]][i] # The number crossed by the proposed path | |
if crossNumber is None or crossNumber in prefix: | |
for n in generateAndroidPattern(prefix + (i, )): | |
yield n | |
print(sum(1 for n in generateAndroidPattern(tuple()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment