Categories

Monthly Archives: October 2009

Hamming Distance

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

001
002
003
004
005
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: ,

Date and Time

I recently came across a script that used date and time information in this manner

001
002
003
004
005
006
$stuff =  "Stuff"
$monthday = Get-Date -Format "ddMMM"
$year = Get-Date -Format yyyy
$time = Get-Date -Format "HH:mm:ss"
$data = "$monthday" + "," + "$year" + "," + "$time" + "," + "$stuff"
$data

 

This gives a result of

12Oct,2009,15:20:32,Stuff

I didn’t like the way get-date was used three times – its messy .  In most cases it won’t make much difference but if the script runs across a day boundary you just might get some odd results.

The same results can be achieved with

001
002
003
004
$stuff =  "Stuff"
$date = Get-Date
$data = "{0:dd}{1:MMM},{2:yyyy},{3:HH}:{4:mm}:{5:ss},$stuff" -f $date, $date, $date, $date, $date, $date
$data

 

which will give a result of

12Oct,2009,15:20:38,Stuff

The time differences are related to me clicking across different tabs in ISE

In this way we only call Get-date once and use the .NET string formatting to give the results we want.  Much neater and we don’t need to worry about time or date boundaries

 

Technorati Tags: ,,,