Straddling checkerboard: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Scala contribution added.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 179:
}
MsgBox % dec</lang>
 
=={{header|C}}==
{{libheader|GLib}}
Line 492 ⟶ 493:
decoded: INTHEWINTER1965/WEWEREHUNGRY/JUSTBARELYALIVE</lang>
 
=={{header|C sharp|C#}}==
Translation of [[Straddling_checkerboard#Java|Java]] via [[Straddling_checkerboard#D|D]]
<lang csharp>using System;
Line 845 ⟶ 846:
N.decode "8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640"|>Seq.iter(fun n->printf "%s" n);; //IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
</lang>
 
=={{header|Go}}==
<lang go>package main
Line 928 ⟶ 930:
YOUHAVEPUTON7.5POUNDSSINCEISAWYOU.
</pre>
 
=={{header|Haskell}}==
<lang haskell>import Data.Char
Line 983 ⟶ 986:
Decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main()
Line 1,480 ⟶ 1,484:
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
</pre>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 1,640 ⟶ 1,645:
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
</pre >
 
 
=={{header|Perl}}==
Line 1,704 ⟶ 1,708:
Encoded: 13957839363509783697874306781397890578974539936590781347843083207878791798798798783678743067885972839363935
Decoded: ONE.NIGHT.IT.WAS.ON.THE.TWENTIETH.OF.MARCH..1888.I.WAS.RETURNING</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.03}}
The .trans method in Perl 6 improves on Perl 5's tr/// by allowing multi-character translation tables.
 
We build the full table during .new, which simplifies .encode and .decode.
<lang perl6>class Straddling_Checkerboard {
has @!flat_board; # 10x3 stored as 30x1
has $!plain2code; # full translation table, invertable
has @!table; # Printable layout, like Wikipedia entry
my $numeric_escape = '/';
my $exclude = /<-[A..Z0..9.]>/; # Omit the escape character
 
method display_table { gather { take ~ .list for @!table } };
 
method decode ( Str $s --> Str ) {
$s.trans($!plain2code.antipairs);
}
 
method encode ( Str $s, :$collapse? --> Str ) {
my $replace = $collapse ?? '' !! '.';
$s.uc.subst( $exclude, $replace, :g ).trans($!plain2code);
}
 
submethod BUILD ( :$alphabet, :$u where 0..9, :$v where 0..9 ) {
die if $u == $v;
die if $alphabet.comb.sort.join ne [~] flat './', 'A'..'Z';
 
@!flat_board = $alphabet.uc.comb;
@!flat_board.splice( $u min $v, 0, Any );
@!flat_board.splice( $u max $v, 0, Any );
 
@!table = [ ' ',| [ 0 .. 9] ],
[ ' ',|@!flat_board[ 0 .. 9].map: {.defined ?? $_ !! ' '} ],
[ $u, |@!flat_board[10 .. 19] ],
[ $v, |@!flat_board[20 .. 29] ];
 
my @order = 0..9; # This may be passed as a param in the future
 
my @nums = flat @order,
@order.map({ +"$u$_" }),
@order.map({ +"$v$_" });
 
my %c2p = @nums Z=> @!flat_board;
%c2p{$_}:delete if %c2p{$_} eqv Any for keys %c2p;
my %p2c = %c2p.invert;
%p2c{$_} = %p2c{$numeric_escape} ~ $_ for 0..9;
$!plain2code = [%p2c.keys] => [%p2c.values];
}
}
 
sub MAIN ( :$u = 3, :$v = 7, :$alphabet = 'HOLMESRTABCDFGIJKNPQUVWXYZ./' ) {
my Straddling_Checkerboard $sc .= new: :$u, :$v, :$alphabet;
$sc.display_table;
 
for 0..1 -> $collapse {
my $original = 'One night-it was on the twentieth of March, 1888-I was returning';
my $en = $sc.encode($original, :$collapse);
my $de = $sc.decode($en);
say '';
say "Original: $original";
say "Encoded: $en";
say "Decoded: $de";
}
}</lang>
 
Output:<pre> 0 1 2 3 4 5 6 7 8 9
H O L M E S R T
3 A B C D F G I J K N
7 P Q U V W X Y Z . /
 
Original: One night-it was on the twentieth of March, 1888-I was returning
Encoded: 13957839363509783697874306781397890578974539936590781347843083207878791798798798783678743067885972839363935
Decoded: ONE.NIGHT.IT.WAS.ON.THE.TWENTIETH.OF.MARCH..1888.I.WAS.RETURNING
 
Original: One night-it was on the twentieth of March, 1888-I was returning
Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
Decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>
 
=={{header|Phix}}==
Line 2,186 ⟶ 2,111:
450582425181653945125016505180125423293721256216286286288653970163758524
ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
The .trans method in Perl 6 improves on Perl 5's tr/// by allowing multi-character translation tables.
 
We build the full table during .new, which simplifies .encode and .decode.
<lang perl6>class Straddling_Checkerboard {
has @!flat_board; # 10x3 stored as 30x1
has $!plain2code; # full translation table, invertable
has @!table; # Printable layout, like Wikipedia entry
my $numeric_escape = '/';
my $exclude = /<-[A..Z0..9.]>/; # Omit the escape character
 
method display_table { gather { take ~ .list for @!table } };
 
method decode ( Str $s --> Str ) {
$s.trans($!plain2code.antipairs);
}
 
method encode ( Str $s, :$collapse? --> Str ) {
my $replace = $collapse ?? '' !! '.';
$s.uc.subst( $exclude, $replace, :g ).trans($!plain2code);
}
 
submethod BUILD ( :$alphabet, :$u where 0..9, :$v where 0..9 ) {
die if $u == $v;
die if $alphabet.comb.sort.join ne [~] flat './', 'A'..'Z';
 
@!flat_board = $alphabet.uc.comb;
@!flat_board.splice( $u min $v, 0, Any );
@!flat_board.splice( $u max $v, 0, Any );
 
@!table = [ ' ',| [ 0 .. 9] ],
[ ' ',|@!flat_board[ 0 .. 9].map: {.defined ?? $_ !! ' '} ],
[ $u, |@!flat_board[10 .. 19] ],
[ $v, |@!flat_board[20 .. 29] ];
 
my @order = 0..9; # This may be passed as a param in the future
 
my @nums = flat @order,
@order.map({ +"$u$_" }),
@order.map({ +"$v$_" });
 
my %c2p = @nums Z=> @!flat_board;
%c2p{$_}:delete if %c2p{$_} eqv Any for keys %c2p;
my %p2c = %c2p.invert;
%p2c{$_} = %p2c{$numeric_escape} ~ $_ for 0..9;
$!plain2code = [%p2c.keys] => [%p2c.values];
}
}
 
sub MAIN ( :$u = 3, :$v = 7, :$alphabet = 'HOLMESRTABCDFGIJKNPQUVWXYZ./' ) {
my Straddling_Checkerboard $sc .= new: :$u, :$v, :$alphabet;
$sc.display_table;
 
for 0..1 -> $collapse {
my $original = 'One night-it was on the twentieth of March, 1888-I was returning';
my $en = $sc.encode($original, :$collapse);
my $de = $sc.decode($en);
say '';
say "Original: $original";
say "Encoded: $en";
say "Decoded: $de";
}
}</lang>
 
Output:<pre> 0 1 2 3 4 5 6 7 8 9
H O L M E S R T
3 A B C D F G I J K N
7 P Q U V W X Y Z . /
 
Original: One night-it was on the twentieth of March, 1888-I was returning
Encoded: 13957839363509783697874306781397890578974539936590781347843083207878791798798798783678743067885972839363935
Decoded: ONE.NIGHT.IT.WAS.ON.THE.TWENTIETH.OF.MARCH..1888.I.WAS.RETURNING
 
Original: One night-it was on the twentieth of March, 1888-I was returning
Encoded: 139539363509369743061399059745399365901344308320791798798798367430685972839363935
Decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING</pre>
 
=={{header|REXX}}==
Line 2,546 ⟶ 2,551:
decoded: ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
</pre>
 
 
 
=={{header|zkl}}==
10,327

edits