Last active
December 2, 2018 16:43
-
-
Save MattOates/fa3a306e4e057d6d133cf8445bae1aed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
sub character_differences(Str $str1, Str $str2) { | |
$str1.comb Z~~ $str2.comb | |
} | |
sub hamming_distance(Str $str1, Str $str2) { | |
my @differences = character_differences($str1, $str2); | |
return @differences.grep(* == False).elems; | |
} | |
sub letters_the_same(Str $str1, Str $str2) { | |
my @differences = character_differences($str1, $str2); | |
my @positions_common = @differences.pairs.grep(*.value == True).map(*.key); | |
return @positions_common.map({$str1.substr($_, 1)}).join('') | |
} | |
for lines().combinations: 2 -> ($id1, $id2) { | |
if hamming_distance($id1, $id2) == 1 { | |
say letters_the_same($id1, $id2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment