Sorted them both

Weekly challenge 217 — 15 May 2023

Week 217 - 15 May 2023

Task 1

You are given a n x n matrix where n >= 2. Write a script to find 3rd smallest element in the sorted matrix.

All that is needed here is to concatenate the rows, sort them numerically, and the answer is the third element in the list.

#!/usr/bin/perl use v5.16; # The Weekly Challenge - 2023-05-15 use utf8; # Week 217 task 1 - Sorted matrix use strict; # Peter Campbell Smith use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge sorted_matrix([3, 1, 2], [5, 2, 4], [0, 1, 3]); sorted_matrix([2, 1], [4, 5]); sorted_matrix([1, 0, 3], [0, 0, 0], [1, 2, 1]); sorted_matrix([1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [9, 8, 7, 6, 5], [0, 2, 4, 6, 8], [9, 7, 5, 3, 1]); sub sorted_matrix { my (@list, @rows, $j); # concatenate rows and then sort the list push @list, @$_ for @_; @list = sort { $a <=> $b } @list; # show results $rows[$j ++] = '[' . join(', ', @$_) . ']' for @_; say qq[\nInput: \@matrix = (] . join(', ', @rows) . ')'; say qq[Output: $list[2]]; }

Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) Output: 1 Input: @matrix = ([2, 1], [4, 5]) Output: 4 Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) Output: 0 Input: @matrix = ([1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [9, 8, 7, 6, 5], [0, 2, 4, 6, 8], [9, 7, 5, 3, 1]) Output: 1

The content of this website which has been created by

Peter Campbell Smith is hereby placed in the public domain

Peter Campbell Smith is hereby placed in the public domain