exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

NucleotideCount.pm (875B) - raw


      1 package NucleotideCount;
      2 use strict;
      3 use warnings;
      4 use Exporter qw<import>;
      5 our @EXPORT_OK = qw<count_nucleotides>;
      6 
      7 sub count_nucleotides {
      8   my ($strand) = @_;
      9   my %count = (
     10         'A' => 0,
     11         'C' => 0,
     12         'G' => 0,
     13         'T' => 0
     14         );
     15   if ($strand =~ /[^ACGT]/) {
     16       die "Invalid nucleotide in strand";
     17   } else {
     18       foreach (split //, $strand) {
     19           $count{$_}++;
     20       }
     21   }
     22   return \%count;
     23 }
     24 
     25 #
     26 # Better solution from pnyman at Exercism.org:
     27 # https://exercism.org/tracks/perl5/exercises/nucleotide-count/solutions/pnyman
     28 #
     29 #        sub count_nucleotides {
     30 #            my ($strand) = @_;
     31 #            die "Invalid nucleotide in strand" if grep {/[^ACGT]/} $strand;
     32 #            my %nucleotides = map { $_ => 0 } qw( A C G T );
     33 #            map { $nucleotides{$_}++ } split //, $strand;
     34 #            \%nucleotides;
     35 #        }
     36 #
     37 
     38 1;