MathGroup Archive 2005

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

Search the Archive

Re:Re: Set of strings reducing problem

  • To: mathgroup at
  • Subject: [mg58594] Re:[mg58584] Re: Set of strings reducing problem
  • From: "Edson Ferreira" <edsferr at>
  • Date: Sat, 9 Jul 2005 04:07:54 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

> compareString compares two strings and generates the desired string if
> there is a single mismatch.  Otherwise it generates {}.
> In[1]:=
> compareStrings[a_String,a_String]:={};
> compareStrings[str1_String,str2_String]/;Equal[Length[str1],Length[str2]]:=
>     Module[{pos,chars1,chars2,thr,rules},
>       rules={{"1","X"}|{"X","1"}:>"D", {"1","2"}|{"2","1"} :>
> "M",{"1","U"}|{"U","1"}|{"M","X"}|{"X","M"}|{"2","D"}|{"D","2"}:>
>             "T",{"2","X"}|{"X","2"}:>"U"};
>       chars1=Characters[str1]; chars2=Characters[str2];
>       thr=Thread[{chars1,chars2}];
>       pos=Position[thr,{a_,b_}/;b=!=a,{1},2];
>         If[ Equal[Length[pos],2],{},
> StringJoin[ReplacePart[chars1,Extract[thr,First[pos]]/.rules,pos]]]];
> Note the last argument of Position.  There is no need to search beyond
> the second mismatch.
> In[3]:=
> compareStrings["11112111","1111X111"]
> Out[3]=
> 1111U111
> compareStrings["11112111","1111X121"]
> Out[4]=
> {}
> It wasn't exactly clear what you meant by taking all transformations.
> If you want to look at all possible pairs of the original strings,
> generating a new string from a pair only if there is a single mismatch,
> and then looking at the collection of all the generated strings, you
> can:
> In[12]:=
> genStrings[a_List]:=Flatten[compareStrings[#[[1]],#[[2]]]&/@Subsets[a,{2}]];
> In[14]:=
> origList={"11112111","1111X111","11111111","11112111","21122211","D1122211"};
> In[15]:=
> genStrings[origList]
> Out[15]=
> {1111U111,1111M111,1111D111,1111U111,1111M111,T1122211}

From your example at the end I have the following observations:

- You gave at origList a set with two equal strings. This does not happen. The strings are all unique.

- The different character, as you noticed, can ocurr at any position.

- The length of all strings is the same.

- In your example the expected result is:

The reason is because all I want is a reduced set of string, thus when you compare two strings and you find they differ by only one character, you ELIMINATE the original ones and ADD the NEW one to the original set.

For instance : {"T11TTTTT"} would be generated from a set with 729 unique strings !!!!!

I would be glad to get the shortest "joined" elements list.

I would like to thank all mathematica users for the great support!!


Edson Ferreira
UOL Fone: Fale com o Brasil e o Mundo com até 90% de economia.

  • Prev by Date: Re: GroebnerBasis (was Re: Documentation)
  • Next by Date: Re: Set of strings reducing problem
  • Previous by thread: Re: Set of strings reducing problem
  • Next by thread: Fw: Re: Set of strings reducing problem