diff options
| -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; |
