Best Way to Find and Replace Adjacent Elements in a List?

Posted 1 month ago
297 Views
|
11 Replies
|
8 Total Likes
|
 Hi everyone,I have a function that returns a list with a series of boolean value False followed by a series of boolean value True like this: {False, False, False, True, True, True, True, True} The False's always come before the True's, and the number of False's does not have to equal the number of True's. I need to replace the last False in the series—the third one in my example—with True. This works Flatten[ReplaceAll[ Split[{False, False, False, True, True, True, True, True}, #1 =!= #2 &], {False, True} -> {True, True}]] but it seems like there should be a simpler or more elegant way. Any thoughts?Greg
11 Replies
Sort By:
Posted 1 month ago
 I would use SequenceReplace: SequenceReplace[a, {False, True} -> Sequence[True, True]] where a is your input list.
Posted 1 month ago
 Brilliant, Sander!I use Sequence so rarely that it never occurred to me that there may be a SequenceReplace function.Many thanks,Greg
Posted 1 month ago
 it's a new function since 11.3 so, actually, this also has been my first use-case… combinations of Split, or just regular replace also generally works fine (and faster). Not sure about the performance of sequence replace, and if it is critical…
Posted 1 month ago
 For earlier versions of Mathematica we can manage like this: {False, False, False, True, True, True, True, True} /. {smth1___, False, True, smth2___} :> {smth1, True, True, smth2} 
Posted 1 month ago
 This would replace just the first occurrence of it, and is therefore very different.
Posted 1 month ago
 Yes, of course, a delayed rule. Good stuff! Very fast.Greg
Posted 1 month ago
 Good point. My function is restricted in its use and is applied only to situations where there is just one occurrence of the pattern. Your way, however, has an advantage if error-checking for more than one occurrence needs to be built in.Greg
Posted 1 month ago
 If it is really of the form: False False … … True True True, you can also use solutions like: p = LengthWhile[a, Not@*TrueQ] a[[p]] = True; or even just: Append[Rest[a], True] or Rest[RotateLeft[Prepend[a, True]]] or (c = #; c[[-1]] = True; c) &[RotateLeft[a]] or Most[RotateLeft[Prepend[a, True], 2]] or ReplacePart[RotateLeft[a], -1 -> True] oretc etc
Posted 1 month ago
 That's pretty slick. Something for me to brush up on because I can see where I can see some other neat uses.Thanks again,Greg