1

(C++! I didn't know if I should mention it or not) (Keep the order if at all possible!)

Say I have, and I do, the string ABaC.

I have each character in that string in a vector called temp.

So I have temp[0] = A, temp[1] = B, temp[3] = a, temp[4] = C.

What I would like to do is right a program that outputs every permutation of that string, that results from removing 0 capital letters, then 1 capital letter, then two capital letters, then all 3.

The reason I'm removing capitals is... you shouldn't focus on capitals. It happened that I needed to remove all the capitals here but , for let's say ADbd, I don't need to remove D. So really, an algorithm for removing a set of already known characters from a string.

So it would output:

ABaC|BaC|AaC|ABa|aC|Aa|Ba|a

Not looking for efficiency here or too brilliant of an algorithm. Something simple and long or short and stupid I am more than happy with as well.

This is part of an ongoing project i'm working on that removes lambda productions (which you fine folk helped me with already) So this is the step where I need to construct the new production rules by removing the nullable variables from the rule, one by one, and so on and output every permutation.

But, y'all can ignore that bit. Think of it just as the string. So, any help is greatly appreciated.

Thank ya kindly.

neojb1989
  • 181
  • 1
  • 3
  • 12

3 Answers3

2

I'm not sure what language you're working with, so I've provided the high-level steps below.

  1. Walk through your string and create a set capitalIndexes of all the indicies at which capital letters exist.
  2. For each set s in the powerset of capitalIndexes, print out each character of the string except for the those located at an index in s, then print \n.

The only complicated bit here is generating the powerset; here is another answer that provides a means of doing this in C++.

Community
  • 1
  • 1
cheeken
  • 33,663
  • 4
  • 35
  • 42
  • C++! I didn't know if I should include the language or not, I'll do that in tags, then read your comment. – neojb1989 Dec 01 '12 at 02:15
  • 2
    C++? My condolences! I've added a link to my answer that may be of use. – cheeken Dec 01 '12 at 02:20
  • Actually, I already have a part in my program that generates the power set of nullable variables i'm getting rid of :) – neojb1989 Dec 01 '12 at 02:21
  • Ahh but i reread your thing, yea generating that power set of the capitalindex is not something I have. I'll take a look at your link there. Do you think you could give me a little C++ code to get started on that step 2? I have a vector indices which now holds all of the nonterminals (this whole thing is based on grammars so they call them nonterminals) that are nullable (I'm building up to CNF and the first step is the removal of lambda productions and yada yada). So I need to generate than the powerset of my nullable variables? – neojb1989 Dec 01 '12 at 02:27
  • Sorry, I'm not a C++ dude. I was hoping the other answer would be enough. The powerset problem is a common one, though; I'm sure you can find copy-paste-able solution with some Googling if that other answer isn't of assistance. – cheeken Dec 01 '12 at 02:33
  • But just to clarify, i'm find the set of all subsets of my set of nullable variables right? That's what your step 2 says? – neojb1989 Dec 01 '12 at 02:34
  • Yes. By taking the powerset, you find every combination of "having" and "not having" for each item in the set, which is precisely what you're aiming to accomplish. – cheeken Dec 01 '12 at 02:43
  • Hey, I found a useful permutationizer thingy for ints! This is exactly what I needed so between you and this guy, I think I can get this done. http://stackoverflow.com/a/9253062/933194 – neojb1989 Dec 01 '12 at 03:42
2

Here you three capital letters and 8 solutions. That should give you an idea (2^3 = 8).

If you have n capital letters, loop through the numbers 0 -> 2^n - 1. For each number you use its binary representation to determine whether a capital letter is included or not.

000 -> a

001 -> aC

010 -> Ba

011 -> BaC

etc.

user515430
  • 3,341
  • 2
  • 17
  • 13
1

You didn't specified any language, so I write it in pseudo-language:

Function(string temp, int startindex):

output temp
for i = startindex to temp.length-1 {
  if temp[i] is capital {
    temp.remove(i)
    Function(temp.clone(), i)
  }
}

You start it with Function(temp, 0).

Note that it gives the result possibly in other order than you want. (Not sure from your question how much the order is important for you.)

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • Well I had used the word permutation because I believe with permutations, order matters right? I'm not sure. But I would prefer to keep the order the same. – neojb1989 Dec 01 '12 at 02:20
  • Is this a void function then? – neojb1989 Dec 01 '12 at 02:24
  • Yes, `output temp` adds an item to the output, so it can return void. Actually, you can replace the recursion by a queue, and then it will work in your wanted order. (This recursive algorithm is like using a stack. And you probably want queue to keep the right output order.) Also note that real *permutations* never remove any items. They just change the order of items. – Al Kepp Dec 01 '12 at 02:27
  • I had a difficult time finding the right words for the title so I went with permutations which my brain remembers has something to do with order, thank you. I'll give this the look over. – neojb1989 Dec 01 '12 at 02:30