Output the anti-clockwise inward spiral of a 2D array
Clash Royale CLAN TAG#URR8PPP
up vote
14
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.
Example Given:
$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$
The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.
Now we repeat the process for the inner values. This will end up with a matrix like the following
$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$
And the inner values is then $ 6,10,11,7 $
The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $
Rules
- Assume non-empty input
- Assume matrix values as positive integers
- Standard I/O Methods apply
- Standard code-golf rules and winning criteria apply
Some test cases
Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13
--------------------------------------------------------
Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8
-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6
code-golf matrix
add a comment |Â
up vote
14
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.
Example Given:
$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$
The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.
Now we repeat the process for the inner values. This will end up with a matrix like the following
$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$
And the inner values is then $ 6,10,11,7 $
The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $
Rules
- Assume non-empty input
- Assume matrix values as positive integers
- Standard I/O Methods apply
- Standard code-golf rules and winning criteria apply
Some test cases
Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13
--------------------------------------------------------
Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8
-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6
code-golf matrix
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
5
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago
add a comment |Â
up vote
14
down vote
favorite
up vote
14
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.
Example Given:
$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$
The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.
Now we repeat the process for the inner values. This will end up with a matrix like the following
$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$
And the inner values is then $ 6,10,11,7 $
The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $
Rules
- Assume non-empty input
- Assume matrix values as positive integers
- Standard I/O Methods apply
- Standard code-golf rules and winning criteria apply
Some test cases
Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13
--------------------------------------------------------
Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8
-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6
code-golf matrix
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clockwise fashion. The output must start from the outside to the inside and the initial point always is going to be $(0,0)$.
Example Given:
$$ beginbmatrix colorblue1&colorred2&colorred3&colorred4 \ colorred5&6&7&colorred8 \ colorred9&10&11&colorred12 \ colorred13&colorred14&colorred15&colorred16endbmatrix $$
The edge values in counterclockwise is then $ 1,5,9,13,14,15,16,12,8,4,3,2 $.
Now we repeat the process for the inner values. This will end up with a matrix like the following
$$ beginbmatrix colorblue6&colorred7 \ colorred10&colorred11 endbmatrix$$
And the inner values is then $ 6,10,11,7 $
The final result will be then $ 1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7 $
Rules
- Assume non-empty input
- Assume matrix values as positive integers
- Standard I/O Methods apply
- Standard code-golf rules and winning criteria apply
Some test cases
Input
[
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10,11,12,13,14],
[15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13
--------------------------------------------------------
Input
[
[1,2,3],
[3,2,1],
[4,5,6],
[6,5,4],
[7,8,9],
[9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8
-----------------------------------------------------
Input
[
[1]
]
Output
1
-----------------------------------
Input
[
[1, 2],
[2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
[1,2,3,6,7],
[2,4,3,2,1],
[3,2,4,5,6],
[6,5,6,5,4],
[10,4,7,8,9],
[12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6
code-golf matrix
code-golf matrix
edited 7 mins ago
hakr14
1,16529
1,16529
asked yesterday
Luis felipe De jesus Munoz
3,24611049
3,24611049
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
5
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago
add a comment |Â
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
5
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
5
5
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago
add a comment |Â
14 Answers
14
active
oldest
votes
up vote
11
down vote
R, 54 bytes
Several bytes saved by @Giuseppe and @J.Doe.
f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))
Try it online!
Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
It was pointed out that ncol(m)
could be golfed to sum(m)
to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised byt()
to not have to use anis.null
test that was in my original attempts.
â ngm
22 hours ago
Isn't that lastm
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
â J.Doe
21 hours ago
add a comment |Â
up vote
9
down vote
Python 2, 52 bytes
f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()
Try it online!
add a comment |Â
up vote
7
down vote
Pyth, 9 bytes
shMM.utC_
Try it here!
How?
shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
.u Until a result that has occurred before is found, loop and collect...
_ Reverse the matrix (reverse the order of its rows).
C Transpose.
t Remove first element.
hMM For each element in the resulting 3D array, get the heads of its elements.
s Flatten.
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
add a comment |Â
up vote
5
down vote
Stax, 7 bytes
ôQÃÂG÷ÃÂ<
Run and debug it
It takes an array of rows on one line, and produces newline-separated output.
Unpacked, ungolfed, and commented, it looks like this.
W repeat the rest of the program until cancelled explicitly
rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
|c assert matrix is truthy. (has rows) cancel otherwise.
B remove the top row of the matrix, and push separately to main stack
rm reverse the top row (this fixes the rotation direction), and print each value
Run this one
add a comment |Â
up vote
4
down vote
Pyth, 20 bytes
J.TQWJ=+YhJ=J_.TtJ)Y
Try it here
Explanation
J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ Call the transposed input J.
WJ ) While J is not empty...
=+YhJ ... put the top row into Y (initially )...
=J tJ ... remove the top row...
_.T ... reverse and transpose (rotate clockwise).
Y Output the result.
add a comment |Â
up vote
3
down vote
Clean, 69 bytes
import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=
@o?
Try it online!
Moves the next row/column to the head of the list so it can pattern matched in the argument.
For the first example in the challenge, this looks like:
@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @
add a comment |Â
up vote
3
down vote
oK, 12 bytes
*+,/(1_+|:)
Try it online!
This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:)
.
+|: /reverse, then transpose (rotate right)
1_ /remove first line
( ) /fixpoint of the above, keeping intermediate results (scan)
,/ /concatenate all the rows
*+ /get the first element of each row
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 89 bytes
f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:
Try it online!
Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.
add a comment |Â
up vote
2
down vote
05AB1E, 13 11 bytes
ÃÂRøÃÂRÃÂ}ïÃÂõK
-2 bytes thanks to @Emigna.
Try it online or verify all test cases.
Explanation:
ÃÂ # Loop until the stack no longer changes:
R # Reverse the order of the rows
# i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# â [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
ø # Zip, swapping rows and column
# â [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
ÃÂ # Head extracted
# â [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
R # Reverse this row
# â [1,5,9,13]
ÃÂ # Pop and push it to the global array
} # After the loop:
ï # Push the global array
# i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
ÃÂ # Flatten it
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
õK # Remove all empty string (and output it implicitly)
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]
add a comment |Â
up vote
2
down vote
Julia 1.0, 50 bytes
f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]
Try it online!
Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.
E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):
f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]
However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
add a comment |Â
up vote
1
down vote
Charcoal, 25 bytes
âÂÂâ®ÂEAâ®ÂùøWøëâÂÂEçøâ°â®ÂEøçüûøIâÂÂø
Try it online! Link is to verbose version of code. Explanation:
âÂÂâ®ÂEAâ®Âùø
Rotate the input by 180ð. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)
Wøë
Repeat until the array is empty.
âÂÂEçøâ°â®ÂEøçüûø
Rotate the array by 90ð.
IâÂÂø
Remove the last row of the array and print the element as strings on separate lines.
add a comment |Â
up vote
1
down vote
Ruby, 65 bytes
->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x
Try it online!
add a comment |Â
up vote
1
down vote
APL (Dyalog), 24 22 bytes
ÃÂâ¢âµ:âµ[;1],âÂÂâÂÂâÂÂ0 1âÂÂâµâÂÂâ¬
Try it online!
add a comment |Â
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
R, 54 bytes
Several bytes saved by @Giuseppe and @J.Doe.
f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))
Try it online!
Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
It was pointed out that ncol(m)
could be golfed to sum(m)
to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised byt()
to not have to use anis.null
test that was in my original attempts.
â ngm
22 hours ago
Isn't that lastm
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
â J.Doe
21 hours ago
add a comment |Â
up vote
11
down vote
R, 54 bytes
Several bytes saved by @Giuseppe and @J.Doe.
f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))
Try it online!
Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
It was pointed out that ncol(m)
could be golfed to sum(m)
to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised byt()
to not have to use anis.null
test that was in my original attempts.
â ngm
22 hours ago
Isn't that lastm
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
â J.Doe
21 hours ago
add a comment |Â
up vote
11
down vote
up vote
11
down vote
R, 54 bytes
Several bytes saved by @Giuseppe and @J.Doe.
f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))
Try it online!
Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
It was pointed out that ncol(m)
could be golfed to sum(m)
to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)
R, 54 bytes
Several bytes saved by @Giuseppe and @J.Doe.
f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))
Try it online!
Recursively strip off the first column and row-reverse/transpose (making the bottom row the new first column) the rest of the matrix until you end up with only one column. Ungolfed "traditional" version:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
It was pointed out that ncol(m)
could be golfed to sum(m)
to save another byte because we are allowed to assume positive integer matrix values. But I'll leave it like this since it works for all matrices (even matrices of strings!)
edited 2 hours ago
answered 23 hours ago
ngm
2,74923
2,74923
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised byt()
to not have to use anis.null
test that was in my original attempts.
â ngm
22 hours ago
Isn't that lastm
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
â J.Doe
21 hours ago
add a comment |Â
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised byt()
to not have to use anis.null
test that was in my original attempts.
â ngm
22 hours ago
Isn't that lastm
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.
â J.Doe
21 hours ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
22 hours ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
and full disclosure, I had about a 200 byte solution that didn't even work, so kudos to ya! I'll probably get around to awarding you a bounty for this once the question is eligible for a bounty.
â Giuseppe
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised by
t()
to not have to use an is.null
test that was in my original attempts.â ngm
22 hours ago
@Giuseppe back to 59 bytes! I was pleasantly surprised by
t()
to not have to use an is.null
test that was in my original attempts.â ngm
22 hours ago
Isn't that last
m
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.â J.Doe
21 hours ago
Isn't that last
m
going to be null anyway, so you can change the if-statement for 54 bytes. Seems to work for the test cases.â J.Doe
21 hours ago
add a comment |Â
up vote
9
down vote
Python 2, 52 bytes
f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()
Try it online!
add a comment |Â
up vote
9
down vote
Python 2, 52 bytes
f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()
Try it online!
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Python 2, 52 bytes
f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()
Try it online!
Python 2, 52 bytes
f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()
Try it online!
answered 23 hours ago
TFeld
12.5k2835
12.5k2835
add a comment |Â
add a comment |Â
up vote
7
down vote
Pyth, 9 bytes
shMM.utC_
Try it here!
How?
shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
.u Until a result that has occurred before is found, loop and collect...
_ Reverse the matrix (reverse the order of its rows).
C Transpose.
t Remove first element.
hMM For each element in the resulting 3D array, get the heads of its elements.
s Flatten.
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
add a comment |Â
up vote
7
down vote
Pyth, 9 bytes
shMM.utC_
Try it here!
How?
shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
.u Until a result that has occurred before is found, loop and collect...
_ Reverse the matrix (reverse the order of its rows).
C Transpose.
t Remove first element.
hMM For each element in the resulting 3D array, get the heads of its elements.
s Flatten.
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Pyth, 9 bytes
shMM.utC_
Try it here!
How?
shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
.u Until a result that has occurred before is found, loop and collect...
_ Reverse the matrix (reverse the order of its rows).
C Transpose.
t Remove first element.
hMM For each element in the resulting 3D array, get the heads of its elements.
s Flatten.
Pyth, 9 bytes
shMM.utC_
Try it here!
How?
shMM.utC_ Full program. Takes a 2D array (matrix) from STDIN.
.u Until a result that has occurred before is found, loop and collect...
_ Reverse the matrix (reverse the order of its rows).
C Transpose.
t Remove first element.
hMM For each element in the resulting 3D array, get the heads of its elements.
s Flatten.
answered 20 hours ago
Mr. Xcoder
30.9k758195
30.9k758195
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
add a comment |Â
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
19 hours ago
add a comment |Â
up vote
5
down vote
Stax, 7 bytes
ôQÃÂG÷ÃÂ<
Run and debug it
It takes an array of rows on one line, and produces newline-separated output.
Unpacked, ungolfed, and commented, it looks like this.
W repeat the rest of the program until cancelled explicitly
rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
|c assert matrix is truthy. (has rows) cancel otherwise.
B remove the top row of the matrix, and push separately to main stack
rm reverse the top row (this fixes the rotation direction), and print each value
Run this one
add a comment |Â
up vote
5
down vote
Stax, 7 bytes
ôQÃÂG÷ÃÂ<
Run and debug it
It takes an array of rows on one line, and produces newline-separated output.
Unpacked, ungolfed, and commented, it looks like this.
W repeat the rest of the program until cancelled explicitly
rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
|c assert matrix is truthy. (has rows) cancel otherwise.
B remove the top row of the matrix, and push separately to main stack
rm reverse the top row (this fixes the rotation direction), and print each value
Run this one
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Stax, 7 bytes
ôQÃÂG÷ÃÂ<
Run and debug it
It takes an array of rows on one line, and produces newline-separated output.
Unpacked, ungolfed, and commented, it looks like this.
W repeat the rest of the program until cancelled explicitly
rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
|c assert matrix is truthy. (has rows) cancel otherwise.
B remove the top row of the matrix, and push separately to main stack
rm reverse the top row (this fixes the rotation direction), and print each value
Run this one
Stax, 7 bytes
ôQÃÂG÷ÃÂ<
Run and debug it
It takes an array of rows on one line, and produces newline-separated output.
Unpacked, ungolfed, and commented, it looks like this.
W repeat the rest of the program until cancelled explicitly
rM rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
|c assert matrix is truthy. (has rows) cancel otherwise.
B remove the top row of the matrix, and push separately to main stack
rm reverse the top row (this fixes the rotation direction), and print each value
Run this one
answered 21 hours ago
recursive
4,6061220
4,6061220
add a comment |Â
add a comment |Â
up vote
4
down vote
Pyth, 20 bytes
J.TQWJ=+YhJ=J_.TtJ)Y
Try it here
Explanation
J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ Call the transposed input J.
WJ ) While J is not empty...
=+YhJ ... put the top row into Y (initially )...
=J tJ ... remove the top row...
_.T ... reverse and transpose (rotate clockwise).
Y Output the result.
add a comment |Â
up vote
4
down vote
Pyth, 20 bytes
J.TQWJ=+YhJ=J_.TtJ)Y
Try it here
Explanation
J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ Call the transposed input J.
WJ ) While J is not empty...
=+YhJ ... put the top row into Y (initially )...
=J tJ ... remove the top row...
_.T ... reverse and transpose (rotate clockwise).
Y Output the result.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Pyth, 20 bytes
J.TQWJ=+YhJ=J_.TtJ)Y
Try it here
Explanation
J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ Call the transposed input J.
WJ ) While J is not empty...
=+YhJ ... put the top row into Y (initially )...
=J tJ ... remove the top row...
_.T ... reverse and transpose (rotate clockwise).
Y Output the result.
Pyth, 20 bytes
J.TQWJ=+YhJ=J_.TtJ)Y
Try it here
Explanation
J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ Call the transposed input J.
WJ ) While J is not empty...
=+YhJ ... put the top row into Y (initially )...
=J tJ ... remove the top row...
_.T ... reverse and transpose (rotate clockwise).
Y Output the result.
answered 23 hours ago
Mnemonic
4,5021629
4,5021629
add a comment |Â
add a comment |Â
up vote
3
down vote
Clean, 69 bytes
import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=
@o?
Try it online!
Moves the next row/column to the head of the list so it can pattern matched in the argument.
For the first example in the challenge, this looks like:
@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @
add a comment |Â
up vote
3
down vote
Clean, 69 bytes
import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=
@o?
Try it online!
Moves the next row/column to the head of the list so it can pattern matched in the argument.
For the first example in the challenge, this looks like:
@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Clean, 69 bytes
import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=
@o?
Try it online!
Moves the next row/column to the head of the list so it can pattern matched in the argument.
For the first example in the challenge, this looks like:
@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @
Clean, 69 bytes
import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=
@o?
Try it online!
Moves the next row/column to the head of the list so it can pattern matched in the argument.
For the first example in the challenge, this looks like:
@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @
edited 19 hours ago
answered 19 hours ago
ÃÂurous
5,42311031
5,42311031
add a comment |Â
add a comment |Â
up vote
3
down vote
oK, 12 bytes
*+,/(1_+|:)
Try it online!
This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:)
.
+|: /reverse, then transpose (rotate right)
1_ /remove first line
( ) /fixpoint of the above, keeping intermediate results (scan)
,/ /concatenate all the rows
*+ /get the first element of each row
add a comment |Â
up vote
3
down vote
oK, 12 bytes
*+,/(1_+|:)
Try it online!
This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:)
.
+|: /reverse, then transpose (rotate right)
1_ /remove first line
( ) /fixpoint of the above, keeping intermediate results (scan)
,/ /concatenate all the rows
*+ /get the first element of each row
add a comment |Â
up vote
3
down vote
up vote
3
down vote
oK, 12 bytes
*+,/(1_+|:)
Try it online!
This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:)
.
+|: /reverse, then transpose (rotate right)
1_ /remove first line
( ) /fixpoint of the above, keeping intermediate results (scan)
,/ /concatenate all the rows
*+ /get the first element of each row
oK, 12 bytes
*+,/(1_+|:)
Try it online!
This abuses the fact that oK doesn't seem to care too much about shape for transposition. In k this would be 13 bytes: *:',/(1_+|:)
.
+|: /reverse, then transpose (rotate right)
1_ /remove first line
( ) /fixpoint of the above, keeping intermediate results (scan)
,/ /concatenate all the rows
*+ /get the first element of each row
edited 7 hours ago
answered 17 hours ago
zgrep
1,18139
1,18139
add a comment |Â
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 89 bytes
f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:
Try it online!
Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 89 bytes
f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:
Try it online!
Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
JavaScript (Node.js), 89 bytes
f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:
Try it online!
Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.
JavaScript (Node.js), 89 bytes
f=a=>a>?[...a.map(x=>x[0]),...f(a[0].map((_,i)=>a.map(y=>y[i]).reverse()).slice(1))]:
Try it online!
Takes the first column, transpose the remaining one then reverse each row (= rotate the matrix 90 degrees CW), and then repeat until the array has no more entries.
answered 16 hours ago
Shieru Asakoto
2,040312
2,040312
add a comment |Â
add a comment |Â
up vote
2
down vote
05AB1E, 13 11 bytes
ÃÂRøÃÂRÃÂ}ïÃÂõK
-2 bytes thanks to @Emigna.
Try it online or verify all test cases.
Explanation:
ÃÂ # Loop until the stack no longer changes:
R # Reverse the order of the rows
# i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# â [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
ø # Zip, swapping rows and column
# â [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
ÃÂ # Head extracted
# â [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
R # Reverse this row
# â [1,5,9,13]
ÃÂ # Pop and push it to the global array
} # After the loop:
ï # Push the global array
# i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
ÃÂ # Flatten it
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
õK # Remove all empty string (and output it implicitly)
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]
add a comment |Â
up vote
2
down vote
05AB1E, 13 11 bytes
ÃÂRøÃÂRÃÂ}ïÃÂõK
-2 bytes thanks to @Emigna.
Try it online or verify all test cases.
Explanation:
ÃÂ # Loop until the stack no longer changes:
R # Reverse the order of the rows
# i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# â [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
ø # Zip, swapping rows and column
# â [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
ÃÂ # Head extracted
# â [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
R # Reverse this row
# â [1,5,9,13]
ÃÂ # Pop and push it to the global array
} # After the loop:
ï # Push the global array
# i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
ÃÂ # Flatten it
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
õK # Remove all empty string (and output it implicitly)
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
05AB1E, 13 11 bytes
ÃÂRøÃÂRÃÂ}ïÃÂõK
-2 bytes thanks to @Emigna.
Try it online or verify all test cases.
Explanation:
ÃÂ # Loop until the stack no longer changes:
R # Reverse the order of the rows
# i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# â [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
ø # Zip, swapping rows and column
# â [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
ÃÂ # Head extracted
# â [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
R # Reverse this row
# â [1,5,9,13]
ÃÂ # Pop and push it to the global array
} # After the loop:
ï # Push the global array
# i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
ÃÂ # Flatten it
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
õK # Remove all empty string (and output it implicitly)
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]
05AB1E, 13 11 bytes
ÃÂRøÃÂRÃÂ}ïÃÂõK
-2 bytes thanks to @Emigna.
Try it online or verify all test cases.
Explanation:
ÃÂ # Loop until the stack no longer changes:
R # Reverse the order of the rows
# i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
# â [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
ø # Zip, swapping rows and column
# â [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
ÃÂ # Head extracted
# â [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
R # Reverse this row
# â [1,5,9,13]
ÃÂ # Pop and push it to the global array
} # After the loop:
ï # Push the global array
# i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
ÃÂ # Flatten it
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
õK # Remove all empty string (and output it implicitly)
# â [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7]
edited 8 hours ago
answered 9 hours ago
Kevin Cruijssen
31.7k553171
31.7k553171
add a comment |Â
add a comment |Â
up vote
2
down vote
Julia 1.0, 50 bytes
f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]
Try it online!
Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.
E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):
f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]
However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!
add a comment |Â
up vote
2
down vote
Julia 1.0, 50 bytes
f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]
Try it online!
Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.
E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):
f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]
However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Julia 1.0, 50 bytes
f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]
Try it online!
Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.
E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):
f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]
However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!
Julia 1.0, 50 bytes
f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;end]
Try it online!
Julia has a convenient built-in to rotate the matrix by 90 degrees eliminating the need for transpose-reverse operations.
Oddly enough, the shortest way I found to break out of recursion is by using a try-catch block.
E.g., using a normal ternary conditional yields 53 bytes (the whitespaces are mandatory in 1.0 release, with older versions they are actually removable for 49 bytes):
f(m)=[m[:,1];isempty(m) ? : f(rotr90(m[:,2:end]))]
However, I'm very new to Julia, so could easily miss a golfier way. Please let me know if you are aware of any!
edited 3 hours ago
answered 3 hours ago
Kirill L.
2,8961117
2,8961117
add a comment |Â
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
answered 23 hours ago
Erik the Outgolfer
29.8k42899
29.8k42899
add a comment |Â
add a comment |Â
up vote
1
down vote
Charcoal, 25 bytes
âÂÂâ®ÂEAâ®ÂùøWøëâÂÂEçøâ°â®ÂEøçüûøIâÂÂø
Try it online! Link is to verbose version of code. Explanation:
âÂÂâ®ÂEAâ®Âùø
Rotate the input by 180ð. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)
Wøë
Repeat until the array is empty.
âÂÂEçøâ°â®ÂEøçüûø
Rotate the array by 90ð.
IâÂÂø
Remove the last row of the array and print the element as strings on separate lines.
add a comment |Â
up vote
1
down vote
Charcoal, 25 bytes
âÂÂâ®ÂEAâ®ÂùøWøëâÂÂEçøâ°â®ÂEøçüûøIâÂÂø
Try it online! Link is to verbose version of code. Explanation:
âÂÂâ®ÂEAâ®Âùø
Rotate the input by 180ð. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)
Wøë
Repeat until the array is empty.
âÂÂEçøâ°â®ÂEøçüûø
Rotate the array by 90ð.
IâÂÂø
Remove the last row of the array and print the element as strings on separate lines.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Charcoal, 25 bytes
âÂÂâ®ÂEAâ®ÂùøWøëâÂÂEçøâ°â®ÂEøçüûøIâÂÂø
Try it online! Link is to verbose version of code. Explanation:
âÂÂâ®ÂEAâ®Âùø
Rotate the input by 180ð. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)
Wøë
Repeat until the array is empty.
âÂÂEçøâ°â®ÂEøçüûø
Rotate the array by 90ð.
IâÂÂø
Remove the last row of the array and print the element as strings on separate lines.
Charcoal, 25 bytes
âÂÂâ®ÂEAâ®ÂùøWøëâÂÂEçøâ°â®ÂEøçüûøIâÂÂø
Try it online! Link is to verbose version of code. Explanation:
âÂÂâ®ÂEAâ®Âùø
Rotate the input by 180ð. This is for two reasons: a) the last row is the easiest to remove, and b) it's easier to loop if the row is removed at the end of the loop. (I did try reflecting and outputting clockwise but that took an extra byte.)
Wøë
Repeat until the array is empty.
âÂÂEçøâ°â®ÂEøçüûø
Rotate the array by 90ð.
IâÂÂø
Remove the last row of the array and print the element as strings on separate lines.
edited 7 hours ago
answered 21 hours ago
Neil
76.4k744173
76.4k744173
add a comment |Â
add a comment |Â
up vote
1
down vote
Ruby, 65 bytes
->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x
Try it online!
add a comment |Â
up vote
1
down vote
Ruby, 65 bytes
->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Ruby, 65 bytes
->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x
Try it online!
Ruby, 65 bytes
->ax,*a=a.transpose;(w,*a=a.transpose.reverse;x+=w)while a[0];x
Try it online!
answered 5 hours ago
G B
6,9871326
6,9871326
add a comment |Â
add a comment |Â
up vote
1
down vote
APL (Dyalog), 24 22 bytes
ÃÂâ¢âµ:âµ[;1],âÂÂâÂÂâÂÂ0 1âÂÂâµâÂÂâ¬
Try it online!
add a comment |Â
up vote
1
down vote
APL (Dyalog), 24 22 bytes
ÃÂâ¢âµ:âµ[;1],âÂÂâÂÂâÂÂ0 1âÂÂâµâÂÂâ¬
Try it online!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
APL (Dyalog), 24 22 bytes
ÃÂâ¢âµ:âµ[;1],âÂÂâÂÂâÂÂ0 1âÂÂâµâÂÂâ¬
Try it online!
APL (Dyalog), 24 22 bytes
ÃÂâ¢âµ:âµ[;1],âÂÂâÂÂâÂÂ0 1âÂÂâµâÂÂâ¬
Try it online!
answered 2 hours ago
Zacharý
4,90511035
4,90511035
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173869%2foutput-the-anti-clockwise-inward-spiral-of-a-2d-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
So are we going clockwise or counterclockwise?
â LegionMammal978
23 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
23 hours ago
5
Anti- and counter-clockwise are both correct, with each more common in BrEng and AmEng, respectively. If you really want to confuse, you could use widdershins as well.
â Digital Trauma
22 hours ago