def _lfc(content, equality=False): """ EXAMPLES:: sage: from sage.combinat.necklace import _lfc sage: list(_lfc([3,3])) #necklaces [[0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] sage: list(_lfc([3,3], equality=True)) #Lyndon words [[0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] """ content = list(content) a = [0] * sum(content) content[0] -= 1 k = len(content) rng_k = range(k) rng_k.reverse() dll = DoublyLinkedList(rng_k) if not content[0]: # == 0 dll.hide(0) for z in _list_fixed_content(a, content, 2, 1, k, dll, equality=equality): yield z
def _ffc(content, equality=False): """ EXAMPLES:: sage: from sage.combinat.necklace import _ffc sage: list(_ffc([3,3])) #necklaces [[0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] sage: list(_ffc([3,3], equality=True)) #Lyndon words [[0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] """ e = list(content) a = [len(e) - 1] * sum(e) r = [0] * sum(e) a[0] = 0 e[0] -= 1 k = len(e) rng_k = range(k) rng_k.reverse() dll = DoublyLinkedList(rng_k) if not e[0]: # == 0 dll.hide(0) for x in _fast_fixed_content(a, e, 2, 1, k, r, 2, dll, equality=equality): yield x
def _lfc(content, equality=False): """ EXAMPLES:: sage: from sage.combinat.necklace import _lfc sage: list(_lfc([3,3])) #necklaces [[0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] sage: list(_lfc([3,3], equality=True)) #Lyndon words [[0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] """ content = list(content) a = [0]*sum(content) content[0] -= 1 k = len(content) rng_k = range(k) rng_k.reverse() dll = DoublyLinkedList(rng_k) if content[0] == 0: dll.hide(0) for z in _list_fixed_content(a, content, 2, 1, k, dll, equality=equality): yield z
def _ffc(content, equality=False): """ EXAMPLES:: sage: from sage.combinat.necklace import _ffc sage: list(_ffc([3,3])) #necklaces [[0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] sage: list(_ffc([3,3], equality=True)) #Lyndon words [[0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1]] """ e = list(content) a = [len(e)-1]*sum(e) r = [0] * sum(e) a[0] = 0 e[0] -= 1 k = len(e) rng_k = range(k) rng_k.reverse() dll = DoublyLinkedList(rng_k) if e[0] == 0: dll.hide(0) for x in _fast_fixed_content(a, e, 2, 1, k, r, 2, dll, equality=equality): yield x
def __iter__(self): """ An iterator for all permutations of k things from range(n). EXAMPLES:: sage: from sage.combinat.permutation_nk import PermutationsNK sage: [ p for p in PermutationsNK(3,2)] [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]] sage: len(PermutationsNK(5,4).list()) 120 sage: [1, 2, 2, 0] in PermutationsNK(5,4).list() False """ n, k = self._n, self._k if k == 0: yield [] return if k > n: return range_n = range(n) available = range(n) available = DoublyLinkedList(range_n) L = range(k) L[-1] = 'begin' for i in range(k - 1): available.hide(i) finished = False while not finished: L[-1] = available.next_value[L[-1]] if L[-1] != 'end': yield L[:] continue finished = True for i in reversed(range(k - 1)): value = L[i] available.unhide(value) value = available.next_value[value] if value != 'end': L[i] = value available.hide(value) value = 'begin' for i in range(i + 1, k - 1): L[i] = value = available.next_value[value] available.hide(value) L[-1] = available.next_value[value] yield L[:] finished = False break return
def __iter__(self): """ An iterator for all permutations of k things from range(n). EXAMPLES:: sage: from sage.combinat.permutation_nk import PermutationsNK sage: [ p for p in PermutationsNK(3,2)] [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]] sage: len(PermutationsNK(5,4).list()) 120 sage: [1, 2, 2, 0] in PermutationsNK(5,4).list() False """ n, k = self._n, self._k if k == 0: yield [] return if k>n: return range_n = range(n) available = range(n) available = DoublyLinkedList(range_n) L = range(k) L[-1] = 'begin' for i in range(k-1): available.hide(i) finished = False while not finished: L[-1] = available.next_value[L[-1]] if L[-1] != 'end': yield L[:] continue finished = True for i in reversed(range(k-1)): value = L[i] available.unhide(value) value = available.next_value[value] if value != 'end': L[i] = value available.hide(value) value = 'begin' for i in range(i+1, k-1): L[i] = value = available.next_value[value] available.hide(value) L[-1] = available.next_value[value] yield L[:] finished = False break return