diff options
| -rw-r--r-- | challenge-195/dave-jacoby/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-195/dave-jacoby/perl/ch-2.pl | 29 |
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; +} + |
