aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2023-04-10 10:09:14 +0100
committerGitHub <noreply@github.com>2023-04-10 10:09:14 +0100
commit9708225cfd6d9d3aad0928f4b2c3e7d0618eabc0 (patch)
tree0b3119129ba5688a91a93f367eb3b4a8a37e20cc
parent4eb9782a746173721822a9ffa29d6f14297a6dca (diff)
downloadperlweeklychallenge-club-9708225cfd6d9d3aad0928f4b2c3e7d0618eabc0.tar.gz
perlweeklychallenge-club-9708225cfd6d9d3aad0928f4b2c3e7d0618eabc0.tar.bz2
perlweeklychallenge-club-9708225cfd6d9d3aad0928f4b2c3e7d0618eabc0.zip
Create ch-1.pl
-rw-r--r--challenge-212/james-smith/perl/ch-1.pl38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-212/james-smith/perl/ch-1.pl b/challenge-212/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..fb23119d75
--- /dev/null
+++ b/challenge-212/james-smith/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/local/bin/perl
+
+use strict;
+use warnings;
+use feature qw(say);
+use Test::More;
+
+my @TESTS = (
+ [ ['Perl',2,22,19,9], 'Raku' ],
+ [ ['Raku',24,4,7,17], 'Perl' ],
+);
+
+
+sub jumping_letters {
+ # Stitch back into word
+ return join '',
+ # Like ord below chr acts on $_ if no parameters
+ # are passed...
+ map { chr }
+ # Do the maths.... now this is where things get
+ # a little cheeky.... ord acts on $_ which is the
+ # letter, shift returns the next value of @_ which
+ # is the shift!
+ # 96&ord| .... preserves the 64 & 32 bit - it is
+ # the 32 represents upper or lowercase
+ # the 64 indicates that this is a letter (sort of)
+ # 31&ord removes these and returns the numeric
+ # position of the number in the alphabet - we subtract
+ # one to get the zero based position + shift it
+ # wrap and them move back to a one based position.
+ map { (96&ord) | ( (31&ord) -1 + shift)%26 +1 }
+ # Split into individual letters;
+ split //,
+ ## This is the word we are "changing"
+ shift;
+}
+
+is( jumping_letters( @{$_->[0]} ), $_->[1] ) for @TESTS;