Comments on Backspace an array
Parent
Backspace an array
+7
−0
Challenge
Given an array consisting of positive integers and 0s, return it with 0s acting like backspaces.
Leading backspaces do nothing, and more backspaces than elements also does nothing.
Credit: A comment on this video
Test Cases
[0,0,0,0,0,5,7] -> [5,7]
[1,2,0,3] -> [1,3]
[1,5,5,0,2,0,0,8] -> [1,8]
[1,2,3,4,0,0,9] -> [1,2,9]
[1,2,0,0,0,0,0] -> []
+3
−0
[Jelly], 6 bytes ṣ0Ṗ;¥/ …
3y ago
+3
−0
Scala, 45 bytes ```scala ./: …
3y ago
+1
−0
[JavaScript (Node.js)], 41 40 …
3y ago
+1
−0
[Python 3.8 (pre-release)], 62 …
3y ago
+0
−0
Japt, 9 bytes ô rÈÔÅÔcY …
3y ago
+0
−0
[Husk], 6 bytes Fo+hx0 …
3y ago
Post
+1
−0
JavaScript (Node.js), 41 40 bytes
Saved 1 byte thanks to Shaggy
x=>x.map(e=>e?a.push(e):a.pop(),a=[])&&a
x => //x is the input
x.map(e=> //For every element e in x
e? //If e is not 0
a.push(e) //Add it to the accumulator
:a.pop(), //Otherwise, pop the last element
a=[]) //Initial value of the accumulator
&&a //Return the accumulator
1 comment thread