Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Permutations/C-sharp/permutations-1.cs
Normal file
9
Task/Permutations/C-sharp/permutations-1.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
public static class Extension
|
||||
{
|
||||
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values) where T : IComparable<T>
|
||||
{
|
||||
if (values.Count() == 1)
|
||||
return new[] { values };
|
||||
return values.SelectMany(v => Permutations(values.Where(x => x.CompareTo(v) != 0)), (v, p) => p.Prepend(v));
|
||||
}
|
||||
}
|
||||
1
Task/Permutations/C-sharp/permutations-2.cs
Normal file
1
Task/Permutations/C-sharp/permutations-2.cs
Normal file
|
|
@ -0,0 +1 @@
|
|||
Enumerable.Range(0,5).Permutations()
|
||||
31
Task/Permutations/C-sharp/permutations-3.cs
Normal file
31
Task/Permutations/C-sharp/permutations-3.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
public class Permutations<T>
|
||||
{
|
||||
public static System.Collections.Generic.IEnumerable<T[]> AllFor(T[] array)
|
||||
{
|
||||
if (array == null || array.Length == 0)
|
||||
{
|
||||
yield return new T[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int pick = 0; pick < array.Length; ++pick)
|
||||
{
|
||||
T item = array[pick];
|
||||
int i = -1;
|
||||
T[] rest = System.Array.FindAll<T>(
|
||||
array, delegate(T p) { return ++i != pick; }
|
||||
);
|
||||
foreach (T[] restPermuted in AllFor(rest))
|
||||
{
|
||||
i = -1;
|
||||
yield return System.Array.ConvertAll<T, T>(
|
||||
array,
|
||||
delegate(T p) {
|
||||
return ++i == 0 ? item : restPermuted[i - 1];
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Task/Permutations/C-sharp/permutations-4.cs
Normal file
14
Task/Permutations/C-sharp/permutations-4.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace Permutations_On_RosettaCode
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string[] list = "a b c d".Split();
|
||||
foreach (string[] permutation in Permutations<string>.AllFor(list))
|
||||
{
|
||||
System.Console.WriteLine(string.Join(" ", permutation));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Task/Permutations/C-sharp/permutations-5.cs
Normal file
28
Task/Permutations/C-sharp/permutations-5.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
class Permutations
|
||||
{
|
||||
static int n = 4;
|
||||
static int [] buf = new int [n];
|
||||
static bool [] used = new bool [n];
|
||||
|
||||
static void Main()
|
||||
{
|
||||
for (int i = 0; i < n; i++) used [i] = false;
|
||||
rec(0);
|
||||
}
|
||||
|
||||
static void rec(int ind)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (!used [i])
|
||||
{
|
||||
used [i] = true;
|
||||
buf [ind] = i;
|
||||
if (ind + 1 < n) rec(ind + 1);
|
||||
else Console.WriteLine(string.Join(",", buf));
|
||||
used [i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Task/Permutations/C-sharp/permutations-6.cs
Normal file
26
Task/Permutations/C-sharp/permutations-6.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
class Permutations
|
||||
{
|
||||
static int n = 4;
|
||||
static int [] buf = new int [n];
|
||||
static int [] next = new int [n+1];
|
||||
|
||||
static void Main()
|
||||
{
|
||||
for (int i = 0; i < n; i++) next [i] = i + 1;
|
||||
next[n] = 0;
|
||||
rec(0);
|
||||
}
|
||||
|
||||
static void rec(int ind)
|
||||
{
|
||||
for (int i = n; next[i] != n; i = next[i])
|
||||
{
|
||||
buf [ind] = next[i];
|
||||
next[i]=next[next[i]];
|
||||
if (ind < n - 1) rec(ind + 1);
|
||||
else Console.WriteLine(string.Join(",", buf));
|
||||
next[i] = buf [ind];
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Task/Permutations/C-sharp/permutations-7.cs
Normal file
38
Task/Permutations/C-sharp/permutations-7.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// Always returns the same array which is the one passed to the function
|
||||
public static IEnumerable<T[]> HeapsPermutations<T>(T[] array)
|
||||
{
|
||||
var state = new int[array.Length];
|
||||
|
||||
yield return array;
|
||||
|
||||
for (var i = 0; i < array.Length;)
|
||||
{
|
||||
if (state[i] < i)
|
||||
{
|
||||
var left = i % 2 == 0 ? 0 : state[i];
|
||||
var temp = array[left];
|
||||
array[left] = array[i];
|
||||
array[i] = temp;
|
||||
yield return array;
|
||||
state[i]++;
|
||||
i = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
state[i] = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a different array for each permutation
|
||||
public static IEnumerable<T[]> HeapsPermutationsWrapped<T>(IEnumerable<T> items)
|
||||
{
|
||||
var array = items.ToArray();
|
||||
return HeapsPermutations(array).Select(mutating =>
|
||||
{
|
||||
var arr = new T[array.Length];
|
||||
Array.Copy(mutating, arr, array.Length);
|
||||
return arr;
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue