aboutsummaryrefslogtreecommitdiff
path: root/challenge-097
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-31 11:09:43 +0000
committerGitHub <noreply@github.com>2021-01-31 11:09:43 +0000
commit1b6ee0217ca077428261f062d9c5933a830c1613 (patch)
treef95e74e03d4f13664c5a83df28de25b3338e1902 /challenge-097
parent4d2baf723422257b618129172726535b97eb0fa0 (diff)
parentb829f5fda637a2a84dcaa2a1ea4e59980a9f2c87 (diff)
downloadperlweeklychallenge-club-1b6ee0217ca077428261f062d9c5933a830c1613.tar.gz
perlweeklychallenge-club-1b6ee0217ca077428261f062d9c5933a830c1613.tar.bz2
perlweeklychallenge-club-1b6ee0217ca077428261f062d9c5933a830c1613.zip
Merge pull request #3422 from LoneWolfiNTj/new-branch
Submitting solutions for both tasks in challeng 097.
Diffstat (limited to 'challenge-097')
-rwxr-xr-xchallenge-097/LoneWolfiNTj/README1
-rwxr-xr-xchallenge-097/LoneWolfiNTj/perl/ch-1.pl60
-rwxr-xr-xchallenge-097/LoneWolfiNTj/perl/ch-2.pl59
3 files changed, 120 insertions, 0 deletions
diff --git a/challenge-097/LoneWolfiNTj/README b/challenge-097/LoneWolfiNTj/README
new file mode 100755
index 0000000000..bf42ad2482
--- /dev/null
+++ b/challenge-097/LoneWolfiNTj/README
@@ -0,0 +1 @@
+Solution by Robbie Hatley (github user "LoneWolfiNTj"). \ No newline at end of file
diff --git a/challenge-097/LoneWolfiNTj/perl/ch-1.pl b/challenge-097/LoneWolfiNTj/perl/ch-1.pl
new file mode 100755
index 0000000000..3623f5e16e
--- /dev/null
+++ b/challenge-097/LoneWolfiNTj/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#! /usr/bin/perl
+# caesar-cipher.pl
+use v5.30;
+sub error;
+error and exit 666 if @ARGV != 1;
+my $N = shift @ARGV;
+error and exit 666 if $N !~ /^-?\d+$/;
+error and exit 666 if $N < -26 || $N > 26;
+my @upper = split //,'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+my @lower = split //,'abcdefghijklmnopqrstuvwxyz';
+my @strings = <>;
+say '';
+foreach my $S (@strings)
+{
+ foreach my $index (0..((length $S)-1))
+ {
+ my $ord = ord(substr($S,$index,1));
+ if ($ord >= 65 && $ord <= 90)
+ {substr($S,$index,1) = $upper[($ord-65-$N)%26];}
+ if ($ord >= 97 && $ord <= 122)
+ {substr($S,$index,1) = $lower[($ord-97-$N)%26];}
+ }
+ print $S;
+}
+exit 0;
+sub error
+{
+ print ((<<' END_OF_ERROR') =~ s/^ //gmr);
+ Input Error: \"caesar-cipher.pl\" requires exactly 1 command-line
+ argument, which must be a positive integer in the closed interval
+ [-26,26]. This will be used as a "rotate" value for performing a
+ "Caesar Cipher" on the input. Positive values will perform a left
+ rotate and Negative values will perform a right rotate.
+
+ The input should be a string (or a series of strings) containing
+ English letters (and perhaps some other characters). The letters
+ will be alphabetically "rotated" to different letters and the other
+ characters will be passed through unchanged.
+
+ The input must be fed to this program through stdin,
+ either from a file redirect:
+
+ $ caesar-cipher.pl 17 < myfile.txt
+
+ or via a pipe:
+
+ $ echo 'Sullenly, sadly, silently he walked home.' | caesar-cipher.pl 8
+
+ or via the keyboard:
+
+ $ caesar-cipher.pl 17
+ Seventeen times she smote her foe with her axe,[Enter]
+ but each time he withstood her savage blows.[Enter][Ctrl-D]
+
+ Bnenwcnnw crvnb bqn bvxcn qna oxn frcq qna jgn,
+ kdc njlq crvn qn frcqbcxxm qna bjejpn kuxfb.
+
+ END_OF_ERROR
+ return 1;
+} # end sub error
diff --git a/challenge-097/LoneWolfiNTj/perl/ch-2.pl b/challenge-097/LoneWolfiNTj/perl/ch-2.pl
new file mode 100755
index 0000000000..7707f4eb9b
--- /dev/null
+++ b/challenge-097/LoneWolfiNTj/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#! /usr/bin/perl
+# binary-substrings.pl
+use v5.30;
+sub error;
+error and exit 666 if @ARGV != 2;
+my $B = shift @ARGV;
+my $S = shift @ARGV;
+error and exit 666 if $B !~ /^[01]{2,}$/;
+error and exit 666 if $S !~ /^[1-9]\d*$/;
+my $L = length $B;
+error and exit 666 if 0 != $L % $S;
+say "\$B = $B \$S = $S";
+my @substrs;
+foreach my $group ( 0 .. $L/$S - 1 )
+{
+ push @substrs, substr($B, $S*$group, $S);
+}
+say "Consecutive $S-element substrings of $B:";
+say for @substrs;
+my @flips;
+foreach my $index ( 0 .. $L/$S - 1 )
+{
+ foreach my $group ( 0 .. $L/$S - 1 )
+ {
+ next if $group == $index;
+ foreach my $digit ( 0 .. $S - 1 )
+ {
+ ++$flips[$index] if substr($substrs[$group], $digit, 1)
+ != substr($substrs[$index], $digit, 1);
+ }
+ }
+}
+my $best_idx = 0;
+my $best_fls = 1987654321;
+foreach my $index ( 0 .. $L/$S - 1 )
+{
+ if ($flips[$index] < $best_fls)
+ {$best_fls = $flips[$index]; $best_idx = $index;}
+}
+
+say "Lowest number of flips to equalize substrings = $best_fls,";
+say "achieved by using substring index $best_idx ($substrs[$best_idx])";
+say "as a \"paragon\" and flipping all other substrings to match it.";
+
+exit 0;
+sub error
+{
+ my $NA = shift;
+ print ((<<' END_OF_ERROR') =~ s/^ //gmr);
+ Input Error. "binary-substrings.pl" requires exactly 2 arguments.
+ The first argument ("$B") must be a binary-number string at least 2 digits
+ long with a non-prime number of digits. The second argument ("$S") must be
+ a positive integer which is a factor of the length of the binary-number
+ string. This program will then print the consecutive $S-long substrings of
+ $B, and show the minimum number of digit flips to make all of the substrings
+ identical to the first.
+ END_OF_ERROR
+ return 1;
+} # end sub error_msg ($)