PDA

View Full Version : rearranging string builder arrays (C#)

12-20-06, 06:38 PM
is there a way i could get a program to arrange a string builder array into every possible combination in c# ?

thanks

12-20-06, 06:44 PM
or do i need a string builder, maybe just an array ?

cx323
12-20-06, 09:56 PM
what do you mean by combination?

12-21-06, 03:16 AM
for example the user enters: "cat"
the program would output:

cat
act
atc
tac
tca

every possible combination of those letters...

kayson
12-21-06, 12:12 PM
Not to be a nitpicker, but those are permutations. In combinations, order of the elements doesnt matter. And you missed "cta" :p As for the program, I imagine that you would use some sort of substring function to split the word into individual letters, then a bunch of loops to construct the various permutations. At the end you'd probably have to search through and eliminate any rendunancies since you can get the same permutation twice if you have more than one of a letter.

12-21-06, 03:59 PM
well i used the following to seperate the characters:

char[] characters = word.tochararray();

that seperates them into an array.

Midnight Dream
12-21-06, 07:28 PM
My thought would be do some sort of calculation based on the number of letters there are to figure the number of combinations there would be, then do a for loop.

12-21-06, 07:40 PM
My thought would be do some sort of calculation based on the number of letters there are to figure the number of combinations there would be, then do a for loop.

well ive sorted the calculation ou to workout how many there are for a given string, just got to work on ectually gettin the permutations.

Midnight Dream
12-21-06, 07:48 PM
Ok, you know how many combinations there are. Now do the calculations to figure out how many times you can move one letter to get a different combination. Then use your for loop to manipulate the letters. For example, say you have 9 combinations, and can move 1 letter 3 times before you get the same combination. When i is less than 3, move letter[0], when iis less than 6, move letter[1], etc etc.

There will be some variations and whatnot, due to the compensation of having 1 less combination per letter movement, but you get the idea.

12-21-06, 07:52 PM
cool thanks for the help ill try this tomorrow, im getting tired now lol... nearly 2am

thanks again

kayson
12-21-06, 10:56 PM
The number of combinations is n! where n is the length of the string. You couldn't just use a for loop. You'd need at least one for each spot in the word, and then you'd need to check all of your for loops to ensure that each letter is used only once and you don't have 2 for loops using the same letter. Here's a sample that will work with 3 letter words (in PHP, but syntax should be similar):

<?php

//Assuming you have split the word into the array \$word
\$word[0] = "a";
\$word[1] = "b";
\$word[2] = "c";

for ( \$i = 0; \$i < count(\$word); \$i++ )
{
\$example1 = \$word[\$i];
for ( \$j = 0; \$j < count(\$word); \$j++ )
{
if ( \$j != \$i ) //Make sure its not the same character as the one from first loop
{
\$example1 .= \$word[\$j];
}
for ( \$k = 0; \$k < count(\$word); \$k++ )
{
if (\$k != \$j && \$k != \$i )
{
\$example 1.= \$word[\$k];
}
}
}
}

It only works with 3 letter cuz theres only 3 words...

cx323
12-21-06, 11:07 PM
have a look at this link for a permutation class

12-22-06, 03:50 AM
have a look at this link for a permutation class

thnks for that

Midnight Dream
12-22-06, 11:21 AM
The number of combinations is n! where n is the length of the string. You couldn't just use a for loop. You'd need at least one for each spot in the word, and then you'd need to check all of your for loops to ensure that each letter is used only once and you don't have 2 for loops using the same letter. Here's a sample that will work with 3 letter words (in PHP, but syntax should be similar):

It only works with 3 letter cuz theres only 3 words...

The number of combinations is depenent upon if "cat" is the same as "tac".
cat, tca, atc, tac, act, cta