Re: Pure Function for String Selection
- To: mathgroup at smc.vnet.net
- Subject: [mg60969] Re: Pure Function for String Selection
- From: Peter Pein <petsie at dordos.net>
- Date: Wed, 5 Oct 2005 02:28:15 -0400 (EDT)
- References: <dht479$hl1$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Edson Ferreira schrieb: > Dear members, > > I want to define a pure function to filter a set of strings. > > The strings that compose the set have all the same length and the only characters in these strings are "1", "X" and "2". > > The function that I want is like the one bellow: > > In[1]:= > Unprotect[D]; > In[2]:= > U={"2","X"}; > In[3]:= > M={"1","2"}; > In[4]:= > D={"1","X"}; > In[5]:= > T={"1","2","X"}; > In[6]:= > L=Flatten[Outer[StringJoin,T,T,T,D]]; > In[7]:= > L = Select[L, Count[Characters[#], "1"] > 1 &]; > > In this case, it counts the number of characters "1" in each string and select the ones that have more than one "1". > > I want a pure function, to be applied like the one in the example above, but for a different task. > > For each string, I want it to count the maximum number of repeated characters for each character. > > In other words, It must count the maximum number of repeated "1", "X" and "2" for each string. > > The string must be "selected" if: > > The longest run of repeated "1" is shorter than 8 characters > AND > The longest run of repeated "X" is shorter than 6 characters > AND > The longest run of repeated "2" is shorter than 6 characters > > For example: > "11112X122X1XXX" should be "selected" > (there are four "1" in sequence, 3 "X" in sequence and 2 "2" in sequence) > > "122XXXXXX222XX" should NOT be "selected" > (there are six "X" in sequence) > > "11111111222112" should NOT be "selected" > (there are 8 "1" in sequence) > > Thanks a lot !!!!! > > Edson Ferreira > > Something like this: In[1]:= Select[ {"11112X122X1XXX", "122XXXXXX222XX", "11111111222112"}, Function[s, And @@ Apply[ Max[Cases[Split[Characters[s]], sl:{#1..} :> Length[sl]] ] < #2 & , {{"1", 8}, {"X", 6}, {"2", 6}}, {1}]] ] Out[1]= {"11112X122X1XXX"} ? Peter