Could you please stop shuffling the deck and play already?
$begingroup$
Challenge:
Input: A list of distinct positive integers within the range $[1, text{list-size}]$.
Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the list is split in two halves, and these halves are interleaved (i.e. riffle-shuffling the list [1,2,3,4,5,6,7,8,9,10]
once would result in [1,6,2,7,3,8,4,9,5,10]
, so for this challenge the input [1,6,2,7,3,8,4,9,5,10]
would result in 1
).
Challenge rules:
- You can assume the list will only contain positive integers in the range $[1, text{list-size}]$ (or $[0, text{list-size}-1]$ if you choose to have 0-indexed input-lists).
- You can assume all input-lists will either be a valid riffle-shuffled list, or a sorted list which isn't shuffled (in which case the output is
0
). - You can assume the input-list will contain at least three values.
Step-by-step example:
Input: [1,3,5,7,9,2,4,6,8]
Unshuffling it once becomes: [1,5,9,4,8,3,7,2,6]
, because every even 0-indexed item comes first [1, ,5, ,9, ,4, ,8]
, and then all odd 0-indexed items after that [ ,3, ,7, ,2, ,6, ]
.
The list isn't ordered yet, so we continue:
Unshuffling the list again becomes: [1,9,8,7,6,5,4,3,2]
Again becomes: [1,8,6,4,2,9,7,5,3]
Then: [1,6,2,7,3,8,4,9,5]
And finally: [1,2,3,4,5,6,7,8,9]
, which is an ordered list, so we're done unshuffling.
We unshuffled the original [1,3,5,7,9,2,4,6,8]
five times to get to [1,2,3,4,5,6,7,8,9]
, so the output is 5
in this case.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input Output
[1,2,3] 0
[1,2,3,4,5] 0
[1,3,2] 1
[1,6,2,7,3,8,4,9,5,10] 1
[1,3,5,7,2,4,6] 2
[1,8,6,4,2,9,7,5,3,10] 2
[1,9,8,7,6,5,4,3,2,10] 3
[1,5,9,4,8,3,7,2,6,10] 4
[1,3,5,7,9,2,4,6,8] 5
[1,6,11,5,10,4,9,3,8,2,7] 6
[1,10,19,9,18,8,17,7,16,6,15,5,14,4,13,3,12,2,11,20] 10
[1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20] 17
[1,141,32,172,63,203,94,234,125,16,156,47,187,78,218,109,249,140,31,171,62,202,93,233,124,15,155,46,186,77,217,108,248,139,30,170,61,201,92,232,123,14,154,45,185,76,216,107,247,138,29,169,60,200,91,231,122,13,153,44,184,75,215,106,246,137,28,168,59,199,90,230,121,12,152,43,183,74,214,105,245,136,27,167,58,198,89,229,120,11,151,42,182,73,213,104,244,135,26,166,57,197,88,228,119,10,150,41,181,72,212,103,243,134,25,165,56,196,87,227,118,9,149,40,180,71,211,102,242,133,24,164,55,195,86,226,117,8,148,39,179,70,210,101,241,132,23,163,54,194,85,225,116,7,147,38,178,69,209,100,240,131,22,162,53,193,84,224,115,6,146,37,177,68,208,99,239,130,21,161,52,192,83,223,114,5,145,36,176,67,207,98,238,129,20,160,51,191,82,222,113,4,144,35,175,66,206,97,237,128,19,159,50,190,81,221,112,3,143,34,174,65,205,96,236,127,18,158,49,189,80,220,111,2,142,33,173,64,204,95,235,126,17,157,48,188,79,219,110,250]
45
code-golf number array-manipulation integer
$endgroup$
|
show 4 more comments
$begingroup$
Challenge:
Input: A list of distinct positive integers within the range $[1, text{list-size}]$.
Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the list is split in two halves, and these halves are interleaved (i.e. riffle-shuffling the list [1,2,3,4,5,6,7,8,9,10]
once would result in [1,6,2,7,3,8,4,9,5,10]
, so for this challenge the input [1,6,2,7,3,8,4,9,5,10]
would result in 1
).
Challenge rules:
- You can assume the list will only contain positive integers in the range $[1, text{list-size}]$ (or $[0, text{list-size}-1]$ if you choose to have 0-indexed input-lists).
- You can assume all input-lists will either be a valid riffle-shuffled list, or a sorted list which isn't shuffled (in which case the output is
0
). - You can assume the input-list will contain at least three values.
Step-by-step example:
Input: [1,3,5,7,9,2,4,6,8]
Unshuffling it once becomes: [1,5,9,4,8,3,7,2,6]
, because every even 0-indexed item comes first [1, ,5, ,9, ,4, ,8]
, and then all odd 0-indexed items after that [ ,3, ,7, ,2, ,6, ]
.
The list isn't ordered yet, so we continue:
Unshuffling the list again becomes: [1,9,8,7,6,5,4,3,2]
Again becomes: [1,8,6,4,2,9,7,5,3]
Then: [1,6,2,7,3,8,4,9,5]
And finally: [1,2,3,4,5,6,7,8,9]
, which is an ordered list, so we're done unshuffling.
We unshuffled the original [1,3,5,7,9,2,4,6,8]
five times to get to [1,2,3,4,5,6,7,8,9]
, so the output is 5
in this case.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input Output
[1,2,3] 0
[1,2,3,4,5] 0
[1,3,2] 1
[1,6,2,7,3,8,4,9,5,10] 1
[1,3,5,7,2,4,6] 2
[1,8,6,4,2,9,7,5,3,10] 2
[1,9,8,7,6,5,4,3,2,10] 3
[1,5,9,4,8,3,7,2,6,10] 4
[1,3,5,7,9,2,4,6,8] 5
[1,6,11,5,10,4,9,3,8,2,7] 6
[1,10,19,9,18,8,17,7,16,6,15,5,14,4,13,3,12,2,11,20] 10
[1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20] 17
[1,141,32,172,63,203,94,234,125,16,156,47,187,78,218,109,249,140,31,171,62,202,93,233,124,15,155,46,186,77,217,108,248,139,30,170,61,201,92,232,123,14,154,45,185,76,216,107,247,138,29,169,60,200,91,231,122,13,153,44,184,75,215,106,246,137,28,168,59,199,90,230,121,12,152,43,183,74,214,105,245,136,27,167,58,198,89,229,120,11,151,42,182,73,213,104,244,135,26,166,57,197,88,228,119,10,150,41,181,72,212,103,243,134,25,165,56,196,87,227,118,9,149,40,180,71,211,102,242,133,24,164,55,195,86,226,117,8,148,39,179,70,210,101,241,132,23,163,54,194,85,225,116,7,147,38,178,69,209,100,240,131,22,162,53,193,84,224,115,6,146,37,177,68,208,99,239,130,21,161,52,192,83,223,114,5,145,36,176,67,207,98,238,129,20,160,51,191,82,222,113,4,144,35,175,66,206,97,237,128,19,159,50,190,81,221,112,3,143,34,174,65,205,96,236,127,18,158,49,189,80,220,111,2,142,33,173,64,204,95,235,126,17,157,48,188,79,219,110,250]
45
code-golf number array-manipulation integer
$endgroup$
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
@OlivierGrégoire The[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases[1,3,5,7,2,4,6] = 2
(length 7) and[1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.
$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
1
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
2
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both[1,6,2,7,3,8,4,9,5,10]
or[6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.
$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20
|
show 4 more comments
$begingroup$
Challenge:
Input: A list of distinct positive integers within the range $[1, text{list-size}]$.
Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the list is split in two halves, and these halves are interleaved (i.e. riffle-shuffling the list [1,2,3,4,5,6,7,8,9,10]
once would result in [1,6,2,7,3,8,4,9,5,10]
, so for this challenge the input [1,6,2,7,3,8,4,9,5,10]
would result in 1
).
Challenge rules:
- You can assume the list will only contain positive integers in the range $[1, text{list-size}]$ (or $[0, text{list-size}-1]$ if you choose to have 0-indexed input-lists).
- You can assume all input-lists will either be a valid riffle-shuffled list, or a sorted list which isn't shuffled (in which case the output is
0
). - You can assume the input-list will contain at least three values.
Step-by-step example:
Input: [1,3,5,7,9,2,4,6,8]
Unshuffling it once becomes: [1,5,9,4,8,3,7,2,6]
, because every even 0-indexed item comes first [1, ,5, ,9, ,4, ,8]
, and then all odd 0-indexed items after that [ ,3, ,7, ,2, ,6, ]
.
The list isn't ordered yet, so we continue:
Unshuffling the list again becomes: [1,9,8,7,6,5,4,3,2]
Again becomes: [1,8,6,4,2,9,7,5,3]
Then: [1,6,2,7,3,8,4,9,5]
And finally: [1,2,3,4,5,6,7,8,9]
, which is an ordered list, so we're done unshuffling.
We unshuffled the original [1,3,5,7,9,2,4,6,8]
five times to get to [1,2,3,4,5,6,7,8,9]
, so the output is 5
in this case.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input Output
[1,2,3] 0
[1,2,3,4,5] 0
[1,3,2] 1
[1,6,2,7,3,8,4,9,5,10] 1
[1,3,5,7,2,4,6] 2
[1,8,6,4,2,9,7,5,3,10] 2
[1,9,8,7,6,5,4,3,2,10] 3
[1,5,9,4,8,3,7,2,6,10] 4
[1,3,5,7,9,2,4,6,8] 5
[1,6,11,5,10,4,9,3,8,2,7] 6
[1,10,19,9,18,8,17,7,16,6,15,5,14,4,13,3,12,2,11,20] 10
[1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20] 17
[1,141,32,172,63,203,94,234,125,16,156,47,187,78,218,109,249,140,31,171,62,202,93,233,124,15,155,46,186,77,217,108,248,139,30,170,61,201,92,232,123,14,154,45,185,76,216,107,247,138,29,169,60,200,91,231,122,13,153,44,184,75,215,106,246,137,28,168,59,199,90,230,121,12,152,43,183,74,214,105,245,136,27,167,58,198,89,229,120,11,151,42,182,73,213,104,244,135,26,166,57,197,88,228,119,10,150,41,181,72,212,103,243,134,25,165,56,196,87,227,118,9,149,40,180,71,211,102,242,133,24,164,55,195,86,226,117,8,148,39,179,70,210,101,241,132,23,163,54,194,85,225,116,7,147,38,178,69,209,100,240,131,22,162,53,193,84,224,115,6,146,37,177,68,208,99,239,130,21,161,52,192,83,223,114,5,145,36,176,67,207,98,238,129,20,160,51,191,82,222,113,4,144,35,175,66,206,97,237,128,19,159,50,190,81,221,112,3,143,34,174,65,205,96,236,127,18,158,49,189,80,220,111,2,142,33,173,64,204,95,235,126,17,157,48,188,79,219,110,250]
45
code-golf number array-manipulation integer
$endgroup$
Challenge:
Input: A list of distinct positive integers within the range $[1, text{list-size}]$.
Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the list is split in two halves, and these halves are interleaved (i.e. riffle-shuffling the list [1,2,3,4,5,6,7,8,9,10]
once would result in [1,6,2,7,3,8,4,9,5,10]
, so for this challenge the input [1,6,2,7,3,8,4,9,5,10]
would result in 1
).
Challenge rules:
- You can assume the list will only contain positive integers in the range $[1, text{list-size}]$ (or $[0, text{list-size}-1]$ if you choose to have 0-indexed input-lists).
- You can assume all input-lists will either be a valid riffle-shuffled list, or a sorted list which isn't shuffled (in which case the output is
0
). - You can assume the input-list will contain at least three values.
Step-by-step example:
Input: [1,3,5,7,9,2,4,6,8]
Unshuffling it once becomes: [1,5,9,4,8,3,7,2,6]
, because every even 0-indexed item comes first [1, ,5, ,9, ,4, ,8]
, and then all odd 0-indexed items after that [ ,3, ,7, ,2, ,6, ]
.
The list isn't ordered yet, so we continue:
Unshuffling the list again becomes: [1,9,8,7,6,5,4,3,2]
Again becomes: [1,8,6,4,2,9,7,5,3]
Then: [1,6,2,7,3,8,4,9,5]
And finally: [1,2,3,4,5,6,7,8,9]
, which is an ordered list, so we're done unshuffling.
We unshuffled the original [1,3,5,7,9,2,4,6,8]
five times to get to [1,2,3,4,5,6,7,8,9]
, so the output is 5
in this case.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input Output
[1,2,3] 0
[1,2,3,4,5] 0
[1,3,2] 1
[1,6,2,7,3,8,4,9,5,10] 1
[1,3,5,7,2,4,6] 2
[1,8,6,4,2,9,7,5,3,10] 2
[1,9,8,7,6,5,4,3,2,10] 3
[1,5,9,4,8,3,7,2,6,10] 4
[1,3,5,7,9,2,4,6,8] 5
[1,6,11,5,10,4,9,3,8,2,7] 6
[1,10,19,9,18,8,17,7,16,6,15,5,14,4,13,3,12,2,11,20] 10
[1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20] 17
[1,141,32,172,63,203,94,234,125,16,156,47,187,78,218,109,249,140,31,171,62,202,93,233,124,15,155,46,186,77,217,108,248,139,30,170,61,201,92,232,123,14,154,45,185,76,216,107,247,138,29,169,60,200,91,231,122,13,153,44,184,75,215,106,246,137,28,168,59,199,90,230,121,12,152,43,183,74,214,105,245,136,27,167,58,198,89,229,120,11,151,42,182,73,213,104,244,135,26,166,57,197,88,228,119,10,150,41,181,72,212,103,243,134,25,165,56,196,87,227,118,9,149,40,180,71,211,102,242,133,24,164,55,195,86,226,117,8,148,39,179,70,210,101,241,132,23,163,54,194,85,225,116,7,147,38,178,69,209,100,240,131,22,162,53,193,84,224,115,6,146,37,177,68,208,99,239,130,21,161,52,192,83,223,114,5,145,36,176,67,207,98,238,129,20,160,51,191,82,222,113,4,144,35,175,66,206,97,237,128,19,159,50,190,81,221,112,3,143,34,174,65,205,96,236,127,18,158,49,189,80,220,111,2,142,33,173,64,204,95,235,126,17,157,48,188,79,219,110,250]
45
code-golf number array-manipulation integer
code-golf number array-manipulation integer
edited Mar 13 at 10:59
Kevin Cruijssen
asked Mar 11 at 10:19
Kevin CruijssenKevin Cruijssen
40.8k566210
40.8k566210
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
@OlivierGrégoire The[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases[1,3,5,7,2,4,6] = 2
(length 7) and[1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.
$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
1
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
2
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both[1,6,2,7,3,8,4,9,5,10]
or[6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.
$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20
|
show 4 more comments
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
@OlivierGrégoire The[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases[1,3,5,7,2,4,6] = 2
(length 7) and[1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.
$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
1
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
2
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both[1,6,2,7,3,8,4,9,5,10]
or[6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.
$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
@OlivierGrégoire The
[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases [1,3,5,7,2,4,6] = 2
(length 7) and [1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
@OlivierGrégoire The
[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases [1,3,5,7,2,4,6] = 2
(length 7) and [1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
1
1
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
2
2
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both
[1,6,2,7,3,8,4,9,5,10]
or [6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both
[1,6,2,7,3,8,4,9,5,10]
or [6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20
|
show 4 more comments
23 Answers
23
active
oldest
votes
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a deck with 1-indexed cards as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
Given a deck $[c_0,ldots,c_{L-1}]$ of length $L$, we define:
$$x_n=begin{cases}
2^nbmod L&text{if }Ltext{ is odd}\
2^nbmod (L-1)&text{if }Ltext{ is even}\
end{cases}$$
And we look for $n$ such that $c_{x_n}=2$.
JavaScript (ES6), 57 52 50 bytes
Expects a deck with 0-indexed cards as input.
f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)
Try it online!
How?
Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.
Given a deck of length $L$, this code looks for $n$ such that:
$$c_2equivleft(frac{k+1}{2}right)^npmod k$$
where $c_2$ is the second card and $k$ is defined as:
$$k=begin{cases}
L&text{if }Ltext{ is odd}\
L-1&text{if }Ltext{ is even}\
end{cases}$$
$endgroup$
add a comment |
$begingroup$
Python 2, 39 bytes
f=lambda x:x[1]-2and-~f(x[::2]+x[1::2])
Try it online!
-4 thanks to Jonathan Allan.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
Mar 11 at 14:50
add a comment |
$begingroup$
Jelly, 8 bytes
ŒœẎ$ƬiṢ’
Try it online!
How?
ŒœẎ$ƬiṢ’ - Link: list of integers A
Ƭ - collect up until results are no longer unique...
$ - last two links as a monad:
Œœ - odds & evens i.e. [a,b,c,d,...] -> [[a,c,...],[b,d,...]]
Ẏ - tighten -> [a,c,...,b,d,...]
Ṣ - sort A
i - first (1-indexed) index of sorted A in collected shuffles
’ - decrement
$endgroup$
add a comment |
$begingroup$
Perl 6, 34 32 bytes
-2 bytes thanks to Jo King
{(.[(2 X**^$_)X%$_-1+|1]...2)-1}
Try it online!
Similar to Arnauld's approach. The index of the second card after n shuffles is 2**n % k
with k defined as in Arnauld's answer.
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 22 bytesSBCS
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.
The TIO link contains two test cases.
Explanation:
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵ ⍝ if the array is sorted:
⍵≡⍳≢⍵ ⍝ array = 1..length(array)
:0 ⍝ then return 0
⋄ ⍝ otherwise
1+ ⍝ increment
∇ ⍝ the value of the recursive call with this argument:
⍵[ ] ⍝ index into the argument with these indexes:
⍳⍴⍵ ⍝ - generate a range from 1 up to the size of ⍵
2| ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒ ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.
¹
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
->⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
add a comment |
$begingroup$
Perl 6, 36 34 32 bytes
-2 bytes thanks to nwellnhof
$!={.[1]-2&&$!(.sort:{$++%2})+1}
Try it online!
Reverse riffle shuffles by sorting by the index modulo 2 until the list is sorted, then returns the length of the sequence.
It's funny, I don't usually try the recursive approach for Perl 6, but this time it ended up shorter than the original.
Explanation:
$!={.[1]-2&&$!(.sort:{$++%2})+1}
$!={ } # Assign the anonymous code block to $!
.[1]-2&& # While the list is not sorted
$!( ) # Recursively call the function on
.sort:{$++%2} # It sorted by the parity of each index
+1 # And return the number of shuffles
$endgroup$
add a comment |
$begingroup$
R, 58 55 45 bytes
a=scan();while(a[2]>2)a=matrix(a,,2,F<-F+1);F
Try it online!
Simulates the sorting process. Input is 1-indexed, returns FALSE
for 0.
$endgroup$
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
add a comment |
$begingroup$
05AB1E (legacy), 9 bytes
[DāQ#ι˜]N
Try it online!
Explanation
[ # ] # loop until
ā # the 1-indexed enumeration of the current list
D Q # equals a copy of the current list
ι˜ # while false, uninterleave the current list and flatten
N # push the iteration index N as output
$endgroup$
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtinÅ≠
that inspired this challenge. :)
$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
add a comment |
$begingroup$
Java (JDK), 59 bytes
a->{int c=0;for(;a[(1<<c)%(a.length-1|1)]>2;)c++;return c;}
Try it online!
Works reliably only for arrays with a size less than 31 or solutions with less than 31 iterations. For a more general solution, see the following solution with 63 bytes:
a->{int i=1,c=0;for(;a[i]>2;c++)i=i*2%(a.length-1|1);return c;}
Try it online!
Explanation
In a riffle, the next position is the previous one times two modulo either length if it's odd or length - 1 if it's even.
So I'm iterating over all indices using this formula until I find the value 2 in the array.
Credits
- -8 bytes thanks to Kevin Cruijssen. (Previous algorithm, using array)
- -5 bytes thanks to Arnauld.
$endgroup$
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
|
show 5 more comments
$begingroup$
APL(NARS), chars 49, bytes 98
{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
why use in the deepest loop, one algo that should be nlog(n), when we can use one linear n? just for few bytes more?
[⍵≡⍵[⍋⍵] O(nlog n) and the confront each element for see are in order using ∧/¯1↓⍵≤1⌽⍵ O(n)]test:
f←{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
f ,1
0
f 1 2 3
0
f 1,9,8,7,6,5,4,3,2,10
3
f 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20
17
$endgroup$
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
add a comment |
$begingroup$
Ruby, 42 bytes
f=->d,r=1{d[r]<3?0:1+f[d,r*2%(1|~-d.max)]}
Try it online!
How:
Search for number 2 inside the array: if it's in second position, the deck hasn't been shuffled, otherwise check the positions where successive shuffles would put it.
$endgroup$
add a comment |
$begingroup$
R, 70 72 bytes
x=scan();i=0;while(any(x>sort(x))){x=c(x[y<-seq(x)%%2>0],x[!y]);i=i+1};i
Try it online!
Now handles the zero shuffle case.
$endgroup$
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
add a comment |
$begingroup$
C (GCC) 64 63 bytes
-1 byte from nwellnhof
i,r;f(c,v)int*v;{for(i=r=1;v[i]>2;++r)i=i*2%(c-1|1);return~-r;}
This is a drastically shorter answer based on Arnauld's and Olivier Grégoire's answers. I'll leave my old solution below since it solves the slightly more general problem of decks with cards that are not contiguous.
Try it online
C (GCC) 162 bytes
a[999],b[999],i,r,o;f(c,v)int*v;{for(r=0;o=1;++r){for(i=c;i--;(i&1?b:a)[i/2]=v[i])o=(v[i]>v[i-1]|!i)&o;if(o)return r;for(i+=o=c+1;i--;)v[i]=i<o/2?a[i]:b[i-o/2];}}
Try it online
a[999],b[999],i,r,o; //pre-declare variables
f(c,v)int*v;{ //argument list
for(r=0;o=1;++r){ //major loop, reset o (ordered) to true at beginning, increment number of shuffles at end
for(i=c;i--;(i&1?b:a)[i/2]=v[i]) //loop through v, split into halves a/b as we go
o=(v[i]>v[i-1]|!i)&o; //if out of order set o (ordered) to false
if(o) //if ordered
return r; //return number of shuffles
//note that i==-1 at this point
for(i+=o=c+1;i--;)//set i=c and o=c+1, loop through v
v[i]=i<o/2?a[i]:b[i-o/2];//set first half of v to a, second half to b
}
}
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
Inspired be Ven's APL solution.
Explanation:
^: ^:a: while
(2<1{]) the 1-st (zero-indexed) element is greater than 2
( ) do the following and keep the intermediate results
i.@# make a list form 0 to len-1
2| find modulo 2 of each element
/: sort the argument according the list of 0's and 1's
1 }. drop the first row of the result
#@ and take the length (how many rows -> steps)
K (ngn/k), 25 bytes
Thanks to ngn for the advice and for his K interpreter!
{#1_{~2=x@1}{x@<2!!#x}x}
Try it online!
$endgroup$
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
add a comment |
$begingroup$
R, 85 bytes
s=scan();u=sort(s);k=0;while(any(u[seq(s)]!=s)){k=k+1;u=as.vector(t(matrix(u,,2)))};k
Try it online.
Explanation
Stupid (brute force) method, much less elegant than following the card #2.
Instead of unshuffling the input s
we start with a sorted vector u
that we progressively shuffle until it is identical with s
. This gives warnings (but shuffle counts are still correct) for odd lengths of input due to folding an odd-length vector into a 2-column matrix; in that case, in R, missing data point is filled by recycling of the first element of input.
The loop will never terminate if we provide a vector that cannot be unshuffled.
Addendum: you save one byte if unshuffling instead. Unlike the answer above, there is no need to transpose with t()
, however, ordering is byrow=TRUE
which is why T
appears in matrix()
.
R, 84 bytes
s=scan();u=sort(s);k=0;while(any(s[seq(u)]!=u)){k=k+1;s=as.vector(matrix(s,,2,T))};k
Try it online!
New contributor
$endgroup$
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
add a comment |
$begingroup$
PowerShell, 116 114 108 84 78 bytes
-24 bytes thanks to Erik the Outgolfer's solution.
-6 bytes thanks to mazzy.
param($a)for(;$a[1]-2){$n++;$t=@{};$a|%{$t[$j++%2]+=,$_};$a=$t.0+$t.1;$j=0}+$n
Try it online!
$endgroup$
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
add a comment |
$begingroup$
Wolfram Language (Mathematica), 62 bytes
c=0;While[Sort[a]!=a,a=a[[1;;-1;;2]]~Join~a[[2;;-1;;2]];c++];c
Try it online!
Explanation
The input list is a
. It is unriffled and compared with the sorted list until they match.
$endgroup$
add a comment |
$begingroup$
Red, 87 79 78 bytes
func[b][c: 0 while[b/2 > 2][c: c + 1 b: append extract b 2 extract next b 2]c]
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5 -pa
, 77 bytes
map{push@{$_%2},$_}0..$#F;$_=0;++$_,@s=sort{$a-$b}@F=@F[@0,@1]while"@F"ne"@s"
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 13 11 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
g1 Í©ÒßUñÏu
Try it or run all test cases
g1 Í©ÒßUñÏu :Implicit input of integer array U
g1 :Get the element at 0-based index 1
Í :Subtract from 2
© :Logical AND with
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
The script has two line: the first one defines a function y
, the second line calls y
with the implicit Q
(evaluated stdin) argument.
L?SIb0hys%L2>Bb1
L function y(b)
? if...
SIb the Invariant b == sort(b) holds
0 return 0
h otherwise increment...
y ...the return of a recursive call with:
B the current argument "bifurcated", an array of:
b - the original argument
> 1 - same with the head popped off
L map...
% 2 ...take only every 2nd value in each array
s and concat them back together
¹
$endgroup$
add a comment |
$begingroup$
PowerShell, 62 71 70 66 bytes
+9 bytes when Test cases with an even number of elements added.
-1 byte with splatting.
-4 bytes: wrap the expression with $i
,$j
to a new scope.
for($a=$args;$a[1]-2;$a=&{($a|?{++$j%2})+($a|?{$i++%2})}){$n++}+$n
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3, 40 bytes
f=lambda x:x[1]-2and 1+f(x[::2]+x[1::2]) # 1-based
f=lambda x:x[1]-1and 1+f(x[::2]+x[1::2]) # 0-based
Try it online!
I need to refresh the page more frequently: missed Erik the Outgolfer's edit doing a similar trick =)
$endgroup$
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181299%2fcould-you-please-stop-shuffling-the-deck-and-play-already%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
23 Answers
23
active
oldest
votes
23 Answers
23
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a deck with 1-indexed cards as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
Given a deck $[c_0,ldots,c_{L-1}]$ of length $L$, we define:
$$x_n=begin{cases}
2^nbmod L&text{if }Ltext{ is odd}\
2^nbmod (L-1)&text{if }Ltext{ is even}\
end{cases}$$
And we look for $n$ such that $c_{x_n}=2$.
JavaScript (ES6), 57 52 50 bytes
Expects a deck with 0-indexed cards as input.
f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)
Try it online!
How?
Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.
Given a deck of length $L$, this code looks for $n$ such that:
$$c_2equivleft(frac{k+1}{2}right)^npmod k$$
where $c_2$ is the second card and $k$ is defined as:
$$k=begin{cases}
L&text{if }Ltext{ is odd}\
L-1&text{if }Ltext{ is even}\
end{cases}$$
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a deck with 1-indexed cards as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
Given a deck $[c_0,ldots,c_{L-1}]$ of length $L$, we define:
$$x_n=begin{cases}
2^nbmod L&text{if }Ltext{ is odd}\
2^nbmod (L-1)&text{if }Ltext{ is even}\
end{cases}$$
And we look for $n$ such that $c_{x_n}=2$.
JavaScript (ES6), 57 52 50 bytes
Expects a deck with 0-indexed cards as input.
f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)
Try it online!
How?
Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.
Given a deck of length $L$, this code looks for $n$ such that:
$$c_2equivleft(frac{k+1}{2}right)^npmod k$$
where $c_2$ is the second card and $k$ is defined as:
$$k=begin{cases}
L&text{if }Ltext{ is odd}\
L-1&text{if }Ltext{ is even}\
end{cases}$$
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a deck with 1-indexed cards as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
Given a deck $[c_0,ldots,c_{L-1}]$ of length $L$, we define:
$$x_n=begin{cases}
2^nbmod L&text{if }Ltext{ is odd}\
2^nbmod (L-1)&text{if }Ltext{ is even}\
end{cases}$$
And we look for $n$ such that $c_{x_n}=2$.
JavaScript (ES6), 57 52 50 bytes
Expects a deck with 0-indexed cards as input.
f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)
Try it online!
How?
Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.
Given a deck of length $L$, this code looks for $n$ such that:
$$c_2equivleft(frac{k+1}{2}right)^npmod k$$
where $c_2$ is the second card and $k$ is defined as:
$$k=begin{cases}
L&text{if }Ltext{ is odd}\
L-1&text{if }Ltext{ is even}\
end{cases}$$
$endgroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a deck with 1-indexed cards as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
Given a deck $[c_0,ldots,c_{L-1}]$ of length $L$, we define:
$$x_n=begin{cases}
2^nbmod L&text{if }Ltext{ is odd}\
2^nbmod (L-1)&text{if }Ltext{ is even}\
end{cases}$$
And we look for $n$ such that $c_{x_n}=2$.
JavaScript (ES6), 57 52 50 bytes
Expects a deck with 0-indexed cards as input.
f=(a,x=1,k=a.length-1|1)=>a[1]-x%k&&1+f(a,x*-~k/2)
Try it online!
How?
Since JS is lacking native support for extracting array slices with a custom stepping, simulating the entire riffle-shuffle would probably be rather costly (but to be honest, I didn't even try). However, the solution can also be found by just looking at the 2nd card and the total number of cards in the deck.
Given a deck of length $L$, this code looks for $n$ such that:
$$c_2equivleft(frac{k+1}{2}right)^npmod k$$
where $c_2$ is the second card and $k$ is defined as:
$$k=begin{cases}
L&text{if }Ltext{ is odd}\
L-1&text{if }Ltext{ is even}\
end{cases}$$
edited Mar 13 at 9:39
answered Mar 11 at 11:32
ArnauldArnauld
79.2k796330
79.2k796330
add a comment |
add a comment |
$begingroup$
Python 2, 39 bytes
f=lambda x:x[1]-2and-~f(x[::2]+x[1::2])
Try it online!
-4 thanks to Jonathan Allan.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
Mar 11 at 14:50
add a comment |
$begingroup$
Python 2, 39 bytes
f=lambda x:x[1]-2and-~f(x[::2]+x[1::2])
Try it online!
-4 thanks to Jonathan Allan.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
Mar 11 at 14:50
add a comment |
$begingroup$
Python 2, 39 bytes
f=lambda x:x[1]-2and-~f(x[::2]+x[1::2])
Try it online!
-4 thanks to Jonathan Allan.
$endgroup$
Python 2, 39 bytes
f=lambda x:x[1]-2and-~f(x[::2]+x[1::2])
Try it online!
-4 thanks to Jonathan Allan.
edited Mar 12 at 6:03
answered Mar 11 at 11:36
Erik the OutgolferErik the Outgolfer
32.6k429105
32.6k429105
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
Mar 11 at 14:50
add a comment |
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
Mar 11 at 14:50
$begingroup$
Save four bytes with
f=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
Save four bytes with
f=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
Mar 11 at 14:48
$begingroup$
@JonathanAllan Oh, of course! Well...
!=
can be -
. ;-)$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
@JonathanAllan Oh, of course! Well...
!=
can be -
. ;-)$endgroup$
– Erik the Outgolfer
Mar 11 at 14:49
$begingroup$
Ah, yeah caveat emptor :D (or just
x[1]>2
I guess)$endgroup$
– Jonathan Allan
Mar 11 at 14:50
$begingroup$
Ah, yeah caveat emptor :D (or just
x[1]>2
I guess)$endgroup$
– Jonathan Allan
Mar 11 at 14:50
add a comment |
$begingroup$
Jelly, 8 bytes
ŒœẎ$ƬiṢ’
Try it online!
How?
ŒœẎ$ƬiṢ’ - Link: list of integers A
Ƭ - collect up until results are no longer unique...
$ - last two links as a monad:
Œœ - odds & evens i.e. [a,b,c,d,...] -> [[a,c,...],[b,d,...]]
Ẏ - tighten -> [a,c,...,b,d,...]
Ṣ - sort A
i - first (1-indexed) index of sorted A in collected shuffles
’ - decrement
$endgroup$
add a comment |
$begingroup$
Jelly, 8 bytes
ŒœẎ$ƬiṢ’
Try it online!
How?
ŒœẎ$ƬiṢ’ - Link: list of integers A
Ƭ - collect up until results are no longer unique...
$ - last two links as a monad:
Œœ - odds & evens i.e. [a,b,c,d,...] -> [[a,c,...],[b,d,...]]
Ẏ - tighten -> [a,c,...,b,d,...]
Ṣ - sort A
i - first (1-indexed) index of sorted A in collected shuffles
’ - decrement
$endgroup$
add a comment |
$begingroup$
Jelly, 8 bytes
ŒœẎ$ƬiṢ’
Try it online!
How?
ŒœẎ$ƬiṢ’ - Link: list of integers A
Ƭ - collect up until results are no longer unique...
$ - last two links as a monad:
Œœ - odds & evens i.e. [a,b,c,d,...] -> [[a,c,...],[b,d,...]]
Ẏ - tighten -> [a,c,...,b,d,...]
Ṣ - sort A
i - first (1-indexed) index of sorted A in collected shuffles
’ - decrement
$endgroup$
Jelly, 8 bytes
ŒœẎ$ƬiṢ’
Try it online!
How?
ŒœẎ$ƬiṢ’ - Link: list of integers A
Ƭ - collect up until results are no longer unique...
$ - last two links as a monad:
Œœ - odds & evens i.e. [a,b,c,d,...] -> [[a,c,...],[b,d,...]]
Ẏ - tighten -> [a,c,...,b,d,...]
Ṣ - sort A
i - first (1-indexed) index of sorted A in collected shuffles
’ - decrement
edited Mar 11 at 20:15
answered Mar 11 at 10:40
Jonathan AllanJonathan Allan
53.2k535172
53.2k535172
add a comment |
add a comment |
$begingroup$
Perl 6, 34 32 bytes
-2 bytes thanks to Jo King
{(.[(2 X**^$_)X%$_-1+|1]...2)-1}
Try it online!
Similar to Arnauld's approach. The index of the second card after n shuffles is 2**n % k
with k defined as in Arnauld's answer.
$endgroup$
add a comment |
$begingroup$
Perl 6, 34 32 bytes
-2 bytes thanks to Jo King
{(.[(2 X**^$_)X%$_-1+|1]...2)-1}
Try it online!
Similar to Arnauld's approach. The index of the second card after n shuffles is 2**n % k
with k defined as in Arnauld's answer.
$endgroup$
add a comment |
$begingroup$
Perl 6, 34 32 bytes
-2 bytes thanks to Jo King
{(.[(2 X**^$_)X%$_-1+|1]...2)-1}
Try it online!
Similar to Arnauld's approach. The index of the second card after n shuffles is 2**n % k
with k defined as in Arnauld's answer.
$endgroup$
Perl 6, 34 32 bytes
-2 bytes thanks to Jo King
{(.[(2 X**^$_)X%$_-1+|1]...2)-1}
Try it online!
Similar to Arnauld's approach. The index of the second card after n shuffles is 2**n % k
with k defined as in Arnauld's answer.
edited Mar 13 at 13:19
answered Mar 11 at 13:56
nwellnhofnwellnhof
7,34511128
7,34511128
add a comment |
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 22 bytesSBCS
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.
The TIO link contains two test cases.
Explanation:
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵ ⍝ if the array is sorted:
⍵≡⍳≢⍵ ⍝ array = 1..length(array)
:0 ⍝ then return 0
⋄ ⍝ otherwise
1+ ⍝ increment
∇ ⍝ the value of the recursive call with this argument:
⍵[ ] ⍝ index into the argument with these indexes:
⍳⍴⍵ ⍝ - generate a range from 1 up to the size of ⍵
2| ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒ ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.
¹
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
->⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 22 bytesSBCS
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.
The TIO link contains two test cases.
Explanation:
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵ ⍝ if the array is sorted:
⍵≡⍳≢⍵ ⍝ array = 1..length(array)
:0 ⍝ then return 0
⋄ ⍝ otherwise
1+ ⍝ increment
∇ ⍝ the value of the recursive call with this argument:
⍵[ ] ⍝ index into the argument with these indexes:
⍳⍴⍵ ⍝ - generate a range from 1 up to the size of ⍵
2| ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒ ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.
¹
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
->⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 22 bytesSBCS
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.
The TIO link contains two test cases.
Explanation:
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵ ⍝ if the array is sorted:
⍵≡⍳≢⍵ ⍝ array = 1..length(array)
:0 ⍝ then return 0
⋄ ⍝ otherwise
1+ ⍝ increment
∇ ⍝ the value of the recursive call with this argument:
⍵[ ] ⍝ index into the argument with these indexes:
⍳⍴⍵ ⍝ - generate a range from 1 up to the size of ⍵
2| ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒ ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.
¹
$endgroup$
APL (Dyalog Unicode), 35 26 23 22 bytesSBCS
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help, Erik the Outgolfer for -3 and ngn for -1.
The TIO link contains two test cases.
Explanation:
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
{⍵≡⍳≢⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]} ⍝ function takes one argument: ⍵, the array
⍵≡⍳≢⍵ ⍝ if the array is sorted:
⍵≡⍳≢⍵ ⍝ array = 1..length(array)
:0 ⍝ then return 0
⋄ ⍝ otherwise
1+ ⍝ increment
∇ ⍝ the value of the recursive call with this argument:
⍵[ ] ⍝ index into the argument with these indexes:
⍳⍴⍵ ⍝ - generate a range from 1 up to the size of ⍵
2| ⍝ - %2: generate a binary mask like [1 0 1 0 1 0]
⍒ ⍝ - grade (sorts but returns indexes instead of values), so we have the indexes of all the 1s first, then the 0s.
¹
edited yesterday
answered Mar 11 at 12:53
VenVen
2,41511223
2,41511223
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
->⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
add a comment |
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
->⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
1
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
Mar 11 at 13:26
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
Mar 11 at 13:29
$begingroup$
∧/2≤/⍵
-> ⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
∧/2≤/⍵
-> ⍵≡⍳≢⍵
$endgroup$
– ngn
Mar 12 at 15:58
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
$begingroup$
@ngn didn't realize the array had no holes. Thanks!
$endgroup$
– Ven
Mar 12 at 16:59
add a comment |
$begingroup$
Perl 6, 36 34 32 bytes
-2 bytes thanks to nwellnhof
$!={.[1]-2&&$!(.sort:{$++%2})+1}
Try it online!
Reverse riffle shuffles by sorting by the index modulo 2 until the list is sorted, then returns the length of the sequence.
It's funny, I don't usually try the recursive approach for Perl 6, but this time it ended up shorter than the original.
Explanation:
$!={.[1]-2&&$!(.sort:{$++%2})+1}
$!={ } # Assign the anonymous code block to $!
.[1]-2&& # While the list is not sorted
$!( ) # Recursively call the function on
.sort:{$++%2} # It sorted by the parity of each index
+1 # And return the number of shuffles
$endgroup$
add a comment |
$begingroup$
Perl 6, 36 34 32 bytes
-2 bytes thanks to nwellnhof
$!={.[1]-2&&$!(.sort:{$++%2})+1}
Try it online!
Reverse riffle shuffles by sorting by the index modulo 2 until the list is sorted, then returns the length of the sequence.
It's funny, I don't usually try the recursive approach for Perl 6, but this time it ended up shorter than the original.
Explanation:
$!={.[1]-2&&$!(.sort:{$++%2})+1}
$!={ } # Assign the anonymous code block to $!
.[1]-2&& # While the list is not sorted
$!( ) # Recursively call the function on
.sort:{$++%2} # It sorted by the parity of each index
+1 # And return the number of shuffles
$endgroup$
add a comment |
$begingroup$
Perl 6, 36 34 32 bytes
-2 bytes thanks to nwellnhof
$!={.[1]-2&&$!(.sort:{$++%2})+1}
Try it online!
Reverse riffle shuffles by sorting by the index modulo 2 until the list is sorted, then returns the length of the sequence.
It's funny, I don't usually try the recursive approach for Perl 6, but this time it ended up shorter than the original.
Explanation:
$!={.[1]-2&&$!(.sort:{$++%2})+1}
$!={ } # Assign the anonymous code block to $!
.[1]-2&& # While the list is not sorted
$!( ) # Recursively call the function on
.sort:{$++%2} # It sorted by the parity of each index
+1 # And return the number of shuffles
$endgroup$
Perl 6, 36 34 32 bytes
-2 bytes thanks to nwellnhof
$!={.[1]-2&&$!(.sort:{$++%2})+1}
Try it online!
Reverse riffle shuffles by sorting by the index modulo 2 until the list is sorted, then returns the length of the sequence.
It's funny, I don't usually try the recursive approach for Perl 6, but this time it ended up shorter than the original.
Explanation:
$!={.[1]-2&&$!(.sort:{$++%2})+1}
$!={ } # Assign the anonymous code block to $!
.[1]-2&& # While the list is not sorted
$!( ) # Recursively call the function on
.sort:{$++%2} # It sorted by the parity of each index
+1 # And return the number of shuffles
edited Mar 12 at 6:01
answered Mar 11 at 10:29
Jo KingJo King
25k359128
25k359128
add a comment |
add a comment |
$begingroup$
R, 58 55 45 bytes
a=scan();while(a[2]>2)a=matrix(a,,2,F<-F+1);F
Try it online!
Simulates the sorting process. Input is 1-indexed, returns FALSE
for 0.
$endgroup$
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
add a comment |
$begingroup$
R, 58 55 45 bytes
a=scan();while(a[2]>2)a=matrix(a,,2,F<-F+1);F
Try it online!
Simulates the sorting process. Input is 1-indexed, returns FALSE
for 0.
$endgroup$
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
add a comment |
$begingroup$
R, 58 55 45 bytes
a=scan();while(a[2]>2)a=matrix(a,,2,F<-F+1);F
Try it online!
Simulates the sorting process. Input is 1-indexed, returns FALSE
for 0.
$endgroup$
R, 58 55 45 bytes
a=scan();while(a[2]>2)a=matrix(a,,2,F<-F+1);F
Try it online!
Simulates the sorting process. Input is 1-indexed, returns FALSE
for 0.
edited Mar 12 at 13:12
answered Mar 12 at 11:19
Kirill L.Kirill L.
5,5321525
5,5321525
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
add a comment |
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
$begingroup$
Very nice! I was working on a similar approach but using a recursive function, which didn't work out as golfy.
$endgroup$
– user2390246
Mar 12 at 11:55
add a comment |
$begingroup$
05AB1E (legacy), 9 bytes
[DāQ#ι˜]N
Try it online!
Explanation
[ # ] # loop until
ā # the 1-indexed enumeration of the current list
D Q # equals a copy of the current list
ι˜ # while false, uninterleave the current list and flatten
N # push the iteration index N as output
$endgroup$
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtinÅ≠
that inspired this challenge. :)
$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
add a comment |
$begingroup$
05AB1E (legacy), 9 bytes
[DāQ#ι˜]N
Try it online!
Explanation
[ # ] # loop until
ā # the 1-indexed enumeration of the current list
D Q # equals a copy of the current list
ι˜ # while false, uninterleave the current list and flatten
N # push the iteration index N as output
$endgroup$
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtinÅ≠
that inspired this challenge. :)
$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
add a comment |
$begingroup$
05AB1E (legacy), 9 bytes
[DāQ#ι˜]N
Try it online!
Explanation
[ # ] # loop until
ā # the 1-indexed enumeration of the current list
D Q # equals a copy of the current list
ι˜ # while false, uninterleave the current list and flatten
N # push the iteration index N as output
$endgroup$
05AB1E (legacy), 9 bytes
[DāQ#ι˜]N
Try it online!
Explanation
[ # ] # loop until
ā # the 1-indexed enumeration of the current list
D Q # equals a copy of the current list
ι˜ # while false, uninterleave the current list and flatten
N # push the iteration index N as output
answered Mar 11 at 10:24
EmignaEmigna
47k433142
47k433142
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtinÅ≠
that inspired this challenge. :)
$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
add a comment |
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtinÅ≠
that inspired this challenge. :)
$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtin
Å≠
that inspired this challenge. :)$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
I didn't even knew it was possible to output the index outside the loop in the legacy. I thought it would be 0 again at that point, just like in the new 05AB1E version. Nice answer! Shorter than my 10-byter using the unshuffle-builtin
Å≠
that inspired this challenge. :)$endgroup$
– Kevin Cruijssen
Mar 11 at 10:31
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
$begingroup$
@KevinCruijssen: Interesting. I didn't know there was an unshuffle. In this instance it's the same as my version, but unshuffle maintains dimensions on 2D arrays.
$endgroup$
– Emigna
Mar 11 at 11:18
add a comment |
$begingroup$
Java (JDK), 59 bytes
a->{int c=0;for(;a[(1<<c)%(a.length-1|1)]>2;)c++;return c;}
Try it online!
Works reliably only for arrays with a size less than 31 or solutions with less than 31 iterations. For a more general solution, see the following solution with 63 bytes:
a->{int i=1,c=0;for(;a[i]>2;c++)i=i*2%(a.length-1|1);return c;}
Try it online!
Explanation
In a riffle, the next position is the previous one times two modulo either length if it's odd or length - 1 if it's even.
So I'm iterating over all indices using this formula until I find the value 2 in the array.
Credits
- -8 bytes thanks to Kevin Cruijssen. (Previous algorithm, using array)
- -5 bytes thanks to Arnauld.
$endgroup$
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
|
show 5 more comments
$begingroup$
Java (JDK), 59 bytes
a->{int c=0;for(;a[(1<<c)%(a.length-1|1)]>2;)c++;return c;}
Try it online!
Works reliably only for arrays with a size less than 31 or solutions with less than 31 iterations. For a more general solution, see the following solution with 63 bytes:
a->{int i=1,c=0;for(;a[i]>2;c++)i=i*2%(a.length-1|1);return c;}
Try it online!
Explanation
In a riffle, the next position is the previous one times two modulo either length if it's odd or length - 1 if it's even.
So I'm iterating over all indices using this formula until I find the value 2 in the array.
Credits
- -8 bytes thanks to Kevin Cruijssen. (Previous algorithm, using array)
- -5 bytes thanks to Arnauld.
$endgroup$
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
|
show 5 more comments
$begingroup$
Java (JDK), 59 bytes
a->{int c=0;for(;a[(1<<c)%(a.length-1|1)]>2;)c++;return c;}
Try it online!
Works reliably only for arrays with a size less than 31 or solutions with less than 31 iterations. For a more general solution, see the following solution with 63 bytes:
a->{int i=1,c=0;for(;a[i]>2;c++)i=i*2%(a.length-1|1);return c;}
Try it online!
Explanation
In a riffle, the next position is the previous one times two modulo either length if it's odd or length - 1 if it's even.
So I'm iterating over all indices using this formula until I find the value 2 in the array.
Credits
- -8 bytes thanks to Kevin Cruijssen. (Previous algorithm, using array)
- -5 bytes thanks to Arnauld.
$endgroup$
Java (JDK), 59 bytes
a->{int c=0;for(;a[(1<<c)%(a.length-1|1)]>2;)c++;return c;}
Try it online!
Works reliably only for arrays with a size less than 31 or solutions with less than 31 iterations. For a more general solution, see the following solution with 63 bytes:
a->{int i=1,c=0;for(;a[i]>2;c++)i=i*2%(a.length-1|1);return c;}
Try it online!
Explanation
In a riffle, the next position is the previous one times two modulo either length if it's odd or length - 1 if it's even.
So I'm iterating over all indices using this formula until I find the value 2 in the array.
Credits
- -8 bytes thanks to Kevin Cruijssen. (Previous algorithm, using array)
- -5 bytes thanks to Arnauld.
edited Mar 11 at 15:36
answered Mar 11 at 12:40
Olivier GrégoireOlivier Grégoire
9,30511944
9,30511944
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
|
show 5 more comments
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
$begingroup$
163 bytes by using two times
x.clone()
instead of A.copyOf(x,l)
.$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
$begingroup$
163 bytes by using two times
x.clone()
instead of A.copyOf(x,l)
.$endgroup$
– Kevin Cruijssen
Mar 11 at 12:58
2
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
64 bytes
$endgroup$
– Arnauld
Mar 11 at 13:57
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
Mar 11 at 13:59
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
@Arnauld Oh! My new algorithm is actually the same as yours... And I spent half an hour figuring it out by myself...
$endgroup$
– Olivier Grégoire
Mar 11 at 14:04
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
Mar 11 at 14:10
|
show 5 more comments
$begingroup$
APL(NARS), chars 49, bytes 98
{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
why use in the deepest loop, one algo that should be nlog(n), when we can use one linear n? just for few bytes more?
[⍵≡⍵[⍋⍵] O(nlog n) and the confront each element for see are in order using ∧/¯1↓⍵≤1⌽⍵ O(n)]test:
f←{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
f ,1
0
f 1 2 3
0
f 1,9,8,7,6,5,4,3,2,10
3
f 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20
17
$endgroup$
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
add a comment |
$begingroup$
APL(NARS), chars 49, bytes 98
{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
why use in the deepest loop, one algo that should be nlog(n), when we can use one linear n? just for few bytes more?
[⍵≡⍵[⍋⍵] O(nlog n) and the confront each element for see are in order using ∧/¯1↓⍵≤1⌽⍵ O(n)]test:
f←{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
f ,1
0
f 1 2 3
0
f 1,9,8,7,6,5,4,3,2,10
3
f 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20
17
$endgroup$
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
add a comment |
$begingroup$
APL(NARS), chars 49, bytes 98
{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
why use in the deepest loop, one algo that should be nlog(n), when we can use one linear n? just for few bytes more?
[⍵≡⍵[⍋⍵] O(nlog n) and the confront each element for see are in order using ∧/¯1↓⍵≤1⌽⍵ O(n)]test:
f←{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
f ,1
0
f 1 2 3
0
f 1,9,8,7,6,5,4,3,2,10
3
f 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20
17
$endgroup$
APL(NARS), chars 49, bytes 98
{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
why use in the deepest loop, one algo that should be nlog(n), when we can use one linear n? just for few bytes more?
[⍵≡⍵[⍋⍵] O(nlog n) and the confront each element for see are in order using ∧/¯1↓⍵≤1⌽⍵ O(n)]test:
f←{0{∧/¯1↓⍵≤1⌽⍵:⍺⋄(⍺+1)∇⍵[d],⍵[i∼d←↑¨i⊂⍨2∣i←⍳≢⍵]}⍵}
f ,1
0
f 1 2 3
0
f 1,9,8,7,6,5,4,3,2,10
3
f 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20
17
edited Mar 11 at 16:31
answered Mar 11 at 16:03
RosLuPRosLuP
2,226514
2,226514
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
add a comment |
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
That’s the first time I’ve seen someone differentiate between characters and bytes 👍. It always bugs me when I see Unicode characters and they claim that it’s one byte per character. This 😠 is not one byte!
$endgroup$
– Kerndog73
Mar 11 at 21:31
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
Mar 11 at 23:41
add a comment |
$begingroup$
Ruby, 42 bytes
f=->d,r=1{d[r]<3?0:1+f[d,r*2%(1|~-d.max)]}
Try it online!
How:
Search for number 2 inside the array: if it's in second position, the deck hasn't been shuffled, otherwise check the positions where successive shuffles would put it.
$endgroup$
add a comment |
$begingroup$
Ruby, 42 bytes
f=->d,r=1{d[r]<3?0:1+f[d,r*2%(1|~-d.max)]}
Try it online!
How:
Search for number 2 inside the array: if it's in second position, the deck hasn't been shuffled, otherwise check the positions where successive shuffles would put it.
$endgroup$
add a comment |
$begingroup$
Ruby, 42 bytes
f=->d,r=1{d[r]<3?0:1+f[d,r*2%(1|~-d.max)]}
Try it online!
How:
Search for number 2 inside the array: if it's in second position, the deck hasn't been shuffled, otherwise check the positions where successive shuffles would put it.
$endgroup$
Ruby, 42 bytes
f=->d,r=1{d[r]<3?0:1+f[d,r*2%(1|~-d.max)]}
Try it online!
How:
Search for number 2 inside the array: if it's in second position, the deck hasn't been shuffled, otherwise check the positions where successive shuffles would put it.
edited Mar 12 at 7:59
answered Mar 12 at 7:06
G BG B
8,0061429
8,0061429
add a comment |
add a comment |
$begingroup$
R, 70 72 bytes
x=scan();i=0;while(any(x>sort(x))){x=c(x[y<-seq(x)%%2>0],x[!y]);i=i+1};i
Try it online!
Now handles the zero shuffle case.
$endgroup$
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
add a comment |
$begingroup$
R, 70 72 bytes
x=scan();i=0;while(any(x>sort(x))){x=c(x[y<-seq(x)%%2>0],x[!y]);i=i+1};i
Try it online!
Now handles the zero shuffle case.
$endgroup$
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
add a comment |
$begingroup$
R, 70 72 bytes
x=scan();i=0;while(any(x>sort(x))){x=c(x[y<-seq(x)%%2>0],x[!y]);i=i+1};i
Try it online!
Now handles the zero shuffle case.
$endgroup$
R, 70 72 bytes
x=scan();i=0;while(any(x>sort(x))){x=c(x[y<-seq(x)%%2>0],x[!y]);i=i+1};i
Try it online!
Now handles the zero shuffle case.
edited Mar 12 at 12:03
answered Mar 11 at 22:28
Nick KennedyNick Kennedy
80137
80137
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
add a comment |
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
1
1
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
$begingroup$
@user2390246 fair point. Adjusted accordingly
$endgroup$
– Nick Kennedy
Mar 12 at 12:03
add a comment |
$begingroup$
C (GCC) 64 63 bytes
-1 byte from nwellnhof
i,r;f(c,v)int*v;{for(i=r=1;v[i]>2;++r)i=i*2%(c-1|1);return~-r;}
This is a drastically shorter answer based on Arnauld's and Olivier Grégoire's answers. I'll leave my old solution below since it solves the slightly more general problem of decks with cards that are not contiguous.
Try it online
C (GCC) 162 bytes
a[999],b[999],i,r,o;f(c,v)int*v;{for(r=0;o=1;++r){for(i=c;i--;(i&1?b:a)[i/2]=v[i])o=(v[i]>v[i-1]|!i)&o;if(o)return r;for(i+=o=c+1;i--;)v[i]=i<o/2?a[i]:b[i-o/2];}}
Try it online
a[999],b[999],i,r,o; //pre-declare variables
f(c,v)int*v;{ //argument list
for(r=0;o=1;++r){ //major loop, reset o (ordered) to true at beginning, increment number of shuffles at end
for(i=c;i--;(i&1?b:a)[i/2]=v[i]) //loop through v, split into halves a/b as we go
o=(v[i]>v[i-1]|!i)&o; //if out of order set o (ordered) to false
if(o) //if ordered
return r; //return number of shuffles
//note that i==-1 at this point
for(i+=o=c+1;i--;)//set i=c and o=c+1, loop through v
v[i]=i<o/2?a[i]:b[i-o/2];//set first half of v to a, second half to b
}
}
$endgroup$
add a comment |
$begingroup$
C (GCC) 64 63 bytes
-1 byte from nwellnhof
i,r;f(c,v)int*v;{for(i=r=1;v[i]>2;++r)i=i*2%(c-1|1);return~-r;}
This is a drastically shorter answer based on Arnauld's and Olivier Grégoire's answers. I'll leave my old solution below since it solves the slightly more general problem of decks with cards that are not contiguous.
Try it online
C (GCC) 162 bytes
a[999],b[999],i,r,o;f(c,v)int*v;{for(r=0;o=1;++r){for(i=c;i--;(i&1?b:a)[i/2]=v[i])o=(v[i]>v[i-1]|!i)&o;if(o)return r;for(i+=o=c+1;i--;)v[i]=i<o/2?a[i]:b[i-o/2];}}
Try it online
a[999],b[999],i,r,o; //pre-declare variables
f(c,v)int*v;{ //argument list
for(r=0;o=1;++r){ //major loop, reset o (ordered) to true at beginning, increment number of shuffles at end
for(i=c;i--;(i&1?b:a)[i/2]=v[i]) //loop through v, split into halves a/b as we go
o=(v[i]>v[i-1]|!i)&o; //if out of order set o (ordered) to false
if(o) //if ordered
return r; //return number of shuffles
//note that i==-1 at this point
for(i+=o=c+1;i--;)//set i=c and o=c+1, loop through v
v[i]=i<o/2?a[i]:b[i-o/2];//set first half of v to a, second half to b
}
}
$endgroup$
add a comment |
$begingroup$
C (GCC) 64 63 bytes
-1 byte from nwellnhof
i,r;f(c,v)int*v;{for(i=r=1;v[i]>2;++r)i=i*2%(c-1|1);return~-r;}
This is a drastically shorter answer based on Arnauld's and Olivier Grégoire's answers. I'll leave my old solution below since it solves the slightly more general problem of decks with cards that are not contiguous.
Try it online
C (GCC) 162 bytes
a[999],b[999],i,r,o;f(c,v)int*v;{for(r=0;o=1;++r){for(i=c;i--;(i&1?b:a)[i/2]=v[i])o=(v[i]>v[i-1]|!i)&o;if(o)return r;for(i+=o=c+1;i--;)v[i]=i<o/2?a[i]:b[i-o/2];}}
Try it online
a[999],b[999],i,r,o; //pre-declare variables
f(c,v)int*v;{ //argument list
for(r=0;o=1;++r){ //major loop, reset o (ordered) to true at beginning, increment number of shuffles at end
for(i=c;i--;(i&1?b:a)[i/2]=v[i]) //loop through v, split into halves a/b as we go
o=(v[i]>v[i-1]|!i)&o; //if out of order set o (ordered) to false
if(o) //if ordered
return r; //return number of shuffles
//note that i==-1 at this point
for(i+=o=c+1;i--;)//set i=c and o=c+1, loop through v
v[i]=i<o/2?a[i]:b[i-o/2];//set first half of v to a, second half to b
}
}
$endgroup$
C (GCC) 64 63 bytes
-1 byte from nwellnhof
i,r;f(c,v)int*v;{for(i=r=1;v[i]>2;++r)i=i*2%(c-1|1);return~-r;}
This is a drastically shorter answer based on Arnauld's and Olivier Grégoire's answers. I'll leave my old solution below since it solves the slightly more general problem of decks with cards that are not contiguous.
Try it online
C (GCC) 162 bytes
a[999],b[999],i,r,o;f(c,v)int*v;{for(r=0;o=1;++r){for(i=c;i--;(i&1?b:a)[i/2]=v[i])o=(v[i]>v[i-1]|!i)&o;if(o)return r;for(i+=o=c+1;i--;)v[i]=i<o/2?a[i]:b[i-o/2];}}
Try it online
a[999],b[999],i,r,o; //pre-declare variables
f(c,v)int*v;{ //argument list
for(r=0;o=1;++r){ //major loop, reset o (ordered) to true at beginning, increment number of shuffles at end
for(i=c;i--;(i&1?b:a)[i/2]=v[i]) //loop through v, split into halves a/b as we go
o=(v[i]>v[i-1]|!i)&o; //if out of order set o (ordered) to false
if(o) //if ordered
return r; //return number of shuffles
//note that i==-1 at this point
for(i+=o=c+1;i--;)//set i=c and o=c+1, loop through v
v[i]=i<o/2?a[i]:b[i-o/2];//set first half of v to a, second half to b
}
}
edited Mar 12 at 16:53
answered Mar 11 at 19:44
rtpaxrtpax
2965
2965
add a comment |
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
Inspired be Ven's APL solution.
Explanation:
^: ^:a: while
(2<1{]) the 1-st (zero-indexed) element is greater than 2
( ) do the following and keep the intermediate results
i.@# make a list form 0 to len-1
2| find modulo 2 of each element
/: sort the argument according the list of 0's and 1's
1 }. drop the first row of the result
#@ and take the length (how many rows -> steps)
K (ngn/k), 25 bytes
Thanks to ngn for the advice and for his K interpreter!
{#1_{~2=x@1}{x@<2!!#x}x}
Try it online!
$endgroup$
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
Inspired be Ven's APL solution.
Explanation:
^: ^:a: while
(2<1{]) the 1-st (zero-indexed) element is greater than 2
( ) do the following and keep the intermediate results
i.@# make a list form 0 to len-1
2| find modulo 2 of each element
/: sort the argument according the list of 0's and 1's
1 }. drop the first row of the result
#@ and take the length (how many rows -> steps)
K (ngn/k), 25 bytes
Thanks to ngn for the advice and for his K interpreter!
{#1_{~2=x@1}{x@<2!!#x}x}
Try it online!
$endgroup$
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
Inspired be Ven's APL solution.
Explanation:
^: ^:a: while
(2<1{]) the 1-st (zero-indexed) element is greater than 2
( ) do the following and keep the intermediate results
i.@# make a list form 0 to len-1
2| find modulo 2 of each element
/: sort the argument according the list of 0's and 1's
1 }. drop the first row of the result
#@ and take the length (how many rows -> steps)
K (ngn/k), 25 bytes
Thanks to ngn for the advice and for his K interpreter!
{#1_{~2=x@1}{x@<2!!#x}x}
Try it online!
$endgroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
Inspired be Ven's APL solution.
Explanation:
^: ^:a: while
(2<1{]) the 1-st (zero-indexed) element is greater than 2
( ) do the following and keep the intermediate results
i.@# make a list form 0 to len-1
2| find modulo 2 of each element
/: sort the argument according the list of 0's and 1's
1 }. drop the first row of the result
#@ and take the length (how many rows -> steps)
K (ngn/k), 25 bytes
Thanks to ngn for the advice and for his K interpreter!
{#1_{~2=x@1}{x@<2!!#x}x}
Try it online!
edited Mar 13 at 7:14
answered Mar 11 at 20:24
Galen IvanovGalen Ivanov
7,17211034
7,17211034
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
add a comment |
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
converge-iterate, then drop one, and count - this leads to shorter code
$endgroup$
– ngn
Mar 12 at 16:28
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
$begingroup$
@ngn. So, similar to my J solution - I'll try it later, thanks!
$endgroup$
– Galen Ivanov
Mar 12 at 18:16
add a comment |
$begingroup$
R, 85 bytes
s=scan();u=sort(s);k=0;while(any(u[seq(s)]!=s)){k=k+1;u=as.vector(t(matrix(u,,2)))};k
Try it online.
Explanation
Stupid (brute force) method, much less elegant than following the card #2.
Instead of unshuffling the input s
we start with a sorted vector u
that we progressively shuffle until it is identical with s
. This gives warnings (but shuffle counts are still correct) for odd lengths of input due to folding an odd-length vector into a 2-column matrix; in that case, in R, missing data point is filled by recycling of the first element of input.
The loop will never terminate if we provide a vector that cannot be unshuffled.
Addendum: you save one byte if unshuffling instead. Unlike the answer above, there is no need to transpose with t()
, however, ordering is byrow=TRUE
which is why T
appears in matrix()
.
R, 84 bytes
s=scan();u=sort(s);k=0;while(any(s[seq(u)]!=u)){k=k+1;s=as.vector(matrix(s,,2,T))};k
Try it online!
New contributor
$endgroup$
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
add a comment |
$begingroup$
R, 85 bytes
s=scan();u=sort(s);k=0;while(any(u[seq(s)]!=s)){k=k+1;u=as.vector(t(matrix(u,,2)))};k
Try it online.
Explanation
Stupid (brute force) method, much less elegant than following the card #2.
Instead of unshuffling the input s
we start with a sorted vector u
that we progressively shuffle until it is identical with s
. This gives warnings (but shuffle counts are still correct) for odd lengths of input due to folding an odd-length vector into a 2-column matrix; in that case, in R, missing data point is filled by recycling of the first element of input.
The loop will never terminate if we provide a vector that cannot be unshuffled.
Addendum: you save one byte if unshuffling instead. Unlike the answer above, there is no need to transpose with t()
, however, ordering is byrow=TRUE
which is why T
appears in matrix()
.
R, 84 bytes
s=scan();u=sort(s);k=0;while(any(s[seq(u)]!=u)){k=k+1;s=as.vector(matrix(s,,2,T))};k
Try it online!
New contributor
$endgroup$
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
add a comment |
$begingroup$
R, 85 bytes
s=scan();u=sort(s);k=0;while(any(u[seq(s)]!=s)){k=k+1;u=as.vector(t(matrix(u,,2)))};k
Try it online.
Explanation
Stupid (brute force) method, much less elegant than following the card #2.
Instead of unshuffling the input s
we start with a sorted vector u
that we progressively shuffle until it is identical with s
. This gives warnings (but shuffle counts are still correct) for odd lengths of input due to folding an odd-length vector into a 2-column matrix; in that case, in R, missing data point is filled by recycling of the first element of input.
The loop will never terminate if we provide a vector that cannot be unshuffled.
Addendum: you save one byte if unshuffling instead. Unlike the answer above, there is no need to transpose with t()
, however, ordering is byrow=TRUE
which is why T
appears in matrix()
.
R, 84 bytes
s=scan();u=sort(s);k=0;while(any(s[seq(u)]!=u)){k=k+1;s=as.vector(matrix(s,,2,T))};k
Try it online!
New contributor
$endgroup$
R, 85 bytes
s=scan();u=sort(s);k=0;while(any(u[seq(s)]!=s)){k=k+1;u=as.vector(t(matrix(u,,2)))};k
Try it online.
Explanation
Stupid (brute force) method, much less elegant than following the card #2.
Instead of unshuffling the input s
we start with a sorted vector u
that we progressively shuffle until it is identical with s
. This gives warnings (but shuffle counts are still correct) for odd lengths of input due to folding an odd-length vector into a 2-column matrix; in that case, in R, missing data point is filled by recycling of the first element of input.
The loop will never terminate if we provide a vector that cannot be unshuffled.
Addendum: you save one byte if unshuffling instead. Unlike the answer above, there is no need to transpose with t()
, however, ordering is byrow=TRUE
which is why T
appears in matrix()
.
R, 84 bytes
s=scan();u=sort(s);k=0;while(any(s[seq(u)]!=u)){k=k+1;s=as.vector(matrix(s,,2,T))};k
Try it online!
New contributor
edited Mar 13 at 12:47
Stephen
7,49223397
7,49223397
New contributor
answered Mar 13 at 10:45
VolareVolare
212
212
New contributor
New contributor
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
add a comment |
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
$begingroup$
I took the liberty of fixing your title and adding a TIO-link for the test cases (based on the other R answer), and also verified your answer works as intended, so +1 from me and welcome to PPCG! :)
$endgroup$
– Kevin Cruijssen
Mar 13 at 10:52
add a comment |
$begingroup$
PowerShell, 116 114 108 84 78 bytes
-24 bytes thanks to Erik the Outgolfer's solution.
-6 bytes thanks to mazzy.
param($a)for(;$a[1]-2){$n++;$t=@{};$a|%{$t[$j++%2]+=,$_};$a=$t.0+$t.1;$j=0}+$n
Try it online!
$endgroup$
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
add a comment |
$begingroup$
PowerShell, 116 114 108 84 78 bytes
-24 bytes thanks to Erik the Outgolfer's solution.
-6 bytes thanks to mazzy.
param($a)for(;$a[1]-2){$n++;$t=@{};$a|%{$t[$j++%2]+=,$_};$a=$t.0+$t.1;$j=0}+$n
Try it online!
$endgroup$
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
add a comment |
$begingroup$
PowerShell, 116 114 108 84 78 bytes
-24 bytes thanks to Erik the Outgolfer's solution.
-6 bytes thanks to mazzy.
param($a)for(;$a[1]-2){$n++;$t=@{};$a|%{$t[$j++%2]+=,$_};$a=$t.0+$t.1;$j=0}+$n
Try it online!
$endgroup$
PowerShell, 116 114 108 84 78 bytes
-24 bytes thanks to Erik the Outgolfer's solution.
-6 bytes thanks to mazzy.
param($a)for(;$a[1]-2){$n++;$t=@{};$a|%{$t[$j++%2]+=,$_};$a=$t.0+$t.1;$j=0}+$n
Try it online!
edited yesterday
answered 2 days ago
Andrei OdegovAndrei Odegov
47926
47926
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
add a comment |
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
you can save a bit more: Try it online!
$endgroup$
– mazzy
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
$begingroup$
@muzzy, you're right again :) thanks
$endgroup$
– Andrei Odegov
yesterday
add a comment |
$begingroup$
Wolfram Language (Mathematica), 62 bytes
c=0;While[Sort[a]!=a,a=a[[1;;-1;;2]]~Join~a[[2;;-1;;2]];c++];c
Try it online!
Explanation
The input list is a
. It is unriffled and compared with the sorted list until they match.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 62 bytes
c=0;While[Sort[a]!=a,a=a[[1;;-1;;2]]~Join~a[[2;;-1;;2]];c++];c
Try it online!
Explanation
The input list is a
. It is unriffled and compared with the sorted list until they match.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 62 bytes
c=0;While[Sort[a]!=a,a=a[[1;;-1;;2]]~Join~a[[2;;-1;;2]];c++];c
Try it online!
Explanation
The input list is a
. It is unriffled and compared with the sorted list until they match.
$endgroup$
Wolfram Language (Mathematica), 62 bytes
c=0;While[Sort[a]!=a,a=a[[1;;-1;;2]]~Join~a[[2;;-1;;2]];c++];c
Try it online!
Explanation
The input list is a
. It is unriffled and compared with the sorted list until they match.
answered Mar 11 at 15:53
Rainer GlügeRainer Glüge
1313
1313
add a comment |
add a comment |
$begingroup$
Red, 87 79 78 bytes
func[b][c: 0 while[b/2 > 2][c: c + 1 b: append extract b 2 extract next b 2]c]
Try it online!
$endgroup$
add a comment |
$begingroup$
Red, 87 79 78 bytes
func[b][c: 0 while[b/2 > 2][c: c + 1 b: append extract b 2 extract next b 2]c]
Try it online!
$endgroup$
add a comment |
$begingroup$
Red, 87 79 78 bytes
func[b][c: 0 while[b/2 > 2][c: c + 1 b: append extract b 2 extract next b 2]c]
Try it online!
$endgroup$
Red, 87 79 78 bytes
func[b][c: 0 while[b/2 > 2][c: c + 1 b: append extract b 2 extract next b 2]c]
Try it online!
edited Mar 11 at 20:26
answered Mar 11 at 14:59
Galen IvanovGalen Ivanov
7,17211034
7,17211034
add a comment |
add a comment |
$begingroup$
Perl 5 -pa
, 77 bytes
map{push@{$_%2},$_}0..$#F;$_=0;++$_,@s=sort{$a-$b}@F=@F[@0,@1]while"@F"ne"@s"
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5 -pa
, 77 bytes
map{push@{$_%2},$_}0..$#F;$_=0;++$_,@s=sort{$a-$b}@F=@F[@0,@1]while"@F"ne"@s"
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5 -pa
, 77 bytes
map{push@{$_%2},$_}0..$#F;$_=0;++$_,@s=sort{$a-$b}@F=@F[@0,@1]while"@F"ne"@s"
Try it online!
$endgroup$
Perl 5 -pa
, 77 bytes
map{push@{$_%2},$_}0..$#F;$_=0;++$_,@s=sort{$a-$b}@F=@F[@0,@1]while"@F"ne"@s"
Try it online!
answered Mar 11 at 22:00
XcaliXcali
5,445520
5,445520
add a comment |
add a comment |
$begingroup$
Japt, 13 11 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
g1 Í©ÒßUñÏu
Try it or run all test cases
g1 Í©ÒßUñÏu :Implicit input of integer array U
g1 :Get the element at 0-based index 1
Í :Subtract from 2
© :Logical AND with
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
add a comment |
$begingroup$
Japt, 13 11 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
g1 Í©ÒßUñÏu
Try it or run all test cases
g1 Í©ÒßUñÏu :Implicit input of integer array U
g1 :Get the element at 0-based index 1
Í :Subtract from 2
© :Logical AND with
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
add a comment |
$begingroup$
Japt, 13 11 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
g1 Í©ÒßUñÏu
Try it or run all test cases
g1 Í©ÒßUñÏu :Implicit input of integer array U
g1 :Get the element at 0-based index 1
Í :Subtract from 2
© :Logical AND with
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
Japt, 13 11 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
g1 Í©ÒßUñÏu
Try it or run all test cases
g1 Í©ÒßUñÏu :Implicit input of integer array U
g1 :Get the element at 0-based index 1
Í :Subtract from 2
© :Logical AND with
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
edited Mar 12 at 11:46
answered Mar 11 at 11:39
ShaggyShaggy
19k21667
19k21667
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
add a comment |
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
1
1
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
Mar 11 at 23:51
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
The script has two line: the first one defines a function y
, the second line calls y
with the implicit Q
(evaluated stdin) argument.
L?SIb0hys%L2>Bb1
L function y(b)
? if...
SIb the Invariant b == sort(b) holds
0 return 0
h otherwise increment...
y ...the return of a recursive call with:
B the current argument "bifurcated", an array of:
b - the original argument
> 1 - same with the head popped off
L map...
% 2 ...take only every 2nd value in each array
s and concat them back together
¹
$endgroup$
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
The script has two line: the first one defines a function y
, the second line calls y
with the implicit Q
(evaluated stdin) argument.
L?SIb0hys%L2>Bb1
L function y(b)
? if...
SIb the Invariant b == sort(b) holds
0 return 0
h otherwise increment...
y ...the return of a recursive call with:
B the current argument "bifurcated", an array of:
b - the original argument
> 1 - same with the head popped off
L map...
% 2 ...take only every 2nd value in each array
s and concat them back together
¹
$endgroup$
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
The script has two line: the first one defines a function y
, the second line calls y
with the implicit Q
(evaluated stdin) argument.
L?SIb0hys%L2>Bb1
L function y(b)
? if...
SIb the Invariant b == sort(b) holds
0 return 0
h otherwise increment...
y ...the return of a recursive call with:
B the current argument "bifurcated", an array of:
b - the original argument
> 1 - same with the head popped off
L map...
% 2 ...take only every 2nd value in each array
s and concat them back together
¹
$endgroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
The script has two line: the first one defines a function y
, the second line calls y
with the implicit Q
(evaluated stdin) argument.
L?SIb0hys%L2>Bb1
L function y(b)
? if...
SIb the Invariant b == sort(b) holds
0 return 0
h otherwise increment...
y ...the return of a recursive call with:
B the current argument "bifurcated", an array of:
b - the original argument
> 1 - same with the head popped off
L map...
% 2 ...take only every 2nd value in each array
s and concat them back together
¹
edited 2 days ago
answered Mar 11 at 15:27
VenVen
2,41511223
2,41511223
add a comment |
add a comment |
$begingroup$
PowerShell, 62 71 70 66 bytes
+9 bytes when Test cases with an even number of elements added.
-1 byte with splatting.
-4 bytes: wrap the expression with $i
,$j
to a new scope.
for($a=$args;$a[1]-2;$a=&{($a|?{++$j%2})+($a|?{$i++%2})}){$n++}+$n
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 62 71 70 66 bytes
+9 bytes when Test cases with an even number of elements added.
-1 byte with splatting.
-4 bytes: wrap the expression with $i
,$j
to a new scope.
for($a=$args;$a[1]-2;$a=&{($a|?{++$j%2})+($a|?{$i++%2})}){$n++}+$n
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 62 71 70 66 bytes
+9 bytes when Test cases with an even number of elements added.
-1 byte with splatting.
-4 bytes: wrap the expression with $i
,$j
to a new scope.
for($a=$args;$a[1]-2;$a=&{($a|?{++$j%2})+($a|?{$i++%2})}){$n++}+$n
Try it online!
$endgroup$
PowerShell, 62 71 70 66 bytes
+9 bytes when Test cases with an even number of elements added.
-1 byte with splatting.
-4 bytes: wrap the expression with $i
,$j
to a new scope.
for($a=$args;$a[1]-2;$a=&{($a|?{++$j%2})+($a|?{$i++%2})}){$n++}+$n
Try it online!
edited yesterday
answered yesterday
mazzymazzy
2,8351317
2,8351317
add a comment |
add a comment |
$begingroup$
Python 3, 40 bytes
f=lambda x:x[1]-2and 1+f(x[::2]+x[1::2]) # 1-based
f=lambda x:x[1]-1and 1+f(x[::2]+x[1::2]) # 0-based
Try it online!
I need to refresh the page more frequently: missed Erik the Outgolfer's edit doing a similar trick =)
$endgroup$
add a comment |
$begingroup$
Python 3, 40 bytes
f=lambda x:x[1]-2and 1+f(x[::2]+x[1::2]) # 1-based
f=lambda x:x[1]-1and 1+f(x[::2]+x[1::2]) # 0-based
Try it online!
I need to refresh the page more frequently: missed Erik the Outgolfer's edit doing a similar trick =)
$endgroup$
add a comment |
$begingroup$
Python 3, 40 bytes
f=lambda x:x[1]-2and 1+f(x[::2]+x[1::2]) # 1-based
f=lambda x:x[1]-1and 1+f(x[::2]+x[1::2]) # 0-based
Try it online!
I need to refresh the page more frequently: missed Erik the Outgolfer's edit doing a similar trick =)
$endgroup$
Python 3, 40 bytes
f=lambda x:x[1]-2and 1+f(x[::2]+x[1::2]) # 1-based
f=lambda x:x[1]-1and 1+f(x[::2]+x[1::2]) # 0-based
Try it online!
I need to refresh the page more frequently: missed Erik the Outgolfer's edit doing a similar trick =)
edited Mar 11 at 19:32
answered Mar 11 at 19:22
AlexAlex
3263
3263
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f181299%2fcould-you-please-stop-shuffling-the-deck-and-play-already%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
One or two test cases with an odd length and an output greater than 0 would be nice. It's easy to mess the riffle in such cases if you have to write the riffle code by yourself instead of relying on builtins.
$endgroup$
– Olivier Grégoire
Mar 11 at 14:21
$begingroup$
@OlivierGrégoire The
[1,3,5,7,9,2,4,6,8]
is of length 9, but I will add a few more for lengths 7 and 11 perhaps. EDIT: Added the test cases[1,3,5,7,2,4,6] = 2
(length 7) and[1,6,11,5,10,4,9,3,8,2,7] = 6
(length 11). Hope that helps.$endgroup$
– Kevin Cruijssen
Mar 11 at 14:27
$begingroup$
My bad: I was sure the test case you mentioned was of size 8. But thanks for the extra test cases.
$endgroup$
– Olivier Grégoire
Mar 11 at 15:37
1
$begingroup$
Question as currently formulated seems "wrong"... a single riffle shuffle should result in the first and last cards changing, unless you're pulling some kind of con trick! i.e. [6,1,7,2,8,3,9,4,10,5] after a single shuffle of 10 cards.
$endgroup$
– Steve
Mar 12 at 11:05
2
$begingroup$
@Steve I guess you're kinda right. Riffle-shuffling in general simply interleaves two halves, so both
[1,6,2,7,3,8,4,9,5,10]
or[6,1,7,2,8,3,9,4,10,5]
are possible. In my challenge it does mean that the top card will always remain the top card, so it's indeed a bit of a con-trick.. I've never seen someone irl use only riffle-shuffles to shuffle a deck of cards however. Usually they also use other type of shuffles in between. Anyway, it's too late to change the challenge now, so for the sake of this challenge the top card will always remain the top card after a riffle-shuffle.$endgroup$
– Kevin Cruijssen
Mar 12 at 11:20