Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: question in mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80106] Re: question in mathematica
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 12 Aug 2007 07:26:55 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fa11b990e477.46baf2d3@bgu.ac.il> <D370EB89-ACFD-4FFF-B4E7-84A25FF267C5@mimuw.edu.pl> <C82B7D4D-6C0B-47E1-8603-DEE030A6E387@mimuw.edu.pl> <f69ab100e04a.46bc3a23@bgu.ac.il> <f9jlql$3pj$1@smc.vnet.net>

Andrzej Kozlowski wrote:
> First, please send such question to the MathGroup,
> 
> mathgroup 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.

Here is another possible answer, which uses some pattern matching.

In[1]:= Clear[maxima]

maxima[ls_List /; VectorQ[ls, NumberQ]] :=
    Last /@ ((Split[##1, #2 >= #1 & ] & )[ls] /.
         l : {{__}, ___, {_}} :> Most[l])

ls = {9, 2, 10, 3, 14, 9};
maxima[ls]

Out[4]= {9, 10, 14}

Below, we have tested the maxima function against some possible cases.

In[5]:=
ls = {9, 2, 10, 3, 14, 9, 10};
maxima[ls]
ls = {9, 11, 2, 10, 3, 14, 9, 10};
maxima[ls]
ls = {9, 11, 2, 10, 3, a, 14, 9, 10};
maxima[ls]
ls = {9, 11, 2};
maxima[ls]
ls = {9, 2};
maxima[ls]
ls = {2, 9};
maxima[ls]
ls = {2};
maxima[ls]
ls = {2, 2, 2, 9};
maxima[ls]
ls = {2, 2, 2, 9, 9, 10};
maxima[ls]

Out[6]= {9, 10, 14, 10}

Out[8]= {11, 10, 14, 10}

Out[10]= maxima[{9, 11, 2, 10, 3, a, 14, 9, 10}]

Out[12]= {11}

Out[14]= {9}

Out[16]= {9}

Out[18]= {2}

Out[20]= {9}

Out[22]= {10}

-- 
Jean-Marc


  • Prev by Date: Re: 64 bit Kernel does not run on iMac Intel
  • Next by Date: Re: Indexed element treated as number?
  • Previous by thread: Re: question in mathematica
  • Next by thread: Re: question in mathematica