List, map function based on a conditionConditional TableHow to replace an element in a list based on the value of the next element?Indexed Map ThreadChange some elements at the second level of a list based on some conditionsHow-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pairInserting a condition inside a FOR loopFilter a nested list based on conditions on its elements
Did Michelle Obama have a staff of 23; and Melania have a staff of 4?
Why aren't rainbows blurred-out into nothing after they are produced?
Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu
Why do my bicycle brakes get worse and feel more 'squishy" over time?
How much can I judge a company based on a phone screening?
Escape Velocity - Won't the orbital path just become larger with higher initial velocity?
How can I shoot a bow using Strength instead of Dexterity?
How can I find an old paper when the usual methods fail?
What is the most difficult concept to grasp in Calculus 1?
The more + the + comparative degree
What is the farthest a camera can see?
What is the proper name for a circle with a line through it?
Lípínguapua dopo Pêpê
Align rightarrow in table
How do I call a 6-digit Australian phone number with a US-based mobile phone?
What's the relationship betweeen MS-DOS and XENIX?
Telephone number in spoken words
Go to last file in vim
Why won't the Republicans use a superdelegate system like the DNC in their nomination process?
The oceans and the moon
Suspension compromise for urban use
Would the USA be eligible to join the European Union?
What are the advantages of this gold finger shape?
Solving pricing problem heuristically in column generation algorithm for VRP
List, map function based on a condition
Conditional TableHow to replace an element in a list based on the value of the next element?Indexed Map ThreadChange some elements at the second level of a list based on some conditionsHow-to select an entry from a list of pairs that meets a condition depending the 2nd element of each pairInserting a condition inside a FOR loopFilter a nested list based on conditions on its elements
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have the following list:
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1
there are two conditions:
- When a '0' is followed by '1', the value of '0' change to .5
- When a '1' is followed by '0', the value of '0' change to .5
the desired output is:
output = [0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5,
0, .5, 1, .5, 1, .5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1, 1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1]
Who has a suggestion how to get the desired output
list-manipulation conditional map
$endgroup$
add a comment |
$begingroup$
I have the following list:
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1
there are two conditions:
- When a '0' is followed by '1', the value of '0' change to .5
- When a '1' is followed by '0', the value of '0' change to .5
the desired output is:
output = [0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5,
0, .5, 1, .5, 1, .5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1, 1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1]
Who has a suggestion how to get the desired output
list-manipulation conditional map
$endgroup$
2
$begingroup$
should desired[[3]] be1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?
$endgroup$
– user1066
7 hours ago
add a comment |
$begingroup$
I have the following list:
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1
there are two conditions:
- When a '0' is followed by '1', the value of '0' change to .5
- When a '1' is followed by '0', the value of '0' change to .5
the desired output is:
output = [0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5,
0, .5, 1, .5, 1, .5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1, 1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1]
Who has a suggestion how to get the desired output
list-manipulation conditional map
$endgroup$
I have the following list:
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1
there are two conditions:
- When a '0' is followed by '1', the value of '0' change to .5
- When a '1' is followed by '0', the value of '0' change to .5
the desired output is:
output = [0, 0, 0, .5, 1, 1, 1, .5, 1, .5, 0, .5, 1, 1, .5, 1, .5,
0, .5, 1, .5, 1, .5, 1, .5, 1, .5, .5, 1, .5, 0, 0, 0, .5, 1,
1, .5, 1, .5, 0, .5, 1, .5, 1, 1, 1, .5, .5, 1, 1, 1, .5, 0, 0,
0, .5, 1, 1, .5, 1, 0, 0, .5, 1, .5, 1]
Who has a suggestion how to get the desired output
list-manipulation conditional map
list-manipulation conditional map
asked 9 hours ago
Michiel van MensMichiel van Mens
9476 silver badges19 bronze badges
9476 silver badges19 bronze badges
2
$begingroup$
should desired[[3]] be1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?
$endgroup$
– user1066
7 hours ago
add a comment |
2
$begingroup$
should desired[[3]] be1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?
$endgroup$
– user1066
7 hours ago
2
2
$begingroup$
should desired[[3]] be
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?$endgroup$
– user1066
7 hours ago
$begingroup$
should desired[[3]] be
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?$endgroup$
– user1066
7 hours ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Using ReplaceRepeated (//.) and pattern matching:
input //. x___, 0, 1, y___ :> x, 0.5, 1, y, x___, 1, 0, y___ :> x, 1, 0.5, y
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.
$endgroup$
$begingroup$
Tricky. I tried it similar insideCasesbut only one substitution is made. What could be the reason? Thanks!
$endgroup$
– Ulrich Neumann
8 hours ago
1
$begingroup$
/.only tries to replace once per complete match, as I understand, so since each list matchesx___, 1, 0, y___or the other it only undergoes one substitution each.//.explicitly retries until no matches remain. I thinkCasesworks more like/.in that respect.
$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
add a comment |
$begingroup$
For a rewriting problem, use rewriting explicitly:
input //.
x___, 0, 1, y___ -> x, 0.5, 1, y,
x___, 1, 0, y___ -> x, 1, 0.5, y
(*
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
*)
$endgroup$
add a comment |
$begingroup$
You can also use a combination of SequenceReplace and FixedPoint:
f = Map[SequenceReplace[0, 1 -> Sequence[.5, 1], 1, 0 -> Sequence[1, .5]]],
FixedPoint[f, input]
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
You can also use Nest in place of FixedPoint:
Nest[f , input, 2] == %
True
$endgroup$
add a comment |
$begingroup$
This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show
foo[input_List] := Module[n, i, current, next, before,
n = Length[input];
(*handle edge cases*)
If[n == 1, Return[input, Module]];
If[n == 2,
Return[If[input[[1]] == 0 && input[[2]] == 1, 0.5, 1,
If[input[[2]] == 0 && input[[1]] == 1, 1, 0.5, input]],
Module]];
(*general case for list of length [GreaterEqual] 3*)
Table[
current = input[[i]];
If[i == 1,
If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
,
If[i == n,
If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
,
before = input[[i - 1]];
next = input[[i + 1]];
If[current == 0 && next == 1, 0.5,
If[current == 0 && before == 1, 0.5, current]]
]
],
i, 1, Length[input]
]
]
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 1;
Now map foo on the input
result = foo[#] & /@ input
gives
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
I suspect there might be a shorter way to do this if one works harder on it.
$endgroup$
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/3.0/"u003ecc by-sa 3.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%2f203760%2flist-map-function-based-on-a-condition%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Using ReplaceRepeated (//.) and pattern matching:
input //. x___, 0, 1, y___ :> x, 0.5, 1, y, x___, 1, 0, y___ :> x, 1, 0.5, y
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.
$endgroup$
$begingroup$
Tricky. I tried it similar insideCasesbut only one substitution is made. What could be the reason? Thanks!
$endgroup$
– Ulrich Neumann
8 hours ago
1
$begingroup$
/.only tries to replace once per complete match, as I understand, so since each list matchesx___, 1, 0, y___or the other it only undergoes one substitution each.//.explicitly retries until no matches remain. I thinkCasesworks more like/.in that respect.
$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
add a comment |
$begingroup$
Using ReplaceRepeated (//.) and pattern matching:
input //. x___, 0, 1, y___ :> x, 0.5, 1, y, x___, 1, 0, y___ :> x, 1, 0.5, y
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.
$endgroup$
$begingroup$
Tricky. I tried it similar insideCasesbut only one substitution is made. What could be the reason? Thanks!
$endgroup$
– Ulrich Neumann
8 hours ago
1
$begingroup$
/.only tries to replace once per complete match, as I understand, so since each list matchesx___, 1, 0, y___or the other it only undergoes one substitution each.//.explicitly retries until no matches remain. I thinkCasesworks more like/.in that respect.
$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
add a comment |
$begingroup$
Using ReplaceRepeated (//.) and pattern matching:
input //. x___, 0, 1, y___ :> x, 0.5, 1, y, x___, 1, 0, y___ :> x, 1, 0.5, y
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.
$endgroup$
Using ReplaceRepeated (//.) and pattern matching:
input //. x___, 0, 1, y___ :> x, 0.5, 1, y, x___, 1, 0, y___ :> x, 1, 0.5, y
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
Note that there is a slight difference between this and your suggested output, highlighted in bold. As @Nasser received this result by his method as well, I suspect that the original suggested output was in error.
answered 8 hours ago
eyorbleeyorble
6,6281 gold badge11 silver badges30 bronze badges
6,6281 gold badge11 silver badges30 bronze badges
$begingroup$
Tricky. I tried it similar insideCasesbut only one substitution is made. What could be the reason? Thanks!
$endgroup$
– Ulrich Neumann
8 hours ago
1
$begingroup$
/.only tries to replace once per complete match, as I understand, so since each list matchesx___, 1, 0, y___or the other it only undergoes one substitution each.//.explicitly retries until no matches remain. I thinkCasesworks more like/.in that respect.
$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
add a comment |
$begingroup$
Tricky. I tried it similar insideCasesbut only one substitution is made. What could be the reason? Thanks!
$endgroup$
– Ulrich Neumann
8 hours ago
1
$begingroup$
/.only tries to replace once per complete match, as I understand, so since each list matchesx___, 1, 0, y___or the other it only undergoes one substitution each.//.explicitly retries until no matches remain. I thinkCasesworks more like/.in that respect.
$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
$begingroup$
Tricky. I tried it similar inside
Cases but only one substitution is made. What could be the reason? Thanks!$endgroup$
– Ulrich Neumann
8 hours ago
$begingroup$
Tricky. I tried it similar inside
Cases but only one substitution is made. What could be the reason? Thanks!$endgroup$
– Ulrich Neumann
8 hours ago
1
1
$begingroup$
/. only tries to replace once per complete match, as I understand, so since each list matches x___, 1, 0, y___ or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.$endgroup$
– eyorble
8 hours ago
$begingroup$
/. only tries to replace once per complete match, as I understand, so since each list matches x___, 1, 0, y___ or the other it only undergoes one substitution each. //. explicitly retries until no matches remain. I think Cases works more like /. in that respect.$endgroup$
– eyorble
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
$begingroup$
Thanks, I have to think about it.
$endgroup$
– Ulrich Neumann
8 hours ago
add a comment |
$begingroup$
For a rewriting problem, use rewriting explicitly:
input //.
x___, 0, 1, y___ -> x, 0.5, 1, y,
x___, 1, 0, y___ -> x, 1, 0.5, y
(*
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
*)
$endgroup$
add a comment |
$begingroup$
For a rewriting problem, use rewriting explicitly:
input //.
x___, 0, 1, y___ -> x, 0.5, 1, y,
x___, 1, 0, y___ -> x, 1, 0.5, y
(*
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
*)
$endgroup$
add a comment |
$begingroup$
For a rewriting problem, use rewriting explicitly:
input //.
x___, 0, 1, y___ -> x, 0.5, 1, y,
x___, 1, 0, y___ -> x, 1, 0.5, y
(*
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
*)
$endgroup$
For a rewriting problem, use rewriting explicitly:
input //.
x___, 0, 1, y___ -> x, 0.5, 1, y,
x___, 1, 0, y___ -> x, 1, 0.5, y
(*
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
*)
answered 8 hours ago
John DotyJohn Doty
8,7461 gold badge14 silver badges26 bronze badges
8,7461 gold badge14 silver badges26 bronze badges
add a comment |
add a comment |
$begingroup$
You can also use a combination of SequenceReplace and FixedPoint:
f = Map[SequenceReplace[0, 1 -> Sequence[.5, 1], 1, 0 -> Sequence[1, .5]]],
FixedPoint[f, input]
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
You can also use Nest in place of FixedPoint:
Nest[f , input, 2] == %
True
$endgroup$
add a comment |
$begingroup$
You can also use a combination of SequenceReplace and FixedPoint:
f = Map[SequenceReplace[0, 1 -> Sequence[.5, 1], 1, 0 -> Sequence[1, .5]]],
FixedPoint[f, input]
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
You can also use Nest in place of FixedPoint:
Nest[f , input, 2] == %
True
$endgroup$
add a comment |
$begingroup$
You can also use a combination of SequenceReplace and FixedPoint:
f = Map[SequenceReplace[0, 1 -> Sequence[.5, 1], 1, 0 -> Sequence[1, .5]]],
FixedPoint[f, input]
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
You can also use Nest in place of FixedPoint:
Nest[f , input, 2] == %
True
$endgroup$
You can also use a combination of SequenceReplace and FixedPoint:
f = Map[SequenceReplace[0, 1 -> Sequence[.5, 1], 1, 0 -> Sequence[1, .5]]],
FixedPoint[f, input]
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1,
0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1,
1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
You can also use Nest in place of FixedPoint:
Nest[f , input, 2] == %
True
edited 4 hours ago
answered 6 hours ago
kglrkglr
211k10 gold badges242 silver badges484 bronze badges
211k10 gold badges242 silver badges484 bronze badges
add a comment |
add a comment |
$begingroup$
This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show
foo[input_List] := Module[n, i, current, next, before,
n = Length[input];
(*handle edge cases*)
If[n == 1, Return[input, Module]];
If[n == 2,
Return[If[input[[1]] == 0 && input[[2]] == 1, 0.5, 1,
If[input[[2]] == 0 && input[[1]] == 1, 1, 0.5, input]],
Module]];
(*general case for list of length [GreaterEqual] 3*)
Table[
current = input[[i]];
If[i == 1,
If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
,
If[i == n,
If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
,
before = input[[i - 1]];
next = input[[i + 1]];
If[current == 0 && next == 1, 0.5,
If[current == 0 && before == 1, 0.5, current]]
]
],
i, 1, Length[input]
]
]
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 1;
Now map foo on the input
result = foo[#] & /@ input
gives
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
I suspect there might be a shorter way to do this if one works harder on it.
$endgroup$
add a comment |
$begingroup$
This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show
foo[input_List] := Module[n, i, current, next, before,
n = Length[input];
(*handle edge cases*)
If[n == 1, Return[input, Module]];
If[n == 2,
Return[If[input[[1]] == 0 && input[[2]] == 1, 0.5, 1,
If[input[[2]] == 0 && input[[1]] == 1, 1, 0.5, input]],
Module]];
(*general case for list of length [GreaterEqual] 3*)
Table[
current = input[[i]];
If[i == 1,
If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
,
If[i == n,
If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
,
before = input[[i - 1]];
next = input[[i + 1]];
If[current == 0 && next == 1, 0.5,
If[current == 0 && before == 1, 0.5, current]]
]
],
i, 1, Length[input]
]
]
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 1;
Now map foo on the input
result = foo[#] & /@ input
gives
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
I suspect there might be a shorter way to do this if one works harder on it.
$endgroup$
add a comment |
$begingroup$
This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show
foo[input_List] := Module[n, i, current, next, before,
n = Length[input];
(*handle edge cases*)
If[n == 1, Return[input, Module]];
If[n == 2,
Return[If[input[[1]] == 0 && input[[2]] == 1, 0.5, 1,
If[input[[2]] == 0 && input[[1]] == 1, 1, 0.5, input]],
Module]];
(*general case for list of length [GreaterEqual] 3*)
Table[
current = input[[i]];
If[i == 1,
If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
,
If[i == n,
If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
,
before = input[[i - 1]];
next = input[[i + 1]];
If[current == 0 && next == 1, 0.5,
If[current == 0 && before == 1, 0.5, current]]
]
],
i, 1, Length[input]
]
]
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 1;
Now map foo on the input
result = foo[#] & /@ input
gives
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
I suspect there might be a shorter way to do this if one works harder on it.
$endgroup$
This is not a "functional" way to do it. But an old fashioned loop and few if's. But it gives the result you show
foo[input_List] := Module[n, i, current, next, before,
n = Length[input];
(*handle edge cases*)
If[n == 1, Return[input, Module]];
If[n == 2,
Return[If[input[[1]] == 0 && input[[2]] == 1, 0.5, 1,
If[input[[2]] == 0 && input[[1]] == 1, 1, 0.5, input]],
Module]];
(*general case for list of length [GreaterEqual] 3*)
Table[
current = input[[i]];
If[i == 1,
If[input[[i]] == 0 && input[[i + 1]] == 1, 0.5, input[[i]]]
,
If[i == n,
If[input[[n]] == 0 && input[[n - 1]] == 1, 0.5, input[[n]]]
,
before = input[[i - 1]];
next = input[[i + 1]];
If[current == 0 && next == 1, 0.5,
If[current == 0 && before == 1, 0.5, current]]
]
],
i, 1, Length[input]
]
]
input = 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0,
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0,
0, 1, 0, 1;
Now map foo on the input
result = foo[#] & /@ input
gives
0, 0, 0, 0.5, 1, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0,
0.5, 1, 0.5, 1, 0.5, 1, 0.5, 1, 0.5, 0.5, 1, 0.5, 0, 0, 0, 0.5, 1,
1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 0.5,
0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1
I suspect there might be a shorter way to do this if one works harder on it.
edited 8 hours ago
answered 8 hours ago
NasserNasser
61.3k4 gold badges93 silver badges215 bronze badges
61.3k4 gold badges93 silver badges215 bronze badges
add a comment |
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%2f203760%2flist-map-function-based-on-a-condition%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
2
$begingroup$
should desired[[3]] be
1, 1, 0.5, 0.5, 1, 1, 1, 0.5, 0, 0, 0, 0.5, 1, 1, 0.5, 1, 0.5, 0, 0.5, 1, 0.5, 1?$endgroup$
– user1066
7 hours ago