header image

Hamming Distance

Posted by: | October 14, 2009 | No Comment |

Catching up on my reading I came across this post – http://blogs.msdn.com/powershell/archive/2009/09/20/what-s-up-with-command-prefixes.aspx which is a good explanation for why we should use command prefixes when creating cmdlets.  There is a reference to prefixes increasing the Hamming Distance of noun names.

Not knowing what a Hamming Distance is I followed the link to http://en.wikipedia.org/wiki/Hamming_distance and found that it is a measure of the difference between two strings i.e. how many substitutions are required to turn one string into another.

We can find the Hamming Distance as follows

param ([string]$str1 = "stone", [string]$str2 = "roses")
$hd = 0
if ($str1.length -ne $str2.length) {Throw "Strings are not the same length"}
for ($i=0; $i -le ($str1.length 1); $i++){if ($str1.SubString($i,1) -ne $str2.SubString($i,1)) {$hd++}}
Write-Host "Hamming Distance for $str1 and $str2 is $hd"


Input a couple of strings, check they are the same length and then compare on a character by character basis.

The script can be used like this

PS> .\Get-HammingDistance.ps1 toned roses
Hamming Distance for toned and roses is 3

PS> .\Get-HammingDistance.ps1 baston boston
Hamming Distance for baston and boston is 1

PS> .\Get-HammingDistance.ps1 2173896 2233796
Hamming Distance for 2173896 and 2233796 is 3

Technorati Tags: PowerShell,strings
under: Uncategorized