Rosencrantz and Guildenstern are Code
Clash 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:
Standard Loopholes are prohibited- Acceptable answers: a function which takes input through one input string and returns one output string
- If the input string doesn't match the requirements provided above,
your code MUST error out(doesn't matter what kind ofException
orError
) - This is
code-golf
, so the shortest(valid)answer (in each language)
wins!
Bonus points for a one-liner :-) (Not really tho, just cool points)
code-golf code-challenge
New contributor
 |Â
show 1 more comment
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:
Standard Loopholes are prohibited- Acceptable answers: a function which takes input through one input string and returns one output string
- If the input string doesn't match the requirements provided above,
your code MUST error out(doesn't matter what kind ofException
orError
) - This is
code-golf
, so the shortest(valid)answer (in each language)
wins!
Bonus points for a one-liner :-) (Not really tho, just cool points)
code-golf code-challenge
New contributor
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
 |Â
show 1 more comment
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:
Standard Loopholes are prohibited- Acceptable answers: a function which takes input through one input string and returns one output string
- If the input string doesn't match the requirements provided above,
your code MUST error out(doesn't matter what kind ofException
orError
) - This is
code-golf
, so the shortest(valid)answer (in each language)
wins!
Bonus points for a one-liner :-) (Not really tho, just cool points)
code-golf code-challenge
New contributor
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:
Standard Loopholes are prohibited- Acceptable answers: a function which takes input through one input string and returns one output string
- If the input string doesn't match the requirements provided above,
your code MUST error out(doesn't matter what kind ofException
orError
) - This is
code-golf
, so the shortest(valid)answer (in each language)
wins!
Bonus points for a one-liner :-) (Not really tho, just cool points)
code-golf code-challenge
code-golf code-challenge
New contributor
New contributor
edited 25 mins ago
New contributor
asked 2 hours ago
Michael
246
246
New contributor
New contributor
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
 |Â
show 1 more comment
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
 |Â
show 1 more comment
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.
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 12 mins ago
answered 51 mins ago
Jo King
18.1k24199
18.1k24199
add a comment |Â
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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!
answered 1 hour ago
ErikF
1,22917
1,22917
add a comment |Â
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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!
edited 36 mins ago
answered 54 mins ago
Thaufeki
36117
36117
add a comment |Â
add a comment |Â
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.
Michael is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175202%2frosencrantz-and-guildenstern-are-code%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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