aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-27 17:37:50 +0000
committerGitHub <noreply@github.com>2021-11-27 17:37:50 +0000
commitad8b4e642f9747d694e01e47b45fb107a65fc506 (patch)
treecc762170c66e59956a6953c92296497d5793497e
parent6b37925eeb4cf7c6cc22c1ab72e00d257c1eebb2 (diff)
parent350f79e0e32e3cf4fe60ca54378e9fe4e3feeab2 (diff)
downloadperlweeklychallenge-club-ad8b4e642f9747d694e01e47b45fb107a65fc506.tar.gz
perlweeklychallenge-club-ad8b4e642f9747d694e01e47b45fb107a65fc506.tar.bz2
perlweeklychallenge-club-ad8b4e642f9747d694e01e47b45fb107a65fc506.zip
Merge pull request #5287 from ccntrq/challenge-140
Solutions for Challenge 140
-rw-r--r--challenge-140/alexander-pankoff/perl/ch-1.pl89
-rw-r--r--challenge-140/alexander-pankoff/perl/ch-2.pl86
2 files changed, 175 insertions, 0 deletions
diff --git a/challenge-140/alexander-pankoff/perl/ch-1.pl b/challenge-140/alexander-pankoff/perl/ch-1.pl
new file mode 100644
index 0000000000..139791fb6b
--- /dev/null
+++ b/challenge-140/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,89 @@
+use strict;
+use warnings;
+use feature qw'say signatures';
+no warnings 'experimental::signatures';
+
+use Carp;
+use List::Util;
+
+run() unless caller();
+
+sub run() {
+ my $a = prompt_for_binary('a');
+ my $b = prompt_for_binary('b');
+
+ say "Result:";
+ say Binary->new($a) + $b;
+}
+
+sub prompt_for_binary($name) {
+ say "Enter binary number $name";
+ chomp( my $number = <STDIN> );
+
+ if ( $number !~ m/^[01]+$/ ) {
+ say "Invalid binary number.";
+ return prompt_for_binary($name);
+ }
+
+ return $number;
+}
+
+package Binary {
+ use overload '+' => 'add';
+ use overload '""' => sub { ${ shift() } };
+
+ sub new ( $class, $number ) {
+ if ( $number !~ m/^[01]+$/ ) {
+ Carp::confess("Invalid binary number: $number");
+ }
+
+ return bless \$number, $class;
+ }
+
+ sub add {
+ my ( $self, $other ) = @_;
+ if ( ref $other ) {
+ if ( UNIVERSAL::isa( $other, ref $self ) ) {
+ return Binary->new(
+ BinaryUtil::add_binarys( ${$self}, ${other} ) );
+ }
+ else {
+ Carp::confess( "Can't add a ", ref $other, " to a ",
+ ref $self );
+ }
+ }
+ else {
+ $self->add( Binary->new($other) );
+ }
+ }
+}
+
+package BinaryUtil {
+
+ sub add_binarys ( $a, $b ) {
+
+ my @a_digits = reverse split( '', $a );
+ my @b_digits = reverse split( '', $b );
+
+ # we walk the digits of the binary numbers starting with the least
+ # significant digit, add the digits individually, carry the carry around
+ # with us and build our result from the back
+ my ( $out, $carry ) = ( '', 0 );
+ for ( 0 .. List::Util::max( $#a_digits, $#b_digits ) ) {
+ my $res;
+ ( $res, $carry ) = add_binary_digits( $a_digits[$_] // 0,
+ $b_digits[$_] // 0, $carry );
+ $out = $res . $out;
+ }
+
+ return $carry ? $carry . $out : $out;
+ }
+
+ sub add_binary_digits ( $a, $b, $carry ) {
+ my $c = $a + $b + $carry;
+
+ return $c >= 3 ? ( 1, 1 ) : $c == 2 ? ( 0, 1 ) : ( $c, 0 );
+
+ }
+
+}
diff --git a/challenge-140/alexander-pankoff/perl/ch-2.pl b/challenge-140/alexander-pankoff/perl/ch-2.pl
new file mode 100644
index 0000000000..5b663dae91
--- /dev/null
+++ b/challenge-140/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,86 @@
+use strict;
+use warnings;
+use feature qw'say signatures';
+no warnings 'experimental::signatures';
+
+use constant DEBUG => $ENV{DEBUG} // 0;
+
+run() unless caller();
+
+sub run() {
+ my $i = prompt_for_integer('i');
+ my $j = prompt_for_integer('j');
+ my $k = prompt_for_integer('k');
+
+ my $max = $i * $j;
+
+ if ( $k > $max ) {
+ die "Index 'k' ($k) is larger than the table. Max: $max\n";
+ }
+
+ my $matrix = multiplication_matrix( $i, $j );
+ my @sorted_matrix = sort_multiplication_matrix($matrix);
+ my $res = $sorted_matrix[ $k - 1 ];
+
+ if (DEBUG) {
+ say "Since the multiplication of $i x $j is as below:";
+
+ say render_matrix($matrix);
+ say "The sorted multiplication table:";
+ say join( ' ', @sorted_matrix );
+
+ say "Now the " . to_ordinal($k) . " element in the table is '$res'";
+
+ }
+
+ say $res;
+
+}
+
+sub multiplication_matrix ( $i, $j ) {
+ [
+ map {
+ my $row = $_;
+ [
+ map {
+ my $col = $_;
+ $col * $row;
+
+ } ( 1 .. $j )
+ ];
+ } ( 1 .. $i )
+ ];
+}
+
+sub sort_multiplication_matrix($matrix) {
+ sort { $a <=> $b } map { @$_ } @$matrix;
+}
+
+sub render_matrix($matrix) {
+ my $max = $matrix->[-1][-1];
+ my $width = length $max;
+ my $format_str = "%$width" . 'd';
+
+ join(
+ "\n",
+ map {
+ join( " ", map { sprintf( $format_str, $_ ) } @$_ )
+ } @$matrix
+ );
+}
+
+sub to_ordinal($n) {
+ return ( $n == 1 ) ? "1st" : $n == 2 ? "2nd" : $n == 3 ? "3rd" : $n . 'th';
+}
+
+sub prompt_for_integer($name) {
+ say "Enter integer number $name greater or equal to 1.";
+ chomp( my $number = <STDIN> );
+
+ if ( $number !~ m/^\d+$/ || $number < 1 ) {
+ say "Invalid integer.";
+ return prompt_for_integer($name);
+ }
+
+ return $number;
+}