aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordcw <d.white@imperial.ac.uk>2020-12-13 20:59:21 +0000
committerdcw <d.white@imperial.ac.uk>2020-12-13 20:59:21 +0000
commit6f64a5ae8428f270d2a4f86330f3b89edf3a35ef (patch)
tree83472056a6f0bd0b0b72bf1bc5a4754710ed5919
parent931e28a9fe63ad0942cf9f3099191a0e21a978c2 (diff)
downloadperlweeklychallenge-club-6f64a5ae8428f270d2a4f86330f3b89edf3a35ef.tar.gz
perlweeklychallenge-club-6f64a5ae8428f270d2a4f86330f3b89edf3a35ef.tar.bz2
perlweeklychallenge-club-6f64a5ae8428f270d2a4f86330f3b89edf3a35ef.zip
imported my solutions to this week's tasks
-rw-r--r--challenge-090/duncan-c-white/README54
-rwxr-xr-xchallenge-090/duncan-c-white/perl/ch-1.pl53
-rwxr-xr-xchallenge-090/duncan-c-white/perl/ch-2.pl50
3 files changed, 128 insertions, 29 deletions
diff --git a/challenge-090/duncan-c-white/README b/challenge-090/duncan-c-white/README
index a5b283e113..91b90f56ac 100644
--- a/challenge-090/duncan-c-white/README
+++ b/challenge-090/duncan-c-white/README
@@ -1,44 +1,40 @@
-Task 1: "GCD Sum
+Task 1: "DNA Sequence
-You are given a positive integer $N.
-Write a script to sum GCD of all possible unique pairs between 1 and $N.
+DNA is a long, chainlike molecule which has two strands twisted into
+a double helix. The two strands are made up of simpler molecules
+called nucleotides. Each nucleotide is composed of one of the four
+nitrogen-containing nucleobases cytosine (C), guanine (G), adenine (A)
+and thymine (T).
-Example 1:
+You are given DNA sequence, GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG.
- Input: 3
- Output: 3
+Write a script to print nucleobase count in the given DNA sequence. Also
+print the complementary sequence where Thymine (T) on one strand is
+always facing an adenine (A) and vice versa; guanine (G) is always facing
+a cytosine (C) and vice versa.
- gcd(1,2) + gcd(1,3) + gcd(2,3)
+To get the complementary sequence use the following mapping:
-Example 2:
-
- Input: 4
- Output: 7
-
- gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4)
+T => A
+A => T
+G => C
+C => G
"
-My notes: very clearly defined. Haven't used GCDs for a while:-)
+My notes: very clearly defined, simple to do.
-Task 2: "Magical Matrix
+Task 2: "Ethiopian Multiplication
-Write a script to display matrix as below with numbers 1 - 9. Please make sure numbers are used once.
+You are given two positive numbers $A and $B.
-[ a b c ]
-[ d e f ]
-[ g h i ]
+Write a script to demonstrate
-So that it satisfies the following:
+https://rosettacode.org/wiki/Ethiopian_multiplication
-a + b + c = 15
-d + e + f = 15
-g + h + i = 15
-a + d + g = 15
-b + e + h = 15
-c + f + i = 15
-a + e + i = 15
-c + e + g = 15
+using the given numbers.
"
-My notes: clearly defined. Constraint problem.
+My notes: clearly defined, once you follow the link and discover what Ethiopian Multiplication is:-)
+Exercise for the interested reader: why does the method work? It follows directly from how BINARY
+multiplication actually works.
diff --git a/challenge-090/duncan-c-white/perl/ch-1.pl b/challenge-090/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..ea0fb6e625
--- /dev/null
+++ b/challenge-090/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+#
+# Task 1: "DNA Sequence
+#
+# DNA is a long, chainlike molecule which has two strands twisted into
+# a double helix. The two strands are made up of simpler molecules
+# called nucleotides. Each nucleotide is composed of one of the four
+# nitrogen-containing nucleobases cytosine (C), guanine (G), adenine (A)
+# and thymine (T).
+#
+# You are given DNA sequence, GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG.
+#
+# Write a script to print nucleobase count in the given DNA sequence. Also
+# print the complementary sequence where Thymine (T) on one strand is
+# always facing an adenine (A) and vice versa; guanine (G) is always facing
+# a cytosine (C) and vice versa.
+#
+# To get the complementary sequence use the following mapping:
+#
+# T => A
+# A => T
+# G => C
+# C => G
+# "
+#
+# My notes: very clearly defined, simple to do.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+
+my $debug = 0;
+die "Usage: dna [--debug] sequence\n" unless
+ GetOptions( "debug" => \$debug ) &&
+ @ARGV==1;
+my $seq = shift;
+
+my %freq;
+my $comp = "";
+my %comp = qw(T A A T G C C G);
+@freq{keys %comp} = (0) x (keys %comp);
+
+$seq =~ tr/ATCG//cd;
+foreach my $base (split(//,$seq))
+{
+ $freq{$base}++;
+ $comp .= $comp{$base};
+}
+say "complement: $comp";
+say "freq{$_} = $freq{$_}" for sort keys %comp;
diff --git a/challenge-090/duncan-c-white/perl/ch-2.pl b/challenge-090/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..8cabeac23f
--- /dev/null
+++ b/challenge-090/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+#
+# Task 2: "Ethiopian Multiplication
+#
+# You are given two positive numbers $A and $B.
+#
+# Write a script to demonstrate
+# https://rosettacode.org/wiki/Ethiopian_multiplication
+# using the given numbers.
+# "
+#
+# My notes: clearly defined, once you follow the link and discover what Ethiopian Multiplication is:-)
+# Exercise for the interested reader: why does the method work? It follows directly from how BINARY
+# multiplication actually works.
+#
+
+use strict;
+use warnings;
+use Data::Dumper;
+use Function::Parameters;
+use feature 'say';
+use Getopt::Long;
+
+my $debug = 0;
+die "Usage: ethiopian-multiplication [--debug] A B\n" unless
+ GetOptions( "debug" => \$debug ) &&
+ @ARGV==2;
+my( $a, $b ) = @ARGV;
+
+say ethiopian( $a, $b );
+
+
+#
+# my $m = ethiopian( $a, $b );
+# Use Ethiopian Multiplication - the original Egyptian/Ethiopian/Russian
+# form of multiplication using repeated doublings and halvings.
+#
+fun ethiopian( $a, $b )
+{
+ my $r = 0;
+ while( $a >= 1 )
+ {
+ say "r:$r, a:$a, b:$b" if $debug;
+ $r += $b if $a % 2 == 1;
+ $a /= 2; $a = int($a);
+ $b *= 2;
+ }
+ say "r:$r, a:$a, b:$b" if $debug;
+ return $r;
+}