It is required to generate (not randomly) a string from the combination of transmitted characters.

//symbols = [AZ]+[az]+[0-9]+[. , / _] public IEnumerable<string> GenerateString (ArrayList symbols, int LengthOfString) { // Генерация строки // Фильтр (если один и тот же символ "последовательно" повторяется // больше 2-х раз, то строка не валидна и переходить к генерации следующей строки) yield return GeneratedString; } 

Example:

 // symbols = [abcde]; foreach (var string in GenerateString (symbols, 3)) { // Запись в текстовом файле } 

conclusion

abc, bac, cab, bca, abd, dac .... ade, dea, dca ....

and so on. note aab, aaa - so as not to be generated

Closed due to the fact that the essence of the question is not clear to the participants of LEQADA , Nick Volynkin , aleksandr barakin , user194374, Athari 27 Dec '15 at 2:54 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • GenerateString () do you have to generate a single string or list? Why does GenerateString return an IEnumerable <string>? - koks_rs
  • I wanted to use exactly {yield return} I don’t want to keep the principle Generate> Use> Forget in the foreach loop to return only one generated string at each call (very convenient if you continue to use parallelism) - Igor Ceban
  • ..... // more than 2 times, .... and at the same time "aab, aaa - so as not to generate" well, let's say aaa falls under the criterion "more than 2 times", but aab does not. Specify, there can be no consecutive repetitions at all, or is a double possible? - Alexey
  • Just aab = bad aba = good - Igor Ceban
  • If to speak in a programming language, then (..... // more than 2 times, or equal) - Igor Ceban

1 answer 1

 private static IEnumerable<string> GetAllMatches(char[] chars, int length) { int[] indexes = new int[length]; char[] current = new char[length]; for (int i=0; i < length; i++) { current[i] = chars[0]; } do { yield return new string(current); } while (Increment(indexes, current, chars)); } private static bool Increment(int[] indexes, char[] current, char[] chars) { int position = indexes.Length-1; while (position >= 0) { indexes[position]++; if (indexes[position] < chars.Length) { current[position] = chars[indexes[position]]; return true; } indexes[position] = 0; current[position] = chars[0]; position--; } return false; }