Skip to content

Instantly share code, notes, and snippets.

@SadProcessor
Last active December 9, 2015 12:05
Show Gist options
  • Save SadProcessor/48e1049cc50383b34960 to your computer and use it in GitHub Desktop.
Save SadProcessor/48e1049cc50383b34960 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
"@
#Part 1: $list to sorted $array
$array = ($list -split "`n") | Sort-Object -Property Length
#bonus: (it works but I'm not sure it's considered a one-liner...)
$BonusArray = foreach($string in ($list -split "`n")){$string.Trim($string.Split()[0]+' ')} $BonusArray = $BonusArray | Sort-Object -Property Length
#Part 2: $Array to PScustom object $collection
$collection = foreach($item in $array){New-Object -TypeName PSCustomObject -Property @{'Count'=(($item.split()[0])-as [int]);'Item'=$item.Trim($item.split()[0]+' ')}}
#Part 3: $Birdcount/$birdcount2 (question was a bit unclear (for my english): count of bird categories in list? or total count of all birds?)
$birdcount = ($collection -notmatch "lord|ladie|ring|drummer|maid|piper").count
#-or-
$birdcount2=0
foreach($_ in ($collection -notmatch "lord|ladie|ring|drummer|maid|piper")){$birdcount2+=$_.count}
#inspired by Adam Bertram for | in -notmatch >> thks for the tip, will remember that one.
#Part 4: $sum of values in count column of $collection
$sum = 0
for($i=0; $i -lt ($collection.Length);$i++){$sum += ($collection[$i].count)}
#Bonus
$t=0
foreach($d in (1..12)){for($i=1; $i -le $d; $i++){$t += $i }}
cls
#DISPLAY RESULTS: (in the shape of Xmas tree...)
$array,'', $BonusArray,'', $collection,'',"Bird Related Categories: $birdcount",'-or-',"Total Bird Count: $birdcount2",'',"Total Item Count: $sum",'',"Bonus Count: $t"
### Merry Xmas to all ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment