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

Scripting Games 2008 Sudden Death Challenge 3

My solution for Sudden Death Challenge 3 :

$p = gc c:\scripts\Presidents.txt

"Longest first name: " + (($p | select @{n='Length';e={($_ -split ", ")[1].length}},@{n='Name';e={$_}} | sort length)[-1]).name

"Letters not used as initials"
65..90 |? {($p |% {$_.tochararray() -cmatch '[A-Z]'}) -notcontains [char]$_ } |% {[char]$_} 

"Total vowels used: " + ($p |% {$_.tochararray() -match '[aeiou]'}).count

and the result :

PoSH> .\SDEvent3.ps1                                                                                                    
Longest first name: Hayes, Rutherford                                                                                   
Letters not used as initials                                                                                            
I                                                                                                                       
O                                                                                                                       
Q                                                                                                                       
S                                                                                                                       
X                                                                                                                       
Y                                                                                                                       
Total vowels used: 192                                                                                                  
PoSH>                                                                                     

*Note* The -split operator is a CTP2 feature, for PowerShell V2 you will need :

 [regex]::split($_,', ')

*Update*

  • I posted my solution to early, sorry for that
  • I Posted the wrong solution
  • Still some time to deadline, to send in correction but already got the points
  • Thanks to  jturnage for pointing out my mistake

"Longest first name: " + (($p | select @{n='Length';e={($_ -split ", ")[1].length}},@{n='Name';e={($_ -split ", ")[1] + " " + ($_ -split ", ")[0]}} | sort length)[-1]).name 

 

Enjoy,

Greetings /\/\o\/\/

Published Thursday, February 21, 2008 2:05 PM by MoW

Comments

# re: Scripting Games 2008 Sudden Death Challenge 3

Not to poopoo on your script because it's elegant as always, but the event description did state to put the "longest name" part in "firstname lastname" format instead of "lasname, firstname".  I see that you got credit though.  And at the same time, I had the exact same output except mine did have "firstname lastname" but did not get credit.  Grrrr.

Thursday, February 21, 2008 4:22 PM by jturnage

# re: Scripting Games 2008 Sudden Death Challenge 3

Auw your right, missed that piece.

the good line should be :

"Longest first name: " + (($p | select @{n='Length';e={($_ -split ", ")[1].length}},@{n='Name';e={($_ -split ", ")[1] + " " + ($_ -split ", ")[0]}} | sort length)[-1]).name

As I by accident posted my solution to early, still time to send in the fix, but I have points allready , hmmm

Thanks a lot .

greetings /\/\o\/\/

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

# re: Scripting Games 2008 Sudden Death Challenge 3

lol glad you got the points.  My solution is a bit more verbose (only a month or so into powershell, still learning..).  Since you posted yours, guess no harm in posting mine.  Love seeing all the different ways it could be done, definitely helping me learn it.

$OFS=""

$presidents=@()

Get-Content c:\Scripts\presidents.txt |% {$x=$_.Split(",");$presidents+=@(,(($x[0]).Trim(),($x[1]).Trim()))}

$longestFirstName = $presidents | sort {($_[1]).Length} -Descending | select -First 1

[string]::Format("Longest first name: {0} {1}", $longestFirstName[1], $longestFirstName[0])

$chars=@{}

$presidents |% { $chars[$_[0][0]]+=1 ; $chars[$_[1][0]]+=1}

"Letters not used in initials:"

[int][char]"A"..[int][char]"Z" |? {[char]$_ -notmatch "["+[string]($chars.Keys | sort)+"]"} |% {[char]$_}

$vowels=0

$presidents |% { $_ |% {$_.ToCharArray() |% {$vowels += [int]($_ -match "[AEIOUaeiou]")}} }

"Total vowels used: " + $vowels

Thursday, February 21, 2008 5:20 PM by jturnage

# re: Scripting Games 2008 Sudden Death Challenge 3

Another little improvement:

"Letters not used as initials"

[char[]](65..90) |? {($p |% {$_.tochararray() -cmatch '[A-Z]'}) -notcontains $_ }

One less pipe :)

I like the fact that your solutions are very idiomatic!

Thursday, February 21, 2008 5:48 PM by Bruno Gomes

# re: Scripting Games 2008 Sudden Death Challenge 3

[char[]](65..90) |? {($p |% {[char[]]$_ -cmatch '[A-Z]'}) -notcontains $_ }

;-)

Enjoy,

Greetings /\/\o\/\/

Thursday, February 21, 2008 7:50 PM by MoW

# re: Scripting Games 2008 Sudden Death Challenge 3

While we are at it :

$c = [char[]]($p -join '')
[char[]](65..90) |? {($c -cmatch '[A-Z]') -notcontains $_}
"Total vowels used: " + ($c -match '[aeiou]').count

Enjoy,

Greetings /\/\o\/\/

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

# Scripting Games: Sudden Death Challenge - Event 3 « PowerShell ?? ???????????? ??????????????

Anonymous comments are disabled