Anti clock wise fashion
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clock wise 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
5
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clock wise 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
56 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
From this stackoverflow question
Given a 2D array of size $ M x N $, output the values in a anti-clock wise 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-clock wise 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 53 mins ago
asked 1 hour ago
Luis felipe De jesus Munoz
3,20111047
3,20111047
So are we going clockwise or counterclockwise?
â LegionMammal978
56 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago
add a comment |Â
So are we going clockwise or counterclockwise?
â LegionMammal978
56 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago
So are we going clockwise or counterclockwise?
â LegionMammal978
56 mins ago
So are we going clockwise or counterclockwise?
â LegionMammal978
56 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
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
2
down vote
R, 59 bytes
f=function(m)"if"(ncol(m)-1,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:
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
13 mins 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
11 mins ago
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
Zá¸ÂUÃÂìḢ≬áºÂ
Try it online!
add a comment |Â
up vote
1
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 |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
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
2
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
2
down vote
up vote
2
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 24 mins ago
TFeld
12.4k2834
12.4k2834
add a comment |Â
add a comment |Â
up vote
2
down vote
R, 59 bytes
f=function(m)"if"(ncol(m)-1,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:
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
13 mins 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
11 mins ago
add a comment |Â
up vote
2
down vote
R, 59 bytes
f=function(m)"if"(ncol(m)-1,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:
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
13 mins 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
11 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
R, 59 bytes
f=function(m)"if"(ncol(m)-1,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:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
R, 59 bytes
f=function(m)"if"(ncol(m)-1,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:
f <- function(m)
if(ncol(m) == 1)
m
else
c(m[,1], f(t(m[nrow(m):1,-1])))
answered 18 mins ago
ngm
2,65922
2,65922
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
13 mins 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
11 mins ago
add a comment |Â
wow! I love how the use oft()
prevents thedrop=TRUE
default for`[`
from screwing up theif
condition!
â Giuseppe
13 mins 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
11 mins ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
13 mins ago
wow! I love how the use of
t()
prevents the drop=TRUE
default for `[`
from screwing up the if
condition!â Giuseppe
13 mins 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
11 mins 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
11 mins ago
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 28 mins ago
Erik the Outgolfer
29.8k42899
29.8k42899
add a comment |Â
add a comment |Â
up vote
1
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
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
up vote
1
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 18 mins ago
Mnemonic
4,4721629
4,4721629
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%2fanti-clock-wise-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
56 mins ago
@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
â Luis felipe De jesus Munoz
55 mins ago