Wednesday, April 23, 2008 3:43:42 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 1: Could I Get Your Phone Number?

This builds a hash table that translates phone numbers to dictionary words (if they exist). That's all it does.

To solve this problem, I reduced it to its very essence: a hash table. The keys of the hash table are the phone numbers. The values are dictionary word(s) that correspond to a phone number.

Representative line:

$ns[$phoneNumber]

Now wasn't that easy?

Source

#PROBLEM #1
function Get-SevenLetterWords ($filename)
{
    $allWords = cat $filename
    $allWords | ? { $_.Length -eq 7 } | % { $_.ToUpper() }
}


#POSTSCRIPT - ok, this was gratuitous. I didn't want to build a letter->number
#mapping "the hard way", so, um, I did it the, um, let's call it
#"the easy way", which is below.
function Build-LettersToNumbersMapping
{
    $mapping = @{}
    $digit = 2;
    $counter = 0;
    for ($i = 65; $i -lt (65+26); $i++) {
        if ([char]$i -ne "Q" -and [char]$i -ne "Z") { $counter++ }
        if ($counter -gt 3) {
            $digit++
            $counter = 1
        }
       
        $mapping[ [char]$i ] = [string]$digit
    }
   
    $mapping
}


function Build-NumberSpeller
{   
    $words = Get-SevenLetterWords "C:\Scripts\wordlist.txt"
    $lettersToNumbersMapping = Build-LettersToNumbersMapping
   
    #$i = 1; $max = $words.count
   
    $numberSpeller = @{}
    foreach ($word in $words)
    {
        #write-host "$i of $max"; $i++
       
        $numericTranslation = @()
        foreach ($letter in $word.ToCharArray())
        {
            $numericTranslation += $lettersToNumbersMapping[ $letter ]
        }
       
        $numberSpeller[ ([string]::Join("", $numericTranslation)) ] = $word
    }
   
    $numberSpeller
}


function Solve-Problem1
{
    "Building the number speller; please be patient while it loads…"
    $ns = Build-NumberSpeller
    write-host -noNewLine "Please enter the phone number (without hyphens) and press ENTER: "
    $phoneNumber = read-host
   
    #POSTSCRIPT - I read the discussion of how long each person's solution takes to
    #a) build the number lookup table, b) do the lookup.
    #While my solution is REALLY slow building the table, the lookups are, how you say, INSTANT.
    #Thus is the magic of hash tables.
    $ns[$phoneNumber]
}


Solve-Problem1

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.2.8279.16125

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