# 解 Perl Weekly Challenge 090 -- DNA 序列與衣索比亞乘法

## 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 nucleiobase 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 \$dnaseq = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG';

# Count of each nucleiobase
my %histogram = ( "A" => 0, "T" => 0, "C" => 0, "G" => 0 );
\$dnaseq.comb.map(-> \$c { %histogram{\$c}++ });

# Complementary sequence
my \$complemnet = \$dnaseq.trans('TAGC' => 'ATCG');

# Print everything
say "nucleiobase count";
for 'A', 'T', 'C', 'G' -> \$c {
say "  \$c: " ~ %histogram{\$c};
}
say "Complement: " ~ \$complement;
``````

## TASK #2 › Ethiopian Multiplication

You are given two positive numbers \$A and \$B.

Write a script to demonstrate Ethiopian Multiplication using the given numbers.

1. 在第一行寫下兩數 A, B 。分別記為 A B 兩欄。
2. 在第二行 A 欄處寫下 A\2 ，B 欄處寫下 B×2 之結果
3. 反覆於兩欄下方寫下前一行數字 \2 與 ×2 之結果，直到 A 欄數字變為 1 為止。
4. 取 B 欄中所有對應到 A 欄為奇數之數字之總和，即為 A×B 之結果。

``````# multiply.raku

sub MAIN (Int \$num1, Int \$num2) {
my \$n1 = \$num1;
my \$n2 = \$num2;
my @logs;
push @logs, [ \$n1, \$n2 ];
while \$n1 != 1 {
\$n1 = \$n1 div 2;
\$n2 = \$n2 * 2;
push @logs, [ \$n1, \$n2 ];
}

say "\#\t\$num1 × \$num2\n";
for @logs -> \$log {
my \$star = \$log[0] %% 2 ?? " " !! "+";
say "\t\$log[0]\t\$star \$log[1]";
}
say "-------------------------";
say "\t\t  " ~ @logs.grep({ .[0] % 2 == 1 }).map({ .[1] }).sum ~ "\n";
}``````

``````# raku multiply.raku 42 84
#       42 × 84

42        84
21      + 168
10        336
5       + 672
2         1344
1       + 2688
-------------------------
3528
``````