# Need a function that re-arranges words(!)

Geoff Canyon gcanyon at inspiredlogic.com
Wed Nov 26 01:10:41 CST 2003

```How many words do you want to handle? You say *any* but the number of
permutations increases very quickly, so you'd have to come up with a
different way of solving it if you wanted to handle 100 words, for
example. If N is the number of words, the number of strings to generate
is

N!*2^(N-1)

So 2 words have 4 arrangements, 3 words have 24 arrangements (as shown
below), 4 words have 192 arrangements...10 words have 1,857,945,600
permutations, etc.

For rearranging the words, a recursive function would work nicely. For
selectively pulling out the spaces my first guess would be to count
from 0 to 2^(N-1) -1, convert each number to binary using baseConvert,
and use the 1s and 0s of the resulting string as a guide to inserting a
space or not. As it turned out, it was better to count from 2^(N-1) to
2^N - 1 and then pull the leading "1" in order to get the leading
zeroes.

This will do the trick, but will quickly choke out on longer strings.
Try it with "test this code" or "this is a test" or "fuzzy wuzzy was a
bear" but save your work before attempting "peter piper picked a peck
of pickled peppers" -- it would produce a string over 200MB in size

local sResultList

on rearrangeWords pString,pWordList
if the number of words of pWordList is 1 then
put enumerateSpaces(pString && pWordList) after sResultList
exit rearrangeWords
end if
repeat with i = 1 to the number of words in pWordList
put pWordList into tWordList
delete word i of tWordList
rearrangeWords pString && word i of pWordList,tWordList
end repeat
end rearrangeWords

function enumerateSpaces pString
put empty into tReturn
put the number of words in pString into tWC
repeat with i = 2^(tWC -1) to 2^(tWC)-1
put char 2 to -1 of baseConvert(i,10,2) into tBitString
put 0 into tCounter
repeat for each word W in pString
if tCounter is tWC then
put W & cr after tReturn
else
if char tCounter of tBitString is 1 then
put W & space after tReturn
else
put W after tReturn
end if
end if
end repeat
end repeat
return tReturn
end enumerateSpaces

regards,

Geoff Canyon
gcanyon at inspiredlogic.com

On Nov 24, 2003, at 3:32 PM, valetia at mac.com wrote:

> Hi all,
>
> Help! :) I've been trying to figure this out but it's been a mess, even
> though I think it's probably an easy one.
>
> What I need is rev code that can do this:
>
> Given any phrase, e.g.: "my fat cat"
>
> Spit out all of the following (no need to be in this order):
>
> my fat cat
> my cat fat
> fat my cat
> fat cat my
> cat my fat
> cat fat my
>
> myfat cat
> my fatcat
> myfatcat
>
> mycat fat
> my catfat
> mycatfat
>
> fatmy cat
> fat mycat
> fatmycat
>
> fatcat my
> fat catmy
> fatcatmy
>
> catmy fat
> cat myfat
> catmyfat
>
> catfat my
> cat fatmy
> catfatmy
>
>
> Conditions:
>
> 1. The input phrase can be *any* number of words (not just 3).
>
> 2. Each line of the results must contain all the words within (no
> missing
> words).
>
> Any takers? Thanks in advance! :)
>
> Valetia
>
>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>

```