Created
April 17, 2018 16:16
-
-
Save Ni55aN/877be987884710ccf6bd4d7b8ef4529d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
snail = function(array) { | |
var side = 'top'; | |
var result = []; | |
while(array.length > 0){ | |
switch(side){ | |
case 'top': | |
result.push(...cutRow(0, array)); | |
side = 'right'; | |
break; | |
case 'right': | |
result.push(...cutColumn(array[0].length - 1, array)); | |
side = 'bottom'; | |
break; | |
case 'bottom': | |
result.push(...cutRow(array.length - 1, array, true)); | |
side = 'left'; | |
break; | |
case 'left': | |
result.push(...cutColumn(0, array, true)); | |
side = 'top'; | |
break; | |
} | |
} | |
return result; | |
} | |
function cutRow(i, array, reverse){ | |
const res = array.splice(i, 1)[0]; | |
return reverse?res.reverse():res; | |
} | |
function cutColumn(i, array, reverse){ | |
const res = array.map(arr => arr.splice(i, 1)[0]); | |
return reverse?res.reverse():res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment