Replace zeros in a list with last nonzero valueReplace “,” in a list with “.”Removing trailing zeros from a listReplace interior of matrix with zerosHow to copy with the last 1 with pattern matching method in a listremoving list containing zerosReplace elements of a matrix with zeros using another matrixIdentify the location of the last entry with a particular value in a list
Life insurance as a lottery ticket
Is Yoda made using CGI in the original Star Wars or is it practical effects?
Does TSA Precheck work when US citizens are exiting the US on a registered foreign passport?
Is it a complete sentence: "Caution murmured: it could be a trick, a lure, a trap."?
Is there a difference between downloading / installing a list of packages and downloading each packge by its own?
Does an action-reaction pair always contain the same kind of force?
How good is Dust Devil?
OOP design for multiple user authentication
Is there a specific reason Delta Air Lines is allowed to have a call sign that's also code word in the NATO phonetic alphabet?
A new way of approaching the pole of the Riemann zeta function - and a new conjectured formula
Difference between cross-validation, backtesting, historical simulation, Monte Carlo simulation, bootstrap replication?
Relinquishing Green card at CA/Mexico border
what are these letters in unicode?
If thermodynamics says entropy always increases, how can the universe end in heat death?
Conceptual distinction between " strength" , " force" and " power"?
What was this pickled vegetable which I was served at a middle eastern restaurant?
Why are the Londoners so excited in the hunt without feeling the horror of war?
One of my friends deposited £42 into my account that he had borrowed previously. Will it affect my UK visa application?
是个[adj + noun] vs 是[adj + noun]
How did Robert Baratheon's character develop from a fine warrior to the state seen in the first season?
What is the meaning of "wiped my face with a planet"?
Are there any surviving Ancient Greek letters (epistolary)?
What does this "ICAgICAg…" mean in public key certificates and messages?
Is Bitlocker secure enough for portable storage devices?
Replace zeros in a list with last nonzero value
Replace “,” in a list with “.”Removing trailing zeros from a listReplace interior of matrix with zerosHow to copy with the last 1 with pattern matching method in a listremoving list containing zerosReplace elements of a matrix with zeros using another matrixIdentify the location of the last entry with a particular value in a list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$begingroup$
This question is closely related to the problem of zero crossings.
Suppose I have a list l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
and I want to replace the zeros with the previous nonzero value, like this:
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
A rather clunky way to do this is with:
f = TimeSeries[l /. 0 -> Missing[],
MissingDataMethod -> "Interpolation", InterpolationOrder -> 0]
Then indeed f[-1 + Range@Length@l]
gives the required result.
However, there must be a more elegant way to achieve same, with patterns.
Can anyone supply a suggestion?
list-manipulation pattern-matching
$endgroup$
add a comment
|
$begingroup$
This question is closely related to the problem of zero crossings.
Suppose I have a list l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
and I want to replace the zeros with the previous nonzero value, like this:
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
A rather clunky way to do this is with:
f = TimeSeries[l /. 0 -> Missing[],
MissingDataMethod -> "Interpolation", InterpolationOrder -> 0]
Then indeed f[-1 + Range@Length@l]
gives the required result.
However, there must be a more elegant way to achieve same, with patterns.
Can anyone supply a suggestion?
list-manipulation pattern-matching
$endgroup$
4
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53
add a comment
|
$begingroup$
This question is closely related to the problem of zero crossings.
Suppose I have a list l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
and I want to replace the zeros with the previous nonzero value, like this:
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
A rather clunky way to do this is with:
f = TimeSeries[l /. 0 -> Missing[],
MissingDataMethod -> "Interpolation", InterpolationOrder -> 0]
Then indeed f[-1 + Range@Length@l]
gives the required result.
However, there must be a more elegant way to achieve same, with patterns.
Can anyone supply a suggestion?
list-manipulation pattern-matching
$endgroup$
This question is closely related to the problem of zero crossings.
Suppose I have a list l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
and I want to replace the zeros with the previous nonzero value, like this:
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
A rather clunky way to do this is with:
f = TimeSeries[l /. 0 -> Missing[],
MissingDataMethod -> "Interpolation", InterpolationOrder -> 0]
Then indeed f[-1 + Range@Length@l]
gives the required result.
However, there must be a more elegant way to achieve same, with patterns.
Can anyone supply a suggestion?
list-manipulation pattern-matching
list-manipulation pattern-matching
asked Sep 25 at 12:11
Jonathan KinlayJonathan Kinlay
6273 silver badges11 bronze badges
6273 silver badges11 bronze badges
4
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53
add a comment
|
4
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53
4
4
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53
add a comment
|
6 Answers
6
active
oldest
votes
$begingroup$
SequenceReplace
SequenceReplace[ p:a_, 0.. :> Sequence @@ (p /. 0 -> a)] @ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
or
FixedPoint[SequenceReplace[a_, 0 :> Sequence[a, a]], l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
ReplaceRepeated
An alternative way to use ReplaceRepeated
with a single replacement rule:
l //. a___, b_, 0, c___ :> a, b, b, c
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Memoization
Memoize the last non-zero value (inspired by @WReach's answer):
f[x_] := (f[0] = x; x)
f /@ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Split
+ ReplaceAll
Split[l, #2 == 0 &] /. p : a_, 0 .. :> (p /. 0 -> a) // Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
TimeSeries
+ MissingDataMethod
Using the "Values"
property with OP's f
:
f["Values"]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
add a comment
|
$begingroup$
Define a simple function, using FoldList
op = FoldList[If[#2 == 0, #1, #2] &];
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
op@l
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
$endgroup$
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
add a comment
|
$begingroup$
Perhaps something like this will appeal
l //. a___, 1, 0, b___ -> a, 1, 1, b, a___, -1, 0, b___ -> a, -1, -1, b
$endgroup$
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
add a comment
|
$begingroup$
Given:
list = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
Then:
Module[prev = 0, Replace[list, 0 :> prev, x_ :> (prev = x), 1]]
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
The initial value assigned by prev = 0
is only used for lists that start with a zero -- choose another value if desired.
$endgroup$
add a comment
|
$begingroup$
A few more...
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], i, 2, Length[l]]
l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
fn[l_] := Block[l1 = l,
Set[Part[l1, #[[2]]], Part[l1, #[[1]]]] & /@ SequencePosition[l, _, 0];
l1
]
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
FixedPoint[fn, l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Fold[#1, If[#2 == 0, Last[#1], #2] &, l[[1 ;; 1]], l[[2 ;; -1]]] //Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
add a comment
|
$begingroup$
The creativity demonstrated by all these solutions is impressive.
Here’s a really tough follow-up challenge. Can anyone write a WL program that will generate the code for any one of these solutions?
$endgroup$
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206835%2freplace-zeros-in-a-list-with-last-nonzero-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
SequenceReplace
SequenceReplace[ p:a_, 0.. :> Sequence @@ (p /. 0 -> a)] @ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
or
FixedPoint[SequenceReplace[a_, 0 :> Sequence[a, a]], l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
ReplaceRepeated
An alternative way to use ReplaceRepeated
with a single replacement rule:
l //. a___, b_, 0, c___ :> a, b, b, c
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Memoization
Memoize the last non-zero value (inspired by @WReach's answer):
f[x_] := (f[0] = x; x)
f /@ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Split
+ ReplaceAll
Split[l, #2 == 0 &] /. p : a_, 0 .. :> (p /. 0 -> a) // Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
TimeSeries
+ MissingDataMethod
Using the "Values"
property with OP's f
:
f["Values"]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
add a comment
|
$begingroup$
SequenceReplace
SequenceReplace[ p:a_, 0.. :> Sequence @@ (p /. 0 -> a)] @ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
or
FixedPoint[SequenceReplace[a_, 0 :> Sequence[a, a]], l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
ReplaceRepeated
An alternative way to use ReplaceRepeated
with a single replacement rule:
l //. a___, b_, 0, c___ :> a, b, b, c
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Memoization
Memoize the last non-zero value (inspired by @WReach's answer):
f[x_] := (f[0] = x; x)
f /@ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Split
+ ReplaceAll
Split[l, #2 == 0 &] /. p : a_, 0 .. :> (p /. 0 -> a) // Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
TimeSeries
+ MissingDataMethod
Using the "Values"
property with OP's f
:
f["Values"]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
add a comment
|
$begingroup$
SequenceReplace
SequenceReplace[ p:a_, 0.. :> Sequence @@ (p /. 0 -> a)] @ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
or
FixedPoint[SequenceReplace[a_, 0 :> Sequence[a, a]], l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
ReplaceRepeated
An alternative way to use ReplaceRepeated
with a single replacement rule:
l //. a___, b_, 0, c___ :> a, b, b, c
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Memoization
Memoize the last non-zero value (inspired by @WReach's answer):
f[x_] := (f[0] = x; x)
f /@ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Split
+ ReplaceAll
Split[l, #2 == 0 &] /. p : a_, 0 .. :> (p /. 0 -> a) // Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
TimeSeries
+ MissingDataMethod
Using the "Values"
property with OP's f
:
f["Values"]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
SequenceReplace
SequenceReplace[ p:a_, 0.. :> Sequence @@ (p /. 0 -> a)] @ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
or
FixedPoint[SequenceReplace[a_, 0 :> Sequence[a, a]], l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
ReplaceRepeated
An alternative way to use ReplaceRepeated
with a single replacement rule:
l //. a___, b_, 0, c___ :> a, b, b, c
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Memoization
Memoize the last non-zero value (inspired by @WReach's answer):
f[x_] := (f[0] = x; x)
f /@ l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
Split
+ ReplaceAll
Split[l, #2 == 0 &] /. p : a_, 0 .. :> (p /. 0 -> a) // Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
TimeSeries
+ MissingDataMethod
Using the "Values"
property with OP's f
:
f["Values"]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
edited Sep 27 at 8:49
answered Sep 25 at 12:18
kglrkglr
228k10 gold badges257 silver badges519 bronze badges
228k10 gold badges257 silver badges519 bronze badges
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
add a comment
|
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
1
1
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
One of the reasons I asked this question even though I had a working (although inelegant) solution was to see all the creative ways in which MMA experts are able to use the Wolfram language to solve a problem. Outstanding!
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:41
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
$begingroup$
What sort of (Mathematica) black magic is this!? My +1 sir.
$endgroup$
– Rebel-Scum
Sep 27 at 13:54
add a comment
|
$begingroup$
Define a simple function, using FoldList
op = FoldList[If[#2 == 0, #1, #2] &];
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
op@l
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
$endgroup$
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
add a comment
|
$begingroup$
Define a simple function, using FoldList
op = FoldList[If[#2 == 0, #1, #2] &];
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
op@l
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
$endgroup$
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
add a comment
|
$begingroup$
Define a simple function, using FoldList
op = FoldList[If[#2 == 0, #1, #2] &];
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
op@l
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
$endgroup$
Define a simple function, using FoldList
op = FoldList[If[#2 == 0, #1, #2] &];
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
op@l
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
answered Sep 26 at 19:06
mikadomikado
9,2272 gold badges9 silver badges32 bronze badges
9,2272 gold badges9 silver badges32 bronze badges
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
add a comment
|
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
$begingroup$
This must be the fastest (and simplest) of the lot. The one I knew existed, but never reached.
$endgroup$
– Suba Thomas
Sep 26 at 22:19
add a comment
|
$begingroup$
Perhaps something like this will appeal
l //. a___, 1, 0, b___ -> a, 1, 1, b, a___, -1, 0, b___ -> a, -1, -1, b
$endgroup$
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
add a comment
|
$begingroup$
Perhaps something like this will appeal
l //. a___, 1, 0, b___ -> a, 1, 1, b, a___, -1, 0, b___ -> a, -1, -1, b
$endgroup$
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
add a comment
|
$begingroup$
Perhaps something like this will appeal
l //. a___, 1, 0, b___ -> a, 1, 1, b, a___, -1, 0, b___ -> a, -1, -1, b
$endgroup$
Perhaps something like this will appeal
l //. a___, 1, 0, b___ -> a, 1, 1, b, a___, -1, 0, b___ -> a, -1, -1, b
answered Sep 25 at 12:19
High Performance MarkHigh Performance Mark
1,1187 silver badges14 bronze badges
1,1187 silver badges14 bronze badges
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
add a comment
|
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
$begingroup$
This is the kind of thing I was aiming for, but I couldn't find the right formulation. I find patterns such a challenge (although I do use them extensively). Anyway that you for a great solution.
$endgroup$
– Jonathan Kinlay
Sep 25 at 22:35
add a comment
|
$begingroup$
Given:
list = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
Then:
Module[prev = 0, Replace[list, 0 :> prev, x_ :> (prev = x), 1]]
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
The initial value assigned by prev = 0
is only used for lists that start with a zero -- choose another value if desired.
$endgroup$
add a comment
|
$begingroup$
Given:
list = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
Then:
Module[prev = 0, Replace[list, 0 :> prev, x_ :> (prev = x), 1]]
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
The initial value assigned by prev = 0
is only used for lists that start with a zero -- choose another value if desired.
$endgroup$
add a comment
|
$begingroup$
Given:
list = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
Then:
Module[prev = 0, Replace[list, 0 :> prev, x_ :> (prev = x), 1]]
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
The initial value assigned by prev = 0
is only used for lists that start with a zero -- choose another value if desired.
$endgroup$
Given:
list = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1
Then:
Module[prev = 0, Replace[list, 0 :> prev, x_ :> (prev = x), 1]]
(* -1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1 *)
The initial value assigned by prev = 0
is only used for lists that start with a zero -- choose another value if desired.
answered Sep 25 at 15:47
WReachWReach
56.7k2 gold badges125 silver badges224 bronze badges
56.7k2 gold badges125 silver badges224 bronze badges
add a comment
|
add a comment
|
$begingroup$
A few more...
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], i, 2, Length[l]]
l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
fn[l_] := Block[l1 = l,
Set[Part[l1, #[[2]]], Part[l1, #[[1]]]] & /@ SequencePosition[l, _, 0];
l1
]
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
FixedPoint[fn, l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Fold[#1, If[#2 == 0, Last[#1], #2] &, l[[1 ;; 1]], l[[2 ;; -1]]] //Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
add a comment
|
$begingroup$
A few more...
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], i, 2, Length[l]]
l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
fn[l_] := Block[l1 = l,
Set[Part[l1, #[[2]]], Part[l1, #[[1]]]] & /@ SequencePosition[l, _, 0];
l1
]
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
FixedPoint[fn, l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Fold[#1, If[#2 == 0, Last[#1], #2] &, l[[1 ;; 1]], l[[2 ;; -1]]] //Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
add a comment
|
$begingroup$
A few more...
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], i, 2, Length[l]]
l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
fn[l_] := Block[l1 = l,
Set[Part[l1, #[[2]]], Part[l1, #[[1]]]] & /@ SequencePosition[l, _, 0];
l1
]
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
FixedPoint[fn, l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Fold[#1, If[#2 == 0, Last[#1], #2] &, l[[1 ;; 1]], l[[2 ;; -1]]] //Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
$endgroup$
A few more...
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Do[If[l[[i]] == 0, l[[i]] = l[[i - 1]]], i, 2, Length[l]]
l
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
fn[l_] := Block[l1 = l,
Set[Part[l1, #[[2]]], Part[l1, #[[1]]]] & /@ SequencePosition[l, _, 0];
l1
]
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
FixedPoint[fn, l]
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
l = -1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1;
Fold[#1, If[#2 == 0, Last[#1], #2] &, l[[1 ;; 1]], l[[2 ;; -1]]] //Flatten
-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1
edited Sep 26 at 17:38
answered Sep 26 at 17:30
Suba ThomasSuba Thomas
5,06911 silver badges20 bronze badges
5,06911 silver badges20 bronze badges
add a comment
|
add a comment
|
$begingroup$
The creativity demonstrated by all these solutions is impressive.
Here’s a really tough follow-up challenge. Can anyone write a WL program that will generate the code for any one of these solutions?
$endgroup$
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
add a comment
|
$begingroup$
The creativity demonstrated by all these solutions is impressive.
Here’s a really tough follow-up challenge. Can anyone write a WL program that will generate the code for any one of these solutions?
$endgroup$
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
add a comment
|
$begingroup$
The creativity demonstrated by all these solutions is impressive.
Here’s a really tough follow-up challenge. Can anyone write a WL program that will generate the code for any one of these solutions?
$endgroup$
The creativity demonstrated by all these solutions is impressive.
Here’s a really tough follow-up challenge. Can anyone write a WL program that will generate the code for any one of these solutions?
answered Sep 26 at 22:03
Jonathan KinlayJonathan Kinlay
6273 silver badges11 bronze badges
6273 silver badges11 bronze badges
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
add a comment
|
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
6
6
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
$begingroup$
This isn't an answer. It should either be an extension to the question, or a fresh question referring back to the original one.
$endgroup$
– High Performance Mark
Sep 27 at 9:38
add a comment
|
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206835%2freplace-zeros-in-a-list-with-last-nonzero-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
What should happen when the first element is a zero?
$endgroup$
– Sjoerd Smit
Sep 25 at 14:53