Card shuffles: Difference between revisions
Content added Content deleted
(Added Kotlin) |
SqrtNegInf (talk | contribs) (Added Perl example) |
||
Line 988: | Line 988: | ||
<pre>%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52] |
<pre>%1 = [1, 2, 3, 21, 4, 22, 23, 5, 24, 25, 26, 6, 27, 28, 29, 30, 7, 31, 32, 33, 34, 35, 36, 8, 37, 38, 39, 40, 9, 10, 11, 12, 41, 42, 43, 13, 44, 45, 14, 46, 47, 48, 15, 16, 17, 49, 50, 18, 51, 19, 20, 52] |
||
%2 = [44, 45, 46, 47, 48, 49, 50, 51, 52, 43, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4]</pre> |
%2 = [44, 45, 46, 47, 48, 49, 50, 51, 52, 43, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 23, 24, 25, 26, 27, 28, 29, 30, 31, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 1, 2, 3, 4]</pre> |
||
=={{header|Perl}}== |
|||
Follows the Perl 6 implementation for the overhand shuffle, but uses classic one-liner for riffle. |
|||
<lang perl>sub overhand { |
|||
our @cards; local *cards = shift; |
|||
my(@splits,@shuffle); |
|||
my $x = int +@cards / 5; |
|||
push @splits, (1..$x)[int rand $x] for 1..+@cards; |
|||
while (@cards) { |
|||
push @shuffle, [splice @cards, 0, shift @splits]; |
|||
} |
|||
@cards = flatten(reverse @shuffle); |
|||
} |
|||
sub flatten { map { ref $_ eq 'ARRAY' ? @$_ : $_ } @_ } |
|||
sub riffle { |
|||
our @cards; local *cards = shift; |
|||
splice @cards, @cards/2 - $_, 0, pop @cards for 0 .. (@cards/2)-1; |
|||
} |
|||
@cards = 1..20; |
|||
overhand(\@cards) for 1..10; |
|||
print join ' ', @cards, "\n"; |
|||
@cards = 1..20; |
|||
riffle(\@cards) for 1..10; |
|||
print join ' ', @cards, "\n";</lang> |
|||
{{out}} |
|||
<pre>9 11 5 2 4 14 1 3 8 6 15 13 16 12 19 20 7 18 10 17 |
|||
1 10 19 9 18 8 17 7 16 6 15 5 14 4 13 3 12 2 11 20</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |