diff options
| -rw-r--r-- | challenge-217/solathian/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-217/solathian/perl/ch-2.pl | 31 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-217/solathian/perl/ch-1.pl b/challenge-217/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..71e37f9ccc --- /dev/null +++ b/challenge-217/solathian/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!usr/bin/perl +use v5.36; + +# Challenge 217 - 1 - Sorted Matrix + +# You are given a n x n matrix where n >= 2. +# Write a script to find 3rd smallest element in the sorted matrix. + +sortMatrix ([[3, 1, 2], [5, 2, 4], [0, 1, 3]]); # 1 +sortMatrix ([[2, 1], [4, 5]]); # 4 +sortMatrix ([[1, 0, 3], [0, 0, 0], [1, 2, 1]]); # 0 + +sub sortMatrix($matrixRef) +{ + my @array; + + foreach my $subArray (@$matrixRef) + { + foreach my $elem (@$subArray) + { + push(@array, $elem); + } + } + + @array = sort @array; + + say $array[2]; + +}
\ No newline at end of file diff --git a/challenge-217/solathian/perl/ch-2.pl b/challenge-217/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..b8421c3f97 --- /dev/null +++ b/challenge-217/solathian/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!usr/bin/perl +use v5.36; + +use Algorithm::Combinatorics qw(permutations); + +# Challenge 217 - 2 - Max Number + +# You are given a list of positive integers. +# Write a script to concatenate the integers to form the highest possible value. + +maxNum(1, 23); # 231 +maxNum(10, 3, 2); # 3210 +maxNum(31, 2, 4, 10); # 431210 +maxNum(5, 11, 4, 1, 2); # 542111 +maxNum(1, 10); # 110 + + +sub maxNum(@list) +{ + my $max = -1; + + foreach my $permutation ( permutations(\@list)) + { + my $number = join('', @$permutation); + + $max = $number if( $number > $max ); + } + + say $max; + +}
\ No newline at end of file |
