Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
1 2 3 4
✂ ┄┄┄┄┄┄┄┄┄┄┄┄
5 6 7 8
9 10 11 12
The next part of the spiral isNow you'll notice that the rightmost column of thisthe remaining part, 8 12
, is the next part of the spiral. But what if we turn it 90o counter-clockwise?
8 12
✂ ┄┄┄┄┄┄
7 11 => 11 10 9 => 11 10 9
6 10 7 6 5 ✂ ┄┄┄┄┄┄┄┄┄┄
5 9 7 6 5 => 5 => 5
6 ✂ ┄┄┄
7 6 => 6 7 (end!)
7
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
8 12
✂ ┄┄┄┄┄┄
7 11 => 11 10 9
6 10 7 6 5
5 9
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it.
1 2 3 4
✂ ┄┄┄┄┄┄┄┄┄┄┄┄
5 6 7 8
9 10 11 12
Now you'll notice that the rightmost column of the remaining part, 8 12
, is the next part of the spiral. But what if we turn it 90o counter-clockwise?
8 12
✂ ┄┄┄┄┄┄
7 11 => 11 10 9 => 11 10 9
6 10 7 6 5 ✂ ┄┄┄┄┄┄┄┄┄┄
5 9 7 6 5 => 5 => 5
6 ✂ ┄┄┄
7 6 => 6 7 (end!)
7
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
5 6 7 8 => 8 12
9 10 11 12 7 11
6 10
5 9
Now 8 12
the top row! Chop it off and repeat:
11 10 9 => 8 12
✂ 9┄┄┄┄┄┄
5
7 611 5=> 11 10 9
10 6 10 7 6 5
115 79
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, then get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's just a straightforward nested loop, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
5 6 7 8 => 8 12
9 10 11 12 7 11
6 10
5 9
Now 8 12
the top row! Chop it off and repeat:
11 10 9 => 9 5
7 6 5 10 6 11 7
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, then get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's just a straightforward nested loop, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
5 6 7 8 => 8 12
9 10 11 12 7 11
6 10
5 9
Now 8 12
the top row! Chop it off and repeat:
8 12
✂ ┄┄┄┄┄┄
7 11 => 11 10 9
6 10 7 6 5
5 9
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, then get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's just a straightforward nested loop, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
5 6 7 8 => 8 12
9 10 11 12 7 11
6 10
5 9
Now it's8 12
the top row we want, again! Chop it off and repeat:
11 10 9 => 9 5
7 6 5 10 6
11 7
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, andthen get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's prettyjust a straightforward nested loop, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this. But what if we turn it 90o counter-clockwise?
8 12
7 11
6 10
5 9
Now it's the top row we want, again! Chop it off and repeat:
11 10 9
7 6 5
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, and get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's pretty straightforward, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
I'm not sure what exactly about your code made it a "show-stopper." Your use of tmpArray
has a bit of a smell to it. There are issues of readability (why does the line for(var i=c1;i<=c2;i++)
only have one space in it??). Your core algorithm (getUpperCorner
and getLowerCorner
) isn't documented and is fairly hard to read.
Personally, though, I would have approached this as a straight recursion problem. So you have the following matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Your "spiral" begins with the the top row, 1 2 3 4
. So chop that off and save it. Now you're left with:
5 6 7 8
9 10 11 12
The next part of the spiral is the rightmost column of this, 8 12
. But what if we turn it 90o counter-clockwise?
5 6 7 8 => 8 12
9 10 11 12 7 11
6 10
5 9
Now 8 12
the top row! Chop it off and repeat:
11 10 9 => 9 5
7 6 5 10 6
11 7
By now you've seen the pattern. To get a matrix's "spiral" you just take off the top row, rotate what's left, then get its spiral, and so on.
Def Spiralify( Matrix )
If( Matrix has only one row )
Return( the row )
Else
FirstRow := first row of Matrix
RestOfMatrix := all of Matrix except the first row
NextMatrix := RotateLeft( RestOfMatrix )
NextSpiral := Spiralify( NextMatrix )
Result := JoinArrays( FirstRow, NextSpiral )
Return( Result )
RotateLeft
will actually make up the most lines of code, but it's just a straightforward nested loop, and the rest basically writes itself.
Update
I had some time to write up an actual implementation. Rather than paste it here you can check it out on jsFiddle.
- 2.5k
- 1
- 20
- 29