aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-090/wlmb/perl/ch-1.pl64
-rwxr-xr-xchallenge-090/wlmb/perl/ch-2.pl40
2 files changed, 104 insertions, 0 deletions
diff --git a/challenge-090/wlmb/perl/ch-1.pl b/challenge-090/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..00e54fa1e0
--- /dev/null
+++ b/challenge-090/wlmb/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+# For sequences of DNA get nucleotide counts and its complement
+
+use strict;
+use warnings;
+use v5.10;
+
+use List::Util qw(sum0);
+
+# Receive sequences as strings in @ARGV
+say('Usage: ch-1.pl sequence1 sequence2 ...'), exit 1 unless @ARGV;
+
+my %complement_of; # Map dna nucleotide to its complement
+@complement_of{my @nucleotides=qw(T A G C)}=qw(A T C G); #initialize
+
+foreach my $sequence(map {uc} @ARGV){
+ say("Wrong sequence $sequence"), next unless $sequence=~/^[@nucleotides]*$/;
+ say " Sequence: $sequence";
+ say "Complement: ", complement($sequence);
+ my %count_for=get_count_for($sequence);
+ say "Nucleotide counts:";
+ say "\t$_-$complement_of{$_} $count_for{$_}" for @nucleotides;
+ say "\tTotal\t", sum0 values %count_for;
+}
+
+sub complement { # converts string with a DNA sequence to its complement
+ my $sequence=shift;
+ return join "", map {$complement_of{$_}} split //, $sequence;
+}
+
+sub get_count_for { # count nucleotides of a dna sequence
+ my $sequence=shift;
+ my @nucleotides=split //,$sequence;
+ my %count_for; # counts of nucleotides
+ @count_for{split //, "TAGC"}=((0) x 4); #initialize with 0's
+ map {$count_for{$_}++} split //, $sequence;
+ return %count_for;
+}
+
+# Sample run:
+#
+# $ ./ch-1.pl hithere \
+# GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG \
+# TGCTGGTCGCCG
+#
+# yields:
+#
+# Wrong sequence HITHERE
+# Sequence: GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG
+# Complement: CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC
+# Nucleotide counts:
+# T-A 22
+# A-T 14
+# G-C 13
+# C-G 18
+# Total 67
+# Sequence: TGCTGGTCGCCG
+# Complement: ACGACCAGCGGC
+# Nucleotide counts:
+# T-A 3
+# A-T 0
+# G-C 5
+# C-G 4
+# Total 12
diff --git a/challenge-090/wlmb/perl/ch-2.pl b/challenge-090/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..6be0c0de39
--- /dev/null
+++ b/challenge-090/wlmb/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+# Multiply two numbers using the Ethiopian Multiplication
+
+use strict;
+use warnings;
+use v5.10;
+use integer;
+use List::Util qw(all);
+
+# receive the two numbers in @ARGV
+die 'Usage: ./ch2.pl number1 number2' unless @ARGV==2;
+my ($x, $y)=@ARGV;
+die 'Expected two non-negative integers' # check signs
+ unless all {int($_)==$_ && $_>=0} ($x, $y);
+my $expected_result=$x*$y;
+my $result=0;
+my $result_string="$x x $y = ";
+my $operator="";
+while($x){
+ if($x&1){ # $x is odd, add $y to result
+ print "->"; # flag line
+ $result += $y;
+ $result_string .= "$operator 1 x $y";
+ } else { # $x is even, don't add y
+ $result_string.="$operator 0 x $y";
+ }
+ say "\t$x\t$y";
+ $operator=" + ";
+ $x>>=1; # Divde $x by 2
+ $y<<=1; # Multiply $y by 2
+}
+say " $result_string = $result (Expected: $expected_result)";
+
+# Sample run:
+# $ ./ch-2.pl 11 23
+# -> 11 23
+# -> 5 46
+# 2 92
+# -> 1 184
+# 11 x 23 = 1 x 23 + 1 x 46 + 0 x 92 + 1 x 184 = 253 (Expected: 253)