Re: How Do You Reduce Multiple Elements in a List?
- To: mathgroup at smc.vnet.net
- Subject: [mg33879] Re: How Do You Reduce Multiple Elements in a List?
- From: Tim Brophy <timbrophy at eircom.net>
- Date: Sun, 21 Apr 2002 06:12:29 -0400 (EDT)
- References: <a9r3ve$ltc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Bob, This is a problem given at the 1990 programming competition at the Mathematica conference. The following code, taken from Ilan Vardi's book "Computational Recreations in Mathematica" will work: In[1]:=Attributes[times]={Flat} General::spell1: Possible spelling error: new symbol name "times" is similar to existing symbol "Times". Out[1] = {Flat} In[2]:= times[{a_,m_},{a_,n_}]:=times[{a,m + n}] In[3]:= RunEncode[x_List]:= List @@ times @@ ({#,1}& /@ x) In[4]:= RunEncode[{1,1,1,1,1,2,2,2,4,4,4,4,4,4}] Out[4] = {{1,5},{2,3},{4,6}} On 4/20/02 9:05 AM, in article a9r3ve$ltc$1 at smc.vnet.net, "Bob Harris" <nitlion at mindspring.com> wrote: > Howdy, > > This seems like such a simple thing, yet I've search through the big book > trying to figure it out. Perhaps someone can help me... > > Given a list of integers that generally contains the same number many times, > I'd like to reduce it to a list that just gives me the number/count pairs. > For example, I'd like to convert the list > {1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 4, 4, 4} > into > {{1,5}, {2,3}, (4,6}} > > Is there a built-in function to do this? Seems like a histogramming > operation, but I get no useful matches from ?*Hist*. > > Thanks, > Bob H > >