fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Permutations
  6. {
  7. class FloydPermutation
  8. {
  9. public static void Main(string[] args)
  10. {
  11. RunTest(1, 4);
  12. RunTest(2, 4);
  13. RunTest(3, 4);
  14. RunTest(4, 4);
  15. }
  16.  
  17. private static void RunTest(int count, int max)
  18. {
  19. var random = new Random(7);
  20. var samples = Enumerable.Range(0, 100000)
  21. .Select(_ => GetSample(count, max, random))
  22. .Select(sample => string.Join(", ", sample.Select(x => x.ToString()).ToArray()))
  23. .GroupBy(sample => sample)
  24. .ToArray();
  25. Console.WriteLine("{0} distinct samples of size {1} chosen from [1,{2}]", samples.Length, count, max);
  26. foreach (var grouping in samples.OrderBy(sample => sample.Key))
  27. {
  28. Console.WriteLine("[{0}]: {1}", grouping.Key, grouping.Count());
  29. }
  30. }
  31.  
  32. private static int[] GetSample(int count, int max, Random random)
  33. {
  34. var seen = new Dictionary<int, LinkedListNode<int>>();
  35. var result = new LinkedList<int>();
  36. for (var i = max - count + 1; i <= max; i++)
  37. {
  38. var t = random.Next(1, i + 1);
  39. LinkedListNode<int> node;
  40. if (seen.TryGetValue(t, out node))
  41. {
  42. seen[i] = result.AddAfter(node, i);
  43. }
  44. else
  45. {
  46. seen[t] = result.AddFirst(t);
  47. }
  48. }
  49.  
  50. return result.ToArray();
  51. }
  52. }
  53. }
  54.  
Success #stdin #stdout 2.19s 46360KB
stdin
Standard input is empty
stdout
4 distinct samples of size 1 chosen from [1,4]
[1]: 25134
[2]: 24851
[3]: 24890
[4]: 25125
12 distinct samples of size 2 chosen from [1,4]
[1, 2]: 8185
[1, 3]: 8449
[1, 4]: 8351
[2, 1]: 8319
[2, 3]: 8121
[2, 4]: 8265
[3, 1]: 8333
[3, 2]: 8447
[3, 4]: 8409
[4, 1]: 8319
[4, 2]: 8355
[4, 3]: 8447
24 distinct samples of size 3 chosen from [1,4]
[1, 2, 3]: 4137
[1, 2, 4]: 4211
[1, 3, 2]: 4199
[1, 3, 4]: 4193
[1, 4, 2]: 4109
[1, 4, 3]: 4156
[2, 1, 3]: 4275
[2, 1, 4]: 4109
[2, 3, 1]: 4044
[2, 3, 4]: 4254
[2, 4, 1]: 4154
[2, 4, 3]: 4116
[3, 1, 2]: 4204
[3, 1, 4]: 4171
[3, 2, 1]: 4115
[3, 2, 4]: 4129
[3, 4, 1]: 4319
[3, 4, 2]: 4177
[4, 1, 2]: 4123
[4, 1, 3]: 4069
[4, 2, 1]: 4075
[4, 2, 3]: 4246
[4, 3, 1]: 4212
[4, 3, 2]: 4203
24 distinct samples of size 4 chosen from [1,4]
[1, 2, 3, 4]: 4115
[1, 2, 4, 3]: 4154
[1, 3, 2, 4]: 4275
[1, 3, 4, 2]: 4193
[1, 4, 2, 3]: 4109
[1, 4, 3, 2]: 4156
[2, 1, 3, 4]: 4204
[2, 1, 4, 3]: 4109
[2, 3, 1, 4]: 4137
[2, 3, 4, 1]: 4254
[2, 4, 1, 3]: 4211
[2, 4, 3, 1]: 4116
[3, 1, 2, 4]: 4044
[3, 1, 4, 2]: 4171
[3, 2, 1, 4]: 4199
[3, 2, 4, 1]: 4129
[3, 4, 1, 2]: 4319
[3, 4, 2, 1]: 4177
[4, 1, 2, 3]: 4075
[4, 1, 3, 2]: 4069
[4, 2, 1, 3]: 4123
[4, 2, 3, 1]: 4246
[4, 3, 1, 2]: 4212
[4, 3, 2, 1]: 4203