From 008eecbf476622043107f7439a26f1c2e08a6b09 Mon Sep 17 00:00:00 2001 From: Solathian Date: Sun, 18 Dec 2022 12:16:27 +0100 Subject: Adding files challenge 195 --- challenge-195/solathian/perl/ch-1.pl | 30 +++++++++++++++++++++ challenge-195/solathian/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 challenge-195/solathian/perl/ch-1.pl create mode 100644 challenge-195/solathian/perl/ch-2.pl diff --git a/challenge-195/solathian/perl/ch-1.pl b/challenge-195/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..4a6157e5f5 --- /dev/null +++ b/challenge-195/solathian/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; # signatures, smartmatch + +# Challange 195 - 1 - Special Integers + +# 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. + +specialInt(15); # Output: 14 as except 11 all other integers between 1 and 15 are spcial. +specialInt(35); # Output: 32 as except 11, 22, 33 all others are special. + + +sub specialInt($number) +{ + my $count; + + for(my $i = 1; $i <= $number; $i++) + { + next if( join('', sort( split('', $i))) =~ /00|11|22|33|44|55|66|77|88|99/); + + $count++; + } + + say $count; +} \ No newline at end of file diff --git a/challenge-195/solathian/perl/ch-2.pl b/challenge-195/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..0204782542 --- /dev/null +++ b/challenge-195/solathian/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use List::Util 'max'; + +use feature 'signatures'; +no warnings 'experimental'; # signatures, smartmatch + +# Challange 195 - 2 - Most Frequent Even + +# 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. + + +# mostFreqEven((6,4,4,6,1)); +# Output: 4 since there are only two even numbers 4 and 6. They both appears the equal number of times, so pick the smallest. + +# mostFreqEven((1,3,5,7)); +# Output: -1 since no even numbers found in the list + +# mostFreqEven((1,1,2,6,2)); +# Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears the most. + + + +sub mostFreqEven(@list) +{ + + my @even = grep { $_ % 2 == 0 } @list; + if( @even == 0) + { + say -1; + return; + } + + my $string = join('', @even); + my @array = ($string =~ tr/0/0/, $string =~ tr/2/2/, $string =~ tr/4/4/, $string =~ tr/6/6/, $string =~ tr/8/8/); + + + for(my $i = 0; $i < @array; $i++) + { + if($array[$i] == max(@array)) + { + say $i*2; + last; + } + } + +} \ No newline at end of file -- cgit