Skip to content

Instantly share code, notes, and snippets.

@MattOates
Last active December 2, 2018 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattOates/fa3a306e4e057d6d133cf8445bae1aed to your computer and use it in GitHub Desktop.
Save MattOates/fa3a306e4e057d6d133cf8445bae1aed to your computer and use it in GitHub Desktop.
#!/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