2

I am trying to build a app that can scramble the input letters. I have found code samples that can rearrange:

abc into cba, acb etc.

What I am trying to do though is the above, but also being able to output shorter combinations using only the letters inputted.

So my desired app would be able to sort abc into a, bc, acb etc.

I realise this might require some sort of algorithm or but I haven't been able to find anything related on the web.

Thanks!

Bali C
  • 28,049
  • 34
  • 109
  • 147
  • Should that be `a`, `bc`, `acb` (not `acd`, since d isn't not in the sequence)? – George Duckett Mar 08 '12 at 15:01
  • 3
    Note that `cba` and `acb` are the same *combination*, but are a different *permutation*. – Bart Kiers Mar 08 '12 at 15:02
  • @GeorgeDuckett Yeah, typo, thanks. – Bali C Mar 08 '12 at 15:03
  • I think that your first scramble method only needs a small extension, if you mean to scramble and give back a random input. The most easiest and simplest way, would be to do a 'substring' on your output string. This substring would be a random number then, and you could even extend it a little more to make 2 random numbers. As in. declare Random no. 1 (Random.Next(Input.Count)), declare random no 2. (make sure the Max is not higher than Input.Count - Random no 1. Input.SubString(RandomNo1, RandomNo2). It's just an idea, maybe it helps :) – Honnes Mar 08 '12 at 15:03
  • [Permutations, Combinations, and Variations using C# Generics](http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G) – L.B Mar 08 '12 at 15:11

4 Answers4

2

You need to use the concept of "combinations" in combinatorics - it combines permutations with selections of subsets:

Algorithm to return all combinations of k elements from n

Community
  • 1
  • 1
eouw0o83hf
  • 8,640
  • 3
  • 51
  • 66
0

Are you willing to do this in php? If so - the

 array_rand ($array, $num);

function would work great. It's first argument is the array in question: a. make an array of A-Z the second argument is how many 'letters' or array components to choose b. use:

 rand($min, $max);

to generate a random number between 1 and 26 (characters in the alphabet). c. Loop this function as many times as you'd like.

AshBrad
  • 472
  • 2
  • 15
0

If you know how to get all combinations of all available letters, then just add the space character to the list of possible characters and get all combinations, trimming anything to the left of and including the space (and ignore the empty cases).

For example, for the word IF, you have 'IF' and 'FI'. If you treat space as possible you have

' IF', ' FI', 'I F', 'F I', 'IF ', 'FI '

which, trimming everything left of and including the space, becomes

'IF', 'FI', 'F', 'I', '', ''

Ignoring the empty cases, those are your possible combinations including shorter words.

JTeagle
  • 2,076
  • 14
  • 15
  • I like that, it's quite nifty, I will definitely consider that if nothing else is easier. – Bali C Mar 08 '12 at 15:11
  • If you know how to get all **permutations** of all available letters is what you mean. Also, there's no reason to add a space and trim; there are plenty of combinatorics algorithms that do this far more efficiently: adding that space to your word increases runtime *drastically*, since generation of permutations is an O(n!) operation – eouw0o83hf Mar 08 '12 at 15:22
  • One weakness of this algorithm is that some combinations can occur multiple times. For instance with 'ABC ', 'A' will appear twice, since 'A BC' and 'A CB' is generated. Instead, I suggest generating all permutations of each unique (multi)subset of the given letters. – Anonym Mus Mar 08 '12 at 16:05
  • @stubbscroll - My answer should have said 'trim *away* anything to the left...' - bad wording on my part. I knew what I meant {:v) – JTeagle Mar 08 '12 at 16:18
-2

Do this until string has looped through all it's indexes string at index equal wanted number modulo number base plus character set code wanted number equals wanted number divided by number base continue.

Jesse
  • 8,035
  • 7
  • 42
  • 56
  • Please don't use that kind of language, but thanks for at least trying to at least answer the question. =) – Jesse Apr 13 '13 at 13:58
  • "Generate all combination of letters". Unfortunately i did answer the right question. This algorithm works perfectly for me. I a sorry for the rude language, but i got pissed when i wasn't allowed to post code. (I later figured out that by preceeding code with 4 spaces allows for posting the code) – ausercomment Apr 17 '13 at 12:25
  • It happens. When things don't work, just take a deep breath and drink a glass of water - it helps immensely! =) – Jesse Apr 17 '13 at 12:29