       Re: Permutations

• To: mathgroup at smc.vnet.net
• Subject: [mg65580] Re: Permutations
• From: bghiggins at ucdavis.edu
• Date: Sun, 9 Apr 2006 04:32:05 -0400 (EDT)
• References: <e12s2k\$j79\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Peter,

Here is a fun way of doing it using regular expressions (new in 5.1).
Suppose we have a list of all the characters in the alphabet written as
a string , and we want to swap all pairs except h and r. Then we can
use a Regex expression with StringReplace:

StringReplace[str, RegularExpression["([^h,^r])(\w)"] :> "\$2" ~~ "\$1"]

Here is a specific example

char = CharacterRange["a", "z"]

myfunc[int1_, int2_] := Characters[StringReplace[StringJoin[char], \
RegularExpression["([^" <> ToString[char[[int1]]] <>
",^" <> ToString[char[[int2]]] <> "])(\w)"] :> "\$2" ~~ "\$1"]]

myfunc[7,20]

{b,a,d,c,f,e,g,i,h,k,j,m,l,o,n,q,p,s,r,t,v,u,x,w,z,y}

To swap all pairs we can use

Characters[StringReplace[StringJoin[char],
RegularExpression["(\w)(\w)"] :> \
"\$2" ~~ "\$1"]]

Cheers,

Brian

```

• Prev by Date: Re: FindMinimum during NMimimize
• Next by Date: Joint Entropy
• Previous by thread: Re: Permutations