aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-27 10:59:50 +0000
committerGitHub <noreply@github.com>2023-12-27 10:59:50 +0000
commitdf882a75e7d8032373ef08d3c6967a31745e057c (patch)
tree455da5aa53ce480e52aa7a5c4c4507aea49b9743
parentb0516e5c2a1aeb8cb6aeb4811ebd6b365d71849f (diff)
parent292ffcd983dd4232331dd60dad3e98eff6a4de7e (diff)
downloadperlweeklychallenge-club-df882a75e7d8032373ef08d3c6967a31745e057c.tar.gz
perlweeklychallenge-club-df882a75e7d8032373ef08d3c6967a31745e057c.tar.bz2
perlweeklychallenge-club-df882a75e7d8032373ef08d3c6967a31745e057c.zip
Merge pull request #9301 from steve-g-lynn/branch-for-challenge-249
pwc 249
-rw-r--r--challenge-249/steve-g-lynn/blog.txt1
-rwxr-xr-xchallenge-249/steve-g-lynn/perl/ch-1.pl44
-rwxr-xr-xchallenge-249/steve-g-lynn/perl/ch-2.pl32
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-249/steve-g-lynn/blog.txt b/challenge-249/steve-g-lynn/blog.txt
new file mode 100644
index 0000000000..393fdab0ae
--- /dev/null
+++ b/challenge-249/steve-g-lynn/blog.txt
@@ -0,0 +1 @@
+https://thiujiac.blogspot.com/2023/12/pwc-249.html
diff --git a/challenge-249/steve-g-lynn/perl/ch-1.pl b/challenge-249/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..b15d537721
--- /dev/null
+++ b/challenge-249/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env -S perl -wl
+
+use Data::Dumper qw(Dumper);
+
+local *equal_pairs = sub {
+ #-- return () if input is of odd length
+ ((scalar(@_) % 2) > 0) && (return ());
+
+ #-- count number of occurrences of each element using a hash
+ local %_ = ();
+ map {$_{$_}++} @_;
+
+ #-- if any element occurs an odd number of times, return ()
+ (grep {($_{$_} % 2) > 0} keys %_) && (return ());
+
+ #-- loop thru %_ keys, returning pairs of such keys
+ map {
+ my $k=$_;
+ my $num_pairs=( ($_{$k}) / 2 );
+ map {
+ [$k, $k]
+ } 1 .. $num_pairs;
+ }
+ keys %_;
+};
+
+print Dumper &equal_pairs(3,2,3,2,2,2);
+#$VAR1 = [
+# '3',
+# '3'
+# ];
+#$VAR2 = [
+# '2',
+# '2'
+# ];
+#$VAR3 = [
+# '2',
+# '2'
+# ];
+
+print Dumper &equal_pairs(1,2,3,4);
+# no printed output (empty array)
+
+1;
diff --git a/challenge-249/steve-g-lynn/perl/ch-2.pl b/challenge-249/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..b81273d592
--- /dev/null
+++ b/challenge-249/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env -S perl -wl
+
+local *DI_string_match = sub {
+ my $str = $_[0];
+
+ #-- return 0 (error) if input is not D or I only
+ ($str =~ /^[DI]+$/) || (return 0);
+
+ #-- create mutable temp vars containing highest and lowest indices
+ my ($lowest,$highest)=(0, length($str));
+
+ #-- loop through $str indices,
+ # if str[i] is 'D', assign highest in output[i] and decrement it
+ # if str[i] is 'I', assign lowest in output[i] and increment it
+
+ my @retval = (); #-- return value
+ map {
+ (substr($str,$_,1) eq 'D') ?
+ ($retval[$_] = $highest--) :
+ ($retval[$_] = $lowest++);
+ }
+ 0 .. length($str)-1;
+ ($lowest==$highest) || die "Something wrong ...$!";
+ $retval[length($str)] = $highest; #-- last item left
+ @retval;
+};
+
+print join ',', &DI_string_match('IDID'); #0,4,1,3,2
+print join ',', &DI_string_match('III'); #0,1,2,3
+print join ',', &DI_string_match('DDI'); #3,2,0,1
+
+1;