diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-28 11:17:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-28 11:17:31 +0000 |
| commit | 31159c7068e93b3df4b0a88696cd8b81f3091ac7 (patch) | |
| tree | a903a328f16230940354da52e9365603d525e56b | |
| parent | 67a4b97644aa1081d376f8073fba6c938016361d (diff) | |
| parent | 7b2bc56dcf7849d613cdd0f956bb776b79484877 (diff) | |
| download | perlweeklychallenge-club-31159c7068e93b3df4b0a88696cd8b81f3091ac7.tar.gz perlweeklychallenge-club-31159c7068e93b3df4b0a88696cd8b81f3091ac7.tar.bz2 perlweeklychallenge-club-31159c7068e93b3df4b0a88696cd8b81f3091ac7.zip | |
Merge pull request #9154 from pme/challenge-245
challenge-245
| -rwxr-xr-x | challenge-245/peter-meszaros/perl/ch-1.pl | 44 | ||||
| -rwxr-xr-x | challenge-245/peter-meszaros/perl/ch-2.pl | 67 |
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-245/peter-meszaros/perl/ch-1.pl b/challenge-245/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c112798e54 --- /dev/null +++ b/challenge-245/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# +# You are given two array of languages and its popularity. +# +# Write a script to sort the language based on popularity. +# Example 1 +# +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [ ['perl', 'c', 'python'], [2, 1, 3] ], + [ ['c++', 'haskell', 'java'], [1, 3, 2] ], +]; + +sub sort_language +{ + my $lang = shift; + my $popularity = shift; + + my %langpop = map { $lang->[$_] => $popularity->[$_] } (0..$#$lang); + return [ sort { $langpop{$a} <=> $langpop{$b}} keys %langpop ]; +} + +is_deeply(sort_language($cases->[0]->@*), ['c', 'perl', 'python'], "['perl', 'c', 'python'], [2, 1, 3]"); +is_deeply(sort_language($cases->[1]->@*), ['c++', 'java', 'haskell'], "['c++', 'haskell', 'java'], [1, 3, 2]"); +done_testing(); + +exit 0; + + diff --git a/challenge-245/peter-meszaros/perl/ch-2.pl b/challenge-245/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d010b0e2e0 --- /dev/null +++ b/challenge-245/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# +# 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. +# +# Example 1 +# +# Input: @ints = (8, 1, 9) +# Output: 981 +# +# 981 % 3 == 0 +# +# Example 2 +# +# Input: @ints = (8, 6, 7, 1, 0) +# Output: 8760 +# +# Example 3 +# +# Input: @ints = (1) +# Output: -1 +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use Algorithm::Combinatorics qw/variations/; + +my $cases = [ + [8, 1, 9], + [8, 6, 7, 1, 0], + [1], +]; + +sub largest_of_three +{ + my $l = shift; + + my $num = -1; + my @l; + for my $k (1..(scalar @$l)) { + my $iter = variations($l, $k); + while (my $p = $iter->next) { + push @l, 0+join('', @$p); + } + } + + for my $n (sort {$b <=> $a} @l) { + if ($n % 3 == 0) { + $num = $n; + last; + } + } + + return $num; +} + +is(largest_of_three($cases->[0]), 981, '[8, 1, 9]'); +is(largest_of_three($cases->[1]), 8760, '[8, 6, 7, 1, 0]'); +is(largest_of_three($cases->[2]), -1, '[1]'); +done_testing(); + +exit 0; |
