It only works where both of the lengths of the arrays are equal. Otherwise, it adds cute 'undefined' in the end. Also, these if checks are there for safety. They may be ugly and repetitive but I did not want the calculate every other case.
constsnail=input=>{letfinalResult=[];while(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){consta=input.length;//take first line in wholeif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){result=[...input.shift()];}//take end of each lineif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){for(letindex=0;index<a-1;index++){constelement=input[index].pop();result.push(element);}}//take last line in reverseif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){result=[...result,...input.pop().reverse()];}//take start of each middle lineif(typeofinput!="undefined"&&input!=null&&input.length!=null&&input.length>0){for(letindex=a-3;index>-1;index--){constelement=input[index].shift();result.push(element);}}//add this loop's result to finalResultfinalResult=[...finalResult,...result];}console.log(finalResult);};
defremove(a):# remove first row
a=a[1:]returnadefreversedMy(a):# reverse array
a=list(reversed(a))# reverse any row in array
return[list(reversed(a[i]))foriinrange(len(a))]defsnail(a):iflen(a[0])==0:return[[]]maks=len(a)*len(a[0])tab=[]whilelen(tab)<maks:foriinrange(len(a[0])):tab.append(a[0][i])a=remove(a)m=len(a)foriinrange(0,m):tab.append(a[i][m])a[i].pop(m)a=reversedMy(a)returntab
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Here it is!
It works kinda litterally:
And the results:
Solution in Python based on La blatte's wonderful implementation if anyone is interested!
I did the same thing with nearly 10x more code. Great work!
Much cleaner than mine! Nicely done 🎉
Haskell:
Tricky! Not sure I found the most efficient way to do it - but it works, and I think it's relatively clean(ish) :)
Watch me solve it too: youtu.be/h-OzkY1_8mU
Hey Chris, I made a replica of your solution translated to Python3.5 or greater.
It only works where both of the lengths of the arrays are equal. Otherwise, it adds cute 'undefined' in the end. Also, these if checks are there for safety. They may be ugly and repetitive but I did not want the calculate every other case.
Python
A different take, this one might not appear as elegant, but it is quite a lot faster than the previous variants:
So there was supposed to be an image attached, but it doesn't seem to work, anyhow, for those curious JSBench.me gives the following results:
Above one
2,508,265 ops/s ±0.46%
Chris Achards
411,247 ops/s ±1.04%
La blattes
364,899 ops/s ±3.35%
(Firefox, Windows)
snail([[1, 2, 3, 4, 5, 6],
[20, 21, 22, 23, 24, 7],
[19, 32, 33, 34, 25, 8],
[18, 31, 36, 35, 26, 9],
[17, 30, 29, 28, 27, 10],
[16, 15, 14, 13, 12, 11]]);
Reminds me of this task on Exercism :)
Cool task from hackerrank :)