1

I've got a string ABC and I'm trying to get all forms of it using recursion. For example, my goal is to make the output look like this:

A
B
C
AB
AC
BC

Currently, I'm experiencing a problem and I can't seem figure out why it's doing it. When I step through my code and get to return temp it then goes back up to Passwords(word.Substring(start + 1, end - 1), start + 1, end); even though the function has already completed. When it does this it removes any elements contained with my list.

class Program
    {
        static void Main(string[] args)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\words.txt";
            string text = "abc";
            List<string> passwords = Passwords(text, 0, text.Length);

            foreach (string password in passwords)
            {
                using (StreamWriter writer = new StreamWriter(path))
                {
                    Console.WriteLine(password);
                    writer.WriteLine(password);
                }
            }
        }

        public static List<string> Passwords(string word, int start, int end)
        {
            List<string> temp = new List<string>();
            if (start == end)
            {
                temp.Add(word);
            }

            else if (word.Length == 2)
            {
                char[] input = word.ToCharArray();
                string letter1 = input[0].ToString();
                string letter2 = input[1].ToString();
                string s = letter2 + letter1;
                temp.Add(s);
            }

            else
            {
                if (start < end)
                {
                    Passwords(word.Substring(start + 1, end - 1), start + 1, end);
                }
            }

            return temp;
        }
    }

Can anyone help point me in the right direction as to what I'm doing wrong?

Maria Ines Parnisari
  • 14,229
  • 7
  • 73
  • 112
pallasmedia
  • 1,339
  • 4
  • 24
  • 54
  • 1
    there should be many "how to generate all permutations of XXXXX" questions... Here are some [non-recursive](http://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently)... and [FAQ:C# Algorithm that Re-arranges Chars in a String](http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n). – Alexei Levenkov Jan 19 '14 at 05:44

3 Answers3

3

Main problem I can see is with your recursive call: You're not storing results from this call at all! You should probably add them to temp list:

temp.AddRange(Passwords(word.Substring(start + 1, end - 1), start + 1, end));

But the entire code looks messed up, so I'm not sure it will make it work, because it may not be the only problem.

MarcinJuraszek
  • 118,129
  • 14
  • 170
  • 241
  • +1... also it looks like there should be more than just adding result of the call (missing prefix). – Alexei Levenkov Jan 19 '14 at 05:51
  • 1
    @AlexeiLevenkov I'm pretty sure the entire algorithm is incorrect. I don't think you should increment `start`/`end` when you already send shrinked `word`! But as you said in your comment, there are tons of questions about that algorithmic problem, so I'm not going to dig deeper. – MarcinJuraszek Jan 19 '14 at 05:53
3

It sounds like you want every combination of dropping characters from your source text. You don't want to re-arrange, just return all the in-order subsets.

If so, this might do what you want:

public IEnumerable<string> GetAllInstrings(string text)
{
    yield return text.Substring(0, 1);
    if (text.Length > 1)
    {
        foreach (var element in GetAllInstrings(text.Substring(1)))
        {
            yield return element;
            yield return text.Substring(0, 1) + element;
        }
    }
}

I get the following results from "ABC".

A 
B 
AB 
C 
AC 
BC 
ABC 
Enigmativity
  • 97,521
  • 11
  • 78
  • 153
0

there are many examples on how to do that .

I would consider checking those out and refactor you code since I think it need some changes (like @Alexei Levenkov said - you have problems with the return values).

you can check this example how to do that, and good explanation here

Community
  • 1
  • 1
Mzf
  • 5,014
  • 2
  • 21
  • 36