Re: Word permutations - frustrated by lists.
- To: mathgroup at smc.vnet.net
- Subject: [mg71720] Re: Word permutations - frustrated by lists.
- From: Peter Pein <petsie at dordos.net>
- Date: Mon, 27 Nov 2006 04:04:53 -0500 (EST)
- References: <ekbrks$jga$1@smc.vnet.net>
wooks schrieb:
> Hello. Am relatively new with Mathematica.
>
> I seem to have a general problem with results that look right but for
> embedded lists. Here is an example of my problem. This is supposed to
> be a algorithm to generate all permutations of a string. T
>
> Clear[arrangements]
> arrangements[str_String /; StringLength[str] == 1] := {str};
> arrangements[str_String] := (Prepend[arrangements[StringJoin[
> Rest[#]]]], First[#]) & /@
> NestList[RotateRight, Characters[str], StringLength[str] - 1];
> arrangements["dear"]
>
> PS This is not homework. I'm rewriting some examples from another
> programming book as as practice for Mathematica.
>
What about
In[1]:= Apply[StringJoin, Permutations[Characters["dear"]], {1}]
Out[1]=
{"dear", "dera", "daer", "dare", "drea", "drae", "edar", "edra", "eadr",
"eard", "erda", "erad", "ader", "adre", "aedr", "aerd", "arde", "ared",
"rdea", "rdae", "reda", "read", "rade", "raed"}
?