Monday, November 13, 2017

Powershell Oneliner Contest 2017

Year after year I see many people who were old-fashioned mouse clickers adopting PowerShell and thus the average skill level is rising. At the same time, pushed by the arrival of DevOps, a lot of people who are already pretty confident with code are coming to join the ever-growing community of IT professionals that use PowerShell.

So what is the best way of testing your progress than a tricky PowerShell contest and possibly winning a prize?

Let me announce then the third edition of the PowerShell Oneliner Contest:


Before I announce the three tasks you will have to cope with, let me remind you of the spirit of this game:
  • this must be a unique learning experience, where solutions posted from experienced scripters will benefit the whole community once they are made public
  • novice scripters will have to show a lot of persistence in order to get solutions that work, respect the rules and be creative
  • having fun is of paramount importance while bending the command line in a creative way

GENERAL RULES
  • The contest is split into three tasks of increasing difficulty
  • Each task consists of a simple scenario for which you have to produce the shortest possible oneliner solution
  • You can use every PowerShell version, just state in the comment which version you tested it with
  • No semi-colons
  • Backticks are accepted for readability
  • To submit your entry, create a secret Gist for each task solution and post the URL for the Gist as a comment to this blog article
  • Submitting an entry that is a public Gist will automatically disqualify the entry and participant
  • Sign your comments so that I know who's who and so that I can get in touch with the winner
  • Entries (comments) will not be made public until after the submission deadline
  • 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 contest will run for nine days beginning today until November 21st 12:00 noon (GMT)
  • The winner will be announced on Friday, November 24th on this blog
  • I'll be the only judge

TASK 1: MANIPULATING OUTPUT

Windows Management Instrumentation is an incredibly useful technology for exposing system information. Being able to interact with it from PowerShell is one the first things we all learn. Your first task is to write the shortest possible oneliner that extracts the UNC path of all the local shares from the Win32_Share class.

Expected output:
\\srv01\ADMIN$
\\srv01\C$
\\srv01\E$
\\srv01\F$
\\srv01\IPC$
The use of Win32_Share class is mandatory.

TASK 2: MANDELBROT JOKE

I still remember the first time I saw a computer-generated fractal (it was a Barnsley fern), and have always been impressed by those patterns that repeat and repeat again. What I did learn recently is that the 'fractal' term was invented by a legendary Polish mathematician, Benoit Mandelbrot, who added a B. in the middle of his name: supposedly he intended his middle B. to recursively mean Benoit B. Mandelbrot, thereby including a fractal (his mathematical discovery) in his own name.

Your mission is to write the shortest possible oneliner that answers this question
$question = 'What is the middle name of Benoit B. Mandelbrot?'
by returning
The B in Benoit B. Mandelbrot stands for Benoit B. Mandelbrot.
Reuse of $question variable is mandatory.

TASK 3: TEXT MINING

There's a lot of hype today around data mining techniques and therefore I want you to see how good you are at using PowerShell for a special kind of task. Given the following two text variables
$t1 = "I really like scripting with PowerShell"
$t2 = "PowerShell is a really really nice scripting language"
write a oneliner that is capable of determining text likeness using Cosine Similarity and returns
0.516397779494322
The returned value must be 1 if $t1 and $t2 are identical vectors (same words) and 0 if $t1 and $t2 have no words in common.
The comparison must be case insensitive, meaning that PowerShell and powershell are the same word. The string must be split at any non-word character and only unique elements of the resulting collection are compared.
The oneliner should work against any other pair of text variables, for instance
$t1 = "Unless you work hard, you won’t win."
$t2 = "You must work hard. Otherwise, you won’t win."
must return
0.870388279778489

SOLUTION TESTING

Being able to test your solution and see that it respects the rules is of paramount importance before submitting it. If you are taking part in this contest I suppose you must not be new to Pester. Fellow MVP Jakub Jares (@nohwnd) was kind enough to provide a solution validation tool based on his Assert module:

https://github.com/nohwnd/PowershellContest2017

Use the three provided test files (one per task) as stated in the Readme.md instruction file.

A word of notice: using those tests is not mandatory, but my opinion is that we should all be continuously learning, so if you are new to GitHub and to Pester, I suggest you seize the occasion to learn something useful. You'll just be doing yourself a favor if you use Pester to unit test your oneliners, because human error can always happen and it's a pity if you spend a lot of time providing an answer that does not actually work.

If you are interested in Jakub's Assert module, you can find it here:

https://github.com/nohwnd/Assert

THE PRIZE

Yes, there's a prize! Fellow MVP Mike F Robbins will donate one copy of The No-Nonsense Beginner’s Guide to PowerShell ebook for the winner of the contest. Thanks in advance to Mike for being always so keen to give his contribution to this kind of initiative. His book is one of the best around, as I explained in a previous post.



As a bonus, and if the winner agrees, he/she will intervene as a guest blogger on this blog and will explain how to solve this kind of PowerShell riddle.

If you want to spread the word about this PowerShell contest, feel free to twit about it. You can use the hashtags #poshcontest2017 and #powershell so that other competitors can share their thoughts (not the solutions of course!).

Have fun!


UPDATE 24th Nov: We have a winner! Check it out here.

42 comments:

  1. https://gist.github.com/anonymous/e06a4232d264ec493d279e55188ac601

    For the first one :D

    Hugues BOUTET

    ReplyDelete
  2. Another one shorter 48 Chars
    https://gist.github.com/anonymous/508e837bc5b599baf5bcf669c54c5bd0
    Hugues BOUTET

    ReplyDelete
  3. https://gist.github.com/jwizzle29/023594d9bf254743c850ac558dc197a8

    ReplyDelete
  4. https://gist.github.com/KevinMarquette/0a6ff02bb9bc0131a71bb395ed6ea554
    Task 1, KevinMarquette

    ReplyDelete
  5. Task one.
    https://gist.github.com/Bigj8705/2af740b9846b69080a6061e0ee5d9b13
    Jeffery Hayes
    @DisabledJeff

    ReplyDelete
  6. 1st task was easy enough to do on a short break. I'll have to do some research for 2 & 3.
    https://gist.github.com/mjnbrn/fbaad17fcf0040ab477f2da2e85b491d
    --mjnbrn

    ReplyDelete
  7. Task 1 - PowerShell Version 3.0
    https://gist.github.com/jeremytbrun/63a7fbcf93b03b6e2a1819ab68fc976c

    ReplyDelete
  8. Task #2 - PowerShell Version 3.0
    https://gist.github.com/jeremytbrun/4ef6786081eee77cae495e730dc31fbe

    ReplyDelete
  9. https://gist.github.com/KevinMarquette/47628f1a15991817d33c5e246d30936e
    #task 2

    ReplyDelete
  10. https://gist.github.com/pwsh/7560688f1ab40196717b5ec3b2e29511

    ReplyDelete
  11. TASK 1 - Matthew Dowst (Matthew.Dowst@catapultsystems.com)
    https://gist.github.com/mdowst/66e0a59cff65dce86fad162c01a679d2

    ReplyDelete
  12. TASK 2 - Matthew Dowst
    https://gist.github.com/mdowst/162f2db279a5a7502525391943f81a36

    ReplyDelete
  13. Todd Schertzing
    tschertzing@rigelksecurity.com
    Task 1
    https://gist.github.com/reap-/b88b327a4a77a8bca11ca1977e0ef9b5

    ReplyDelete
  14. Todd Schertzing
    tschertzing@rigelksecurity.com
    task 1 - it may have submitted already, sorry if it did
    https://gist.github.com/reap-/b88b327a4a77a8bca11ca1977e0ef9b5

    ReplyDelete
  15. https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete
  16. Oops - one change to reduce char count on task 3...

    https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete
  17. 1 - https://gist.github.com/HumanEquivalentUnit/0bc465bbf570e717922888d3bd7d0670
    2 - https://gist.github.com/HumanEquivalentUnit/bc364d99d2e42f3a4b2e3cb75f230e59
    3 - https://gist.github.com/HumanEquivalentUnit/529556404b7f9a6a55c3e4e4e0aad02a
    /u/ka-splam

    ReplyDelete
  18. Entry for task 1: https://gist.github.com/tallpaulf/1abb575ca0237c25dabfd1be2a9316c2
    Paul Farrington.

    ReplyDelete
  19. Solution for task 2: https://gist.github.com/tallpaulf/fdfef9dc3b9e36b5fb5236d6e9af3a92
    Tested with PS 3.0
    Paul Farrington.

    ReplyDelete
  20. Task1
    https://gist.github.com/JockserTHD/21d40a5d4bcced792878a409f200ccaf

    ReplyDelete
  21. Task1
    https://gist.github.com/JockserTHD/21d40a5d4bcced792878a409f200ccaf

    ReplyDelete
  22. Task 2
    https://gist.github.com/JockserTHD/be8db56804097230270f88af7b899050

    ReplyDelete
  23. I suppose one more update to my entry for task 3...

    https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete
  24. https://gist.github.com/SimonWahlin/5793164ce61a76eb72f37f5f2a9650fa

    ReplyDelete
  25. And an update to task 1:

    https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete
  26. https://gist.github.com/SimonWahlin/67982b0385b3bcef99425fa2f34d13c2

    ReplyDelete
  27. Entry for Task 1

    https://gist.github.com/Windos/d3aea2c4d46a11718e2d80188b025232

    Josh King (@WindosNZ)

    ReplyDelete
  28. Entry for Task 2

    https://gist.github.com/Windos/9a107ff82bc636a8acdb425730d8f08c

    Josh King (@WindosNZ)

    ReplyDelete
  29. Task 3:
    https://gist.github.com/SimonWahlin/09520ce774d8b41dedcd775ce1d5328c

    ReplyDelete
  30. Here are my entries:

    Task 1 (shares): https://gist.github.com/briantist/d57b8b67d0ecbb94b8098c628e2e0531

    Task 2 (mandelbrot): https://gist.github.com/briantist/cc4c12577f84e30b239a63fb42554705

    Task 3 (mining): https://gist.github.com/briantist/634ad8c7fb4e6b30b1a58194ba31c150

    You can get in touch with me via Twitter @BrianScholer

    ReplyDelete
  31. https://gist.github.com/PatheticFish/6500cd978e2c0de2a8098f885756f7bc

    ReplyDelete
  32. Phew, okay, here's my entry for Task #3...

    https://gist.github.com/Windos/d8dc32cbf13dfb3bdf9c7097fbd2af45

    ~ Joshua King (@WindosNZ)

    ReplyDelete
  33. Task 1 - https://gist.github.com/nomadtales/8b399caa9c78e788283f0a6282742398
    Task 2 - https://gist.github.com/nomadtales/149709d4582ee8ed12030b427a8a3c35
    Task 3 - Still working on this one.

    ReplyDelete
  34. Reduced #3 by an additional number of characters. Provided two examples - one requires that the $x,y,z variables be cleaned between runs of the command which I think doesn't meet the criteria of the contest.

    https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@TheZNerd)

    ReplyDelete
  35. Well... I found out where we differed in task three... this should be my final submission... unless I find something else super creative...

    https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete
  36. I have updated solutions for Task 2 (mandelbrot) and Task 3 (mining):

    Task 2: https://gist.github.com/briantist/cc4c12577f84e30b239a63fb42554705

    Task 3: https://gist.github.com/briantist/634ad8c7fb4e6b30b1a58194ba31c150

    - Brian (Twitter @BrianScholer)

    ReplyDelete
  37. Task 2 (mandelbrot) gist updated: https://gist.github.com/briantist/cc4c12577f84e30b239a63fb42554705

    - Brian (Twitter: @BrianScholer)

    ReplyDelete
  38. Task2:
    https://gist.github.com/SimonWahlin/5793164ce61a76eb72f37f5f2a9650fa

    ReplyDelete
  39. Task3:
    https://gist.github.com/SimonWahlin/09520ce774d8b41dedcd775ce1d5328c

    ReplyDelete
  40. Task2:
    https://gist.github.com/SimonWahlin/5793164ce61a76eb72f37f5f2a9650fa

    ReplyDelete
  41. OK, not really happy with solutions, but also no time to make them better...
    Task 1: https://gist.github.com/bielawb/8dfd09c796fb1a240720dc7acb2b8573
    Task 2: https://gist.github.com/bielawb/2c55761487414069aaeeb6a187f3350b
    Task 3: https://gist.github.com/bielawb/39108cd77e0330d2ae312bf107c18d89

    3rd one in particular was just "get it to work" - no effort done to make it shorter. Oh well, life happened... ;)

    ReplyDelete
  42. And another update to #3... didn't cross my mind that -unique could be shortened to -u: https://gist.github.com/theznerd/1b02ee39ed72cac6553090a3167b5de8

    - Nathan Ziehnert (@theznerd)

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...