Scripting Games 2008 Advanced Windows PowerShell Event 1
The deadline for the first 2 events in the Scripting Games 2008 has passed, and the answers are posted, so I start posting my answers also :
check first the Windows PowerShell solution written by Richard Siddaway, he does a good job explaining the way he worked.
... about the Scripting center solution (Dictionary, random , .wtf ? ) .. nuff rant ?, sorry ;-)
My solution in part looks like Richard's only as I also did think the list was bad I first created my "own" lookup table.
When that table is created I can check the numbers as easy as this :
$num = Read-host 'Give Phone Number'
@($nl |? {$_.Number -eq $num} )[0].name
This takes some time but if you need this function for more number lookups working this way will pay in the end.
in the example below I show how I export this list again so I can use it for later queries using export-csv, and later import it again
PoSH> # first build lookup list , a bit slow for only one number but hey, I do not have to test this.
PoSH>
PoSH> $wl = gc c:\scripts\wordlist.txt |? {$_.length -eq 7 -and $_ -notmatch ' '}
PoSH> $Lookup = @{
>> a=2;b=2;c=2
>> d=3;e=3;f=3
>> g=4;h=4;i=4
>> j=5;k=5;l=5
>> m=6;n=6;o=6
>> p=7;q=7;r=7;s=7
>> t=8;u=8;v=8
>> w=9;x=9;y=9;z=9
>> }
>> $nl = $wl | select @{n='Name';e={$_}},@{n='Number';e={[string]::join('',[string[]]@($_.tochararray() |% {$lookup."$_
"}))}}
>>
PoSH> $num = Read-host 'Give Phone Number'
Give Phone Number: 9737853
PoSH> @($nl |? {$_.Number -eq $num} )[0].name
wrestle
PoSH> $nl | export-csv NumberList.csv
PoSH> $numberlist = import-csv NumberList.csv
PoSH> @($numberlist |? {$_.Number -eq $num} )[0].name
wrestle
PoSH> $numberlist | select -First 5
Name Number
---- ------
abalone 2225663
abandon 2226366
abashed 2227433
abdomen 2236636
abiding 2243464
PoSH>
You can see above that this new List is much more suited for this job, and as it contains objects now we can easy export it again for later use.
Also I did take slightly different road on creating the HashTable, the solution as I did send in for event 1 looks like this :
# first build lookup list , a bit slow for only one number but hey, I do not have to test this.
$wl = gc c:\scripts\wordlist.txt |? {$_.length -eq 7 -and $_ -notmatch ' '}
$Lookup = @{
a=2;b=2;c=2
d=3;e=3;f=3
g=4;h=4;i=4
j=5;k=5;l=5
m=6;n=6;o=6
p=7;q=7;r=7;s=7
t=8;u=8;v=8
w=9;x=9;y=9;z=9
}
$nl = $wl | select @{n='Name';e={$_}},@{n='Number';e={[string]::join('',[string[]]@($_.tochararray() |% {$lookup."$_"}))}}
$num = Read-host 'Give Phone Number'
@($nl |? {$_.Number -eq $num} )[0].name
Note that I decided to "Localize" this event and added Q and Z ;-)
Enjoy,
Greetings /\/\o\/\/