From ff461cc6959c56f5ea9d3c044c976121cecbb96f Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 29 Nov 2021 14:37:37 +0000 Subject: add solution week 140 task 2 in perl --- challenge-140/steven-wilson/perl/ch-2.pl | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-140/steven-wilson/perl/ch-2.pl diff --git a/challenge-140/steven-wilson/perl/ch-2.pl b/challenge-140/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..00bc221751 --- /dev/null +++ b/challenge-140/steven-wilson/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +# Week 140 Task 2 +# Multiplication Table + +use strict; +use warnings; +use feature qw/ say /; +use Lingua::EN::Numbers::Ordinate; + +my ( $i, $j, $k ) = @ARGV; +my @rows; + +for ( my $y = 1; $y <= $i; $y++ ) { + my @row; + for ( my $x = 1; $x <= $j; $x++ ) { + + push @row, ( $x * $y ); + } + push @rows, \@row; +} + +my @multiples; +map { push @multiples, @{$_} } @rows; +my @sorted_multiples = sort @multiples; + +say "Input: \$i = $i, \$j = $j, \$k = $k"; +say "Output: $sorted_multiples[$k-1]\n"; +say "Since the multiplication of $i x $j is as below:\n"; +map { say "\t" . join " ", @{$_} } @rows; +say "\nThe sorted multiplication table:\n"; +say "\t" . join " ", @sorted_multiples; +say "\nNow the " + . ordinate($k) + . " element in the table is \"$sorted_multiples[$k-1]\"."; + -- cgit