aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2020-12-09 11:40:28 -0600
committerLuis Mochan <mochan@fis.unam.mx>2020-12-09 11:40:28 -0600
commita2734b4cbec67d4ebd50ce18256820999927e9b1 (patch)
treeb1a3bf808036b976e1be8b595edab69b915d2b9d
parentd44f0879d277582380b3aef003dcd65fe1bd6e6a (diff)
downloadperlweeklychallenge-club-a2734b4cbec67d4ebd50ce18256820999927e9b1.tar.gz
perlweeklychallenge-club-a2734b4cbec67d4ebd50ce18256820999927e9b1.tar.bz2
perlweeklychallenge-club-a2734b4cbec67d4ebd50ce18256820999927e9b1.zip
Regenerate programs from org tangle
-rwxr-xr-xchallenge-090/wlmb/perl/ch-1.pl31
-rwxr-xr-xchallenge-090/wlmb/perl/ch-2.pl14
2 files changed, 6 insertions, 39 deletions
diff --git a/challenge-090/wlmb/perl/ch-1.pl b/challenge-090/wlmb/perl/ch-1.pl
index 00e54fa1e0..9e5f5c07c4 100755
--- a/challenge-090/wlmb/perl/ch-1.pl
+++ b/challenge-090/wlmb/perl/ch-1.pl
@@ -1,13 +1,12 @@
#!/usr/bin/env perl
# For sequences of DNA get nucleotide counts and its complement
-
+# See https:/wlmb.github.io/2020/12/07/PWC90/#task-1-dna-sequence
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
@@ -32,33 +31,7 @@ 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
+ @count_for{qw(T A G C)}=((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
index 6be0c0de39..781624d2d9 100755
--- a/challenge-090/wlmb/perl/ch-2.pl
+++ b/challenge-090/wlmb/perl/ch-2.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
# Multiply two numbers using the Ethiopian Multiplication
-
+# See https:/wlmb.github.io/2020/12/07/PWC90/#task-2-ethiopian-multiplication
use strict;
use warnings;
use v5.10;
@@ -12,10 +12,12 @@ 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
@@ -24,17 +26,9 @@ while($x){
} else { # $x is even, don't add y
$result_string.="$operator 0 x $y";
}
- say "\t$x\t$y";
+ say "\tx=$x\ty=$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)