Anti clock wise fashion

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









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    57 mins ago










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














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









share|improve this question























  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    57 mins ago










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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 54 mins ago

























asked 1 hour ago









Luis felipe De jesus Munoz

3,20111047




3,20111047











  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    57 mins ago










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
















  • So are we going clockwise or counterclockwise?
    – LegionMammal978
    57 mins ago










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















So are we going clockwise or counterclockwise?
– LegionMammal978
57 mins ago




So are we going clockwise or counterclockwise?
– LegionMammal978
57 mins ago












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




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










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!






share|improve this answer



























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







    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
      14 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
      12 mins ago

















    up vote
    1
    down vote














    Jelly, 9 bytes



    ZḊUƊƬḢ€€Ẏ


    Try it online!






    share|improve this answer



























      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.





      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%2fanti-clock-wise-fashion%23new-answer', 'question_page');

        );

        Post as a guest






























        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!






        share|improve this answer
























          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!






          share|improve this answer






















            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!






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









            TFeld

            12.4k2834




            12.4k2834




















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







                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
                  14 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
                  12 mins ago














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







                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
                  14 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
                  12 mins ago












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







                share|improve this answer













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








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 19 mins ago









                ngm

                2,65922




                2,65922











                • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                  – Giuseppe
                  14 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
                  12 mins ago
















                • wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                  – Giuseppe
                  14 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
                  12 mins ago















                wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                – Giuseppe
                14 mins ago




                wow! I love how the use of t() prevents the drop=TRUE default for `[` from screwing up the if condition!
                – Giuseppe
                14 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
                12 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
                12 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 29 mins ago









                    Erik the Outgolfer

                    29.8k42899




                    29.8k42899




















                        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.





                        share|improve this answer
























                          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.





                          share|improve this answer






















                            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.





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









                            Mnemonic

                            4,4721629




                            4,4721629



























                                 

                                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%2fanti-clock-wise-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

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery