From b8230b5d16e0d0e44c47dee28a9e00a33f1b8d25 Mon Sep 17 00:00:00 2001 From: dasJake Date: Sat, 27 Nov 2021 17:31:05 +0100 Subject: 140 add ch-2.pl --- challenge-140/jake/perl/ch-2.pl | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 challenge-140/jake/perl/ch-2.pl diff --git a/challenge-140/jake/perl/ch-2.pl b/challenge-140/jake/perl/ch-2.pl new file mode 100644 index 0000000000..830111b0c6 --- /dev/null +++ b/challenge-140/jake/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/r/bin/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 = ; +print "width: "; +my $width = ; +print "element to display: "; +my $element = ; +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 ) = @_; + + # access lists from references + my @horizontal_values = @{$horizontal_values // []}; + my @all_values = @{$all_values // []}; + + # once all lines of the table have been written we can return @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 -- cgit From 73f2a2d0f35b4150d3fb5d226fa22b9cfd0af5cb Mon Sep 17 00:00:00 2001 From: dasJake Date: Sat, 27 Nov 2021 17:38:05 +0100 Subject: 140 add ch-2.pl with alternative dereferencing --- challenge-140/jake/perl/ch-2.pl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/challenge-140/jake/perl/ch-2.pl b/challenge-140/jake/perl/ch-2.pl index 830111b0c6..901d2f89e8 100644 --- a/challenge-140/jake/perl/ch-2.pl +++ b/challenge-140/jake/perl/ch-2.pl @@ -36,22 +36,19 @@ sub aggregate_multiplication_table { sub _aggregate_multiplication_table { my ( $vertical_range, $horizontal_range, $height_increment, $horizontal_values, $all_values ) = @_; - # access lists from references - my @horizontal_values = @{$horizontal_values // []}; - my @all_values = @{$all_values // []}; - # once all lines of the table have been written we can return @all_values - return \@all_values if $height_increment > $vertical_range; + #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; + @$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 ); + return _aggregate_multiplication_table ( $vertical_range, $horizontal_range, $height_increment + 1, \@$horizontal_values, \@$all_values ); } \ No newline at end of file -- cgit From aeaf448a0e64c62e58d56402cd515afff09c2529 Mon Sep 17 00:00:00 2001 From: dasJake Date: Sat, 27 Nov 2021 18:35:11 +0100 Subject: 140 remove file bin2dec.pl --- challenge-140/jake/perl/bin2dec.pl | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 challenge-140/jake/perl/bin2dec.pl diff --git a/challenge-140/jake/perl/bin2dec.pl b/challenge-140/jake/perl/bin2dec.pl deleted file mode 100644 index 6514d88e7d..0000000000 --- a/challenge-140/jake/perl/bin2dec.pl +++ /dev/null @@ -1,35 +0,0 @@ -#!/r/bin/perl -use strict; -use warnings; - -# get input -print "binary number: "; -my $binary_input = ; -chomp $binary_input; - -my $a_decimal = binary_to_decimal ( $binary_input ); -print "$a_decimal\n"; - -sub binary_to_decimal { - my ( $binary ) = @_; - - return _binary_to_decimal ( $binary, 0, 0 ); -} - -sub _binary_to_decimal { - my ( $binary_number, $power, $acc, $tail ) = @_; - return $acc if $binary_number eq ''; - - $tail = chop ($binary_number); - - if ( $tail == 1 ) { - $acc += 2**$power; - $power++; - return _binary_to_decimal ( $binary_number, $power, $acc ); - } - - if ( $tail == 0 ) { - $power++; - return _binary_to_decimal ( $binary_number, $power, $acc ); - } -} \ No newline at end of file -- cgit From e4248125d7854e509f97e8c71b8397cc10f6e79e Mon Sep 17 00:00:00 2001 From: dasJake Date: Sat, 27 Nov 2021 18:36:17 +0100 Subject: fix shebang --- challenge-140/jake/perl/ch-1.pl | 2 +- challenge-140/jake/perl/ch-2.pl | 2 +- 2 files changed, 2 insertions(+), 2 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 index 901d2f89e8..70829b1603 100644 --- a/challenge-140/jake/perl/ch-2.pl +++ b/challenge-140/jake/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/r/bin/perl +#!/usr/bin/env perl use strict; use warnings; -- cgit