The weekly challenge 301 - Task 2: Hamming Distance
1 #!/usr/bin/env perl
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 use v5.40;
47
48 hamming_distance(4, 14, 2);
49 hamming_distance(4, 14, 4);
50
51 sub hamming_distance {
52 my @ints = @_;
53 print "Input: (" . join(", ", @ints) . ")\n";
54 my $max_len = 0;
55 my @bins = ();
56 my $dist = 0;
57 foreach my $num (@ints) {
58 my $bin = sprintf ("%b", $num);
59 my $len = length($bin);
60 $max_len = $len if $len > $max_len;
61 push @bins, $bin;
62 }
63 foreach my $i (0..$#bins) {
64 my $len = length($bins[$i]);
65 my $lendiff = $max_len - $len;
66 if($lendiff) {
67 $bins[$i] = '0'x$lendiff . $bins[$i];
68 }
69 foreach my $j ($i+1..$#bins) {
70 my $len = length($bins[$j]);
71 my $lendiff = $max_len - $len;
72 if($lendiff) {
73 $bins[$j] = '0'x$lendiff . $bins[$j];
74 }
75 $dist += _hamming_distance($bins[$i], $bins[$j]);
76 }
77 }
78 print "Output: $dist\n";
79 }
80
81 sub _hamming_distance {
82 my ($i, $j) = @_;
83 my $dist = 0;
84 my @first = split //, $i;
85 my @second = split //, $j;
86 foreach my $k (0..$#first) {
87 $dist++ if $first[$k] != $second[$k];
88 }
89 return $dist;
90 }