Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

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\/\/

Published Wednesday, February 20, 2008 12:55 PM by MoW

Comments

# re: Scripting Games 2008 Advanced Windows PowerShell Event 1

Hi /\/\o\/\/,

Great to compare entries:-)

I used a similar technique to Gaurhoth (see http://chrisjwarwick.spaces.live.com/) - I haven't done any blogging before so would be gratful if you'd give me a plug - thanks!

Looking forward to seeing your calendar entry:-)

Chris

Wednesday, February 20, 2008 3:14 PM by Chris Warwick

# 2008 Scripting Games - Solutions for Advanced Event 1

Wednesday, February 20, 2008 4:48 PM by 2008 Scripting Games - Solutions for Advanced Event 1

# re: Scripting Games 2008 Advanced Windows PowerShell Event 1

Nice solution, but I'd like to point out a Powershell feature that would be a better fit in your code.

Instead of:

e={[string]::join('',[string[]]@($_.tochararray() |% {$lookup."$_"}))}

you could use the array notation and get all the values from the hash at once:

e={$lookup[[string]::join('',[string[]]$_.tochararray() ])}

This code is also about 4 times faster.

Keep up the good work!

Wednesday, February 20, 2008 8:36 PM by Bruno Gomes

# re: Scripting Games 2008 Advanced Windows PowerShell Event 1

If there were a prize for slowest solution, I might win it, as I wrestled the 3^7 possible strings out of powershell, then compared them against a hash table of wordlist.txt.

It took about 10 hours to write, and about 45 seconds to run! (I'm not a programmer by any stretch of the imagination, and should probably be doing the VB Advanced, but I like a challenge :-) )

5 minutes after I finally got it to work, I had an epiphany to just convert the 7 letter words to numbers and match a la MOW.

I still think my solution was better than the Scripting Guy's though :-)

Thursday, February 21, 2008 8:13 AM by nicktf

# re: Scripting Games 2008 Advanced Windows PowerShell Event 1

@ Bruno, Yes thats better

as the scripting guy's solution does not always works, it must be if you scored ;-)

I think the score for best solution goes to Jaykul here.

that is the most fun of the games comparing solutions.

I did hang to quick and dirty for most events (execept event 10 ), or just used what I tought was most nice to do not always the best

I might also score on slowest Prime number solution ;-)

Enjoy, your games

Greetings /\/\o\/\/

Thursday, February 21, 2008 4:56 PM by MoW

# Scripting Games: Advanced PowerShell - Event 1 « PowerShell ?? ???????????? ??????????????

Anonymous comments are disabled