diff options
| -rwxr-xr-x | challenge-245/perlboy1967/perl/ch1.pl | 39 | ||||
| -rwxr-xr-x | challenge-245/perlboy1967/perl/ch2.pl | 49 |
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-245/perlboy1967/perl/ch1.pl b/challenge-245/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..93878d5564 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Sort Language +Submitted by: Mohammad S Anwar + +You are given two array of languages and its popularity. + +Write a script to sort the language based on popularity. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use List::MoreUtils qw(pairwise); + +sub sortLanguage (\@\@) { + map { $_->[0] } + sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } + pairwise { [$a,$b] } @{$_[0]},@{$_[1]}; +} + +is([sortLanguage(@{[qw{Perl C Python}]},@{[2,1,3]})], + [qw(C Perl Python)]); +is([sortLanguage(@{[qw{C++ Haskell Java}]},@{[1,3,2]})], + [qw(C++ Java Haskell)]); +is([sortLanguage(@{[qw{C Basic Assembly}]},@{[1,1,1]})], + [qw(Assembly Basic C)]); + +done_testing; diff --git a/challenge-245/perlboy1967/perl/ch2.pl b/challenge-245/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..8c18d7fde4 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch2.pl @@ -0,0 +1,49 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Largest of Three +Submitted by: Mohammad S Anwar + +You are given an array of integers >= 0. + +Write a script to return the largest number formed by concatenating some of the given +integers in any order which is also multiple of 3. Return -1 if none found. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(permutations subsets); + +sub largestOfThree (@) { + my @l = sort { $b <=> $a } @_; + + my $subsetSize = $#l+1; + while ($subsetSize > 0) { + for my $subset (subsets(\@l,$subsetSize)) { + my @subset = sort { $b <=> $a } @$subset; + for my $p (permutations(\@subset)) { + my $s = join('',@$p); + return $s if ($s % 3 == 0); + } + $subsetSize--; + } + } + return -1; +} + + +is(largestOfThree(8,1,9),981); +is(largestOfThree(8,6,7,1,0),8760); +is(largestOfThree(1),-1); + +done_testing; |
