diff --git a/Metaprogramming/FilterSimple.cs b/Metaprogramming/FilterSimple.cs new file mode 100644 index 0000000..1440186 --- /dev/null +++ b/Metaprogramming/FilterSimple.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Metaprogramming +{ + class FilterSimple + { + + public List filter(String[] names) + { + List result = new List(); + + foreach(String name in names) + { + if ( + name.Length >= 10 && name.Length <= 200 && + name.Contains("Mich") && + name.IndexOf("V") == 0 && + name.EndsWith("ov") && + !( + name.Length >= 50 && + name.Length <= 65 && + name.Contains("Abu") && + name.IndexOf("Lev") == 0 && + name.EndsWith("iov") + ) + ) + result.Add(name); + } + return result; + } + } +} diff --git a/Metaprogramming/Metaprogramming.cs b/Metaprogramming/Metaprogramming.cs new file mode 100644 index 0000000..2794cfd --- /dev/null +++ b/Metaprogramming/Metaprogramming.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Metaprogramming +{ + class Metaprogramming + { + private static Dictionary conditions = new Dictionary() + { + ["length"] = new int[] {10, 200}, + ["contains"] = "Mich", + ["starts"] = "V", + ["ends"] = "ov", + ["not"] = new Dictionary() + { + ["length"] = new int[] {50,65}, + ["contains"] = "Abu", + ["starts"] = "Lev", + ["ends"] = "iov" + } + + }; + + + private static Dictionary> operations = new Dictionary>() + { + ["length"] = (s, v) => s.Length >= ((int[])v)[0] && s.Length <= ((int[])v)[1], + ["contains"] = (s, v) => s.Contains((String)v), + ["starts"] = (s, v) => s.IndexOf((String)v) == 0, + ["ends"] = (s, v) => s.EndsWith((String)v), + ["not"] = (s,v) => !(check(s,(Dictionary) v)) + }; + + private static Boolean check(String s, Dictionary conditions) + { + Boolean valid; + + foreach(var condition in conditions) + { + valid = operations[condition.Key].Invoke(s, condition.Value); + if (!valid) return false; + } + + return true; + } + + public List filter(List names) + { + List result = new List(); + foreach(String name in names) + { + if(check(name, conditions)) + { + result.Add(name); + } + } + return result; + } + } +} diff --git a/Metaprogramming/Program.cs b/Metaprogramming/Program.cs new file mode 100644 index 0000000..d7055b1 --- /dev/null +++ b/Metaprogramming/Program.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Metaprogramming +{ + class Program + { + private static String[] names = + { + "Marcus Aurelius Antoninus Augustus", + "Darth Vader", + "Victor Michailovich Glushkov", + "Victor Michailovich Glushkov1", + "Gottfried Wilhelm von Leibniz", + "Mao Zedong", + "Vladimir Sergeevich Soloviov", + "Ibn Arabi", + "Lev Nikolayevich Tolstoy", + "Muammar Muhammad Abu Minyar al-Gaddafi", + "Rene Descartes", + "Fyodor Mikhailovich Dostoyevsky", + "Benedito de Espinosa" + }; + static void Main(string[] args) + { + new FilterSimple().filter(names).ForEach(delegate (String name) + { + Console.WriteLine(name); + }); + new Metaprogramming().filter(names.ToList()).ForEach(delegate (String name) + { + Console.WriteLine(name); + }); + Console.ReadKey(); + } + } +}