aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-13 12:01:10 +0000
committerGitHub <noreply@github.com>2022-12-13 12:01:10 +0000
commitd81b8f6375ec080d0470673af78c343872868dc5 (patch)
tree8d8c64fe8062d1d2b78dd0631f458f7ce1c3255c
parentbc2c850b4b3ffd7d6d2f10f2622e51e439a3f6e6 (diff)
parent2eb11689346f8bead46931a658ccd30bdd52c921 (diff)
downloadperlweeklychallenge-club-d81b8f6375ec080d0470673af78c343872868dc5.tar.gz
perlweeklychallenge-club-d81b8f6375ec080d0470673af78c343872868dc5.tar.bz2
perlweeklychallenge-club-d81b8f6375ec080d0470673af78c343872868dc5.zip
Merge pull request #7246 from jacoby/master
jacoby finishes Ch 195
-rw-r--r--challenge-195/dave-jacoby/perl/ch-1.pl26
-rw-r--r--challenge-195/dave-jacoby/perl/ch-2.pl29
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-195/dave-jacoby/perl/ch-1.pl b/challenge-195/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..189613873e
--- /dev/null
+++ b/challenge-195/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+my @examples = ( 15, 35 );
+for my $n (@examples) {
+ my $o = special_list($n);
+ say <<"END";
+ Input: \$n = $n
+ Output: $o
+END
+}
+
+sub special_list ( $n ) {
+ return scalar grep { is_special($_) } 1 .. $n;
+}
+
+sub is_special ( $n ) {
+ my %hash;
+ for my $i ( split //, $n ) {
+ return 0 if ++$hash{$i} > 1;
+ }
+ return 1;
+}
diff --git a/challenge-195/dave-jacoby/perl/ch-2.pl b/challenge-195/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..9a704a2c63
--- /dev/null
+++ b/challenge-195/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ min max };
+
+my @examples = ( [ 1, 1, 2, 6, 2 ], [ 1, 3, 5, 7 ], [ 6, 4, 4, 6, 1 ] );
+
+for my $e (@examples) {
+ my $list = join ',', $e->@*;
+ my $o = most_frequent_even( $e->@* );
+ say <<"END";
+ Input: \@list = $list
+ Output: $o
+END
+}
+
+sub most_frequent_even( @list ) {
+ my %hash;
+ map { $hash{$_}++ } grep { 0 == $_ % 2 } @list;
+ if ( scalar keys %hash ) {
+ my $max = max values %hash;
+ return min grep { $hash{$_} == $max } keys %hash;
+ }
+ return -1;
+}
+