Simon Green

Posted on

Challenge 090

# Champion

Mohammad awarded me Team PWC champion for last month. I was totally surprised when reading this weeks challenge to find this out. I feel honoured to have joined the other champions.

And a big thanks to Randi Comrie and Perl Careers team for the reward.

# Task #1 › DNA Sequence

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.

## My solution

I'll admit I know nothing about DNA sequencing, so I took this challenge as a purely programmatic one. The task can be broken down to two parts.

1. To display the count, I use a `foreach` loop to count each character, and another to display the count of each nucleobase.
2. To calculate the complementary sequence I use the tr function to reverse the characters in the string, and display it.

## Example

``````» ./ch-1.pl GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG
Nucleobases count
T: 22
A: 14
G: 13
C: 18

Complementary sequence is 'CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC'
``````

# Task #2 › Ethiopian Multiplication

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

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

## My solution

This was a really interesting challenge for two reasons. Firstly, I learnt a new way to multiple numbers and the way that it works. Secondly was the way to show the result without using fonts.

For this task I use a loop to build an array starting with the two numbers, and then divided the first number (dropping the remainder) and multiplying the second. We end that loop when the first number is 1.

I then display each row with the two numbers with `>` symbol to indicate whether the first number is odd. Finally, I add up all the second numbers where the first number is odd, and display the result.

## Examples

``````» ./ch-2.pl 12 14
12 ×  14
6 ×  28
>  3 ×  56
>  1 × 112

Result is: 56 + 112 = 168

» ./ch-2.pl 25 31
> 25 ×  31
12 ×  62
6 × 124
>  3 × 248
>  1 × 496

Result is: 31 + 248 + 496 = 775
``````