diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-18 07:51:40 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-18 07:51:40 +0000 |
| commit | 7827e33b735b35062a0c49704ec0893801a1c12f (patch) | |
| tree | 0424386592e8f5071d9415d7e7febea6f304e09b /challenge-195 | |
| parent | 01f5c76eadca487d157a8fff2c19a0918a07e3d1 (diff) | |
| parent | 2ea928d077a0a82a4661774dfc3968158031ed70 (diff) | |
| download | perlweeklychallenge-club-7827e33b735b35062a0c49704ec0893801a1c12f.tar.gz perlweeklychallenge-club-7827e33b735b35062a0c49704ec0893801a1c12f.tar.bz2 perlweeklychallenge-club-7827e33b735b35062a0c49704ec0893801a1c12f.zip | |
Merge pull request #7265 from PerlBoy1967/branch-for-challenge-195
w195 - Task 1 & 2
Diffstat (limited to 'challenge-195')
| -rwxr-xr-x | challenge-195/perlboy1967/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-195/perlboy1967/perl/ch-2.pl | 46 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-195/perlboy1967/perl/ch-1.pl b/challenge-195/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..42ed52b0d9 --- /dev/null +++ b/challenge-195/perlboy1967/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 195 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Special Integers +Submitted by: Mohammad S Anwar + +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. + +=cut + +use v5.16; +use common::sense; + +use Test::More; + + +sub numSpecialInt ($) { + return $_[0] if ($_[0] < 11); + + my $r = $_[0]; + + for (map {$_ x length($_[0])} 1 .. 9) { + $r-- if ($_ <= $_[0]); + last if ($_ >= $_[0]); + } + + return $r; +} + + +for ( + [1,1], [5,5], [9,9], + [10,10], [11,10], [12,11], + [110,110], [111,110], [112,111], + [332,330], [333,330], [334,331], + [4443,4440], [4444,4440], [4445,4441], +) { + is(numSpecialInt($$_[0]), $$_[1], "Test $$_[0]"); +} + +done_testing; diff --git a/challenge-195/perlboy1967/perl/ch-2.pl b/challenge-195/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..5ef31600eb --- /dev/null +++ b/challenge-195/perlboy1967/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 195 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Most Frequent Even +Submitted by: Mohammad S Anwar + +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 numbers then return the smallest even integer. For +all other case, return -1. + +=cut + +use v5.16; +use common::sense; + +use List::Util qw(min max); +use List::MoreUtils qw(frequency); + +use Test::More; + + +sub mostFreqEven { + my %f = frequency grep { $_ % 2 == 0} @_; + my $max = max(0,values %f); + return -1 if ($max == 0); + return min(grep{$f{$_} == $max}keys %f); +} + + +for ( + [[1,1,2,6,2],2], + [[1,3,5,7],-1], + [[6,4,4,6,1],4] +) { + is(mostFreqEven(@{$_->[0]}),$_->[1]); +} + +done_testing; |
