My very simple Question How to fill all 1 between 1 and 1 in an array with only 0 and 1
Given an array [1 1 0 1 0], fill as following 0 1 0 1 0 ↑ ↑ 1 1 1 ⟹ 0 1 1 1 0 Given an array [0 1 0 0 1 1 0 1 0 1 0] 0 1 0 0 1 1 0 1 0 1 0 ↑ ↑ ↑ ↑ 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 ↑ + → Do nothing for this 1 Haskell partial solution (Use scanl1) Warm up: scanl1 (+) [1, 2, 3] => 1 3 6 scanl1 (-) [1, 2, 3] => 1 (1-2) (1-2-3) => 1 -1 -4 s = [0, 1, 0, 1, 0] t = scanl1 (\a b -> a /= b) s => 0 (0 /= 1) (0 /= 1 /= 0) (0 /= 1 /= 0 /= 1) (0 /= 1 /= 0 /= 1 /= 0) t = 0 1 1 0 0 Given an array s = [0 1 0 1 0] Assume there are even # of 1 in the s s = 0 1 0 1 0 t = 0 1 1 0 0 OR - - - - - - - - - - - 0 1 1 1 0 It only works if there are EVEN # of 1 in the array r = zipWith(\a b -> a || b) s t r = [0 1 1 1 0] In APL (≠\∨⊣) 0 1 0 1 0 0 1 1 1 0 w ← 0 1 0 1 0 (≠\) w 0 1 1 0 0 w ∨ (≠\) w 0 1 1 1 0 Or oneliner (≠\∨⊣) 0 1 0 1 0 0 1 1 1 0