1
>>> def clockwise(r):
... return list(r[0]) + clockwise(list(reversed(zip(*r[1:])))) if r else []
... 
>>> a = [ 
... [ 1, 2, 3], 
... [ 5, 6, 7], 
... [ 9, 10, 11]]
>>> clockwise(a)
[1, 2, 3, 7, 11, 10, 9, 5, 6]

I'm trying to change the function clockwise into Javascript but can't seem to get it to work.

I've created some methods with similar functions:

function zip(masterArray){//zips 2 arrays
var innerLoop = masterArray.length; //inner loop
var outerLoop = 0;
//get length of shortest
for (var i = 0; i<masterArray.length;i++){
 var a = masterArray[i].length;
 if (outerLoop==0){outerLoop = a;}else if(a < outerLoop){outerLoop = a;}
}
var newOuterArray = new Array(outerLoop);
 for (var x = 0; x<outerLoop;x++){
 var newInnerArray = new Array(innerLoop);
 for (var y = 0; y<innerLoop;y++){
 newInnerArray[y] = masterArray[y][x];
 }
 newOuterArray[x] = newInnerArray;
}
 return newOuterArray;
}
function reversed(arr){
 var newArray = new Array(arr.length);
 var n = 0;
 for(var i=arr.length-1; i>=0; i--){
 newArray[n++] = arr[i];
 }
 return newArray;
}
function clockwise(r){
 if(r.length>0){
 var a = reversed(zip(r.slice(1)));
 a.splice(0,0,r[0]);
 return clockwise(a);
 }else{
 return [];
 }
}

Here's my progress. I'm stuck at the last part: clockwise error in firebug is too much recursion.

Trott
70.7k27 gold badges184 silver badges218 bronze badges
asked Jun 29, 2011 at 17:17

3 Answers 3

2

I think the problem lies in the use of splice. Try something like this instead...

function clockwise(r){
 if(r.length>0){
 var remaining = r.slice(1)
 var a = reversed(zip(remaining));
 return r[0].concat(clockwise(a));
 } else {
 return [];
 }
}
answered Jun 29, 2011 at 17:47
Sign up to request clarification or add additional context in comments.

Comments

1

I think you want the recursive call to clockwise() to be before you prepend r[0] otherwise you never get down to an empty string.

answered Jun 29, 2011 at 17:48

Comments

0

I don't follow what the algorithm is trying to do, but the Javascript version of clockwise looks like it can only ever return []. There are two return paths. One calls clockwise, the other returns [] so the only way out of that function is to return []. That's not how your python version works.

I haven't looked at reversed and zip, but there is a logic difference in the javascript version of clockwise. In the javascript version, you remove the first item in the array, then reverse and zip, then put the first item back on the array and call clockwise on the whole thing. In the Python version, you remvoe the first item in the array, call clocks and reverse on only the item without the first version, then add the first one back on afterwards. Very different logic. The Python version is much more likely to end.

I haven't looked at the other functions reverse and zip, but this looks like a more faithful javascript version of clockwise:

function clockwise(r) {
 if (r.length > 0) {
 var a = clockwise(reversed(zip(r.slice(1)))); // call clockwise on array starting at index 1
 a.splice(0,0,r[0]); // put first value in original array back onto the beginning of the result
 return (a); // return new array
 } else {
 return [];
 }
}
answered Jun 29, 2011 at 18:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.