Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

hmmm

Name: Anonymous 2007-12-23 9:33

Hi, I've been trying to come up with a way to generate all combinations of the letters in a word. I've looked up anagrams solvers but most just compare the number of letters in each. I'll I can think is that it has to do something with factorials.

abcd
abdc

acbd
acdb

adbc
adcb

bacd
I've been trying stuff like this but realized 2 nested loops were only 16 and not 24 combinations. Any links, suggestions, or code would be helpful thanks.


char[] anagram = {'a','b','c','d'};
for (int y = 4; y > 0; y--)
{
    for (int x = 3; x > 0; x--)
    {
        switchChars(anagram, x, y);
        c++;
        Console.WriteLine(anagram);
    }
}

Name: Anonymous 2007-12-23 9:35

[(a,b) | a <- ... , b <- ...]


HASKELL

Name: Anonymous 2007-12-23 9:46

Are you looking for permutations or combinations?

Name: Anonymous 2007-12-23 10:49

IDIOTS ALL OF YOU

Name: Anonymous 2007-12-23 10:54

Permutation plox :o

Name: Anonymous 2007-12-23 11:04

permute [] = [[]]
permute xs = [y : ps | (y,ys) <- (selections xs), ps <- (permute ys)]
  where
  selections [] = []
  selections (x':xs') = (x',xs') : [(y',x':ys') | (y',ys') <- (selections xs')]

Name: Anonymous 2007-12-23 11:08

Thanks anons, I'm looking up Haskell right now.

Name: Anonymous 2007-12-23 11:12

>>7
Read SICP instead and you won't have to generate all the permutations.

Name: Anonymous 2007-12-23 11:26


perms [] = [[]]
perms xs = [y : ys | y <- xs, ys <- perms (delete y xs)]

Name: Anonymous 2007-12-23 12:30

Take a look at the source to 'an'.

Name: Anonymous 2007-12-23 14:17

The algorithm is described by Kenneth H. Rosen, Discrete Mathematics and Its Applications, 2nd edition (NY: McGraw-Hill, 1991), pp. 282-284.

Name: Anonymous 2007-12-23 23:57

typedef std::vector< std::string > StringList;
typedef std::vector< char > CharList;

void permute( CharList left, StringList& out, std::string work = "" ) {
        if ( left.size() == 0 ) {
                out.push_back( work );
        }
        else {
                for ( int i = 0; i < left.size(); ++i ) {
                        CharList tmp = left;
                        std::string newWork = work + left[i];
                        tmp.erase( std::find( tmp.begin(), tmp.end(), tmp[i] ) )
;
                        permute( tmp, newWork, out );
                }
        }
}


LOL SEPPLES!!!!

Name: Anonymous 2009-03-18 2:48

I feel the need, the need for weed!

Marijuana MUST be legalized.

Don't change these.
Name: Email:
Entire Thread Thread List