Last active
August 29, 2015 14:07
-
-
Save elpargo/cc487d07deb7d7d16337 to your computer and use it in GitHub Desktop.
Puedo hacer con Python una aplicación que me genere 6 grupos de 5 caracteres alfanuméricos y que no repita?.
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
import string | |
import random | |
current = set(string.digits+string.letters) | |
for i in range(6): | |
sample = random.sample(current,5) | |
current = current - set(sample) | |
print sample |
desafortunadamente python no tiene un metodo group para agrupar elementos por tamano N http://stackoverflow.com/questions/4998427/how-to-group-elements-in-python-by-n-elements
pero una solucion podria ser:
import string
import random
characters =list(string.ascii_lowercase) + list(range(0,9))
random.shuffle(characters) #no hay un shuffle que me devuelva una coleccion en vez de mutar la original??..characters=random.shuffle(...)
groups = [characters[k:k+5] for k in range(0,len(characters),5)]
groups[0:6]
python es mejor para codigo imperativo, cuando tratas de programar mas funcional es limitado.....
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El truco esta en guardar el resultado de una ejecucion a la otra para evitar que se repitan ya que es un "non-replace"