0
$\begingroup$

I want to do the following, deleting the element of a list that do not match an increasing order as example:

{1,2,3,4,6,5,7,8}->{1,2,3,4,6,7,8}

I've thought of using DeleteCases but I'm not able to specify a criteria for the pattern withing the same list. Any suggestions?

Thanks a lot

$\endgroup$
8
  • $\begingroup$ Your question is under-specified. E.g., do you mean the result list must be strictly increasing, or the elements of it must be cases from the target that were such? $\endgroup$
    – ciao
    Jul 20, 2015 at 21:35
  • $\begingroup$ Hi, maybe I'm misunderstanding. The list I've is exactly like the one in the example I'm putting and I would like to keep all the element that do increase progressively except those one that go agains its previous element. I'm trying to write it properly since I've to apply it to a list of lists $\endgroup$
    – Yyrkoon
    Jul 20, 2015 at 21:48
  • $\begingroup$ You might use Differences to detect decreasing elements, e.g. Sign /@ Differences@{1, 2, 3, 4, 6, 5, 7, 8} and then use that to delete elements from the list. $\endgroup$
    – Stefan R
    Jul 20, 2015 at 21:49
  • $\begingroup$ @Yyrkoon: As stated, there can be lists where a solution outputs the desired result for your one example, but not on others - the example is ambiguous. What should result be for say {1, 2, 3, 4, 6, 5, 6, 6, 7, 8, 2, 4, 9, 6}? In any case, see if Pick[#, UnitStep[Differences@Prepend[#, 0] - 1], 1] &@list or FixedPoint[ Pick[#, UnitStep[Differences@Prepend[#, 0] - 1], 1] &, list] floats your boat... $\endgroup$
    – ciao
    Jul 20, 2015 at 21:53
  • 4
    $\begingroup$ This kind of "here's what I want"..."yes, but I also need this"..."well, almost, but I need this too..." is counter-productive. Please update the question with sufficient use-case examples to cover your needs. $\endgroup$
    – ciao
    Jul 20, 2015 at 22:00

2 Answers 2

3
$\begingroup$

You can use repeated replacement, this will only keep elements that are strictly increasing.

list = {1,2,3,4,6,5,7,8};
list = list//. {a___, x_, y_, b___} /; y < x :> {a, x, b}

Which gives:

{1, 2, 3, 4, 6, 7, 8}

In the case that the all elements in Rest@list are less than the first element, a decreasing list for instance, it will return only the first element.

$\endgroup$
4
  • 1
    $\begingroup$ This will be very slow for large lists, though the OP has not specified size requirements. $\endgroup$
    – ciao
    Jul 20, 2015 at 22:08
  • $\begingroup$ It is instructive if OP is interested in learning patterns more than fast calculations so I thought I'd offer it as an alternative. But you're right it will be slow on large lists. $\endgroup$
    – N.J.Evans
    Jul 20, 2015 at 22:11
  • $\begingroup$ Agreed, and +1 for that. $\endgroup$
    – ciao
    Jul 20, 2015 at 22:13
  • $\begingroup$ Thanks a lot ! to all of you and to @ciao for all his comments I know that it has been hard for you all guys to get it through considering how confusing I'm sometimes $\endgroup$
    – Yyrkoon
    Jul 21, 2015 at 13:28
1
$\begingroup$
a = {1, 2, 5, 6, 8, 5, 4, 9, 12, 11};

Union@(Max /@ Table[Take[a, i], {i, Length[a]}])

(* {1, 2, 5, 6, 8, 9, 12} *)

$\endgroup$
4
  • 3
    $\begingroup$ Union[FoldList[Max, list]]... does same thing. $\endgroup$
    – ciao
    Jul 20, 2015 at 22:07
  • $\begingroup$ @ciao Oooh... indeed. Best answer. +1 $\endgroup$ Jul 20, 2015 at 22:09
  • $\begingroup$ Not even sure it is an answer, until we get clarification :-) And +1 on your take. $\endgroup$
    – ciao
    Jul 20, 2015 at 22:12
  • $\begingroup$ Or DeleteDuplicates[FoldList[Max, list]]. $\endgroup$ Jul 21, 2015 at 1:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.