Just a quick-n-dirty, for huge lists there's faster ways, will update when time permits.
f = With[{s = Split[#, #1 < 80 && #2 >= 80 &]},
Pick[Accumulate@(Length /@ s), Length /@ s, 2]] &;
f@{57, 3, 40, 94, 9, 84, 81, 93, 76, 5, 7, 76, 38, 9, 23, 95, 49, 0, 30, 3}
(* {4, 6, 16} *)
For large lists, this should be quite snappy:
f4 = With[{p = Partition[#, 2, 1]},
Pick[Range@Length@p + 1, UnitStep@Subtract[p, 80], {0, 1}]] &;
Even faster:
f5 = Module[{o = Ordering[Join[{80}, #]], c},
c = First@Pick[Range@Length@o, o, 1];
Subtract[Intersection[o[[;; c - 1]] + 1, o[[c + 1 ;;]]], 1]] &;
And speedier yet:
f3 = Module[{p},
p = Pick[Range@Length@#, UnitStep[Subtract[#, 80]], 1];
If[p =!= {} && p[[1]] == 1, p = Rest@p];
p[[Pick[Range@Length@p,
UnitStep@Subtract[#[[Subtract[p, 1]]], 80], 0]]]] &;
As a comparison, the other answers (so far):
sza = ReplaceList[#, {pre___, x_, y_, ___} /; x < 80 <= y :>
Length[{pre}] + 2] &;
kug1 = With[{lst = #},
Select[Range[2, Length@lst], lst[[# - 1]] < 80 <= lst[[#]] &]] &;
kug2 = With[{lst = #},
Pick[Range[2, Length@lst],
lst[[# - 1]] < 80 <= lst[[#]] & /@ Range[2, Length@lst]]] &;
ubp = (Flatten@Position[Partition[Sign[# - 80], 2, 1], {-1, 1 | 0}] +
1) &;
N.B.: The first three have been fixed to return results as in OP - as written they were not. The 0
alternative was added to ubpdqn`s solution to handle equality.
Using test = RandomInteger[{0, 160}, 110000];
as a test list that will have many transitions, and incrementally increasing the amount of it used:

It can be seen the Replace
based solution quickly becomes unusably slow, Kguler's both perform well (and remarkably similarly), f
leads those by roughly 50%, f4
is about 4X faster yet, ubpdqn's Sign
-based solution falls neatly between f
and f4
, f5
beats those, and f3
handily beats all (it's buried in the noise of the plot). As usual, all timings on the loungebook.
Partition
. It's not unreasonable to wonder ifPosition
works with patterns such as__
or___
, but I believe it looks at elements one by one, and matches them against the pattern one by one.__
would require a global match. I thinkPositon[arr, pattern]
does an equivalent ofDo[If[MatchQ[arr[[i]], pattern], Sow[i]], {i, Length[arr]}]
. $\endgroup$Slot
, did you meanBlank
? $\endgroup$