Skip to content

Instantly share code, notes, and snippets.

@mmarchese
Last active December 14, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmarchese/a7b7428d785b48d85cb2 to your computer and use it in GitHub Desktop.
Save mmarchese/a7b7428d785b48d85cb2 to your computer and use it in GitHub Desktop.
$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
"@
$total = 0
$cumulativeTotal = 0
$totalBirds = 0
$giftsWithBirds = 0
$birds = "Partridge","Turtle Doves","French Hens","Calling Birds","Geese","Swans"
$splitList = $list.Split("`n")
$objectList = $splitList | ForEach-Object {
$numbers = ($_ -creplace '[^0-9]',"")
$words = ($_ -creplace '[^a-z A-Z]',"").TrimStart()
[PSCustomObject]@{
"GiftNumber" = $numbers
"GiftType" = $words
}
}
$sortedByLength = $objectList | Sort-Object {[int]$_.GiftType.length}
$sortedByGifts = $objectList | Sort-Object -Unique {[int]$_.GiftNumber}
$objectList | ForEach-Object {$total += $_.GiftNumber}
$objectList | ForEach-Object {
$giftType = $_.giftType
$giftNumber = $_.GiftNumber
$birds | ForEach-Object {
if ($giftType -like "*$_*") {
$giftsWithBirds++
$totalBirds += $giftNumber
}
}
}
($sortedByGifts.count - 1)..0 | ForEach-Object {
$interationTotal = 0
$_..0 | ForEach-Object {
$objectNumber = $sortedByGifts.GiftNumber[$_]
$interationTotal += $objectNumber
}
$cumulativeTotal += $interationTotal
}
Write-Output "Sorted by gift name length:`n"
$sortedByLength.GiftType
Write-Output "`nSorted by number of gifts:"
$sortedByGifts
Write-Output "`nGifts containing birds in it: $giftsWithBirds"
Write-Output "`nTotal number of birds in gifts: $totalBirds"
Write-Output "`nTotal number of gifts: $total"
Write-Output "`nCumulative number of gifts: $cumulativeTotal"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment