Output a 2D Array in an Anti-clockwise Fashion

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    5 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    5 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
    4 hours ago















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









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    5 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    5 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
    4 hours ago













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 35 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
    5 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    5 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
    4 hours ago

















  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    5 hours ago










  • @LegionMammal978 counterclockwise (I though it was called anti-clockwise)
    – Luis felipe De jesus Munoz
    5 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
    4 hours ago
















So are we going clockwise or counterclockwise?
– LegionMammal978
5 hours ago




So are we going clockwise or counterclockwise?
– LegionMammal978
5 hours ago












@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
– Luis felipe De jesus Munoz
5 hours ago




@LegionMammal978 counterclockwise (I though it was called anti-clockwise)
– Luis felipe De jesus Munoz
5 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
4 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
4 hours ago











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])))







share|improve this answer






















  • 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










  • @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
    4 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
    3 hours ago











  • And since the matrix values are all positive integers, replace ncol by sum for 53 bytes :-)
    – J.Doe
    2 hours ago

















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!






share|improve this answer



























    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.





    share|improve this answer



























      up vote
      2
      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.





      share|improve this answer




















      • Thats cool. As a Pyth beginner I now know I have a lot to learn,
        – ElPedro
        51 mins ago

















      up vote
      1
      down vote














      Jelly, 9 bytes



      ZḊUƊƬḢ€€Ẏ


      Try it online!






      share|improve this answer



























        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






        share|improve this answer



























          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.






          share|improve this answer



























            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] ++ @





            share|improve this answer






















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "200"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              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






























              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])))







              share|improve this answer






















              • 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










              • @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
                4 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
                3 hours ago











              • And since the matrix values are all positive integers, replace ncol by sum for 53 bytes :-)
                – J.Doe
                2 hours ago














              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])))







              share|improve this answer






















              • 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










              • @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
                4 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
                3 hours ago











              • And since the matrix values are all positive integers, replace ncol by sum for 53 bytes :-)
                – J.Doe
                2 hours ago












              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])))







              share|improve this answer















              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])))








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 4 hours ago









              ngm

              2,71922




              2,71922











              • 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










              • @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
                4 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
                3 hours ago











              • And since the matrix values are all positive integers, replace ncol by sum 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










              • 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
                4 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
                3 hours ago











              • And since the matrix values are all positive integers, replace ncol by sum 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
              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
              4 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
              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
              3 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










              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!






              share|improve this answer
























                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!






                share|improve this answer






















                  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!






                  share|improve this answer













                  Python 2, 52 bytes





                  f=lambda a:a and zip(*a)[0]+f(zip(*a[::-1])[1:])or()


                  Try it online!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  TFeld

                  12.4k2835




                  12.4k2835




















                      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.





                      share|improve this answer
























                        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.





                        share|improve this answer






















                          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.





                          share|improve this answer












                          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.






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 4 hours ago









                          Mnemonic

                          4,4921629




                          4,4921629




















                              up vote
                              2
                              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.





                              share|improve this answer




















                              • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                – ElPedro
                                51 mins ago














                              up vote
                              2
                              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.





                              share|improve this answer




















                              • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                – ElPedro
                                51 mins ago












                              up vote
                              2
                              down vote










                              up vote
                              2
                              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.





                              share|improve this answer













                              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.






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              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
                                51 mins ago
















                              • Thats cool. As a Pyth beginner I now know I have a lot to learn,
                                – ElPedro
                                51 mins ago















                              Thats cool. As a Pyth beginner I now know I have a lot to learn,
                              – ElPedro
                              51 mins ago




                              Thats cool. As a Pyth beginner I now know I have a lot to learn,
                              – ElPedro
                              51 mins ago










                              up vote
                              1
                              down vote














                              Jelly, 9 bytes



                              ZḊUƊƬḢ€€Ẏ


                              Try it online!






                              share|improve this answer
























                                up vote
                                1
                                down vote














                                Jelly, 9 bytes



                                ZḊUƊƬḢ€€Ẏ


                                Try it online!






                                share|improve this answer






















                                  up vote
                                  1
                                  down vote










                                  up vote
                                  1
                                  down vote










                                  Jelly, 9 bytes



                                  ZḊUƊƬḢ€€Ẏ


                                  Try it online!






                                  share|improve this answer













                                  Jelly, 9 bytes



                                  ZḊUƊƬḢ€€Ẏ


                                  Try it online!







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 4 hours ago









                                  Erik the Outgolfer

                                  29.8k42899




                                  29.8k42899




















                                      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






                                      share|improve this answer
























                                        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






                                        share|improve this answer






















                                          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






                                          share|improve this answer













                                          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







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 3 hours ago









                                          recursive

                                          4,5661220




                                          4,5661220




















                                              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.






                                              share|improve this answer
























                                                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.






                                                share|improve this answer






















                                                  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.






                                                  share|improve this answer













                                                  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.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered 3 hours ago









                                                  Neil

                                                  76.4k744173




                                                  76.4k744173




















                                                      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] ++ @





                                                      share|improve this answer


























                                                        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] ++ @





                                                        share|improve this answer
























                                                          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] ++ @





                                                          share|improve this answer















                                                          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] ++ @






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 51 mins ago

























                                                          answered 1 hour ago









                                                          Οurous

                                                          5,39311031




                                                          5,39311031



























                                                               

                                                              draft saved


                                                              draft discarded















































                                                               


                                                              draft saved


                                                              draft discarded














                                                              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













































































                                                              Comments

                                                              Popular posts from this blog

                                                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                              What does second last employer means? [closed]

                                                              One-line joke