From e80c488ca8d5272cf34c42d24c69bfa105763ea5 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Thu, 18 Feb 2021 23:44:13 -0500 Subject: Challenge 97 by Jaldhar H. Vyas --- challenge-097/jaldhar-h-vyas/blog.txt | 1 + challenge-097/jaldhar-h-vyas/perl/ch-1.pl | 35 +++++++++++++++++++++++++++++ challenge-097/jaldhar-h-vyas/perl/ch-2.pl | 31 +++++++++++++++++++++++++ challenge-097/jaldhar-h-vyas/raku/ch-1.raku | 11 +++++++++ challenge-097/jaldhar-h-vyas/raku/ch-2.raku | 11 +++++++++ 5 files changed, 89 insertions(+) create mode 100644 challenge-097/jaldhar-h-vyas/blog.txt create mode 100755 challenge-097/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-097/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-097/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-097/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-097/jaldhar-h-vyas/blog.txt b/challenge-097/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..b4b7834bda --- /dev/null +++ b/challenge-097/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2021/02/perl_weekly_challenge_week_97.html diff --git a/challenge-097/jaldhar-h-vyas/perl/ch-1.pl b/challenge-097/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..d5984b1619 --- /dev/null +++ b/challenge-097/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub usage { + print<<"-USAGE-"; + $PROGRAM_NAME + + a message to encrypt + number of letters to shift left in cipher +-USAGE- + exit 0; +} + +sub rotateLeft { + my ($str, $n) = @_; + + for (1 .. $n) { + unshift @{$str}, pop @{$str}; + } + + return @{$str}; +} + +if (scalar @ARGV != 2) { + usage; +} + +my ($S, $N) = @ARGV; + +my @plain = qw/ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z /; +my @cipher = rotateLeft(\@plain, $N); + +say join q{}, map { $cipher[ord($_) - ord('A')] // $_; } (split //, $S); \ No newline at end of file diff --git a/challenge-097/jaldhar-h-vyas/perl/ch-2.pl b/challenge-097/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..9307d73744 --- /dev/null +++ b/challenge-097/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub usage { + print<<"-USAGE-"; + $PROGRAM_NAME + + a binary string + size of substrings +-USAGE- + exit 0; +} + + +if (scalar @ARGV != 2) { + usage; +} + +my ($B, $S) = @ARGV; + +my @substrings = map { '0b' . $_; } unpack("(A$S)*", $B); +my $template = shift @substrings; +my $flips = 0; + +for my $string (@substrings) { + $flips += sprintf('%b', oct($template) ^ oct($string)) =~ tr/1/1/; +} + +say $flips; \ No newline at end of file diff --git a/challenge-097/jaldhar-h-vyas/raku/ch-1.raku b/challenge-097/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..6c36a2b07c --- /dev/null +++ b/challenge-097/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/raku + +sub MAIN( + Str $S, #= a message to encrypt + Int $N #= number of letters to shift left in cipher +) { + my @plain = < A B C D E F G H I J K L M N O P Q R S T U V W X Y Z >; + my @cipher = @plain.rotate(-$N); + + $S.trans(@plain => @cipher).say; +} \ No newline at end of file diff --git a/challenge-097/jaldhar-h-vyas/raku/ch-2.raku b/challenge-097/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..4f02b26376 --- /dev/null +++ b/challenge-097/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/raku + +sub MAIN( + Str $B, #= a binary string + Int $S #= size of substrings +) { + my @substrings = $B.comb($S.Int).map({ $_.parse-base(2); }); + my $template = @substrings.shift; + + say [+] @substrings.map({ sprintf("%b", $template +^ $_) ~~ m:g/ 1 /; }); +} \ No newline at end of file -- cgit