edgarmtze
edgarmtze

Reputation: 25038

Get special combinations of string

Using some code in python that outputs special combinations of string in order: How to do that on c#

def getOrderCombinations(me):
    l = len(me)
    looped = me+ me
    for start in range(0, l):
        for length in range(1, l):
            print(looped[start:start+length])
which gives:

>>> getOrderCombinations("ABCD")
A
AB
ABC
B
BC
BCD
C
CD
CDA
D
DA
DAB

I was trying

public static string[] Combinations(string str)
{
    if (str.Length == 1)    
        return new string[] { str };
    char c = str[str.Length - 1];
    //here I was planning to make recursion 
    string[] returnArray = Combinations(str.Substring(0, str.Length - 1));
    // keep final string combinations
    List<string> finalArray = new List<string>();

    //how to loop correctly and avoid getting all combinations
    foreach (string s in returnArray)
        finalArray.Add(s);
    finalArray.Add(c.ToString());
}

for string 'ABCD', output should be 'A', 'B', 'C', 'D', 'AB', 'BC', 'CD', 'DA', 'ABC', 'BCD', 'CDA', DAB'. Thus, the amount of possible substrings of string length n will always be n*(n-1).

How to do it on c#?

Upvotes: 3

Views: 528

Answers (3)

gpmurthy
gpmurthy

Reputation: 2427

EDIT: Consider the following Code...

string str = "LEQN";
List<char> characters = str.ToCharArray().ToList();
List<string> combinations = new List<string>();
for (int i = 0; i < characters.Count; i++)
{
    int combCount = 1;
    string comb = characters[i].ToString();
    combinations.Add(comb);
    for (int j = 1; j < characters.Count - 1; j++)
    {
        int k = i + j;
        if (k >= characters.Count)
        {
            k = k - characters.Count;
        }
        comb += characters[k];
        combinations.Add(comb);
        combCount++;
    }
}

Good Luck!

Here is the output from the above code...

enter image description here

Upvotes: 3

This should work: Call -> GetCombinations("ABCD");

    List<string> allcombinations = new List<string>();

    public void GetCombinations(string input)
    {
        GetCombinations(1, input);
    }
    private void GetCombinations(int combLength, string input)
    {
        string current = string.Empty;
        for (int index = 0; index < input.Length; ++index)
        {
            if (index + combLength <= input.Length)
            {
                current = input.Substring(index, combLength);
                allcombinations.Add(current);
            }
            else
            {
                int leftOver = input.Length - index;
                current = input.Substring(index, leftOver);
                current += input.Substring(0, combLength - leftOver);
                allcombinations.Add(current);
            }
        }
        if (combLength < input.Length - 1)
        {
            GetCombinations(++combLength, input);
        }
    }

Upvotes: 1

Nicholas Carey
Nicholas Carey

Reputation: 74177

What's wrong with the way it's implemented in python? A direct port should work pretty well, I'd think:

public IEnumerable<string> GetOrderCombinations(string me)
{
  int    l      = me.Length ;
  string looped = me + me   ;

  for ( int start = 0 ; start < l ; ++start )
  {
    for ( int length = 1 ; length < l ; ++length )
    {
      yield return looped.Substring( start , length ) ;
    }
  }
}

You could even make it a Linq one-liner, pretty much a direct port of the Python implementation:

    public static IEnumerable<string> GetOrderCombinations( string me )
    {
        string looped = me + me;
        return Enumerable
               .Range(0,me.Length)
               .SelectMany( x => Enumerable.Range(1,me.Length) , looped.Substring )
               ;
    }

Upvotes: 5

Related Questions