diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-17 15:52:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-17 15:52:43 +0100 |
| commit | 3d365fa57841a84aa7735bb17ba0ee80401204ec (patch) | |
| tree | 81752cf8ea074fd4cb4128d80581ea3b3a0f6223 | |
| parent | fc65009cc7d7296de38d652727600a2691e26636 (diff) | |
| parent | 840b91fce1f0572ad04fab3747b698f38318b3cd (diff) | |
| download | perlweeklychallenge-club-3d365fa57841a84aa7735bb17ba0ee80401204ec.tar.gz perlweeklychallenge-club-3d365fa57841a84aa7735bb17ba0ee80401204ec.tar.bz2 perlweeklychallenge-club-3d365fa57841a84aa7735bb17ba0ee80401204ec.zip | |
Merge pull request #8093 from pme/challenge-217
challenge-217
| -rwxr-xr-x | challenge-217/peter-meszaros/perl/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-217/peter-meszaros/perl/ch-2.pl | 71 |
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-217/peter-meszaros/perl/ch-1.pl b/challenge-217/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..5dfdf7bf20 --- /dev/null +++ b/challenge-217/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# You are given a n x n matrix where n >= 2. +# +# Write a script to find 3rd smallest element in the sorted matrix. +# Example 1 +# +# Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) +# Output: 1 +# +# The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. +# The 3rd smallest of the sorted list is 1. +# +# Example 2 +# +# Input: @matrix = ([2, 1], [4, 5]) +# Output: 4 +# +# The sorted list of the given matrix: 1, 2, 4, 5. +# The 3rd smallest of the sorted list is 4. +# +# Example 3 +# +# Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) +# Output: 0 +# +# The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. +# The 3rd smallest of the sorted list is 0. + +use strict; +use warnings; +use Test::More; + +my $cases = [ + [[3, 1, 2], [5, 2, 4], [0, 1, 3]], + [[2, 1], [4, 5]], + [[1, 0, 3], [0, 0, 0], [1, 2, 1]], +]; + +sub sorted_matrix +{ + my $m = shift; + my $n = shift; + + my @l; + push @l, @$_ for @$m; + @l = sort {$a <=> $b} @l; + + return $l[$n-1]; +} + +is(sorted_matrix($cases->[0], 3), 1, '[[3, 1, 2], [5, 2, 4], [0, 1, 3]]'); +is(sorted_matrix($cases->[1], 3), 4, '[[2, 1], [4, 5]]'); +is(sorted_matrix($cases->[2], 3), 0, '[[1, 0, 3], [0, 0, 0], [1, 2, 1]]'); +done_testing(); + +exit 0; diff --git a/challenge-217/peter-meszaros/perl/ch-2.pl b/challenge-217/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..b26a8bfd8f --- /dev/null +++ b/challenge-217/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +# You are given a list of positive integers. +# +# Write a script to concatenate the integers to form the highest possible value. +# Example 1: +# +# Input: @list = (1, 23) +# Output: 231 +# +# Example 2: +# +# Input: @list = (10, 3, 2) +# Output: 3210 +# +# Example 3: +# +# Input: @list = (31, 2, 4, 10) +# Output: 431210 +# +# Example 4: +# +# Input: @list = (5, 11, 4, 1, 2) +# Output: 542111 +# +# Example 5: +# +# Input: @list = (1, 10) +# Output: 110 + + +use strict; +use warnings; +use Test::More; +use Algorithm::Combinatorics qw/permutations/; + +my $cases = [ + [1, 23], # 0 + [10, 3, 2], # 1 + [31, 2, 4, 10], # 2 + [5, 11, 4, 1, 2], # 3 + [1, 10], # 4 + [4, 41, 4], # 5 + [4, 48, 6], # 6 + [64, 84], # 7 +]; + +sub max_number +{ + my $l = shift; + + my $iter = permutations($l); + my $r = 0; + while (my $c = $iter->next) { + my $v = join('', @$c); + $r = $v if $v > $r; + } + + return $r; +} + +is(max_number($cases->[0]), 231, '[1, 23]'); +is(max_number($cases->[1]), 3210, '[10, 3, 2]'); +is(max_number($cases->[2]), 431210, '[31, 2, 4, 10]'); +is(max_number($cases->[3]), 542111, '[5, 11, 4, 1, 2]'); +is(max_number($cases->[4]), 110, '[1, 10]'); +is(max_number($cases->[5]), 4441, '[4, 41, 4]'); +is(max_number($cases->[6]), 6484, '[4, 48, 6]'); +is(max_number($cases->[7]), 8464, '[64, 84]'); +done_testing(); + +exit 0; |
