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 altered (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$
add a comment |
$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 altered (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
9 hours ago
$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
9 hours ago
$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
8 hours ago
$begingroup$
Is it ok to returnfalse
or other castable values instead of 0?
$endgroup$
– Alex
4 hours ago
add a comment |
$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 altered (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 altered (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 9 hours ago
Kevin Cruijssen
asked 13 hours ago
Kevin CruijssenKevin Cruijssen
40.2k564208
40.2k564208
$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
9 hours ago
$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
9 hours ago
$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
8 hours ago
$begingroup$
Is it ok to returnfalse
or other castable values instead of 0?
$endgroup$
– Alex
4 hours ago
add a comment |
$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
9 hours ago
$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
9 hours ago
$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
8 hours ago
$begingroup$
Is it ok to returnfalse
or other castable values instead of 0?
$endgroup$
– Alex
4 hours ago
$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
9 hours ago
$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
9 hours ago
$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
9 hours ago
$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
9 hours ago
$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
8 hours ago
$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
8 hours ago
$begingroup$
Is it ok to return
false
or other castable values instead of 0?$endgroup$
– Alex
4 hours ago
$begingroup$
Is it ok to return
false
or other castable values instead of 0?$endgroup$
– Alex
4 hours ago
add a comment |
18 Answers
18
active
oldest
votes
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a 1-indexed deck as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
JavaScript (ES6), 57 52 50 bytes
Expects a 0-indexed deck 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.
Returns False
instead of 0
.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
9 hours ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 bytesSBCS
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help and Erik the Outgolfer for -3.
The TIO link contains two test cases.
Old solution:
{i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
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$
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
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
13 hours ago
$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
12 hours ago
add a comment |
$begingroup$
Perl 6, 34 bytes
{(0...{.[2**$^i%($_-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$
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
11 hours ago
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
|
show 1 more comment
$begingroup$
Perl 6, 34 bytes
-2 bytes thanks to nwellnhof
{($_,*.sort({$++%2})...*[1]==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 minus one.
Explanation:
{ } # Anonymous code block
$_, # Starting from the input list
*.sort({$++%2}) # Sort by the parity of each index
... # Until
*[1]==2 # The list is in ascending order
( )-1 # And return the length of the list minus 1
$endgroup$
$begingroup$
34 bytes:{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
add a comment |
$begingroup$
Japt, 13 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
eUñ)?T:ÒßUñÏu
Try it or run all test cases
eUñ)?T:ÒßUñÏu :Implicit input of integer array U
e :Test U for equality with
Uñ : U sorted
) :End test
? :If true
T : Return 0
: :Else return
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
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$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
$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$
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$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
$endgroup$
add a comment |
$begingroup$
C (GCC) 64 bytes
i,r;f(c,v)int*v;{for(i=1,r=0;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$
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$
R, 70 bytes
x=scan();i=1;while(any((x=c(x[y<-seq(x)%%2>0],x[!y]))>sort(x)))i=i+1;i
Try it online!
$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
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES6), 44 bytes
Shorter version suggested by @nwellnhof
Expects a 1-indexed deck as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
JavaScript (ES6), 57 52 50 bytes
Expects a 0-indexed deck 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 1-indexed deck as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
JavaScript (ES6), 57 52 50 bytes
Expects a 0-indexed deck 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 1-indexed deck as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
JavaScript (ES6), 57 52 50 bytes
Expects a 0-indexed deck 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 1-indexed deck as input.
f=(a,x=1)=>a[x]-2&&1+f(a,x*2%(a.length-1|1))
Try it online!
JavaScript (ES6), 57 52 50 bytes
Expects a 0-indexed deck 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 10 hours ago
answered 12 hours ago
ArnauldArnauld
78.4k795327
78.4k795327
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.
Returns False
instead of 0
.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
9 hours ago
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.
Returns False
instead of 0
.
$endgroup$
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
9 hours ago
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.
Returns False
instead of 0
.
$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.
Returns False
instead of 0
.
edited 9 hours ago
answered 12 hours ago
Erik the OutgolferErik the Outgolfer
32.3k429104
32.3k429104
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
9 hours ago
add a comment |
$begingroup$
Save four bytes withf=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...!=
can be-
. ;-)
$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or justx[1]>2
I guess)
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
Save four bytes with
f=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
Save four bytes with
f=lambda x:2!=x[1]and-~f(x[::2]+x[1::2])
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...
!=
can be -
. ;-)$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
@JonathanAllan Oh, of course! Well...
!=
can be -
. ;-)$endgroup$
– Erik the Outgolfer
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or just
x[1]>2
I guess)$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
Ah, yeah caveat emptor :D (or just
x[1]>2
I guess)$endgroup$
– Jonathan Allan
9 hours ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 bytesSBCS
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help and Erik the Outgolfer for -3.
The TIO link contains two test cases.
Old solution:
{i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 bytesSBCS
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help and Erik the Outgolfer for -3.
The TIO link contains two test cases.
Old solution:
{i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}
$endgroup$
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
add a comment |
$begingroup$
APL (Dyalog Unicode), 35 26 23 bytesSBCS
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help and Erik the Outgolfer for -3.
The TIO link contains two test cases.
Old solution:
{i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}
$endgroup$
APL (Dyalog Unicode), 35 26 23 bytesSBCS
{∧/2≤/⍵:0⋄1+∇⍵[⍒2|⍳⍴⍵]}
Try it online!
Thanks to Adám for the help and Erik the Outgolfer for -3.
The TIO link contains two test cases.
Old solution:
{i⊣{i+←1⋄⍵[⍒2|⍳⍴⍵]}⍣{∧/2≤/⍵}⍵⊣i←¯1}
edited 10 hours ago
answered 11 hours ago
VenVen
2,29511123
2,29511123
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
add a comment |
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
1
1
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
Count the recursion depth for -3.
$endgroup$
– Erik the Outgolfer
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
$begingroup$
@EriktheOutgolfer Much better, thanks!
$endgroup$
– Ven
10 hours ago
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$
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
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$
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
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 3 hours ago
answered 13 hours ago
Jonathan AllanJonathan Allan
52.8k535171
52.8k535171
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
add a comment |
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
Would you mind adding an explanation?
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
$begingroup$
@KevinCruijssen - done
$endgroup$
– Jonathan Allan
3 hours ago
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
13 hours ago
$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
12 hours ago
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
13 hours ago
$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
12 hours ago
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 13 hours ago
EmignaEmigna
46.9k433142
46.9k433142
$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
13 hours ago
$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
12 hours ago
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
13 hours ago
$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
12 hours ago
$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
13 hours ago
$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
13 hours ago
$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
12 hours ago
$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
12 hours ago
add a comment |
$begingroup$
Perl 6, 34 bytes
{(0...{.[2**$^i%($_-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 bytes
{(0...{.[2**$^i%($_-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 bytes
{(0...{.[2**$^i%($_-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 bytes
{(0...{.[2**$^i%($_-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 10 hours ago
answered 10 hours ago
nwellnhofnwellnhof
7,31511128
7,31511128
add a comment |
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
11 hours ago
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
|
show 1 more 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
11 hours ago
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
|
show 1 more 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$
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 8 hours ago
answered 11 hours ago
Olivier GrégoireOlivier Grégoire
9,24511944
9,24511944
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
11 hours ago
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
|
show 1 more comment
$begingroup$
163 bytes by using two timesx.clone()
instead ofA.copyOf(x,l)
.
$endgroup$
– Kevin Cruijssen
11 hours ago
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
$begingroup$
163 bytes by using two times
x.clone()
instead of A.copyOf(x,l)
.$endgroup$
– Kevin Cruijssen
11 hours ago
$begingroup$
163 bytes by using two times
x.clone()
instead of A.copyOf(x,l)
.$endgroup$
– Kevin Cruijssen
11 hours ago
2
2
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
64 bytes
$endgroup$
– Arnauld
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$begingroup$
@Arnauld Thanks! I had a hard time figuring how to simplify that "length if odd else length - 1"
$endgroup$
– Olivier Grégoire
10 hours ago
$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
10 hours ago
$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
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
$begingroup$
More precisely, it's equivalent to an improvement over my original algorithm found by @nwellnhof.
$endgroup$
– Arnauld
10 hours ago
|
show 1 more comment
$begingroup$
Perl 6, 34 bytes
-2 bytes thanks to nwellnhof
{($_,*.sort({$++%2})...*[1]==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 minus one.
Explanation:
{ } # Anonymous code block
$_, # Starting from the input list
*.sort({$++%2}) # Sort by the parity of each index
... # Until
*[1]==2 # The list is in ascending order
( )-1 # And return the length of the list minus 1
$endgroup$
$begingroup$
34 bytes:{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
add a comment |
$begingroup$
Perl 6, 34 bytes
-2 bytes thanks to nwellnhof
{($_,*.sort({$++%2})...*[1]==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 minus one.
Explanation:
{ } # Anonymous code block
$_, # Starting from the input list
*.sort({$++%2}) # Sort by the parity of each index
... # Until
*[1]==2 # The list is in ascending order
( )-1 # And return the length of the list minus 1
$endgroup$
$begingroup$
34 bytes:{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
add a comment |
$begingroup$
Perl 6, 34 bytes
-2 bytes thanks to nwellnhof
{($_,*.sort({$++%2})...*[1]==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 minus one.
Explanation:
{ } # Anonymous code block
$_, # Starting from the input list
*.sort({$++%2}) # Sort by the parity of each index
... # Until
*[1]==2 # The list is in ascending order
( )-1 # And return the length of the list minus 1
$endgroup$
Perl 6, 34 bytes
-2 bytes thanks to nwellnhof
{($_,*.sort({$++%2})...*[1]==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 minus one.
Explanation:
{ } # Anonymous code block
$_, # Starting from the input list
*.sort({$++%2}) # Sort by the parity of each index
... # Until
*[1]==2 # The list is in ascending order
( )-1 # And return the length of the list minus 1
edited 2 hours ago
answered 13 hours ago
Jo KingJo King
24.6k357126
24.6k357126
$begingroup$
34 bytes:{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
add a comment |
$begingroup$
34 bytes:{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
$begingroup$
34 bytes:
{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
$begingroup$
34 bytes:
{($_,*.sort({$++%2})...*[1]==2)-1}
$endgroup$
– nwellnhof
8 hours ago
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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
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 7 hours ago
answered 8 hours ago
RosLuPRosLuP
2,146514
2,146514
$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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
$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
2 hours ago
$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
2 hours ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
$begingroup$
@Kerndog73 All is number, but in APL think characters are not numbers... (they seems element in AV array)
$endgroup$
– RosLuP
32 mins ago
add a comment |
$begingroup$
Japt, 13 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
eUñ)?T:ÒßUñÏu
Try it or run all test cases
eUñ)?T:ÒßUñÏu :Implicit input of integer array U
e :Test U for equality with
Uñ : U sorted
) :End test
? :If true
T : Return 0
: :Else return
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
add a comment |
$begingroup$
Japt, 13 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
eUñ)?T:ÒßUñÏu
Try it or run all test cases
eUñ)?T:ÒßUñÏu :Implicit input of integer array U
e :Test U for equality with
Uñ : U sorted
) :End test
? :If true
T : Return 0
: :Else return
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
$endgroup$
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
add a comment |
$begingroup$
Japt, 13 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
eUñ)?T:ÒßUñÏu
Try it or run all test cases
eUñ)?T:ÒßUñÏu :Implicit input of integer array U
e :Test U for equality with
Uñ : U sorted
) :End test
? :If true
T : Return 0
: :Else return
Ò : 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 bytes
Taking my shiny, new, very-work-in-progress interpreter for a test drive.
eUñ)?T:ÒßUñÏu
Try it or run all test cases
eUñ)?T:ÒßUñÏu :Implicit input of integer array U
e :Test U for equality with
Uñ : U sorted
) :End test
? :If true
T : Return 0
: :Else return
Ò : Negation of bitwise NOT of
ß : A recursive call to the programme with input
Uñ : U sorted
Ï : By 0-based indices
u : Modulo 2
edited 12 hours ago
answered 12 hours ago
ShaggyShaggy
19.4k21667
19.4k21667
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
add a comment |
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
$begingroup$
This interpreter looks super cool.
$endgroup$
– recursive
22 mins ago
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 8 hours ago
Rainer GlügeRainer Glüge
1313
1313
add a comment |
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
$endgroup$
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
$endgroup$
add a comment |
$begingroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
$endgroup$
Pyth, 18 bytes
L?SIb0hys%L2>Bb1
y
Try it online!
-2 thanks to @Erik the Outgolfer.
edited 8 hours ago
answered 8 hours ago
VenVen
2,29511123
2,29511123
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 3 hours ago
answered 9 hours ago
Galen IvanovGalen Ivanov
7,07211034
7,07211034
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 4 hours ago
answered 4 hours ago
AlexAlex
3263
3263
add a comment |
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
$endgroup$
add a comment |
$begingroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
$endgroup$
J, 28 bytes
1#@}.(/:2|i.@#)^:(2<1{])^:a:
Try it online!
answered 3 hours ago
Galen IvanovGalen Ivanov
7,07211034
7,07211034
add a comment |
add a comment |
$begingroup$
C (GCC) 64 bytes
i,r;f(c,v)int*v;{for(i=1,r=0;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 bytes
i,r;f(c,v)int*v;{for(i=1,r=0;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 bytes
i,r;f(c,v)int*v;{for(i=1,r=0;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 bytes
i,r;f(c,v)int*v;{for(i=1,r=0;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 3 hours ago
answered 4 hours ago
rtpaxrtpax
2765
2765
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 2 hours ago
XcaliXcali
5,435520
5,435520
add a comment |
add a comment |
$begingroup$
R, 70 bytes
x=scan();i=1;while(any((x=c(x[y<-seq(x)%%2>0],x[!y]))>sort(x)))i=i+1;i
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 70 bytes
x=scan();i=1;while(any((x=c(x[y<-seq(x)%%2>0],x[!y]))>sort(x)))i=i+1;i
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 70 bytes
x=scan();i=1;while(any((x=c(x[y<-seq(x)%%2>0],x[!y]))>sort(x)))i=i+1;i
Try it online!
$endgroup$
R, 70 bytes
x=scan();i=1;while(any((x=c(x[y<-seq(x)%%2>0],x[!y]))>sort(x)))i=i+1;i
Try it online!
answered 1 hour ago
Nick KennedyNick Kennedy
57127
57127
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
9 hours ago
$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
9 hours ago
$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
8 hours ago
$begingroup$
Is it ok to return
false
or other castable values instead of 0?$endgroup$
– Alex
4 hours ago