diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-13 01:05:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-13 01:05:47 +0000 |
| commit | 95dbf9f346367ba43af47a9f2d6d0ff15c843450 (patch) | |
| tree | fa4e5c4b5e7550792c1ccd5bb55f8e51221f42b2 | |
| parent | 931e28a9fe63ad0942cf9f3099191a0e21a978c2 (diff) | |
| parent | e4219ed612d520261ee355db836b721d12fd35db (diff) | |
| download | perlweeklychallenge-club-95dbf9f346367ba43af47a9f2d6d0ff15c843450.tar.gz perlweeklychallenge-club-95dbf9f346367ba43af47a9f2d6d0ff15c843450.tar.bz2 perlweeklychallenge-club-95dbf9f346367ba43af47a9f2d6d0ff15c843450.zip | |
Merge pull request #2959 from jo-37/contrib
Solutions to challenge 090
| -rwxr-xr-x | challenge-090/jo-37/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-090/jo-37/perl/ch-2.pl | 28 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-090/jo-37/perl/ch-1.pl b/challenge-090/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..d19e66e524 --- /dev/null +++ b/challenge-090/jo-37/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +use Test2::V0; + +# Count nucleotides and generate complement +sub complement_dna { + local $_ = shift; + + # Create the complement (providing the count) and return this as + # the final return value. + (y/TAGC/ATCG/, $_); +} + + +is [complement_dna( + 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG')], + [67, 'CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC'], + 'count and complement'; + +is complement_dna( + 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG'), + 'CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC', + 'complement only'; + + +done_testing; diff --git a/challenge-090/jo-37/perl/ch-2.pl b/challenge-090/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..e1d3865f81 --- /dev/null +++ b/challenge-090/jo-37/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use integer; +use Test2::V0; + +# Implement Ethiopian multiplication +sub ethiopian_mult { + my ($p, $q) = splice @_, 0, 2; + # The method is more effective if the first number is the smaller + # one. + ($p, $q) = ($q, $p) if $p > $q; + + my $prod = 0; + while ($p) { + # Add second number if the first is odd + $prod += ($p % 2) * $q; + # Get next pair. + ($p, $q) = ($p / 2, $q * 2); + } + + $prod; +} + +is ethiopian_mult(14, 12), 168, 'Example 1'; +is ethiopian_mult(2**32, 0), 0, 'Multiply by zero'; +is ethiopian_mult(2**32, 1), 2**32, 'Multiply by one'; + +done_testing; |
