aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorboblied <boblied@gmail.com>2023-01-05 21:39:40 -0600
committerboblied <boblied@gmail.com>2023-01-05 21:39:40 -0600
commit72e4a33dc194d2ce0a086699767bf35719277278 (patch)
tree4effe5d711ab1618b7243ad88c394a5fd71e2a23
parenta3955b58c74d643c9cbff8fe2b43e4befb1f32ba (diff)
downloadperlweeklychallenge-club-72e4a33dc194d2ce0a086699767bf35719277278.tar.gz
perlweeklychallenge-club-72e4a33dc194d2ce0a086699767bf35719277278.tar.bz2
perlweeklychallenge-club-72e4a33dc194d2ce0a086699767bf35719277278.zip
Week 195
-rw-r--r--challenge-195/bob-lied/README4
-rw-r--r--challenge-195/bob-lied/perl/ch-1.pl62
-rw-r--r--challenge-195/bob-lied/perl/ch-2.pl63
3 files changed, 127 insertions, 2 deletions
diff --git a/challenge-195/bob-lied/README b/challenge-195/bob-lied/README
index 08e4900b33..a19e6c3a12 100644
--- a/challenge-195/bob-lied/README
+++ b/challenge-195/bob-lied/README
@@ -1,3 +1,3 @@
-Solutions to weekly challenge 194 by Bob Lied
+Solutions to weekly challenge 195 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-194/
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-195/
diff --git a/challenge-195/bob-lied/perl/ch-1.pl b/challenge-195/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..9b523bf9b3
--- /dev/null
+++ b/challenge-195/bob-lied/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge Week 195, Task 1 Special Integers
+#=============================================================================
+# Copyright (c) 2023, Bob Lied
+#=============================================================================
+# You are given a positive integer, $n > 0.
+# Write a script to print the count of all special integers between 1 and $n.
+# An integer is special when all of its digits are unique.
+# Example 1: Input: $n = 15 Output: 14
+# because except 11 all other integers between 1 and 15 are spcial.
+# Example 2: Input: $n = 35 Output: 32
+# because except 11, 22, 33 all others are special.
+#
+#=============================================================================
+
+use v5.36;
+
+use Scalar::Util::Numeric qw/isint/;
+use List::Util qw/any sum/;
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+say ( isint($_) && countSpecial($_) ) for @ARGV;
+
+sub isSpecial($n)
+{
+ my @digitCount = (0) x 10;
+ $digitCount[$_]++ for split('', $n);
+ # We want a 0, not undef or '' for false
+ return ( List::Util::any { $_ > 1 } @digitCount ) ? 0 : 1;
+}
+
+sub countSpecial($n)
+{
+ return List::Util::sum map { isSpecial($_) } 1..$n;
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( isSpecial( 0), 1, "isSpecial 0");
+ is( isSpecial( 1), 1, "isSpecial 1");
+ is( isSpecial( 9), 1, "isSpecial 9");
+ is( isSpecial(22), 0, "isSpecial 22");
+ is( isSpecial(12), 1, "isSpecial 12");
+ is( isSpecial(1234567890), 1, "isSpecial 1-0");
+ is( isSpecial(9878675675), 0, "isSpecial lots");
+
+ is( countSpecial(15), 14, "Example 1");
+ is( countSpecial(35), 32, "Example 2");
+
+ done_testing;
+}
+
diff --git a/challenge-195/bob-lied/perl/ch-2.pl b/challenge-195/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..54b7ce6f74
--- /dev/null
+++ b/challenge-195/bob-lied/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge, Week 195, Task 2 Most Frequent Even
+#=============================================================================
+# Copyright (c) 2023, Bob Lied
+#=============================================================================
+# You are given a list of numbers, @list.
+# Write a script to find most frequent even numbers in the list. In case you
+# get more than one even number, then return the smallest even integer.
+# For all other case, return -1.
+# Example 1 Input: @list = (1,1,2,6,2) Output: 2
+# as there are only 2 even numbers 2 and 6 and of those 2 appears the most.
+# Example 2 Input: @list = (1,3,5,7) Output: -1
+# since no even numbers found in the list
+# Example 3 Input: @list = (6,4,4,6,1) Output: 4
+# since there are only two even numbers 4 and 6. They both appear an equal
+# number of times, so pick the smallest.
+#=============================================================================
+
+use v5.36;
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+# Accept any kind of list separated by spaces or punctuation.
+# Assume all the numbers are integers.
+(my $list = "@ARGV") =~ s/[^0-9]/ /g;
+
+say mostFrequentEven([ split(' ', $list) ]);
+
+sub mostFrequentEven($list)
+{
+ my %frequency;
+ $frequency{$_}++ for grep { $_ % 2 == 0 } $list->@*;
+
+ return -1 if ! keys %frequency;
+
+ # Sort descending by frequency, then ascending by value.
+ # Take the first element of the resulting list.
+ return (sort { $frequency{$b} <=> $frequency{$a} || $a <=> $b } keys %frequency)[0];
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( mostFrequentEven( [1,1,2,6,2] ), 2, "Example 1");
+ is( mostFrequentEven( [1,3,5,7, ] ), -1, "Example 2");
+ is( mostFrequentEven( [6,4,4,6,1] ), 4, "Example 3");
+ is( mostFrequentEven( [] ), -1, "Empty");
+ is( mostFrequentEven( [200] ), 200, "Singleton");
+ is( mostFrequentEven( [24,24,24,24] ), 24, "Just one");
+ is( mostFrequentEven( [1,20,6,20,1,6,4,4] ), 4, "Threeway");
+ is( mostFrequentEven( [1..100, 50, 51] ), 50, "Bigger list");
+
+ done_testing;
+}
+