From fd8e32585a0b9d3abcc7c22c3f30e19354a039ef Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 7 Dec 2020 08:44:10 +0000 Subject: new ch1/2 --- challenge-090/james-smith/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-090/james-smith/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 challenge-090/james-smith/perl/ch-1.pl create mode 100644 challenge-090/james-smith/perl/ch-2.pl diff --git a/challenge-090/james-smith/perl/ch-1.pl b/challenge-090/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..0970dfd554 --- /dev/null +++ b/challenge-090/james-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +## Ah! The day job.... +## Both these we've always used `tr` for as the fastest way to compute the +## DNA count and to get reverse complement of sequence + +my $seq = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'; + +is( revcomp($seq), 'CGGCGACCAGCAACACATCTCTGAGAATGGATAAGGAGTCGATCTGTCTAAATGAAAAGGGGTTTAC' ); +is_deeply( counts($seq), { 'T' => 22, 'A' => 14, 'C' => 18, 'G' => 13 } ); + +done_testing(); + +sub counts { + return { 'T' => $_[0] =~ tr/T/T/, 'A' => $_[0] =~ tr/A/A/, + 'C' => $_[0] =~ tr/C/C/, 'G' => $_[0] =~ tr/G/G/, }; +} + +sub revcomp { + return reverse $_[0] =~ tr/ATCG/TAGC/r; +} + diff --git a/challenge-090/james-smith/perl/ch-2.pl b/challenge-090/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..618a88f999 --- /dev/null +++ b/challenge-090/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; + +## This is ripe for bit operators - as it is about multiplying/diving by 2... +## and checking for the value of the "1s" bit.... + +foreach(1..10) { + my $x = int rand(40); + my $y = int rand(40); + is( eth_mult( $x , $y ), $x*$y ); +} + +done_testing(); + +sub eth_mult { + my( $n, $m ) = @_; + my $res = 0; + while($n) { + $res += $m if $n&1; + $m<<=1; + $n>>=1; + } + return $res; +} + -- cgit From 3ba9a510b320260dda112c82a404c6b33c2958cd Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 7 Dec 2020 15:44:39 +0000 Subject: added verbose mode to show workings --- challenge-090/james-smith/perl/ch-2.pl | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/challenge-090/james-smith/perl/ch-2.pl b/challenge-090/james-smith/perl/ch-2.pl index 618a88f999..a8227b59f2 100644 --- a/challenge-090/james-smith/perl/ch-2.pl +++ b/challenge-090/james-smith/perl/ch-2.pl @@ -8,23 +8,38 @@ use Test::More; ## This is ripe for bit operators - as it is about multiplying/diving by 2... ## and checking for the value of the "1s" bit.... +# +# To execute as a quiet test - use `perl ch-2.pl` +# +# To show workings run `perl ch-2.pl 1` +my $flag = @ARGV && $ARGV[0] eq '1' ? 1 : 0; foreach(1..10) { - my $x = int rand(40); - my $y = int rand(40); - is( eth_mult( $x , $y ), $x*$y ); + my $x = 100 + int rand(400); + my $y = 100 + int rand(400); + is( eth_mult( $x , $y, $flag ), $x*$y ); } done_testing(); sub eth_mult { - my( $n, $m ) = @_; + my( $n, $m, $verbose ) = @_; + $verbose ||= 0; + my $calc = $n*$m; my $res = 0; + say q() if $verbose; while($n) { + printf "%7d x %7d | %7s\n", $n, $m, $n&1 ? $m : q(.) if $verbose; + # The meat - use binary and $res += $m if $n&1; $m<<=1; $n>>=1; } + if( $verbose ) { + say q(------------------+--------); + printf " | %7d (%7d)\n", $res, $calc; + say q(); + } return $res; } -- cgit