Wednesday, April 23, 2008 3:44:08 AM UTC #

If you aren't already in the know, these ten problems are from the 2008 Winter Scripting games.

For each problem, I'm going to quickly sum up the interesting (interesting TO ME) bits of each problem, then I'm going to post the full source.

Event 2: Skating on Thin Ice

Here we do some very basic number crunching: read in scores, cut off the small and the large score, calculate the average. Then sort the results and print the top three.

Actually, in describing this I realize that this is a mostly boring problem. Check out how I use PowerShell's support of collections to chop off parts of them, sort, etc. Otherwise, there's not much here.

I will also point out that many of my solutions are excessively Object Happy. I.e., whenever possible, I construct an object with properties. Most of these times (with the exception of the blackjack game, #10, and the election runoff, #3 immediately below), objects were unnecessary. So, let this be your warning.

Source

#PROBLEM #2
function Create-SkaterObject ($skaterName, $skaterScore)
{
    $o = new-object PSObject
    Add-Member -inputObject $o -memberType NoteProperty -name "Name" -value $skaterName
    Add-Member -inputObject $o -memberType NoteProperty -name "Score" -value $skaterScore
   
    #POSTSCRIPT - ok, this scriptmethod is clearly gratuitous
    Add-Member -inputObject $o -memberType ScriptMethod -name "NameAndScore" -value { "$($this.Name), $($this.Score)" }


    $o
}


$totalScores = 7
function Calculate-Score ($scores)
{
        $thisSkatersTrimmedScores = $scores | sort | select -first ($totalScores - 1) | select -last ($totalScores - 2)
       
        $total = 0
        foreach ($score in $thisSkatersTrimmedScores)
        {
            $total += $score
        }


        $total / ($totalScores - 2 )
}


function Get-SkaterScores ($filename)
{
    $skaterRawData = cat $filename
   
    $scores = @()
    foreach ($skater in $skaterRawData)
    {
        $column = $skater.Split(",")
       
        $thisSkaterName = $column[0]


        $thisSkatersScores = @()
        for ($i = 1; $i -lt $column.Count; $i++)
        {
            $thisSkatersScores += $column[$i]
        }


        $finalScore = Calculate-Score -scores $thisSkatersScores
       
        $scores += Create-SkaterObject -skaterName $thisSkaterName -skaterScore $finalScore
    }
   
    $scores
}


$medals = @( "Gold", "Silver", "Bronze" )
function Solve-Problem2
{
    $allScores = Get-SkaterScores -filename "C:\Scripts\skaters.txt"
    $topScores = $allScores | sort Score -desc | select -first 3
   
    $i = 0
    foreach ($medal in $medals)
    {
        write-host "$medal medal: $($topScores[$i].NameAndScore())"
        $i++
    }
}


Solve-Problem2


2008 Winter Scripting Game Events: Index

Categories: PowerShell
Technorati:
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Syndication

Search
Posts on this page
Categories
Sites I visit regularly
About

Powered by: newtelligence dasBlog 2.1.8102.813

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008, Peter Seale

Send mail to the author(s) E-mail



Sign In