From 0fcfce2743161c670cbacf6969702e8f1794d4fc Mon Sep 17 00:00:00 2001 From: Aut0exec <32361472+aut0exec@users.noreply.github.com> Date: Wed, 17 May 2023 23:14:12 -0500 Subject: Challenge 217 Commit --- challenge-217/aut0exec/perl/challenge_1.pl | 38 ++++++++++++++++++++++++++++++ challenge-217/aut0exec/perl/challenge_2.pl | 24 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 challenge-217/aut0exec/perl/challenge_1.pl create mode 100644 challenge-217/aut0exec/perl/challenge_2.pl diff --git a/challenge-217/aut0exec/perl/challenge_1.pl b/challenge-217/aut0exec/perl/challenge_1.pl new file mode 100644 index 0000000000..119f1b345e --- /dev/null +++ b/challenge-217/aut0exec/perl/challenge_1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +# +# Author: Aut0exec +# +# 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. +# +######################################################################## + +use strict; +use warnings; + +sub flatten { + my @matrix = ([3, 1, 2], [5, 2, 4], [0, 6, 3], [3, 4, 9]); + my @flattened; + my $matrix_len=@matrix; + + for(my $row = 0; $row < $matrix_len; $row++) { + for(my $col = 0; $col < 3; $col++) { + push @flattened, "$matrix[$row][$col]"; + } + } + + return (sort { $a <=> $b } @flattened); +} + +my @sorted = flatten(); + +print ("The sorted list of the given matrix: @sorted\n"); +print ("The 3rd smallest of the sorted list is $sorted[2]\n"); + + diff --git a/challenge-217/aut0exec/perl/challenge_2.pl b/challenge-217/aut0exec/perl/challenge_2.pl new file mode 100644 index 0000000000..3d9d0c8040 --- /dev/null +++ b/challenge-217/aut0exec/perl/challenge_2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# +# Author: Aut0exec +# +# 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 +# +######################################################################## + +use strict; +use warnings; + +my @list = (11, 2, 34, 65, 15, 3, 0); +my @sorted = sort {$a <= $b} (sort { $a <=> $b} @list); +my $list_len = @sorted; + +print("Output: "); +for(my $i = 0; $i < $list_len; $i++) { + print("$sorted[$i]"); +} +print("\n"); -- cgit