diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-27 17:44:59 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-27 17:44:59 +0000 |
| commit | a26901a0e64ad66e2a89ee87d51c86677ee2ab72 (patch) | |
| tree | f3d7f78c16244af7cedd868728617b47d3ab07d8 /challenge-140 | |
| parent | f9abf59346b9b3dff234ff56c8c15d80e5bc578b (diff) | |
| parent | e4248125d7854e509f97e8c71b8397cc10f6e79e (diff) | |
| download | perlweeklychallenge-club-a26901a0e64ad66e2a89ee87d51c86677ee2ab72.tar.gz perlweeklychallenge-club-a26901a0e64ad66e2a89ee87d51c86677ee2ab72.tar.bz2 perlweeklychallenge-club-a26901a0e64ad66e2a89ee87d51c86677ee2ab72.zip | |
Merge pull request #5288 from dasJake/140_ch-2_references
140 ch-2.pl
Diffstat (limited to 'challenge-140')
| -rw-r--r-- | challenge-140/jake/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-140/jake/perl/ch-2.pl | 54 |
2 files changed, 55 insertions, 1 deletions
diff --git a/challenge-140/jake/perl/ch-1.pl b/challenge-140/jake/perl/ch-1.pl index 7e199720aa..c9887edc35 100644 --- a/challenge-140/jake/perl/ch-1.pl +++ b/challenge-140/jake/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/r/bin/perl +#!/usr/bin/env perl use strict; use warnings; diff --git a/challenge-140/jake/perl/ch-2.pl b/challenge-140/jake/perl/ch-2.pl new file mode 100644 index 0000000000..70829b1603 --- /dev/null +++ b/challenge-140/jake/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +### +# You are given 3 positive integers, $i, $j and $k. +# +# Write a script to print the $kth element in the sorted multiplication table of $i and $j. +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 +### + +# get input +print "height: "; +my $height = <STDIN>; +print "width: "; +my $width = <STDIN>; +print "element to display: "; +my $element = <STDIN>; +chomp ( $height, $width, $element ); + +# sort multiplication table +my @sorted = sort { $a <=> $b } @{aggregate_multiplication_table ( $height, $width )}; + +# output desired element +say ( $sorted[$element - 1] ); + +# container sub so main needs to pass only 2 arguments +sub aggregate_multiplication_table { + my ( $height, $width ) = @_; + _aggregate_multiplication_table ( $height, $width, 1 ); +} + +sub _aggregate_multiplication_table { + my ( $vertical_range, $horizontal_range, $height_increment, $horizontal_values, $all_values ) = @_; + + # once all lines of the table have been written we can return @all_values + #push @$all_values,; + return \@$all_values if $height_increment > $vertical_range; + + # we 'write' the first line of the multiplication table. + # after that we run over each value in this first adding each new value to the accumulator @all_values. + # we will repeat this for every line that needs to be added to the table. + @$horizontal_values = 1 ... $horizontal_range; + foreach ( @$horizontal_values ) { + push @$all_values, $_ * $height_increment; + } + + # we need to increase our line counter upon iteration. + # this is necessary for our foreach loop to add the next line in the following iteration. + return _aggregate_multiplication_table ( $vertical_range, $horizontal_range, $height_increment + 1, \@$horizontal_values, \@$all_values ); +}
\ No newline at end of file |
