From 5e03fe60134769d22eb9c118ea88f737593b2c04 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 7 Dec 2020 16:25:26 +0100 Subject: Solution to task 1 --- challenge-090/jo-37/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 challenge-090/jo-37/perl/ch-1.pl 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; -- cgit From d044d074f9325d9b8cf0f9d324a23297761b7258 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 7 Dec 2020 17:30:45 +0100 Subject: Solution to task 2 --- challenge-090/jo-37/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 challenge-090/jo-37/perl/ch-2.pl 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; -- cgit