Skip to content

Commit

Permalink
Minor changes to match Raku implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dango committed Nov 18, 2024
1 parent a9d234b commit c3632fd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
3 changes: 3 additions & 0 deletions exercises/practice/rotational-cipher/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"authors": [
"glennj"
],
"contributors": [
"m-dango"
],
"files": {
"solution": [
"lib/RotationalCipher.pm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use warnings;
use experimental qw<signatures postderef postderef_qq>;

use Exporter qw<import>;
our @EXPORT_OK = qw<rotate>;
our @EXPORT_OK = qw<caesar_cipher>;

# from https://perldoc.pl/perlop#tr/SEARCHLIST/REPLACEMENTLIST/cdsr
# > Because the transliteration table is built at compile time, neither the
# > SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote
# > interpolation. That means that if you want to use variables, you must
# > use an eval()

sub rotate ( $text, $shift ) {
sub caesar_cipher ( $text, $shift ) {
my $alpha = 'abcdefghijklmnopqrstuvwxyz';
my $rotated = substr( $alpha, $shift % 26 ) . substr( $alpha, 0, $shift % 26 );
$alpha .= uc $alpha;
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/rotational-cipher/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
subs: rotate
subs: caesar_cipher
properties:
rotate:
test: |-
use Data::Dmp;
sprintf(<<'END', dmp($case->{input}{text}), $case->{input}{shiftKey}, dmp($case->{expected}), dmp($case->{description}));
is(
rotate(%s, %s),
caesar_cipher(%s, %s),
%s,
%s
);
END
stub: |-
sub rotate ($text, $shift) {
sub caesar_cipher ($text, $shift_key) {
return undef;
}
Expand All @@ -23,7 +23,7 @@ example: |-
# > interpolation. That means that if you want to use variables, you must
# > use an eval()
sub rotate ($text, $shift) {
sub caesar_cipher ($text, $shift) {
my $alpha = 'abcdefghijklmnopqrstuvwxyz';
my $rotated = substr($alpha, $shift % 26) . substr($alpha, 0, $shift % 26);
$alpha .= uc $alpha;
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/rotational-cipher/lib/RotationalCipher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package RotationalCipher;
use v5.40;

use Exporter qw<import>;
our @EXPORT_OK = qw<rotate>;
our @EXPORT_OK = qw<caesar_cipher>;

sub rotate ( $text, $shift ) {
sub caesar_cipher ( $text, $shift_key ) {
return undef;
}

Expand Down
22 changes: 11 additions & 11 deletions exercises/practice/rotational-cipher/t/rotational-cipher.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,64 @@ use Test2::V0;
use FindBin qw<$Bin>;
use lib "$Bin/../lib", "$Bin/../local/lib/perl5";

use RotationalCipher qw<rotate>;
use RotationalCipher qw<caesar_cipher>;

is( # begin: 74e58a38-e484-43f1-9466-877a7515e10f
rotate( "a", 0 ),
caesar_cipher( "a", 0 ),
"a",
"rotate a by 0, same output as input"
); # end: 74e58a38-e484-43f1-9466-877a7515e10f

is( # begin: 7ee352c6-e6b0-4930-b903-d09943ecb8f5
rotate( "a", 1 ),
caesar_cipher( "a", 1 ),
"b",
"rotate a by 1"
); # end: 7ee352c6-e6b0-4930-b903-d09943ecb8f5

is( # begin: edf0a733-4231-4594-a5ee-46a4009ad764
rotate( "a", 26 ),
caesar_cipher( "a", 26 ),
"a",
"rotate a by 26, same output as input"
); # end: edf0a733-4231-4594-a5ee-46a4009ad764

is( # begin: e3e82cb9-2a5b-403f-9931-e43213879300
rotate( "m", 13 ),
caesar_cipher( "m", 13 ),
"z",
"rotate m by 13"
); # end: e3e82cb9-2a5b-403f-9931-e43213879300

is( # begin: 19f9eb78-e2ad-4da4-8fe3-9291d47c1709
rotate( "n", 13 ),
caesar_cipher( "n", 13 ),
"a",
"rotate n by 13 with wrap around alphabet"
); # end: 19f9eb78-e2ad-4da4-8fe3-9291d47c1709

is( # begin: a116aef4-225b-4da9-884f-e8023ca6408a
rotate( "OMG", 5 ),
caesar_cipher( "OMG", 5 ),
"TRL",
"rotate capital letters"
); # end: a116aef4-225b-4da9-884f-e8023ca6408a

is( # begin: 71b541bb-819c-4dc6-a9c3-132ef9bb737b
rotate( "O M G", 5 ),
caesar_cipher( "O M G", 5 ),
"T R L",
"rotate spaces"
); # end: 71b541bb-819c-4dc6-a9c3-132ef9bb737b

is( # begin: ef32601d-e9ef-4b29-b2b5-8971392282e6
rotate( "Testing 1 2 3 testing", 4 ),
caesar_cipher( "Testing 1 2 3 testing", 4 ),
"Xiwxmrk 1 2 3 xiwxmrk",
"rotate numbers"
); # end: ef32601d-e9ef-4b29-b2b5-8971392282e6

is( # begin: 32dd74f6-db2b-41a6-b02c-82eb4f93e549
rotate( "Let's eat, Grandma!", 21 ),
caesar_cipher( "Let's eat, Grandma!", 21 ),
"Gzo'n zvo, Bmviyhv!",
"rotate punctuation"
); # end: 32dd74f6-db2b-41a6-b02c-82eb4f93e549

is( # begin: 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9
rotate( "The quick brown fox jumps over the lazy dog.", 13 ),
caesar_cipher( "The quick brown fox jumps over the lazy dog.", 13 ),
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
"rotate all letters"
); # end: 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9
Expand Down

0 comments on commit c3632fd

Please sign in to comment.