3

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#?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • possible duplicate of [Creating a power set of a Sequence](http://stackoverflow.com/questions/19890781/creating-a-power-set-of-a-sequence) – Servy Nov 21 '13 at 19:23
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 21 '13 at 19:24
  • @Servy: I'm not convinced that is the topic of this question. After reading the question text, it seems to me like the question is more about how to translate something from Python into C#, irrespective of what it does. – O. R. Mapper Nov 21 '13 at 19:40

3 Answers3

5

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 )
               ;
    }
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
3

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

gpmurthy
  • 2,397
  • 19
  • 21
  • 1
    Thanks for pointing it out...Updated the code above and added the screenshot of the output – gpmurthy Nov 21 '13 at 19:34
  • Excuse me I checked out the solution however when testing on "LEQN" I get `E Q N L LE NL EL QL NLE ELQ LEQ QLE NLEQ` which is almost correct because EL and LE would be considered the same, also I am missing NQ, and LNQ... also LEQ and ELQ would be considered the same, could you please help me? – edgarmtze Nov 21 '13 at 23:56
  • Gotcha. I think I understand better. Updated the code and screen shot. Let me know if that works for you. – gpmurthy Nov 22 '13 at 00:17
1

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);
        }
    }
Sourav 'Abhi' Mitra
  • 2,390
  • 16
  • 15