aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-19 05:13:52 +0000
committerGitHub <noreply@github.com>2022-12-19 05:13:52 +0000
commit97d19dfbcf8722157dbaec6018ce8032ab65fa4c (patch)
tree0205076e532ed26c280e914bea4bf24f05249c37
parent1efe84e743e7c1e49083b5c69ae274541672dade (diff)
parent2caf5faf5e275600ff87b92059606f16c9a5acff (diff)
downloadperlweeklychallenge-club-97d19dfbcf8722157dbaec6018ce8032ab65fa4c.tar.gz
perlweeklychallenge-club-97d19dfbcf8722157dbaec6018ce8032ab65fa4c.tar.bz2
perlweeklychallenge-club-97d19dfbcf8722157dbaec6018ce8032ab65fa4c.zip
Merge pull request #7275 from choroba/ech195
Solve 195: Special Integers & Most Frequent Even by E. Choroba
-rwxr-xr-xchallenge-195/e-choroba/perl/ch-1.pl38
-rwxr-xr-xchallenge-195/e-choroba/perl/ch-2.pl21
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-195/e-choroba/perl/ch-1.pl b/challenge-195/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..6f885703bd
--- /dev/null
+++ b/challenge-195/e-choroba/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+use List::Util qw{ product };
+
+sub special_integers ($n) {
+ my $count = _special_integers_shorter(length($n) - 1);
+ my $from = substr '1023456789', 0, length $n;
+ for ($from .. $n) {
+ ++$count unless /(.).*\1/
+ }
+ return $count
+}
+
+sub _special_integers_shorter ($l) {
+ my $count = 0;
+ for my $size (1 .. $l) {
+ $size = 10 if $size > 10;
+ my @digits = (9, map 9 - $_, 0 .. $size - 2);
+ $count += product(@digits);
+ }
+ return $count
+}
+
+use Test::More tests => 6;
+
+is special_integers(15), 14, 'Example 1';
+is special_integers(35), 32, 'Example 2';
+
+is special_integers(987), 738, 'length 3';
+is special_integers(9999), 5274, 'length 4';
+is special_integers(99999), 32490, 'length 5';
+is special_integers(999999), 168570, 'length 6';
+
+# Too slow:
+# is special_integers(999999999), 5611770, 'length 9';
diff --git a/challenge-195/e-choroba/perl/ch-2.pl b/challenge-195/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..1efce0e6aa
--- /dev/null
+++ b/challenge-195/e-choroba/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub most_frequent_even ($list) {
+ my @even = grep 0 == $_ % 2, @$list;
+ my %freq;
+ ++$freq{$_} for @even;
+ return (
+ sort { $freq{$b} <=> $freq{$a} }
+ sort { $a <=> $b }
+ keys %freq
+ )[0] // -1
+}
+
+use Test::More tests => 3;
+
+is most_frequent_even([1,1,2,6,2]), 2, 'Example 1';
+is most_frequent_even([1,3,5,7]), -1, 'Example 2';
+is most_frequent_even([6,4,4,6,1]), 4, 'Example 3';