Rosencrantz and Guildenstern are Code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












In the absurdist play Rosencrantz and Guildenstern are Dead, the two main characters Rosencrantz and Guildenstern(or are they?) are always mixing up which of them is who—or sometimes which of their own body parts is which—because of a perceived lack of individual identity. Wouldn't it be absurd if they even shuffled around their names?



Your task it to write a function which takes in a string of an even length(and by design, a multiple of 4) that is greater than 7 characters, split it, and shuffle it.



The splitting shall be as follows:
the string will be of format "abscd", with s acting as a seperator character. The first section and the separator, abs will be the first half of the string, whereas the second half will be cd



The length of a will be (string length / 4) - 1



The length of b will be (string length / 4)



The length of s will be 1



The length of c will be (string length / 4) + 1



The length of d will be (string length / 4) - 1



This may be really confusing, so let me show you
with some examples



("a" + "bb" + "s" + "ccc" + "d").length //8
1 2 1 3 1
|-------4--------| |----4-----| <--- (4 is half of 8)

("rosen" + "crantz" + "&" + "guilden" + "stern").length //24
5 6 1 7 5

("foo" + "barr" + "?" + "barry" + "foo").length
3 4 1 5 3


Finally:



You then shuffle the parts around, outputting adscb



ex. "rosencrantz&guildenstern" --> "rosenstern&guildencrantz"



"foobarr?barryfoo" --> "foofoo?barrybarr"



Rulez:




  1. Standard Loopholes are prohibited

  2. Acceptable answers: a function which takes input through one input string and returns one output string

  3. If the input string doesn't match the requirements provided above,
    your code MUST error out(doesn't matter what kind of Exception or
    Error)

  4. This is code-golf, so the shortest(valid)answer (in each language)
    wins!


  5. Bonus points for a one-liner :-) (Not really tho, just cool points)









share|improve this question









New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
    – Jonathan Frech
    2 hours ago










  • @JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
    – Michael
    2 hours ago







  • 1




    tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
    – Windmill Cookies
    2 hours ago






  • 1




    Things to Avoid when writing challenges (Input Validation).
    – Jo King
    1 hour ago










  • @JoKing thanks for spotting that!
    – Michael
    25 mins ago














up vote
2
down vote

favorite












In the absurdist play Rosencrantz and Guildenstern are Dead, the two main characters Rosencrantz and Guildenstern(or are they?) are always mixing up which of them is who—or sometimes which of their own body parts is which—because of a perceived lack of individual identity. Wouldn't it be absurd if they even shuffled around their names?



Your task it to write a function which takes in a string of an even length(and by design, a multiple of 4) that is greater than 7 characters, split it, and shuffle it.



The splitting shall be as follows:
the string will be of format "abscd", with s acting as a seperator character. The first section and the separator, abs will be the first half of the string, whereas the second half will be cd



The length of a will be (string length / 4) - 1



The length of b will be (string length / 4)



The length of s will be 1



The length of c will be (string length / 4) + 1



The length of d will be (string length / 4) - 1



This may be really confusing, so let me show you
with some examples



("a" + "bb" + "s" + "ccc" + "d").length //8
1 2 1 3 1
|-------4--------| |----4-----| <--- (4 is half of 8)

("rosen" + "crantz" + "&" + "guilden" + "stern").length //24
5 6 1 7 5

("foo" + "barr" + "?" + "barry" + "foo").length
3 4 1 5 3


Finally:



You then shuffle the parts around, outputting adscb



ex. "rosencrantz&guildenstern" --> "rosenstern&guildencrantz"



"foobarr?barryfoo" --> "foofoo?barrybarr"



Rulez:




  1. Standard Loopholes are prohibited

  2. Acceptable answers: a function which takes input through one input string and returns one output string

  3. If the input string doesn't match the requirements provided above,
    your code MUST error out(doesn't matter what kind of Exception or
    Error)

  4. This is code-golf, so the shortest(valid)answer (in each language)
    wins!


  5. Bonus points for a one-liner :-) (Not really tho, just cool points)









share|improve this question









New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
    – Jonathan Frech
    2 hours ago










  • @JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
    – Michael
    2 hours ago







  • 1




    tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
    – Windmill Cookies
    2 hours ago






  • 1




    Things to Avoid when writing challenges (Input Validation).
    – Jo King
    1 hour ago










  • @JoKing thanks for spotting that!
    – Michael
    25 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











In the absurdist play Rosencrantz and Guildenstern are Dead, the two main characters Rosencrantz and Guildenstern(or are they?) are always mixing up which of them is who—or sometimes which of their own body parts is which—because of a perceived lack of individual identity. Wouldn't it be absurd if they even shuffled around their names?



Your task it to write a function which takes in a string of an even length(and by design, a multiple of 4) that is greater than 7 characters, split it, and shuffle it.



The splitting shall be as follows:
the string will be of format "abscd", with s acting as a seperator character. The first section and the separator, abs will be the first half of the string, whereas the second half will be cd



The length of a will be (string length / 4) - 1



The length of b will be (string length / 4)



The length of s will be 1



The length of c will be (string length / 4) + 1



The length of d will be (string length / 4) - 1



This may be really confusing, so let me show you
with some examples



("a" + "bb" + "s" + "ccc" + "d").length //8
1 2 1 3 1
|-------4--------| |----4-----| <--- (4 is half of 8)

("rosen" + "crantz" + "&" + "guilden" + "stern").length //24
5 6 1 7 5

("foo" + "barr" + "?" + "barry" + "foo").length
3 4 1 5 3


Finally:



You then shuffle the parts around, outputting adscb



ex. "rosencrantz&guildenstern" --> "rosenstern&guildencrantz"



"foobarr?barryfoo" --> "foofoo?barrybarr"



Rulez:




  1. Standard Loopholes are prohibited

  2. Acceptable answers: a function which takes input through one input string and returns one output string

  3. If the input string doesn't match the requirements provided above,
    your code MUST error out(doesn't matter what kind of Exception or
    Error)

  4. This is code-golf, so the shortest(valid)answer (in each language)
    wins!


  5. Bonus points for a one-liner :-) (Not really tho, just cool points)









share|improve this question









New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











In the absurdist play Rosencrantz and Guildenstern are Dead, the two main characters Rosencrantz and Guildenstern(or are they?) are always mixing up which of them is who—or sometimes which of their own body parts is which—because of a perceived lack of individual identity. Wouldn't it be absurd if they even shuffled around their names?



Your task it to write a function which takes in a string of an even length(and by design, a multiple of 4) that is greater than 7 characters, split it, and shuffle it.



The splitting shall be as follows:
the string will be of format "abscd", with s acting as a seperator character. The first section and the separator, abs will be the first half of the string, whereas the second half will be cd



The length of a will be (string length / 4) - 1



The length of b will be (string length / 4)



The length of s will be 1



The length of c will be (string length / 4) + 1



The length of d will be (string length / 4) - 1



This may be really confusing, so let me show you
with some examples



("a" + "bb" + "s" + "ccc" + "d").length //8
1 2 1 3 1
|-------4--------| |----4-----| <--- (4 is half of 8)

("rosen" + "crantz" + "&" + "guilden" + "stern").length //24
5 6 1 7 5

("foo" + "barr" + "?" + "barry" + "foo").length
3 4 1 5 3


Finally:



You then shuffle the parts around, outputting adscb



ex. "rosencrantz&guildenstern" --> "rosenstern&guildencrantz"



"foobarr?barryfoo" --> "foofoo?barrybarr"



Rulez:




  1. Standard Loopholes are prohibited

  2. Acceptable answers: a function which takes input through one input string and returns one output string

  3. If the input string doesn't match the requirements provided above,
    your code MUST error out(doesn't matter what kind of Exception or
    Error)

  4. This is code-golf, so the shortest(valid)answer (in each language)
    wins!


  5. Bonus points for a one-liner :-) (Not really tho, just cool points)






code-golf code-challenge






share|improve this question









New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 25 mins ago





















New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Michael

246




246




New contributor




Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Michael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
    – Jonathan Frech
    2 hours ago










  • @JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
    – Michael
    2 hours ago







  • 1




    tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
    – Windmill Cookies
    2 hours ago






  • 1




    Things to Avoid when writing challenges (Input Validation).
    – Jo King
    1 hour ago










  • @JoKing thanks for spotting that!
    – Michael
    25 mins ago












  • 2




    Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
    – Jonathan Frech
    2 hours ago










  • @JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
    – Michael
    2 hours ago







  • 1




    tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
    – Windmill Cookies
    2 hours ago






  • 1




    Things to Avoid when writing challenges (Input Validation).
    – Jo King
    1 hour ago










  • @JoKing thanks for spotting that!
    – Michael
    25 mins ago







2




2




Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
– Jonathan Frech
2 hours ago




Regarding your gangsta rulez: It is generally discouraged to favour "functions", as they are hard to define in general. Furthermore, handling invalid input is also mostly avoided, as it usually boils down to annoying boiler-plate code.
– Jonathan Frech
2 hours ago












@JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
– Michael
2 hours ago





@JonathanFrech I think the challenge of input validation is an interesting problem, as it can be handled a variety of ways from array traversal, to branching logic, to RegEx testing, so optimization of these can add a extra challenge ¯_(ツ)_/¯ I'll try that for another code golf challenge though :-)
– Michael
2 hours ago





1




1




tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
– Windmill Cookies
2 hours ago




tfw everyone is trying to get more flexible io methods to make the code in their favorite golfing lang. and tfw it happens on almost all questions
– Windmill Cookies
2 hours ago




1




1




Things to Avoid when writing challenges (Input Validation).
– Jo King
1 hour ago




Things to Avoid when writing challenges (Input Validation).
– Jo King
1 hour ago












@JoKing thanks for spotting that!
– Michael
25 mins ago




@JoKing thanks for spotting that!
– Michael
25 mins ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote














Perl 6, 78 bytes





*.comb[0..*/4-2,3* */4+1..*,*/2-1/(*>7)..3* */4,*/4-1/(*%%4)..*/2-2].flat.join


Try it online!



Anonymous code block that takes a string and returns the modified string if valid, else returns a division by zero error. I know I can modify an array directly, so I could swap the two sections of the string, but I can't quite figure it out.






share|improve this answer





























    up vote
    1
    down vote














    C (gcc), 154 bytes



    Returns an empty string if the input string length is not divisible by 4. Also assumes adscb order, as implied by the examples.





    #define x memcpy
    char t[80];l;f(char*s)l=strlen(s);l%4?*t=0:(l/=4,x(t,s,l-1),x(t+l-1,s+3*l+1,l-1),x(t+2*l-2,s+2*l-1,l+2),x(t+3*l,s+l-1,l),t[4*l]=0);l=t;


    Try it online!






    share|improve this answer



























      up vote
      1
      down vote














      K (ngn/k), 112 bytes



      f:((#:x)%2)#x)x;a:((k:(#:x)%4)-1)#*g;b:(-k)#*g;c:(k+1)#*;s:$[0=4!#:x;f x;l]


      Hopefully can be golfed down more, included an extra function for testing if string length is divisible by four, outputs error with reference to undeclared variable if input is invalid



      Try it online!






      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: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        Michael is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175202%2frosencrantz-and-guildenstern-are-code%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote














        Perl 6, 78 bytes





        *.comb[0..*/4-2,3* */4+1..*,*/2-1/(*>7)..3* */4,*/4-1/(*%%4)..*/2-2].flat.join


        Try it online!



        Anonymous code block that takes a string and returns the modified string if valid, else returns a division by zero error. I know I can modify an array directly, so I could swap the two sections of the string, but I can't quite figure it out.






        share|improve this answer


























          up vote
          2
          down vote














          Perl 6, 78 bytes





          *.comb[0..*/4-2,3* */4+1..*,*/2-1/(*>7)..3* */4,*/4-1/(*%%4)..*/2-2].flat.join


          Try it online!



          Anonymous code block that takes a string and returns the modified string if valid, else returns a division by zero error. I know I can modify an array directly, so I could swap the two sections of the string, but I can't quite figure it out.






          share|improve this answer
























            up vote
            2
            down vote










            up vote
            2
            down vote










            Perl 6, 78 bytes





            *.comb[0..*/4-2,3* */4+1..*,*/2-1/(*>7)..3* */4,*/4-1/(*%%4)..*/2-2].flat.join


            Try it online!



            Anonymous code block that takes a string and returns the modified string if valid, else returns a division by zero error. I know I can modify an array directly, so I could swap the two sections of the string, but I can't quite figure it out.






            share|improve this answer















            Perl 6, 78 bytes





            *.comb[0..*/4-2,3* */4+1..*,*/2-1/(*>7)..3* */4,*/4-1/(*%%4)..*/2-2].flat.join


            Try it online!



            Anonymous code block that takes a string and returns the modified string if valid, else returns a division by zero error. I know I can modify an array directly, so I could swap the two sections of the string, but I can't quite figure it out.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 12 mins ago

























            answered 51 mins ago









            Jo King

            18.1k24199




            18.1k24199




















                up vote
                1
                down vote














                C (gcc), 154 bytes



                Returns an empty string if the input string length is not divisible by 4. Also assumes adscb order, as implied by the examples.





                #define x memcpy
                char t[80];l;f(char*s)l=strlen(s);l%4?*t=0:(l/=4,x(t,s,l-1),x(t+l-1,s+3*l+1,l-1),x(t+2*l-2,s+2*l-1,l+2),x(t+3*l,s+l-1,l),t[4*l]=0);l=t;


                Try it online!






                share|improve this answer
























                  up vote
                  1
                  down vote














                  C (gcc), 154 bytes



                  Returns an empty string if the input string length is not divisible by 4. Also assumes adscb order, as implied by the examples.





                  #define x memcpy
                  char t[80];l;f(char*s)l=strlen(s);l%4?*t=0:(l/=4,x(t,s,l-1),x(t+l-1,s+3*l+1,l-1),x(t+2*l-2,s+2*l-1,l+2),x(t+3*l,s+l-1,l),t[4*l]=0);l=t;


                  Try it online!






                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote










                    C (gcc), 154 bytes



                    Returns an empty string if the input string length is not divisible by 4. Also assumes adscb order, as implied by the examples.





                    #define x memcpy
                    char t[80];l;f(char*s)l=strlen(s);l%4?*t=0:(l/=4,x(t,s,l-1),x(t+l-1,s+3*l+1,l-1),x(t+2*l-2,s+2*l-1,l+2),x(t+3*l,s+l-1,l),t[4*l]=0);l=t;


                    Try it online!






                    share|improve this answer













                    C (gcc), 154 bytes



                    Returns an empty string if the input string length is not divisible by 4. Also assumes adscb order, as implied by the examples.





                    #define x memcpy
                    char t[80];l;f(char*s)l=strlen(s);l%4?*t=0:(l/=4,x(t,s,l-1),x(t+l-1,s+3*l+1,l-1),x(t+2*l-2,s+2*l-1,l+2),x(t+3*l,s+l-1,l),t[4*l]=0);l=t;


                    Try it online!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    ErikF

                    1,22917




                    1,22917




















                        up vote
                        1
                        down vote














                        K (ngn/k), 112 bytes



                        f:((#:x)%2)#x)x;a:((k:(#:x)%4)-1)#*g;b:(-k)#*g;c:(k+1)#*;s:$[0=4!#:x;f x;l]


                        Hopefully can be golfed down more, included an extra function for testing if string length is divisible by four, outputs error with reference to undeclared variable if input is invalid



                        Try it online!






                        share|improve this answer


























                          up vote
                          1
                          down vote














                          K (ngn/k), 112 bytes



                          f:((#:x)%2)#x)x;a:((k:(#:x)%4)-1)#*g;b:(-k)#*g;c:(k+1)#*;s:$[0=4!#:x;f x;l]


                          Hopefully can be golfed down more, included an extra function for testing if string length is divisible by four, outputs error with reference to undeclared variable if input is invalid



                          Try it online!






                          share|improve this answer
























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote










                            K (ngn/k), 112 bytes



                            f:((#:x)%2)#x)x;a:((k:(#:x)%4)-1)#*g;b:(-k)#*g;c:(k+1)#*;s:$[0=4!#:x;f x;l]


                            Hopefully can be golfed down more, included an extra function for testing if string length is divisible by four, outputs error with reference to undeclared variable if input is invalid



                            Try it online!






                            share|improve this answer















                            K (ngn/k), 112 bytes



                            f:((#:x)%2)#x)x;a:((k:(#:x)%4)-1)#*g;b:(-k)#*g;c:(k+1)#*;s:$[0=4!#:x;f x;l]


                            Hopefully can be golfed down more, included an extra function for testing if string length is divisible by four, outputs error with reference to undeclared variable if input is invalid



                            Try it online!







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 36 mins ago

























                            answered 54 mins ago









                            Thaufeki

                            36117




                            36117




















                                Michael is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                Michael is a new contributor. Be nice, and check out our Code of Conduct.












                                Michael is a new contributor. Be nice, and check out our Code of Conduct.











                                Michael is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175202%2frosencrantz-and-guildenstern-are-code%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