Created
July 25, 2017 19:47
-
-
Save dharmatech/4af7ae6274f36ec45e3e92a6c6be815f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Collections; | |
using static System.Console; | |
namespace pinter_8.A._1_multiplying_permutations | |
{ | |
public static class Extensions | |
{ | |
public static T display<T>(this T obj) { WriteLine(obj); return obj; } | |
public static Cycles ToCycles(this IEnumerable<Cycle> seq) => new Cycles(seq); | |
} | |
// https://stackoverflow.com/questions/2495791/custom-collection-initializers | |
public class Cycle : IEnumerable<int> | |
{ | |
public List<int> ls; | |
public IEnumerator<int> GetEnumerator() => ls.GetEnumerator(); | |
IEnumerator IEnumerable.GetEnumerator() => ls.GetEnumerator(); | |
public Cycle() => ls = new List<int>(); | |
public Cycle(IEnumerable<int> seq) => ls = seq.ToList(); | |
public override string ToString() => "(" + String.Join("", ls.Select(elt => elt.ToString())) + ")"; | |
public void Add(int i) => ls.Add(i); | |
public Cycles to_transpositions() => new Cycles(ls.Reverse<int>().Skip(1).Select(elt => new Cycle { ls.Last(), elt })); | |
} | |
public class Cycles : IEnumerable<Cycle> | |
{ | |
public List<Cycle> ls; | |
public IEnumerator<Cycle> GetEnumerator() => ls.GetEnumerator(); | |
IEnumerator IEnumerable.GetEnumerator() => ls.GetEnumerator(); | |
public Cycles() { ls = new List<Cycle>(); } | |
public Cycles(IEnumerable<Cycle> seq) => ls = seq.ToList(); | |
public static Cycles from_string(string s) | |
{ | |
var result = new List<Cycle>(); | |
foreach (var elt in s) | |
{ | |
if (elt == '(') result.Add(new Cycle()); | |
if (Char.IsDigit(elt)) result.Last().ls.Add(elt - '0'); | |
} | |
return new Cycles() { ls = result }; | |
} | |
public override string ToString() => String.Join("", ls.Select(elt => elt.ToString())); | |
public void Add(Cycle cycle) => ls.Add(cycle); | |
public Cycles to_transpositions() => ls.SelectMany(cycle => cycle.to_transpositions()).ToCycles(); | |
} | |
public class Permutation | |
{ | |
public Dictionary<int, int> f; | |
public static Permutation from_string(string s) => | |
new Permutation() | |
{ | |
f = | |
Enumerable.Range(1, s.Count()) | |
.Zip( | |
s.Select(elt => elt - '0'), | |
(k, v) => (key: k, value: v)) | |
.ToDictionary(kv => kv.key, kv => kv.value) | |
}; | |
public void display() | |
{ | |
foreach (var key in f.Keys.OrderBy(elt => elt)) Write($"{key} "); WriteLine(); | |
foreach (var key in f.Keys.OrderBy(elt => elt)) Write($"{f[key]} "); WriteLine(); | |
} | |
public Cycle get_cycle(int i) | |
{ | |
var result = new Cycle(); | |
while (true) | |
{ | |
if (result.Contains(i)) return result; | |
result.Add(i); | |
i = f[i]; | |
} | |
} | |
public Cycles to_disjoint_cycles() => | |
Enumerable.Range(1, f.Keys.Max()) | |
.Aggregate( | |
new Cycles(), | |
(cycles, i) => | |
cycles.SelectMany(elt => elt).Contains(i) ? cycles : | |
f[i] == i ? cycles : | |
cycles.Concat(new [] { get_cycle(i) }).ToCycles()); | |
} | |
class Program | |
{ | |
static void display_permutation_as_disjoint_cycles(string s) | |
{ | |
Permutation.from_string(s) .display(); WriteLine(); | |
Permutation.from_string(s).to_disjoint_cycles().display(); | |
WriteLine("--------------------"); | |
} | |
static void Main(string[] args) | |
{ | |
display_permutation_as_disjoint_cycles("492517683"); | |
display_permutation_as_disjoint_cycles("749238165"); | |
display_permutation_as_disjoint_cycles("795312486"); | |
display_permutation_as_disjoint_cycles("987436512"); | |
Cycles.from_string("(123)(456)").display(); | |
Cycles.from_string("(123)(456)").to_transpositions().display(); | |
Cycles.from_string("(12345)").to_transpositions().display(); | |
WriteLine(); | |
Cycles.from_string("(137428)" ).to_transpositions().display(); | |
Cycles.from_string("(416)(8235)" ).to_transpositions().display(); | |
Cycles.from_string("(123)(456)(1574)").to_transpositions().display(); | |
Permutation.from_string("31428765").to_disjoint_cycles().to_transpositions().display(); | |
WriteLine(Cycles.from_string("(123)(456)")); | |
Cycles.from_string("(123)(456)").display(); | |
Cycles.from_string("(123)(456)").to_transpositions().display(); | |
new Cycle { 1, 2, 3 }.display(); | |
String.Join("", new List<int>() { 1, 2, 3 }.Select(elt => elt.ToString())).display(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment