Output a 2D Array in an Anti-clockwise Fashion
Clash Royale CLAN TAG#URR8PPP
up vote
7
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
7
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
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
3
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
3 hours ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
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 16 mins ago
Veskah
48110
48110
asked 5 hours ago
Luis felipe De jesus Munoz
3,21111047
3,21111047
So are we going clockwise or counterclockwise?
â LegionMammal978
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
3
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
3 hours ago
add a comment |Â
So are we going clockwise or counterclockwise?
â LegionMammal978
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
3
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
3 hours ago
So are we going clockwise or counterclockwise?
â LegionMammal978
4 hours ago
So are we going clockwise or counterclockwise?
â LegionMammal978
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
3
3
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
3 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
3 hours ago
add a comment |Â
8 Answers
8
active
oldest
votes
up vote
8
down vote
R, 54 bytes
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 rotate 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])))
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
4 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
4 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
3 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
2 hours ago
And since the matrix values are all positive integers, replacencol
bysum
for 53 bytes :-)
â J.Doe
2 hours ago
add a comment |Â
up vote
5
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
3
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
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
add a comment |Â
up vote
1
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
1
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
32 mins ago
add a comment |Â
up vote
0
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
0
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 |Â
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
R, 54 bytes
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 rotate 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])))
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
4 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
4 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
3 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
2 hours ago
And since the matrix values are all positive integers, replacencol
bysum
for 53 bytes :-)
â J.Doe
2 hours ago
add a comment |Â
up vote
8
down vote
R, 54 bytes
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 rotate 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])))
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
4 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
4 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
3 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
2 hours ago
And since the matrix values are all positive integers, replacencol
bysum
for 53 bytes :-)
â J.Doe
2 hours ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
R, 54 bytes
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 rotate 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])))
R, 54 bytes
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 rotate 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])))
edited 2 hours ago
answered 4 hours ago
ngm
2,71922
2,71922
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
4 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
4 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
3 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
2 hours ago
And since the matrix values are all positive integers, replacencol
bysum
for 53 bytes :-)
â J.Doe
2 hours ago
add a comment |Â
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
4 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
4 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
3 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
2 hours ago
And since the matrix values are all positive integers, replacencol
bysum
for 53 bytes :-)
â J.Doe
2 hours ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
4 hours ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
4 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
4 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
4 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
3 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
3 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
2 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
2 hours ago
And since the matrix values are all positive integers, replace
ncol
by sum
for 53 bytes :-)â J.Doe
2 hours ago
And since the matrix values are all positive integers, replace
ncol
by sum
for 53 bytes :-)â J.Doe
2 hours ago
add a comment |Â
up vote
5
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
5
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
5
down vote
up vote
5
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 4 hours ago
TFeld
12.4k2835
12.4k2835
add a comment |Â
add a comment |Â
up vote
3
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
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
up vote
3
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 4 hours ago
Mnemonic
4,4921629
4,4921629
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 4 hours ago
Erik the Outgolfer
29.8k42899
29.8k42899
add a comment |Â
add a comment |Â
up vote
1
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
1
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
1
down vote
up vote
1
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 2 hours ago
recursive
4,5661220
4,5661220
add a comment |Â
add a comment |Â
up vote
1
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
32 mins ago
add a comment |Â
up vote
1
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
32 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
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 1 hour ago
Mr. Xcoder
30.8k758195
30.8k758195
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
32 mins ago
add a comment |Â
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
32 mins ago
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
32 mins ago
Thats cool. As a Pyth beginner I now know I have a lot to learn,
â ElPedro
32 mins ago
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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.
answered 3 hours ago
Neil
76.4k744173
76.4k744173
add a comment |Â
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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 31 mins ago
answered 41 mins ago
ÃÂurous
5,39311031
5,39311031
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-a-2d-array-in-an-anti-clockwise-fashion%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
4 hours ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
4 hours ago
3
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
3 hours ago