Re: RE: Re: question in mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg80216] Re: [mg80148] RE: [mg80143] Re: question in mathematica
- From: Carl Woll <carlw at wolfram.com>
- Date: Wed, 15 Aug 2007 04:11:35 -0400 (EDT)
- References: <fa11b990e477.46baf2d3@bgu.ac.il> <200708130842.EAA27991@smc.vnet.net> <200708141049.GAA08106@smc.vnet.net>
Harvey P. Dale wrote:
>Here's another way:
>
>
>
>maxlst[l_List] := Module[{l2 = l, i, len}, i = 2; len = Length[l2];
>While[i <= len, l2[[i]] = Max[l2[[i]], l2[[i-1]]]; i++];
>Transpose[Tally[l2]][[1]]]
>
>
A slight refinement of this idea for number data:
maxlst2[l_List] := Union[ FoldList[Max, First@l, Rest@l] ]
In my tests with long lists, maxlst2 is about an order of magnitude
faster. If the data can have stuff like E and Pi, then one should use
Tally[ ... ][[All,1]] instead of Union[ .. ].
Carl Woll
Wolfram Research
>
>
>ls={9,2,10,3,14,9}
>
>
>
>maxlst[ls]
>
>
>
>{9,10,14}
>
>
>
> Best,
>
>
>
> Harvey
>
>
>
>Harvey P. Dale
>
>University Professor of Philanthropy and the Law
>
>Director, National Center on Philanthropy and the Law
>
>New York University School of Law
>
>Room 206A
>
>110 West 3rd Street
>
>New York, N.Y. 10012-1074
>
>tel: 212-998-6161
>
>fax: 212-995-3149
>
>
>
>________________________________
>
>From: m.r at inbox.ru [mailto:m.r at inbox.ru]
>Sent: Monday, August 13, 2007 4:42 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg80148] [mg80143] Re: question in mathematica
>
>On Aug 11, 1:43 am, Andrzej Kozlowski <a... at mimuw.edu.pl> wrote:
>
>
>>First, please send such question to the MathGroup,
>>
>>mathgr... at smc.vnet.net
>>
>>not me personally. (I really have desire, tiem or ability to replace
>>the enitre MathGroup.)
>>So I have decided to post this question to the MathGroup in case
>>someone else finds it interesting.
>>
>>Also, there is something about this question and the earlier you sent
>>me that make sme suspicious. What do you say "you need to use
>>recursion and pattern matching, Select and Join"? This sounds to me
>>like some sort of test problem so I have decided to answer it but
>>without using any of these functions (although it may not be the
>>simplest way to do this). So here is my answer:
>>
>> ls = {9, 2, 10, 3, 14, 9};
>>
>>Reverse[Last[Last[Reap[NestWhile[With[{a = First[Ordering[#, -1]]},
>>Sow[#[[a]]]; Take[#, a - 1]] &,ls,Length[#] > 0 &]]]]]
>>
>>{9, 10, 14}
>>
>>On 10 Aug 2007, at 10:12, Ivan Egorov wrote:
>>
>>
>>
>>>I have one more question.
>>>
>>>
>>>Write a function maxima[lis_List] which, given a list of numbers,
>>>produces a list of those
>>>
>>>
>>>numbers greater than all those that precede them. For example
>>>
>>>
>>>maxima[{ 9, 2, 10, 3, 14, 9}] returns { 9, 10, 14}. You need to use
>>>recursion, pattern matching,
>>>
>>>
>>>Select and Join.
>>>
>>>
>>>=E2=80=8E
>>>
>>>
>
>I'll make it easier for myself and use pattern matching:
>
>In[1]:= ReplaceList[{9, 2, 10, 3, 14, 9},
> {s___, x_, ___} /; Max[s] < x :> x]
>
>Out[1]= {9, 10, 14}
>
>Maxim Rytin
>m.r at inbox.ru
>
>
>
- References:
- Re: question in mathematica
- From: m.r@inbox.ru
- RE: Re: question in mathematica
- From: "Harvey P. Dale" <hpd1@nyu.edu>
- Re: question in mathematica