diff options
| author | pme <hauptadler@gmail.com> | 2023-11-27 21:10:15 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2023-11-27 21:10:15 +0100 |
| commit | 7b2bc56dcf7849d613cdd0f956bb776b79484877 (patch) | |
| tree | b8578e14a10363d9972fa4b8f75b68da14e3a420 /challenge-245 | |
| parent | a91d49494a545d745c5c622afa3a9646bf1ac774 (diff) | |
| download | perlweeklychallenge-club-7b2bc56dcf7849d613cdd0f956bb776b79484877.tar.gz perlweeklychallenge-club-7b2bc56dcf7849d613cdd0f956bb776b79484877.tar.bz2 perlweeklychallenge-club-7b2bc56dcf7849d613cdd0f956bb776b79484877.zip | |
challenge-245
Diffstat (limited to '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; |
