Advertisement
paulogp

Combinacoes possiveis a serem realizadas numa lista

Aug 7th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # paulogp
  3. def lista_combinacoes(n, k, L):
  4.     '''
  5.    Retorna todas as combinacoes possiveis a serem realizadas numa lista
  6.    n colunas de L
  7.    k colunas a filtrar de L
  8.    L = [] inicialmente
  9.    PS: Funcao recursiva
  10.    '''
  11.     if k != 0:
  12.         for x in range(0, n):
  13.             if (x not in L):
  14.                 if len(L) == 0:
  15.                     lista_combinacoes(n, k-1, L+[x])
  16.                 elif (x > L[len(L)-1]):
  17.                     lista_combinacoes(n, k-1, L+[x])
  18.     else:
  19.         print L
  20.  
  21. # Exemplo
  22. lista_combinacoes(6, 2, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement