aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-01 11:00:30 +0100
committerGitHub <noreply@github.com>2023-05-01 11:00:30 +0100
commit6fc3f1f176125aa62d44c418bf9da889d51c30e4 (patch)
tree9d3bef95cbc9ba6738045da346f9df483049a6ab
parent776700e5636d27e7b711a260e482621f5776e21f (diff)
parent0371e1546e272ffdf9c4db83ab2fe8bdf1d39943 (diff)
downloadperlweeklychallenge-club-6fc3f1f176125aa62d44c418bf9da889d51c30e4.tar.gz
perlweeklychallenge-club-6fc3f1f176125aa62d44c418bf9da889d51c30e4.tar.bz2
perlweeklychallenge-club-6fc3f1f176125aa62d44c418bf9da889d51c30e4.zip
Merge pull request #7998 from steve-g-lynn/branch-for-challenge-215
challenge 215
-rwxr-xr-xchallenge-215/steve-g-lynn/perl/ch-1.pl31
-rwxr-xr-xchallenge-215/steve-g-lynn/perl/ch-2.pl46
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-215/steve-g-lynn/perl/ch-1.pl b/challenge-215/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..3a8107d6d1
--- /dev/null
+++ b/challenge-215/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env -S perl -wl
+
+#-- this script conforms to perl 4 syntax as specified
+#-- in the 1st edition of Programming perl (pink camel)
+#
+
+local *odd_one_out = sub {
+ local (@words)=@_;
+
+ local *is_sorted = sub { #not idiomatic but I prefer readable
+ local ($string)=@_;
+ local $chk=join('',sort {$a cmp $b} split(//,$string));
+ return ($string eq $chk);
+ };
+
+ local ($ctr, $word, @retval); $ctr=0;
+ for $word (@words) {
+ if (&is_sorted($word)) {
+ push @retval, $word;
+ }
+ else {
+ $ctr++;
+ }
+ }
+ print $ctr;
+ return @retval;
+};
+
+print &odd_one_out('abc','xyz','tsu');
+print &odd_one_out('rat','cab','dad');
+print &odd_one_out('x','y','z');
diff --git a/challenge-215/steve-g-lynn/perl/ch-2.pl b/challenge-215/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..d3961b9789
--- /dev/null
+++ b/challenge-215/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env -S perl -wl
+
+#this script conforms to perl 4 syntax
+#as specified in Programming perl 1st edition (the pink camel)
+
+local *number_placement=sub {
+ local ($count, @numbers)=@_;
+
+ #-- helper sub-subs
+ local *is_conformant = sub { #-- all inputs 0 or 1 ?
+ local (@numbers) = @_;
+ local $number;
+
+ for $number (@numbers) {
+ ($number =~ /^0$/ || $number =~ /^1$/) || die "Non-conformant input";
+ }
+ };
+
+ local *has_1_neighbor = sub {
+ local ($indx)=@_;
+
+ ( ($indx > 0) && ($numbers[$indx-1] =~ /^1$/) )
+ ||
+ ( ($indx < @numbers-1) && ($numbers[$indx+1] =~ /^1$/ ));
+ };
+
+ #-- back to the main part of the subroutine
+ &is_conformant( @numbers );
+
+ local ($indx,$ctr);
+ $ctr=0;
+
+ for $indx (0 .. @numbers-1) {
+ ( ($numbers[$indx] =~ /^0$/) && (!(&has_1_neighbor($indx))) )
+ && ($ctr++);
+
+ last if ($ctr >= $count);
+ }
+
+ ($ctr >= $count) ? 1 : 0;
+};
+
+print &number_placement(1,1,0,0,0,1);
+print &number_placement(2,1,0,0,0,1);
+print &number_placement(3,1,0,0,0,0,0,0,0,1);
+