diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-13 13:54:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-13 13:54:33 +0000 |
| commit | a9d2308a63b1fd151b6ec596faecaf3880669283 (patch) | |
| tree | 566da15197b7e2856d6949ddc9420170bd354abb | |
| parent | 8a42c26f2b60130c648d864f7963444663267110 (diff) | |
| parent | b7e18e6a373d130efe1e6667446b04b9e6421f15 (diff) | |
| download | perlweeklychallenge-club-a9d2308a63b1fd151b6ec596faecaf3880669283.tar.gz perlweeklychallenge-club-a9d2308a63b1fd151b6ec596faecaf3880669283.tar.bz2 perlweeklychallenge-club-a9d2308a63b1fd151b6ec596faecaf3880669283.zip | |
Merge pull request #2973 from simongreen-net/swg-090
sgreen solution to challenge 090
| -rw-r--r-- | challenge-090/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-090/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-090/sgreen/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-090/sgreen/perl/ch-2.pl | 41 |
4 files changed, 74 insertions, 2 deletions
diff --git a/challenge-090/sgreen/README.md b/challenge-090/sgreen/README.md index 518f1c9558..63e68425dc 100644 --- a/challenge-090/sgreen/README.md +++ b/challenge-090/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 089 +# The Weekly Challenge 090 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-089-f3) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-090-41) diff --git a/challenge-090/sgreen/blog.txt b/challenge-090/sgreen/blog.txt new file mode 100644 index 0000000000..b3109941ff --- /dev/null +++ b/challenge-090/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-090-41 diff --git a/challenge-090/sgreen/perl/ch-1.pl b/challenge-090/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..653e9ede21 --- /dev/null +++ b/challenge-090/sgreen/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $string = shift; + + # Sanity check the input + die "You must enter the DNA sequence\n" unless $string; + die "Unexpected character in the DNA sequence\n" + unless $string =~ /^[TACG]+$/; + + # Count the nucleobases and display them + my %count = (); + say 'Nucleobases count'; + foreach my $nucleobase ( split //, $string ) { + ++$count{$nucleobase}; + } + foreach my $nucleobase (qw(T A G C)) { + say $nucleobase, ': ', $count{$nucleobase} // 0; + } + + # Display the complementary sequence + $string =~ tr/TAGC/ATCG/; + say "\nComplementary sequence is '$string'"; +} + +main(@ARGV); diff --git a/challenge-090/sgreen/perl/ch-2.pl b/challenge-090/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..b42d85cd00 --- /dev/null +++ b/challenge-090/sgreen/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util 'sum'; + +sub main { + my ( $A, $B ) = @_; + + # Sanity check + die "You must enter two positive numbers\n" unless $A and $B; + die "The first value is not a postive integer" unless $A =~ /^[1-9][0-9]*$/; + die "The second value is not a postive integer" + unless $B =~ /^[1-9][0-9]*$/; + my $format = sprintf '%%s %%%dd × %%%dd', length($A), length( $A * $B ); + + # Divide the first number (ignoring remainder), multiple second + # until $A = 1. + my @table = ( [ $A, $B ] ); + while ( $A > 1 ) { + $A = int( $A / 2 ); + $B *= 2; + push @table, [ $A, $B ]; + } + + # Display the table + my @addition = (); + foreach my $row (@table) { + # If the first number is odd, add it to the list + my $odd = $row->[0] % 2; + say sprintf $format, ( $odd ? '>' : ' ' ), @$row; + push @addition, $row->[1] if $odd; + } + + say "\nResult is: ", join( ' + ', @addition ), ' = ', sum(@addition); +} + +main(@ARGV); + |
