Advertisement
Fhernd

VT2.15-Generar-Conjunto-Potencia.linq

Feb 19th, 2017
38,892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. void Main()
  2. {
  3.     string conjunto = "123";
  4.    
  5.     HashSet<string> conjuntoPotencia = GenerarPermutacionParcial(conjunto);
  6.  
  7.     // Por cada permutación generar los subconjuntos posibles:
  8.     Enumerable.Range(0, conjunto.Length)
  9.         .ToList()
  10.         .ForEach(x =>
  11.         {
  12.             Enumerable.Range(0, conjunto.Length)
  13.             .ToList()
  14.             .ForEach(y =>
  15.             {
  16.                 // Genera subconjuntos por medio de la partición de la permutación:
  17.                 conjuntoPotencia.Add(conjuntoPotencia.ElementAt(x).Substring(0, y));
  18.                 conjuntoPotencia.Add(conjuntoPotencia.ElementAt(x).Substring(y + 1));
  19.             }
  20.             );
  21.         });
  22.  
  23.     conjuntoPotencia.Select(sub => new String(sub.ToCharArray()
  24.        .OrderBy(elemento => elemento)
  25.        .ToArray()))
  26.         .Distinct()
  27.         .OrderBy(sub => sub.Length)
  28.         .Dump("Conjunto Potencia de {1, 2, 3}");
  29. }
  30.  
  31. private HashSet<string> GenerarPermutacionParcial(string word)
  32. {
  33.     return new HashSet<string>(
  34.         Enumerable.Range(0, word.Length)
  35.         .Select(i => word.Remove(i, 1).Insert(0, word[i].ToString())));
  36. } // 123, 213, 312
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement