Scramble...

1. tt
Member
Join Date
Dec 1969
Posts
46

## Scramble...

Hi!<BR><BR>I´ve asked this before and really wants an answer...:-)<BR><BR>Has anyone a function that returns all distinct combination of a word sent to it?<BR><BR>Ie. Feed function scramble with "goo" and it will return:<BR>goo<BR>ogo<BR>oog<BR>Nothing more cause every distinct combination is "filled/taken"<BR>But if You feed it with "abc" then it would return:<BR>abc<BR>bac<BR>bca<BR>cba<BR>cab<BR>...< BR><BR>Ie: return all distinct combination of a single world that You feed it with!<BR><BR>What i need is a function that grab the input word (one word) and then scramble it into all of it´s combinations.<BR><BR>My solution so far is really bad because it´s like "bruteForce" and will give me too many answers if I feed it with "aaaa" when the solution of that just would be a single "aaaa"!<BR><BR>Please give me an smart idea!<BR><BR>Best // TT

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ehhhh...probably not too hard...

Let&#039;s see...<BR><BR>First, sort the letters of the input word into order.<BR><BR>So if you give it "feed" it sorts to "deef".<BR><BR>Now as you produce combos, it&#039;s easy to check if you are about to reuse the same letter.<BR><BR>But having said that...<BR><BR>I&#039;d bet that the brute force method is easier and, in the long run, just as fast.<BR><BR>You just create all possible arrangements and put them in alphabetical order [easy to do if you first sort the letters, as above...should fall out automatically!] and then it&#039;s trivial to eliminate duplicate words, as they&#039;ll be adjacent in the array.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Of course though

before you do anything, you have to determine there are more there just the same char in the string.....<BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well, you don't HAVE to...

...but if it occurs often enough to make the test worthwhile, then yeah.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Well...if you dont

then like tt mentioned you will get multiple conbinations of the same string<BR><BR><BR>aaaa will only return aaaa...why do anything for that....<BR><BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## If the letters are chosen randomly...

...then you will only get 4 repeated letters about 1 time in a thousand if I did the math right. If the overhead of checking for 4 repeated is more than 1/10th of 1% of the total time, then it&#039;s not worth doing.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Math was faulty...

4 repeated letters happens randomly only 0.005% of the time. 5 times out of 100,000.<BR><BR>So if the overhead of making the check exceeds 0.005% of the time to do the generation, it&#039;s not worth it.<BR><BR>But, of course, this assumes random combinations.<BR><BR>

8. tt
Member
Join Date
Dec 1969
Posts
46

## RE: Ehhhh...probably not too hard...

Hi Bill!<BR>Long time no see!<BR><BR>What I was asking for was a function that was asking itselves for every distinct combination of a given word I suggest.<BR><BR>The word You feed the function with is more importent then the spelling ...<BR><BR>Ie. AAAAAAAP gives just a few combinations when ABCDEFGF wou´d give You a lot more combinations!<BR><BR>What I mean is not to feed the function with a row of signs and then get the amount of combinations of the array of them... Just get all distinctint combinations of the input if You understand my bad englingsh/swedish!<BR><BR>Best // TT

9. tt
Member
Join Date
Dec 1969
Posts
46

## RE: Scramble...

More explanisations...<BR><BR>Nr 1 is "Blue"<BR>Nr 2 is "Red"<BR>Nr 3 is "Red"<BR>Nr 4 is "Yellow"<BR><BR>What I need is all distinct combinations of them and if I feed some scramble functin with more colors the it woul´d undaerstand it!<BR><BR>Thanks in advance // TT

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## But have you considered...

...*HOW MANY* combinations you will get???<BR><BR>ABCD would give you 24 combinations. Not bad.<BR><BR>ABCDEFG would give you 5040 combinations! What are you going to *DO* with that many combinations????<BR><BR>As for the right answer: It depends on HOW LIKELY you are to give a "word" with duplicated letters to the function. That is, what percent of the time will there be two duplicated letters? What percent of the time will there be three duplicated letters? etc.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•