diff options
Diffstat (limited to 'challenge-195')
| -rw-r--r-- | challenge-195/pip/README | 1 | ||||
| -rwxr-xr-x | challenge-195/pip/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-195/pip/perl/ch-2.pl | 44 |
3 files changed, 68 insertions, 0 deletions
diff --git a/challenge-195/pip/README b/challenge-195/pip/README new file mode 100644 index 0000000000..95d2433a48 --- /dev/null +++ b/challenge-195/pip/README @@ -0,0 +1 @@ +Solutions by Pip Stuart. diff --git a/challenge-195/pip/perl/ch-1.pl b/challenge-195/pip/perl/ch-1.pl new file mode 100755 index 0000000000..22edb052b7 --- /dev/null +++ b/challenge-195/pip/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +# Perl-Weekly-Challenge Week-195 Task-1 +# Take $n && count all special ints up to it. +# Special integers have all unique digits. +# Example 1: +# In-put: $n = 15 +# Output: 14 as except 11 all other integers between 1 and 15 are spcial. +# Example 2: +# In-put: $n = 35 +# Output: 32 as except 11, 22, 33 all others are special. +use strict;use warnings;use utf8;use v5.10;my $d8VS='MCDL6MCD'; +sub Spcl {my $n = shift(@_) || 0;my $c=0;my $o='';my %s=();my $j; + for(1..$n){my $f=0;my @d = split(//, "$_");my %h=(); + for my $g (@d){if(exists($h{$g})){$s{$_}=1;$f=1;}$h{$g}=1;} + next if($f);$c++; + } $j=join(', ',sort {$a <=> $b} keys(%s)) if(keys(%s));$j=~ s/(.*) /$1 and / if(keys(%s)); + $o="$c";$o.=" as except $j " if(keys(%s));$o.='all ';$o.='other ' if(keys(%s));$o.="integers between 1 and $n are special.";say $o;} +if (@ARGV){ + for(@ARGV){Spcl($_);}} +else{ + Spcl( 15); + Spcl( 35); + Spcl(255);} diff --git a/challenge-195/pip/perl/ch-2.pl b/challenge-195/pip/perl/ch-2.pl new file mode 100755 index 0000000000..6c0f230504 --- /dev/null +++ b/challenge-195/pip/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl +# Perl-Weekly-Challenge Week-195 Task-2 +# Write script to find most frequent even numbers in list. In case you get more than one even number return smallest even integer. 4 all other case, return -1. +# Example 1: +# In-put: @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: +# In-put: @list = (1,3,5,7) +# Output: -1 since no even numbers found in the list +# Example 3: +# In-put: @list = (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. +use strict;use warnings;use utf8;use v5.10;my $d8VS='MCDL5pip'; +sub Most {my %freq=();my $outp='';my $mfen=0;my $maxv=-1;my %maxe=();my $jevz='';my $jmez='';for(@_){ + if($_ % 2 == 0){ + if(exists($freq{$_})){$freq{$_}++;} + else{ $freq{$_}=1;} } } + if(keys(%freq)){ + my @valz=sort {$a <=> $b} values(%freq); + for(@valz){$maxv=$_; + for my $fkey (keys(%freq)){ + if($freq{$fkey} == $_){ + $maxe{$fkey}=1 if($maxv > 0 && $maxv == $valz[-1]); } } } + if (scalar(keys(%maxe))){ $jevz=join(', ',sort {$a <=> $b} keys(%freq)); + if(scalar(keys(%freq)) <= 2){$jevz=join(' ' ,sort {$a <=> $b} keys(%freq));} + $jmez= join(', ',sort {$a <=> $b} keys(%maxe)); + if(scalar(keys(%maxe)) <= 2){$jmez=join(' ' ,sort {$a <=> $b} keys(%maxe));} + $jevz=~ s/(.*) /$1 and /; + $jmez=~ s/(.*) /$1 and /;} + if(scalar(keys(%maxe)) == 1){$mfen=(keys(%maxe))[0]; + }else{ $mfen=(sort {$a <=> $b} keys(%maxe))[0];} + $outp=sprintf("$mfen since there are only %d even numbers: $jevz",scalar(keys(%freq))); + if (scalar(keys(%maxe)) == 1){$outp.=" and of those $mfen appears the most.";} + else{$outp.=". $jmez appear an equal most number of times, so pick the smallest.";}} + else{ $outp='-1 since no even numbers found in the list.';} + say $outp;} +if(@ARGV){ + Most(@ARGV);} +else{ + Most(1,1,2,6,2); + Most(1,3,5,7 ); + Most(6,4,4,6,1); + Most(2,4,4,4,6,6,6,8,8); + Most(2,4,4,6,6,6,8,8,8);} |
