MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Permutations


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"]]



To swap all pairs we can use

RegularExpression["(\w)(\w)"] :> \
"$2" ~~ "$1"]]



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