Generation of the row from the combination of symbols (Brute Force) - C#


  • QA Engineer

    It is necessary to generate (not at home) a line from a combination of the symbols transferred.

    //symbols = [A-Z]+[a-z]+[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
    ab,aaa - not generated



  • 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;
    }
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2