/// <summary> /// Initialize the variations for constructors. /// </summary> /// <param name="values">List of values to select variations from.</param> /// <param name="lowerIndex">The size of each variation set to return.</param> /// <param name="type">The type of variations set to generate.</param> private void Initialize(IList <T> values, int lowerIndex, GenerateOption type) { myMetaCollectionType = type; myLowerIndex = lowerIndex; myValues = new List <T>(); myValues.AddRange(values); if (type == GenerateOption.WithoutRepetition) { List <int> myMap = new List <int>(); int index = 0; for (int i = 0; i < myValues.Count; ++i) { if (i >= myValues.Count - myLowerIndex) { myMap.Add(index++); } else { myMap.Add(Int32.MaxValue); } } myPermutations = new Permutations <int>(myMap); } else { ; // myPermutations isn't used. } }
/// <summary> /// Initialize the combinations by settings a copy of the values from the /// </summary> /// <param name="values">List of values to select combinations from.</param> /// <param name="lowerIndex">The size of each combination set to return.</param> /// <param name="type">The type of Combinations set to generate.</param> /// <remarks> /// Copies the array and parameters and then creates a map of booleans that will /// be used by a permutations object to refence the subset. This map is slightly /// different based on whether the type is with or without repetition. /// /// When the type is WithoutRepetition, then a map of upper index elements is /// created with lower index false's. /// E.g. 8 choose 3 generates: /// Map: {1 1 1 1 1 0 0 0} /// Note: For sorting reasons, false denotes inclusion in output. /// /// When the type is WithRepetition, then a map of upper index - 1 + lower index /// elements is created with the falses indicating that the 'current' element should /// be included and the trues meaning to advance the 'current' element by one. /// E.g. 8 choose 3 generates: /// Map: {1 1 1 1 1 1 1 1 0 0 0} (7 trues, 3 falses). /// </remarks> private void Initialize(IList <T> values, int lowerIndex, GenerateOption type) { myMetaCollectionType = type; myLowerIndex = lowerIndex; myValues = new List <T>(); myValues.AddRange(values); List <bool> myMap = new List <bool>(); if (type == GenerateOption.WithoutRepetition) { for (int i = 0; i < myValues.Count; ++i) { if (i >= myValues.Count - myLowerIndex) { myMap.Add(false); } else { myMap.Add(true); } } } else { for (int i = 0; i < values.Count - 1; ++i) { myMap.Add(true); } for (int i = 0; i < myLowerIndex; ++i) { myMap.Add(false); } } myPermutations = new Permutations <bool>(myMap); }
/// <summary> /// Construct a enumerator with the parent object. /// </summary> /// <param name="source">The source Permutations object.</param> public Enumerator(Permutations <T> source) { myParent = source; myLexicographicalOrders = new int[source.myLexicographicOrders.Length]; source.myLexicographicOrders.CopyTo(myLexicographicalOrders, 0); Reset(); }
public static void Solution() { // İlgili tüm permütasyonların listesi çıkarılıyor // En fazla üç adet virgül alabileceği için adet de virgül ekleniyor dizine Permutations <string> Per = new Permutations <string>(new List <string> { "A", "B", "C", "D", ",", ",", "," }, GenerateOption.WithoutRepetition); List <string> list = new List <string>(); foreach (List <string> line in Per) { // Permütasyon listesindeki string dizileri stringe çeviriliyor string sol = ""; foreach (string s in line) { sol += s; } // Ardarda gelen virgüller teke indiriliyor sol = sol.Replace(",,,", ","); sol = sol.Replace(",,", ","); // İlk karakteri virgül olan stringlerden ilk virgül kaldırılıyor if (sol[0] == ',') { sol = sol.Remove(0, 1); } // Son karakteri virgül olan stringlerden son virgül kaldırılıyor if (sol[sol.Length - 1] == ',') { sol = sol.Remove(sol.Length - 1, 1); } // Baştan ve sondan boşluk varsa temizleniyor sol.Trim(); // String virgüllerden bölünerek tekrar biz dizi haline getiriliyor string[] elements = sol.Split(','); // Dizideki elemanlar ilk elemanında göre sıralanıp tekrar bir stringe dönüştürülüyor string newString = ""; foreach (string s in elements.OrderBy(a => a)) { newString += s + ","; } // String'e dönüştürme esnasında en sona eklenen virgül siliniyor. newString = newString.Remove(newString.Length - 1, 1); // Elde edilen yeni string listeye ekleniyor list.Add(newString); } // Oluşan listedeki dublike elemanlar temizlenerek yeni liste başka bir listeye atanıyor. List <string> newList = list.Distinct().ToList(); // Oluşan temizlenmiş yeni liste ilk elemana göre yeniden sıralandırılarak ekrana yazdırılıyor foreach (string sol in newList.OrderBy(a => a[0]).ThenBy(a => a)) { Console.WriteLine(sol); } Console.WriteLine(newList.Count); Console.ReadLine(); }