Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created December 3, 2012 12:04
Show Gist options
  • Save v2e4lisp/4194576 to your computer and use it in GitHub Desktop.
Save v2e4lisp/4194576 to your computer and use it in GitHub Desktop.
necklace problem... (done)
# nl(s) is short for necklace(s)
# generate all candidate necklaces
def nls_init(n, m, necklaces = ['']):
if not m: return map(lambda x: n*'1'+x, necklaces)
if not n: return map(lambda x: m*'0'+x, necklaces)
n1 = nls_init(n, m-1, map(lambda x: '0'+x, necklaces))
n2 = nls_init(n-1, m, map(lambda x: '1'+x, necklaces))
return n1 + n2
# rotate a necklace
def nl_rotate(necklace):
length = len(necklace)
return [necklace[i:] + necklace[:i] for i in range(0, length)]
# filter out the bad necklaces
def nls_filter(necklaces):
template = []
nls_new = []
for nl in necklaces:
if nl not in template:
# update template
# append it to identified necklaces list (aka nls_new)
template += nl_rotate(nl)
nls_new.append(nl)
return nls_new
# def nls_rotate(necklaces):
# return [nl_rotate(nl) for nl in necklaces]
necklaces = nls_init(10, 10)
print necklaces, len(necklaces), nls_filter(necklaces)
# emacs gist interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment