aboutsummaryrefslogtreecommitdiff
path: root/challenge-097
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-31 19:10:43 +0000
committerGitHub <noreply@github.com>2021-01-31 19:10:43 +0000
commit9edc6c343355cd4e89acd15c64a6942c49d50f46 (patch)
treefd474944604beb7accd43c05c6ed0a73d6cdb4d2 /challenge-097
parent0afb9dcad93d2d616178d419614b84b61714114b (diff)
parent08c66c46647e387961d052186b9ad8ae5170221c (diff)
downloadperlweeklychallenge-club-9edc6c343355cd4e89acd15c64a6942c49d50f46.tar.gz
perlweeklychallenge-club-9edc6c343355cd4e89acd15c64a6942c49d50f46.tar.bz2
perlweeklychallenge-club-9edc6c343355cd4e89acd15c64a6942c49d50f46.zip
Merge pull request #3428 from wanderdoc/master
Solutions to challenge-097.
Diffstat (limited to 'challenge-097')
-rw-r--r--challenge-097/wanderdoc/perl/ch-1.pl43
-rw-r--r--challenge-097/wanderdoc/perl/ch-2.pl50
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-097/wanderdoc/perl/ch-1.pl b/challenge-097/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..240b0cde3b
--- /dev/null
+++ b/challenge-097/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given string $S containing alphabets A..Z only and a number $N. Write a script to encrypt the given string $S using Caesar Cipher with left shift of size $N.
+Example
+
+Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3
+Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"
+Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
+Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW
+
+Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
+Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
+
+=cut
+
+use Test::More;
+
+sub caesar
+{
+ my ($str, $num) = @_;
+
+ my @alphabet = 'A' .. 'Z';
+
+ my %encrypt;
+ $encrypt{q( )} = q( );
+ @encrypt{@alphabet} = @alphabet[ $#alphabet - $num + 1 .. $#alphabet,
+ 0 .. $#alphabet - $num];
+ $str = join '',
+ map $encrypt{$_},
+ split(//,$str);
+
+ return $str;
+}
+
+
+
+
+is(caesar(qq(THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG), 3),
+ qq(QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD), 'Example');
+done_testing(); \ No newline at end of file
diff --git a/challenge-097/wanderdoc/perl/ch-2.pl b/challenge-097/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..acdec1bd6c
--- /dev/null
+++ b/challenge-097/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a binary string $B and an integer $S. Write a script to split the binary string $B of size $S and then find the minimum number of flips required to make it all the same.
+Example 1: Input: $B = “101100101”, $S = 3
+Output: 1
+Binary Substrings: "101": 0 flip "100": 1 flip to make it "101" "101": 0 flip
+Example 2: Input $B = “10110111”, $S = 4
+Output: 2
+Binary Substrings: "1011": 0 flip "0111": 2 flips to make it "1011"
+
+=cut
+
+
+use List::Util qw(min sum);
+use Test::More;
+
+sub count_flips
+{
+ my ($str, $int) = @_;
+ my $length = length($str);
+
+ die "Size mismatch!$/" if ($length % $int or $length <= $int);
+
+ my $format = "a${int}" x ($length/$int);
+
+ my @arr = unpack $format, $str;
+
+ my $min = $length;
+ for my $idx ( 0 .. $#arr )
+ {
+ my $comparing = $arr[$idx];
+ my $sum = sum( map { unpack '%32b*', $comparing ^ $_ }
+ @arr[grep $_ != $idx, 0 .. $#arr] );
+ $min = $sum if $sum < $min;
+ }
+
+
+ return $min;
+}
+
+
+is(count_flips(qw(101100101), 3), 1, 'Example 1');
+is(count_flips(qw(10110111), 4), 2, 'Example 2');
+is(count_flips(qw(100111110), 3), 2, 'My Example');
+
+
+done_testing(); \ No newline at end of file