commit cac7dabefda10a1f578b2bce6ddf9edaac003b1b
parent 4ef9c30e3796bf14a7fef31944fa73843a4d5375
Author: Samir Parikh <noreply@samirparikh.com>
Date: Tue, 8 Mar 2022 15:01:11 +0000
get all hamming tests to pass
Diffstat:
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/hamming/Hamming.pm b/hamming/Hamming.pm
@@ -6,7 +6,16 @@ our @EXPORT_OK = qw<hamming_distance>;
sub hamming_distance {
my ( $strand1, $strand2 ) = @_;
- return undef;
+ die "left and right strands must be of equal length"
+ unless ( length $strand1 == length $strand2 );
+ print "strand1 = $strand1\nstrand2 = $strand2\n";
+ my $hamming_distance = 0;
+ foreach my $i (0 .. (length $strand1) - 1) {
+ if ( substr($strand1, $i, 1) ne substr($strand2, $i, 1)) {
+ $hamming_distance++;
+ }
+ }
+ return $hamming_distance;
}
1;