Skip to main content
added 129 characters in body
Source Link
rm -rf
  • 89.3k
  • 21
  • 297
  • 480

Here is another way:

With[{acc = Accumulate@data}, acc[[;; -#]] ~Join~ data[[-# + 1 ;;]] &@Tr@HeavisideTheta@acc]&@Count[acc, _?Positive]]
(* {-1050, -1000, 1, 1950, 1950, 1950, 525, 0} *)

You can also use Tr@Boole@Thread[acc > 0]] instead of Count[...] or if you want ≥0, then you can use Tr@UnitStep[acc].

Here is another way:

With[{acc = Accumulate@data}, acc[[;; -#]] ~Join~ data[[-# + 1 ;;]] &@Tr@HeavisideTheta@acc]
(* {-1050, -1000, 1, 1950, 1950, 1950, 525, 0} *)

Here is another way:

With[{acc = Accumulate@data}, acc[[;; -#]] ~Join~ data[[-# + 1 ;;]] &@Count[acc, _?Positive]]
(* {-1050, -1000, 1, 1950, 1950, 1950, 525, 0} *)

You can also use Tr@Boole@Thread[acc > 0]] instead of Count[...] or if you want ≥0, then you can use Tr@UnitStep[acc].

Source Link
rm -rf
  • 89.3k
  • 21
  • 297
  • 480

Here is another way:

With[{acc = Accumulate@data}, acc[[;; -#]] ~Join~ data[[-# + 1 ;;]] &@Tr@HeavisideTheta@acc]
(* {-1050, -1000, 1, 1950, 1950, 1950, 525, 0} *)