Wednesday, June 24, 2015

I have always enjoyed taking part in most of the Powershell contests out there since Powershell is kind of a recreational administration language to me. I am thinking, for example, of the fun I had taking part in the oneliner contest (which I won) organized by fellow MVP Mike F Robbins, or of the Powershell Golf organized in 2013 by Robert Robelo on Twitter.

THE IDEAS BEHIND THE CONTEST

Being quite proud of my blog, and despite the fact that I am more of a system administrator than a developer, I have unilaterally decided to organize my self-hosted, self-managed Powershell Oneliner Contest.

The three ideas behind this game are that:

experienced competitors (and I, of course!) should come away with a lot of tips from the brightest Powershell minds that will hopefully take part in the event

novice Powershell scripters should learn that persistence pays off when looking for a solution to such a contest

this is a fairly good occasion to sharpen your skills for the soon-to-start Powershell Scripting Games held by the Powershell.org community

GENERAL RULES

The contest is split into three tasks. Each task consists of a simple scenario for which you have to produce a oneliner solution.

Submit your solutions via a comment to this blog article by 11AM (PDT) on Wednesday, July 1st, which is the deadline.

You MUST submit only one task solution per comment, so that I can easily see who was the first to submit the shortest solution on a per-task basis.

Should you find a shorter oneliner to solve a task you are allowed to post additional comments (just remember to sign your comments so that I know who's who).

UPDATE - Please don't sign your comments as Unknown or I won't be able to know who you are.

If you use a Powershell version other than v4, I am okay with it, but please mention it in the comment so that I can test it on the same version. Powershell v5 is welcome.

Entries (comments) will not be made public until after the submission deadline.

SCORING

The shortest solution in terms of chars wins.

The first person to produce the shortest working solutions to a task will get 1 point, the second 2 points, the third 3 points and so on.

The person with the lowest total score over the three mandatory tasks will be the winner.

The winner will be announced on Saturday, July 4th on this blog and the prize will be awarded at that time.

I'll be the only judge.

THE PRIZE

What? Is there a prize? Yes, of course. The prize will be a printed copy of the excellent book 'Hyper-V Best Practices' (5-Stars on amazon.com) by fellow MVP Benedict Berger (@Benedict_Berger) offered by Packt Publishing!!!

I was the reviewer of the book as well of the ton of Powershell code it contains. I can tell you this is the book to read in these Hyper-V times!

As a bonus, and if the winner agrees, he/she will intervene as a guest blogger on this blog and will explain how one goes about learning the hidden bits of Windows Powershell. Sounds cool, doesn't it?Let's now dive into the contest tasks.

TASK 1 - Who's taller?

This is a rather easy challenge and I hope it will make for a good warm-up session for your Powershell muscles, especially if you are a beginner. Given a $n variable showed below, write the shortest possible oneliner that outputs the absolute value of the largest double-precision floating-point number in the array.

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

Expected output

Task-specific rules

The object in the output can be of any type

UPDATE - The one-liner must work also with different values of $n

TASK 2 - Can you count to five?

Time to delve into something harder. Write the shortest possible Powershell oneliner that outputs the number 12345.

Expected output

Task-specific rules

You are not allowed to use the digits 0 to 9 in your oneliner

The object in the output must be of type Int32

Your displayed output shouldn't contain any other char

TASK 3 - Powershell is the secret word

I hope that by this time you are enjoying the competition. It's time for the last hard task. Write the shortest possible Powershell oneliner that outputs the word 'PowerShell' starting from a text string composed only of uppercase Xs' and whitespaces.

Expected output

Task-specific rules

The string composed of X's and whitespaces must be part of the oneliner (backticks accepted!) and not be a variable defined on a separate line

On your marks, set, go! And remember to have fun, it's just a game meant to produce something useful for the growing and growing Powershell community!UPDATE - Check-Task FunctionI have decided to update this blog post with a function that will allow you to check if your proposed solutions are correct in terms of output and respect the task assignments. Kudos for this function go to my friend Joey!

2.[int]::parse(-join("▲'".tochararray()|%{[int]$_}),'He')cut and paste can't set characters below ascii 32, and I must comment that in doube quotes after -join must be character with ascii code 30 and single quote ' (ascii 39) solution is 55 characters long

Solution1 new correction. My second entry for question 1 works but only because the number in the array is of negative value. Multiplying by -1 works in this case. If the Largest Double was positive then my solution wouldn't work. New solution is 43 characters:[math]::abs(($n|?{$_-is"Double"}|Sort)[-1])

This is kind of cheating but technically correct. Have to copy/paste this into the shell (and there is a chance the encoding will be lost along the way). I found this by running this command:[char]12345

Update: since apparently the "largest" was before converted to absolute values, new solution to task 1:($n|?{$_-is[Double]}|sort|%{[math]::abs($_)})[-1]Same amount of characters, still Powershell 5.(I blame language barriers for this one..)

New solution for question 3 using 64 characters. You say that I can use as many Xs and whitespaces as I like or need. I choose to use none :)""|%{1,32,40,22,35,4,25,22,29,29|%{Write-Host([char]($_+79))-N}}If this solution is not acceptable then I will add an X and a whitespace in the initial string. Pls let me know /Simon

This uses some ... creative interpretation of the word "whitespace", including not only space, but also everything else Unicode has to offer ;). Hexadecimal encoding of the word PowerShell by mapping 0–F to X and whitespace characters.

Very trivial encoding of indexes into a reservoir of characters to use. Unary coding of the indexes. Now, this might be a bit debatable, as I defined the characters that can be produced from the encoding, but changing the X-and-whitespace string will change the output, so I guess it should count (also contains only X and spaces, without going crazy on the whitespace rule like previous solutions).

This gives me all green and the following result: 43: [math]::Abs(($n|?{$_-is[double]}|sort)[-1]) 10: +[char]'〹' 20: "X "|%{'PowerShell'}

Before you consider "X "|%{"PowerShell"} as a joke then ask yourself this...Is your start string and your end string the same string?Does... [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemAnsi($startString...) [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemAnsi($endString...)yield the same pointer value? If not then a pure replacement ought to be within the rules since you say nothing about the "PowerShell" string not to be part of the oneliner. Or you could be more specific in what type of algorithm you are expecting. :)

New entry for task 3. I solved my earlier question. Write-Host does not output a string. That is why I was getting red in Check-Task. Anyway new entry below: 71 chars"X"|%{[string]::Concat((0,31,39,21,34,3,24,21,28,28|%{[char]($_+80)}))}