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;








3












$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










share|improve this question









$endgroup$









  • 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

















3












$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










share|improve this question









$endgroup$









  • 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













3












3








3





$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










share|improve this question









$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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 9 hours ago









Michiel van MensMichiel van Mens

9476 silver badges19 bronze badges




9476 silver badges19 bronze badges










  • 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












  • 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







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










4 Answers
4






active

oldest

votes


















4












$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.






share|improve this answer









$endgroup$














  • $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




    $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


















2












$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
*)





share|improve this answer









$endgroup$






















    2












    $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







    share|improve this answer











    $endgroup$






















      0












      $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.






      share|improve this answer











      $endgroup$

















        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
        );



        );













        draft saved

        draft discarded


















        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









        4












        $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.






        share|improve this answer









        $endgroup$














        • $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




          $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















        4












        $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.






        share|improve this answer









        $endgroup$














        • $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




          $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













        4












        4








        4





        $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.






        share|improve this answer









        $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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        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 inside Cases but 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 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$
          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




          $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$
        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













        2












        $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
        *)





        share|improve this answer









        $endgroup$



















          2












          $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
          *)





          share|improve this answer









          $endgroup$

















            2












            2








            2





            $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
            *)





            share|improve this answer









            $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
            *)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 8 hours ago









            John DotyJohn Doty

            8,7461 gold badge14 silver badges26 bronze badges




            8,7461 gold badge14 silver badges26 bronze badges
























                2












                $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







                share|improve this answer











                $endgroup$



















                  2












                  $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







                  share|improve this answer











                  $endgroup$

















                    2












                    2








                    2





                    $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







                    share|improve this answer











                    $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








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 4 hours ago

























                    answered 6 hours ago









                    kglrkglr

                    211k10 gold badges242 silver badges484 bronze badges




                    211k10 gold badges242 silver badges484 bronze badges
























                        0












                        $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.






                        share|improve this answer











                        $endgroup$



















                          0












                          $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.






                          share|improve this answer











                          $endgroup$

















                            0












                            0








                            0





                            $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.






                            share|improve this answer











                            $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.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            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






























                                draft saved

                                draft discarded
















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480