The only golf I play

x-defaultI’m a PowerShell geek. And I fancy one-liners (a lot). When I participated in the Scripting Games in 2011 I did try to solve any advanced problem with one-liner and once that was complete, I would start investing time in making it a proper function/module. That’s why whenever I see PowerShell golf contest popping up somewhere I’m always up for the game. I usually don’t win these but sole process involved in making something as short as possible (it also becomes cryptic, but that’s just a side effect) is well worth it.

The most recent contest like that was done by my fellow MVP, Carlo. You can read all the rules and assignments (and by the time you read this post – probably also the results) here. In this post you can take a look at my solutions, with explanation why I did things the way I did them. I’m quite sure people found smarter solutions (I’m bit busy these days so I couldn’t spend too much time on mine). I kind of missed “par” in this contest, to tell me if there is proven working solution (done by author of contest) that is shorter than mine. It would probably give me that extra kick (and keep me awake during the nights). Winking smile

Largest absolute number

In this task we got a list of items:

$n = -1,-2,-5,-8.9,'b',-9.11,-6,-3,-2,-9.1,-1,-1.4,'a'

Our task was to get only doubles, pick the largest one, get absolute value. My solution:

[math]::Abs(($n|?{$_-is[double]}|sort)[-1])

Getting doubles is done with Where-Object cmdlet (using operator –is). Next thing I do is sort, that pick the last one in the set. Finally I get absolute value from this number using static method Abs on [math] class.

Display number without writing any digits

Funny idea, right? Well, for me obvious candidate was [char] class. In the end I’ve decided to use a short string, and that’s the solution I’ve posted:

+(-join[int[]][char[]]'{-')

Step by step: I take string “{-“, convert it into char array, take numeric values for both chars (that happen to be 123 and 45), join resulting array, and use unary + to turn string into integer. There is a shorter option though: just use a char 12345. Winking smile Tricky part here is that I was not sure if pasting it into comment on the blog will not break it. This is my code, if it fails – try to replace silly char I’ve used with what you get when you run [char]12345:

+[char]'〹'

Making X shine

Last task was particularly hard: we were asked to use string of X’es and white spaces and turn it into word PowerShell with proper casing and all. I decided that since there are 8 distinct characters used I can just use “encoded” string, where each combination of 3 chars represents one char from my string:

@{
'X  ' = 'P'
' X ' = 'o'
'  X' = 'w'
'XX ' = 'e'
'X X' = 'r'
' XX' = 'S'
'XXX' = 'h'
'   ' = 'l'
}

I also decided to use [regex] Replace method with match evaluator. That allows us to run any code for any match found in initial string. My solution broken into few lines for readability:

[regex]'(.{3})'|
    % Re* 'X   X   XXX X X XXXXXXX       '{
        switch($args.Groups[1].Value){
            'X  '{'P'}
            ' X '{'o'}
            '  X'{'w'}
            'XX '{'e'}
            'X X'{'r'}
            ' XX'{'S'}
            'XXX'{'h'}
            '   '{'l'}
        }
    }

It could have been easier I believe with the use of hashtable, but that would require semicolons (not allowed) in “one-line” scenario:

[regex]'(...)'|
    % Re* 'X   X   XXX X X XXXXXXX       '{
        @{
            'X  '='P'
            ' X '='o'
            '  X'='w'
            'XX '='e'
            'X X'='r'
            ' XX'='S'
            'XXX'='h'
            '   '='l'
        }[$args.Groups[1].value]
    }

Also, as you can see using a crappy regex makes solution a bit shorter.

And that’s it – waiting for another challenge. I wish I could come up with the stuff like this myself. Preferably achieving something useful too, but that would be just an icing on the cake. Winking smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s