       Re: position of letters in a string

• To: mathgroup at smc.vnet.net
• Subject: [mg53838] Re: [mg53803] position of letters in a string
• From: János <janos.lobb at yale.edu>
• Date: Sat, 29 Jan 2005 06:02:46 -0500 (EST)
• References: <200501280743.CAA00311@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```On Jan 28, 2005, at 2:43 AM, zak wrote:

> in the list of strings:
> lst={"catcatz","zrzttys"}
> i want some procedure to give the letters in every string a number
> depending on its position and discarding the recurrent letters such
> that:
> first string: c=1; a=2; t=3; z=4
> second string: z=1; r=2; t=3; y=4; s=5
> zak
>
In the Help of Union there is an UnsortedUnion function. I use that one
here.

In:=
lst = {"catcatz", "zrzttys"}
Out=
{"catcatz", "zrzttys"}

In:=
UnsortedUnion[x_] :=
Module[{f},
f[y_] := (f[y] = Sequence[
]; y); f /@ x]; In:=
lst1 = (UnsortedUnion[
Characters[#1]] & ) /@
lst
Out=
{{"c", "a", "t", "z"},
{"z", "r", "t", "y", "s"}}

In:=
pairedLst = First[
Last[Reap[While[lst1 =!=
{}, current =
First[lst1];
lst1 = Rest[lst1];
Sow[({#1, Flatten[
Position[current,
#1]]} & ) /@
current]; ]]]]
Out=
{{{"c", {1}}, {"a", {2}},
{"t", {3}}, {"z", {4}}},
{{"z", {1}}, {"r", {2}},
{"t", {3}}, {"y", {4}},
{"s", {5}}}}

I am sure there are shorter versions. Flatten is not needed if you need
their Positions right away.

János

----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)

```

• Prev by Date: Re: Solving matrix equation
• Next by Date: Re: Integrate a Piecewise funition, stange behaviour
• Previous by thread: position of letters in a string
• Next by thread: Re: position of letters in a string