Skip to content

Instantly share code, notes, and snippets.

@Agoxandr
Last active December 15, 2021 06:56
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 Agoxandr/54bdcecfcde1eb24efe5deb060d3471a to your computer and use it in GitHub Desktop.
Save Agoxandr/54bdcecfcde1eb24efe5deb060d3471a to your computer and use it in GitHub Desktop.
Bestimmen Sie begründet die Anzahl der Permutationen von [6] mit höchstens drei Fixpunkten.
var unchaged = new int[] { 1, 2, 3, 4, 5, 6 };
var perms = GetPermutations(unchaged, 6);
int counter = 0;
var tolerances = new int[] { 0, 0, 0, 0 };
foreach (var perm in perms)
{
var list = perm.ToList();
var text = "";
int tolerance = 0;
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
if (item == unchaged[i])
{
if (tolerance < 4)
{
tolerance++;
}
else
{
text = "";
break;
}
}
text += item + " ";
}
if (text != "")
{
if (tolerance < 4)
{
tolerances[tolerance]++;
counter++;
Console.WriteLine(text + "Fixed points " + tolerance);
}
}
}
Console.WriteLine(counter);
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Tolerance " + i + " " + tolerances[i]);
}
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1).SelectMany(t => list.Where(o => !t.Contains(o)), (t1, t2) => t1.Concat(new T[] { t2 }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment