diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-28 22:37:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-28 22:37:20 +0000 |
| commit | bd0282fbbc67aebf824663b2d09b3f5b45e6dd9b (patch) | |
| tree | de61ac943b34a1a004087df1303222639b260f74 | |
| parent | fb0fe52f0a11011dc5baaee81034a888a044b811 (diff) | |
| parent | 4db9aacffd4ef48b60a7242f73b3f5da79133a7e (diff) | |
| download | perlweeklychallenge-club-bd0282fbbc67aebf824663b2d09b3f5b45e6dd9b.tar.gz perlweeklychallenge-club-bd0282fbbc67aebf824663b2d09b3f5b45e6dd9b.tar.bz2 perlweeklychallenge-club-bd0282fbbc67aebf824663b2d09b3f5b45e6dd9b.zip | |
Merge pull request #5297 from PerlBoy1967/branch-for-challenge-140
Branch for challenge 140
| -rw-r--r--[-rwxr-xr-x] | challenge-139/perlboy1967/perl/ch-2.pl | 5 | ||||
| -rwxr-xr-x | challenge-140/perlboy1967/perl/ch-1.pl | 88 | ||||
| -rwxr-xr-x | challenge-140/perlboy1967/perl/ch-2.pl | 25 |
3 files changed, 115 insertions, 3 deletions
diff --git a/challenge-139/perlboy1967/perl/ch-2.pl b/challenge-139/perlboy1967/perl/ch-2.pl index d065fe75e3..9bfa980f52 100755..100644 --- a/challenge-139/perlboy1967/perl/ch-2.pl +++ b/challenge-139/perlboy1967/perl/ch-2.pl @@ -58,7 +58,7 @@ sub repeatedDivDigits { $rSeen{$n}[1] -= length($1); } - return substr($result, + return substr($result, $rSeen{$n}[0], $rSeen{$n}[1] - $rSeen{$n}[0]); } @@ -67,10 +67,9 @@ my @primes; my $i = 1; while (scalar(@primes) < 5) { - push(@primes,$i) + push(@primes,$i) if (is_prime($i) and length(repeatedDivDigits($i)) == $i-1); $i++ } printf "%s\n",join("\n",@primes); - diff --git a/challenge-140/perlboy1967/perl/ch-1.pl b/challenge-140/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..b3e8358af1 --- /dev/null +++ b/challenge-140/perlboy1967/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 139 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-139/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Add Binary +Submitted by: Mohammad S Anwar + +You are given two decimal-coded binary numbers, $a and $b. + +Write a script to simulate the addition of the given binary numbers. + + || The script should simulate something like $a + $b. (operator overloading) + +=cut + +use v5.16; +use strict; +use warnings; + +my $b1 = new myBin(0b001); +my $b2 = new myBin(0b101); +my $b3 = new myBin(0b011); + +printf "b1: %s, b2: %s, b3: %s\n", $b1, $b2, $b3; + +my $A = new myBin($b3); +printf "A: %s\n", $A; + +my $B = new myBin($b1 + 7); +printf "B: %s (%s + 7)\n", $B, $b1; +$B += $b2; +printf "B: %s (%s + 7 + %s)\n", $B, $b1, $b2; +$B += $b3; +printf "B: %s (%s + %s + %s)\n", $B, $b1, $b2, $b3; +$B = $b1 + $b3; +printf "B: %s (%s + %s)\n", $B, $b1, $b3; +$B = 9 + $b1; +printf "B: %s (%s + %s)\n", $B, 9, $b1; + + +package myBin; + +use List::MoreUtils qw(pairwise); + +use overload + '""' => sub { '0b'.join '',reverse @{$_[0]} }, + '+' => \&_plus; + +sub new { + my ($self,$arg) = @_; + + # Accept a myBin object to initialize + # or an integer number + my @data = (); + if (defined $arg) { + if (ref $arg) { + @data = @$arg; + } else { + @data = reverse split //,sprintf '%b', $arg; + } + } + + bless \@data,$self; +} + +sub _plus { + my ($self, $other) = @_; + + $other = new myBin($other) if (!ref $other); + + my ($carry,$res) = (0); + my @result = pairwise { + no warnings 'once'; + $res = ($a//0) + ($b//0) + $carry; + $carry = ($res >> 1); + $res &= 1; + } @$self, @$other; + push(@result,1) if $carry; + + bless \@result; +} + +1; diff --git a/challenge-140/perlboy1967/perl/ch-2.pl b/challenge-140/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..8ce285cc84 --- /dev/null +++ b/challenge-140/perlboy1967/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 140 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Multiplication Table +Submitted by: Mohammad S Anwar + +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. + +=cut + +use v5.16; +use strict; +use warnings; + +use List::MoreUtils qw(arrayify); +my($i,$j,$k)=@ARGV; +printf"%s\n",(sort{$a<=>$b}arrayify map{my$n=$_;$_=[map{$n*$_}1..$j]}1..$i)[$k-1]; |
