Created
June 29, 2017 20:21
-
-
Save dharmatech/06e15574632325361b531749eac44ba8 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.RegularExpressions; | |
namespace pinter_5.F._1 | |
{ | |
public static class Extensions | |
{ | |
// https://stackoverflow.com/a/6362642/268581 | |
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunksize) | |
{ | |
while (source.Any()) | |
{ | |
yield return source.Take(chunksize); | |
source = source.Skip(chunksize); | |
} | |
} | |
} | |
class Program | |
{ | |
static Dictionary<string,string> string_to_dictionary(string s) => | |
s | |
.Split((char[])null, StringSplitOptions.RemoveEmptyEntries) | |
.Chunk(2) | |
.ToDictionary(eq => eq.ToList()[0], eq => eq.ToList()[1]); | |
public static IEnumerable<TResult> ZipMany<TSource, TResult>( | |
IEnumerable<IEnumerable<TSource>> source, | |
Func<IEnumerable<TSource>, TResult> selector) | |
{ | |
// ToList is necessary to avoid deferred execution | |
var enumerators = source.Select(seq => seq.GetEnumerator()).ToList(); | |
try | |
{ | |
while (true) | |
{ | |
foreach (var e in enumerators) | |
{ | |
bool b = e.MoveNext(); | |
if (!b) yield break; | |
} | |
// Again, ToList (or ToArray) is necessary to avoid deferred execution | |
yield return selector(enumerators.Select(e => e.Current).ToList()); | |
} | |
} | |
finally | |
{ | |
foreach (var e in enumerators) | |
e.Dispose(); | |
} | |
} | |
static IEnumerable<string> generate(Dictionary<string,string> eqs, string s) | |
{ | |
var results = new List<string>(); | |
foreach (var elt in eqs) | |
{ | |
if (new Regex(elt.Key).IsMatch(s)) | |
results.Add(new Regex(elt.Key).Replace(s, elt.Value, 1)); | |
if (new Regex(elt.Value).IsMatch(s)) | |
results.Add(new Regex(elt.Value).Replace(s, elt.Key, 1)); | |
} | |
foreach (var result in results) yield return result; | |
foreach (var elt in ZipMany(results.Select(elt => generate(eqs, elt)), elts => elts).SelectMany(elts => elts)) | |
yield return elt; | |
} | |
static void table(List<string> G, Dictionary<string, string> eqs) | |
{ | |
Console.Write(" |"); foreach (var y in G) Console.Write($"{y,-5}|"); Console.WriteLine(); | |
Console.Write("-----|"); foreach (var y in G) Console.Write("-----|"); Console.WriteLine(); | |
foreach (var x in G) | |
{ | |
Console.Write($"{x,5}|"); | |
foreach (var y in G) | |
{ | |
var result = generate(eqs, x + y).First(elt => G.Contains(elt)); | |
Console.Write($"{result,-5}|"); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
//var G = new List<string>() { "e", "a", "b", "bb", "ab", "abb" }; | |
//var eqs = new Dictionary<string, string> | |
//{ { "e", "" }, { "aa", "e" }, { "bbb", "e" }, { "ba", "abb" } }; | |
//table(G, eqs); | |
var G = new List<string>() { "e", "a", "b", "bb", "bbb", "ab", "abb", "abbb" }; | |
var eqs = new Dictionary<string, string> | |
{ { "e", "" }, { "aa", "e" }, { "bbbb", "e" }, { "ba", "abbb" } }; | |
table(G, eqs); | |
// var G = new List<string>() { "e", "a", "b", "bb", "bbb", "ab", "abb", "abbb" }; | |
//var G = "e a b bb bbb ab abb abbb".Split(null).ToList(); | |
//var eqs = string_to_dictionary("aaaa e aa bb ba abbb"); eqs["e"] = ""; | |
//table(G, eqs); | |
// pinter 5.G.4 | |
//var G = "e b bb bbb a ab abb abbb".Split(null).ToList(); | |
//var eqs = string_to_dictionary("aa e bbbb e ba abbb"); eqs["e"] = ""; | |
//table(G, eqs); | |
// pinter 5.G.5 | |
//var G = "e b bb bbb a ab abb abbb".Split(null).ToList(); | |
//var eqs = string_to_dictionary("bbbb e ba ab aa e"); eqs["e"] = ""; | |
//table(G, eqs); | |
// pinter 5.G.6 | |
//var G = "e b bb a ab abb ba bab babb bba bbab bbabb".Split(null).ToList(); | |
//var eqs = string_to_dictionary("bbb e aa e aba bbabb"); eqs["e"] = ""; | |
//table(G, eqs); | |
//var G = "e b bb a ab abb ba bab babb aba abab ababb".Split(null).ToList(); | |
//var eqs = string_to_dictionary("bbb e bba abab baba abb aa e"); eqs["e"] = ""; | |
//table(G, eqs); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment