From d44f0879d277582380b3aef003dcd65fe1bd6e6a Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Tue, 8 Dec 2020 22:37:13 -0600 Subject: Add solutions to challenge 90 --- challenge-090/wlmb/perl/ch-1.pl | 64 +++++++++++++++++++++++++++++++++++++++++ challenge-090/wlmb/perl/ch-2.pl | 40 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100755 challenge-090/wlmb/perl/ch-1.pl create mode 100755 challenge-090/wlmb/perl/ch-2.pl 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) -- cgit From a2734b4cbec67d4ebd50ce18256820999927e9b1 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Wed, 9 Dec 2020 11:40:28 -0600 Subject: Regenerate programs from org tangle --- challenge-090/wlmb/perl/ch-1.pl | 31 ++----------------------------- challenge-090/wlmb/perl/ch-2.pl | 14 ++++---------- 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) -- cgit From d25727270333aa9c090a967d0c46d467393cf908 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Wed, 9 Dec 2020 12:27:59 -0600 Subject: Test for integer --- challenge-090/wlmb/perl/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-090/wlmb/perl/ch-2.pl b/challenge-090/wlmb/perl/ch-2.pl index 781624d2d9..44f9f1ab56 100755 --- a/challenge-090/wlmb/perl/ch-2.pl +++ b/challenge-090/wlmb/perl/ch-2.pl @@ -9,9 +9,9 @@ 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 ($x, $y)= @ARGV; # +die 'Expected two non-negative integers' # check they are postitive integers + unless all {int($_) eq $_ && $_>=0} ($x, $y); #Use eq to avoid implicit int conversion my $expected_result=$x*$y; my $result=0; -- cgit From b656bc813129c410b0ccd81449a55222a4f8dcc6 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Wed, 9 Dec 2020 12:30:21 -0600 Subject: Add blog entry --- challenge-090/wlmb/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-090/wlmb/blog.txt diff --git a/challenge-090/wlmb/blog.txt b/challenge-090/wlmb/blog.txt new file mode 100644 index 0000000000..c05dda5cfa --- /dev/null +++ b/challenge-090/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2020/12/07/PWC90/ -- cgit