Skip to content

Instantly share code, notes, and snippets.

@netheril96
Created September 25, 2015 11:09
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 netheril96/1d22d388f53bcd8ff35a to your computer and use it in GitHub Desktop.
Save netheril96/1d22d388f53bcd8ff35a to your computer and use it in GitHub Desktop.
Algorithm to count the number of Android patterns
#!/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