aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-25 19:43:21 +0000
committerGitHub <noreply@github.com>2021-01-25 19:43:21 +0000
commit36d1832e503fc8b091e0e19f678627aab3ab63fb (patch)
tree08c01d58181882dc45a76e2726151aa16fb7e039
parentfcad77840d011bba81e63bb4b76adfdcf06fc96f (diff)
parent256fae8a5acc550f9ef0d4e39f101e4ddeaba4cd (diff)
downloadperlweeklychallenge-club-36d1832e503fc8b091e0e19f678627aab3ab63fb.tar.gz
perlweeklychallenge-club-36d1832e503fc8b091e0e19f678627aab3ab63fb.tar.bz2
perlweeklychallenge-club-36d1832e503fc8b091e0e19f678627aab3ab63fb.zip
Merge pull request #3380 from jan-perl/new-branch
submission
-rw-r--r--challenge-097/jan-perl/README1
-rwxr-xr-xchallenge-097/jan-perl/perl/ch-1.pl24
-rwxr-xr-xchallenge-097/jan-perl/perl/ch-2.pl58
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-097/jan-perl/README b/challenge-097/jan-perl/README
new file mode 100644
index 0000000000..70e335d7d7
--- /dev/null
+++ b/challenge-097/jan-perl/README
@@ -0,0 +1 @@
+Solution by Jan Hoogenraad
diff --git a/challenge-097/jan-perl/perl/ch-1.pl b/challenge-097/jan-perl/perl/ch-1.pl
new file mode 100755
index 0000000000..83a7c9bdeb
--- /dev/null
+++ b/challenge-097/jan-perl/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use warnings;
+
+sub inittrs($) {
+ my ($shift) = @_;
+ my $p1=" ";
+ my $p2=" ";
+ for my $t (0..25) {
+ $p1 .= chr(ord('A')+$t);
+ $p2 .= chr(ord('A')+($t-$shift)%26);
+ }
+ return ($p1,$p2);
+}
+
+$S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
+$N = 3;
+
+($plain,$cipher)= inittrs($N);
+print "Plain: $plain\n";
+print "Cipher: $cipher\n";
+
+eval ("\$S=~ tr/$plain/$cipher/");
+print "$S\n";
diff --git a/challenge-097/jan-perl/perl/ch-2.pl b/challenge-097/jan-perl/perl/ch-2.pl
new file mode 100755
index 0000000000..537c4d4ed6
--- /dev/null
+++ b/challenge-097/jan-perl/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+use warnings;
+
+
+sub nflip2(\@\@) {
+ local (*p0arr,*saarr) = @_;
+ my $nftot=0;
+ for my $l (0.. $#p0arr) {
+ $nftot += ($p0arr[$l] != $saarr[$l]);
+ }
+ return $nftot;
+}
+
+sub nflip($\@) {
+ local ($p0,*sa) = @_;
+ my @p0arr = split("",$p0);
+ my $nftot=0;
+ for my $t (@sa){
+ my @saarr = split("",$t);
+ $nftot += nflip2(@p0arr,@saarr);
+ }
+ return $nftot;
+}
+
+sub doit($$) {
+ my ($B,$S) = @_;
+ print "\n$B,$S\n";
+ my @sa=unpack("(a$S)*",$B);
+ my @ba=map {nflip( $_,@sa)} @sa;
+ my $low=$S * @ba;
+ my $lidx=0;
+ for my $t (0 .. $#ba) {
+ if ($low> $ba[$t]){
+ $low= $ba[$t];
+ $lidx=$t;
+ }
+ }
+ print "Output: $ba[$lidx]\n";
+ print "Reference: $lidx: $sa[$lidx]\n";
+ print "Binary Substrings:\n";
+ my @p0arr = split("",$sa[$lidx]);
+ for my $t (@sa) {
+ my @saarr = split("",$t);
+ my $nftot = nflip2(@p0arr,@saarr);
+ print "\"$t\": $nftot flips";
+ print " tot make it \"$sa[$lidx]\"" if ($nftot);
+ print "\n";
+ }
+}
+
+
+$B = "101100101"; $S = 3;
+doit($B,$S);
+$B = "10110111"; $S = 4;
+doit($B,$S);
+
+