Created
January 5, 2016 00:46
-
-
Save BennettBenson/fc4b73be4f0403c89df6 to your computer and use it in GitHub Desktop.
Powershell.org Scripting Games 2015-12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$list = @" | |
1 Partridge in a pear tree | |
2 Turtle Doves | |
3 French Hens | |
4 Calling Birds | |
5 Golden Rings | |
6 Geese a laying | |
7 Swans a swimming | |
8 Maids a milking | |
9 Ladies dancing | |
10 Lords a leaping | |
11 Pipers piping | |
12 Drummers drumming | |
"@ | |
#Split $list into a collection of entries, as you typed them, and sort the results by length. | |
write-output "" | |
write-output "Here is a list of all objects sorted by length" | |
$list -split "`n" | Sort-Object -Property "Length" | |
# As a bonus, see if you can sort the length without the number. | |
write-output "" | |
write-output "Here is a list of all objects sorted by length without the preceding number" | |
$list -split "`n" -replace "^\d{1,2}[\W_]","" | Sort-Object -Property "Length" | |
#Turn each line into a custom object with a properties for Count and Item. | |
$listlines = $list -split "`n" | |
$objects = @() | |
foreach ($listline in $listlines) { | |
$object = New-Object –TypeName PSObject | |
$listline.split(" ",2) | foreach { | |
if ($_.length -le 2) { | |
$object | Add-Member –Type NoteProperty –Name Count –Value $_ | |
} else { | |
$object | Add-Member –Type NoteProperty –Name Item –Value $_ | |
} | |
} | |
$objects += $object | |
} | |
write-output "" | |
write-output "Here is a list of all objects" | |
write-output $objects | |
#Using your custom objects, what is the total number of all bird-related items? | |
$birdcount = 0 | |
for ($i = 0; $i -le $objects.Count; $i++) { | |
switch -wildcard ($objects[$i].item) { | |
"*Partridge*" {$birdcount++} | |
"*Doves*" {$birdcount++} | |
"*Hens*" {$birdcount++} | |
"*Birds*" {$birdcount++} | |
"*Geese*" {$birdcount++} | |
"*Swans*" {$birdcount++} | |
} | |
} | |
write-output "" | |
write-output "The count of all birds is $birdcount" | |
#What is the total count of all items? | |
write-output "" | |
write-output "The total count of all objects is $($objects.Count)" | |
#Turn each line into a custom object with a properties for Count and Item. | |
$listlines = $list -split "`n" | |
$objects = @() | |
foreach ($listline in $listlines) { | |
$object = New-Object –TypeName PSObject | |
$listline.split(" ",2) | foreach { | |
if ($_.length -le 2) { | |
$object | Add-Member –Type NoteProperty –Name Count –Value $_ | |
} else { | |
$object | Add-Member –Type NoteProperty –Name Item –Value $_ | |
} | |
} | |
$objects += $object | |
} | |
write-output "" | |
write-output "Here is a list of all objects" | |
write-output $objects |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment