exercism-perl5

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

Hamming.pm (534B) - raw


      1 package Hamming;
      2 use strict;
      3 use warnings;
      4 use Exporter qw<import>;
      5 our @EXPORT_OK = qw<hamming_distance>;
      6 
      7 sub hamming_distance {
      8   my ( $strand1, $strand2 ) = @_;
      9   die "left and right strands must be of equal length"
     10     unless ( length $strand1 == length $strand2 );
     11   print "strand1 = $strand1\nstrand2 = $strand2\n";
     12   my $hamming_distance = 0;
     13   foreach my $i (0 .. (length $strand1) - 1) {
     14     if ( substr($strand1, $i, 1) ne substr($strand2, $i, 1)) {
     15         $hamming_distance++;
     16     }
     17   }
     18   return $hamming_distance;
     19 }
     20 
     21 1;