Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
21
Task/Topswops/Erlang/topswops.erl
Normal file
21
Task/Topswops/Erlang/topswops.erl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
-module( topswops ).
|
||||
|
||||
-export( [get_1_first/1, swap/1, task/0] ).
|
||||
|
||||
get_1_first( [1 | _T] ) -> 0;
|
||||
get_1_first( List ) -> 1 + get_1_first( swap(List) ).
|
||||
|
||||
swap( [N | _T]=List ) ->
|
||||
{Swaps, Remains} = lists:split( N, List ),
|
||||
lists:reverse( Swaps ) ++ Remains.
|
||||
|
||||
task() ->
|
||||
Permutations = [{X, permute:permute(lists:seq(1, X))} || X <- lists:seq(1, 10)],
|
||||
Swops = [{N, get_1_first_many(N_permutations)} || {N, N_permutations} <- Permutations],
|
||||
Topswops = [{N, lists:max(N_swops)} || {N, N_swops} <- Swops],
|
||||
io:fwrite( "N topswaps~n" ),
|
||||
[io:fwrite("~p ~p~n", [N, Max]) || {N, Max} <- Topswops].
|
||||
|
||||
|
||||
|
||||
get_1_first_many( N_permutations ) -> [get_1_first(X) || X <- N_permutations].
|
||||
32
Task/Topswops/PL-I/topswops.pli
Normal file
32
Task/Topswops/PL-I/topswops.pli
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
(subscriptrange):
|
||||
topswap: procedure options (main); /* 12 November 2013 */
|
||||
declare cards(*) fixed (2) controlled, t fixed (2);
|
||||
declare dealt(*) bit(1) controlled;
|
||||
declare (count, i, m, n, c1, c2) fixed binary;
|
||||
declare random builtin;
|
||||
|
||||
do n = 1 to 10;
|
||||
allocate cards(n), dealt(n);
|
||||
/* Take the n cards, in order ... */
|
||||
do i = 1 to n; cards(i) = i; end;
|
||||
/* ... and shuffle them. */
|
||||
do i = 1 to n;
|
||||
c1 = random*n+1; c2 = random*n+1;
|
||||
t = cards(c1); cards(c1) = cards(c2); cards(c2) = t;
|
||||
end;
|
||||
/* If '1' is the first card, game is trivial; swap it with another. */
|
||||
if cards(1) = 1 & n > 1 then
|
||||
do; t = cards(1); cards(1) = cards(2); cards(2) = t; end;
|
||||
|
||||
count = 0;
|
||||
do until (cards(1) = 1);
|
||||
/* take the value of the first card, M, and reverse the first M cards. */
|
||||
m = cards(1);
|
||||
do i = 1 to m/2;
|
||||
t = cards(i); cards(i) = cards(m-i+1); cards(m-i+1) = t;
|
||||
end;
|
||||
count = count + 1;
|
||||
end;
|
||||
put skip edit (n, ':', count) (f(2), a, f(4));
|
||||
end;
|
||||
end topswap;
|
||||
35
Task/Topswops/Perl/topswops.pl
Normal file
35
Task/Topswops/Perl/topswops.pl
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
sub next_swop {
|
||||
my( $max, $level, $p, $d ) = @_;
|
||||
my $swopped = 0;
|
||||
for( 2..@$p ){ # find possibilities
|
||||
my @now = @$p;
|
||||
if( $_ == $now[$_-1] ) {
|
||||
splice @now, 0, 0, reverse splice @now, 0, $_;
|
||||
$swopped = 1;
|
||||
next_swop( $max, $level+1, \@now, [ @$d ] );
|
||||
}
|
||||
}
|
||||
for( 1..@$d ) { # create possibilities
|
||||
my @now = @$p;
|
||||
my $next = shift @$d;
|
||||
if( not $now[$next-1] ) {
|
||||
$now[$next-1] = $next;
|
||||
splice @now, 0, 0, reverse splice @now, 0, $next;
|
||||
$swopped = 1;
|
||||
next_swop( $max, $level+1, \@now, [ @$d ] );
|
||||
}
|
||||
push @$d, $next;
|
||||
}
|
||||
$$max = $level if !$swopped and $level > $$max;
|
||||
}
|
||||
|
||||
sub topswops {
|
||||
my $n = shift;
|
||||
my @d = 2..$n;
|
||||
my @p = ( 1, (0) x ($n-1) );
|
||||
my $max = 0;
|
||||
next_swop( \$max, 0, \@p, \@d );
|
||||
return $max;
|
||||
}
|
||||
|
||||
printf "Maximum swops for %2d cards: %2d\n", $_, topswops $_ for 1..10;
|
||||
Loading…
Add table
Add a link
Reference in a new issue