diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-11-22 17:53:20 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-11-22 17:53:20 +0000 |
| commit | db6df2a5ea0ad435c2d5952632494c4b1894ef81 (patch) | |
| tree | ef010923c1b82baefaefbc632b28fcc6c4551d13 | |
| parent | e57e8ba97ca974deeadbd7137390e99a38d8304d (diff) | |
| download | perlweeklychallenge-club-db6df2a5ea0ad435c2d5952632494c4b1894ef81.tar.gz perlweeklychallenge-club-db6df2a5ea0ad435c2d5952632494c4b1894ef81.tar.bz2 perlweeklychallenge-club-db6df2a5ea0ad435c2d5952632494c4b1894ef81.zip | |
added solution for #140
| -rw-r--r-- | challenge-140/james-smith/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-140/james-smith/perl/ch-2.pl | 30 |
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-140/james-smith/perl/ch-1.pl b/challenge-140/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..c4d6576644 --- /dev/null +++ b/challenge-140/james-smith/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ [ 11, 1 ] , 100 ], + [ [ 101, 1 ] , 110 ], + [ [ 100, 11 ] , 111 ], +); + +say DecBin->new($_->[0][0]) + DecBin->new($_->[0][1]) == DecBin->new($_->[1]) ? 'OK' : 'FAIL' foreach @TESTS; + +package DecBin; + +use overload ('+','bin_add','==','comp'); + +sub new { return bless \$_[1], $_[0]; } + +sub comp { ${$_[0]} == ${$_[1]}; } + +sub bin_add { + my($t,$c,$m,$a,$b) = (0,0,1,${$_[0]},${$_[1]}); + $c+=$a%10+$b%10,$t+=$m*($c&1),$m*=10,$c>>=1,$a=int$a/10,$b=int$b/10 while$a||$b||$c; + DecBin->new($t); +} + diff --git a/challenge-140/james-smith/perl/ch-2.pl b/challenge-140/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..8f8e2545bc --- /dev/null +++ b/challenge-140/james-smith/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ [2,3,1], 1 ], + [ [2,3,2], 2 ], + [ [2,3,3], 2 ], + [ [2,3,4], 3 ], + [ [2,3,5], 4 ], + [ [2,3,6], 6 ], + [ [3,3,6], 4 ], +); + +is( get_num(@{$_->[0]}), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub get_num { + my($i,$j,$k,%h) = @_; + $a=$_, map { $h{$a*$_}++ } 1..$j for 1..$i; + $k-=$h{$_}, ($k<1) && (return $_) for sort { $a<=>$b } keys %h; +} + |
