diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2021-02-18 23:44:13 -0500 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2021-02-18 23:44:13 -0500 |
| commit | e80c488ca8d5272cf34c42d24c69bfa105763ea5 (patch) | |
| tree | 584c02137c4395931bab2353407fae859e7216bd /challenge-097 | |
| parent | fd74e66af1b93c344ce8bc4076760538bc1d0651 (diff) | |
| download | perlweeklychallenge-club-e80c488ca8d5272cf34c42d24c69bfa105763ea5.tar.gz perlweeklychallenge-club-e80c488ca8d5272cf34c42d24c69bfa105763ea5.tar.bz2 perlweeklychallenge-club-e80c488ca8d5272cf34c42d24c69bfa105763ea5.zip | |
Challenge 97 by Jaldhar H. Vyas
Diffstat (limited to 'challenge-097')
| -rw-r--r-- | challenge-097/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-097/jaldhar-h-vyas/perl/ch-1.pl | 35 | ||||
| -rwxr-xr-x | challenge-097/jaldhar-h-vyas/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-097/jaldhar-h-vyas/raku/ch-1.raku | 11 | ||||
| -rwxr-xr-x | challenge-097/jaldhar-h-vyas/raku/ch-2.raku | 11 |
5 files changed, 89 insertions, 0 deletions
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 <S> <N> + + <S> a message to encrypt + <N> 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 <S> <N> + + <S> a binary string + <N> 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 |
