From f0c370d55e316544dc388f1548e947cca01ae538 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 17 Oct 2021 12:54:29 +0200 Subject: Task 2 Challenge 134 LK Perl --- challenge-134/lubos-kolouch/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-134/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-134/lubos-kolouch/perl/ch-2.pl b/challenge-134/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..7e697a8118 --- /dev/null +++ b/challenge-134/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,50 @@ +use strict; +use warnings; +use feature qw/say/; + +sub print_table_get_items { + my ( $m, $n ) = @_; + + # TODO : proper formatting, counting of digits etc. + # TODO : split to two functions, one function printing AND returning the items is ugly + + my %items; + + print "x | "; + print "$_ " for ( 1 .. $m ); + say ""; + say "---------"; + + for my $i ( 1 .. $n ) { + print "$i | "; + + for my $j ( 1 .. $m ) { + print $i * $j . " "; + $items{ $i * $j } = 1; + } + say ""; + } + + return %items; +} + +sub print_results_get_count { + my $what = shift; + + # TODO : split to two functions, one function printing AND returning count is ugly + + print "Distinct counts: "; + for my $key ( sort { $a <=> $b } keys %$what ) { + print "$key "; + } + say ""; + + return scalar keys %$what; +} + +my %result = print_table_get_items( 3, 3 ); +my $elem_count = print_results_get_count( \%result ); + +use Test::More; +is( $elem_count, 6 ); +done_testing; -- cgit