2bam

Untitled

Jul 12th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Collections.Concurrent;
  9.  
  10. static class PExt {
  11.     //param         parametro inicial de recursion
  12.     //expand        Debe expandir un nivel de soluciones
  13.     //mutateParam   Tomando cada elemento de solucion y el parametro del nivel actual, debe crear un parametro para la recursion
  14.     public static IEnumerable<T> FlatRecursive<T, P>(P param, Func<P, IEnumerable<T>> expand, Func<T, P, P> mutateParam) {
  15.         var s = new Stack<Tuple<P, IEnumerable<T>>>();
  16.         s.Push(Tuple.Create(param, expand(param)));
  17.         while(s.Any()) {
  18.             var curr = s.Pop();
  19.             foreach(var x in curr.Item2) {
  20.                 yield return x;
  21.                 var newParam = mutateParam(x, curr.Item1);
  22.                 s.Push(Tuple.Create(newParam, expand(newParam)));
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. namespace ParallelTest {
  29.     class Program {
  30.         //Esto algo mejora el uso de CPUs lo que paraleliza es el procesamiento de un segmento de rama del árbol de soluciones
  31.         // a diferencia de lo que realmente sería poderoso, que es sobre la expansión misma y concatenacion de las ramas
  32.         static IEnumerable<string> AllCombinationsNoRepeat_Stack(string prefix, int length) {
  33.             return
  34.                 PExt.FlatRecursive(
  35.                     Tuple.Create(prefix, length)
  36.                     , p =>
  37.                         p.Item2 == 0
  38.                         ? new string[] { }  //Cambia la semantica, porque el último trabaría en loop infinito
  39.                                             //  (igual llega porque la condicion de corte sucede después del pop)
  40.                         : new[] { p.Item1 }.Concat(
  41.                                 Enumerable.Range(0, 9)
  42.                                     .AsParallel()
  43.                                     .Select(n => p.Item1+n)                                        
  44.                                     .Where(s => s.GroupBy(ch=>ch).All(g=>g.Take(2).Count() < 2))         
  45.                             )
  46.                     , (x, p) => Tuple.Create(x, p.Item2-1)
  47.                     )
  48.                     ;
  49.         }
  50.  
  51.         static void Main(string[] args) {
  52.             foreach(var x in AllCombinationsNoRepeat_Stack("", 9))
  53.                 Console.WriteLine(x);
  54.  
  55.             Console.ReadKey();
  56.  
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment