Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Extracting information from lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63356] Re: Extracting information from lists
  • From: "dkr" <dkrjeg at adelphia.net>
  • Date: Sun, 25 Dec 2005 02:19:34 -0500 (EST)
  • References: <dojefh$fmb$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Tony,

The following extracts the positions of the first elements of runs of
all integers, provided the integer has at least one run whose length
exceeds one.  Thus if

In[1]:= egList={1,1,2,2,2,1,2,3,1,1,1,3,2,2};

the extracted positions will be {1,3,6,7,9,13}, corresponding to the
beginning of the runs of 1 and 2.

In[2]:=
s1={First[#],Length[#]}&/@Split[egList]
Out[2]=
{{1,2},{2,3},{1,1},{2,1},{3,1},{1,3},{3,1},{2,2}}

The first element of each sublist gives the integer involved in the
run, and the second element gives the length of the run.

In[3]:=
s2=FoldList[#1+Last@#2&,1,Most@s1]
Out[3]=
{1,3,6,7,8,9,12,13}

s2 gives the positions of the first elements of all runs, including
those corresponding to integers (in this case the integer 3) which
don't have any runs exceeding one in length.

In[6]:=
s3=Cases[s1,{a_,b_Integer}/;b>1:>a]//Union
Out[6]=
{1,2}

s3 contains each integer which has a maximal run exceeding one in
length.

In[8]:=
Pick[s2,s1,{a_Integer,_}/;MemberQ[s3,a]]
Out[8]=
{1,3,6,7,9,13}

Finally we pick only those positions in s2 which correpond to integers
having a maximal run exceeding one in length.

Putting it all together:

In[9]:=
fn[x:{__Integer}]:=Module[{s1,s2,s3},
      s1={First[#],Length[#]}&/@Split[x];
      s2=FoldList[#1+Last@#2&,1,Most@s1];
      s3=Cases[s1,{a_,b_Integer}/;b>1:>a]//Union;
      Pick[s2,s1,{a_Integer,_}/;MemberQ[s3,a]]
      ];

In[10]:=
fn[egList]
Out[10]=
{1,3,6,7,9,13}
In[11]:=
fn[{7,9,1,6,8,1,1,6,5,1,1,1,8,7,6,1,1,1,1,7}]
Out[11]=
{3,6,10,16}
In[12]:=
fn[{1,2,3}]
Out[12]=
{}

dkr


  • Prev by Date: Re: Extracting information from lists
  • Next by Date: Re: Extracting information from lists
  • Previous by thread: Re: Extracting information from lists
  • Next by thread: Re: Extracting information from lists