Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created February 20, 2017 01:47
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 Fhernd/f57a0cf1858550ddf88791b3d31d0c5c to your computer and use it in GitHub Desktop.
Save Fhernd/f57a0cf1858550ddf88791b3d31d0c5c to your computer and use it in GitHub Desktop.
Generación de conjunto potencia a partir de un conjunto. Programación funcional.
void Main()
{
string conjunto = "123";
HashSet<string> conjuntoPotencia = GenerarPermutacionParcial(conjunto);
// Por cada permutación generar los subconjuntos posibles:
Enumerable.Range(0, conjunto.Length)
.ToList()
.ForEach(x =>
{
Enumerable.Range(0, conjunto.Length)
.ToList()
.ForEach(y =>
{
// Genera subconjuntos por medio de la partición de la permutación:
conjuntoPotencia.Add(conjuntoPotencia.ElementAt(x).Substring(0, y));
conjuntoPotencia.Add(conjuntoPotencia.ElementAt(x).Substring(y + 1));
}
);
});
conjuntoPotencia.Select(sub => new String(sub.ToCharArray()
.OrderBy(elemento => elemento)
.ToArray()))
.Distinct()
.OrderBy(sub => sub.Length)
.Dump("Conjunto Potencia de {1, 2, 3}");
}
private HashSet<string> GenerarPermutacionParcial(string word)
{
return new HashSet<string>(
Enumerable.Range(0, word.Length)
.Select(i => word.Remove(i, 1).Insert(0, word[i].ToString())));
} // 123, 213, 312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment