PowerShell: Simple Challenge

As I said before, I am a regular at my local PowerShell user group. I keenly look forward to our monthly meetings. We share a lot amongst ourselves. It is a small but close-knit group. Recently, one of the members, Sergii Cherkashyn came up with a coding challenge for the group. This blog post is about the challenge. Using PowerShell, we had to generate three shapes. This is the kind of simple stuff that keeps the meetings interesting and fun.

Challenge 1

Generate this:


#
##
###
####
#####
######
#######
########
#########
##########
###########
############
#############
##############
###############
################
#################
##################
###################
####################

Challenge 2

Generate this:

                   #
                  ##
                 ###
                ####
               #####
              ######
             #######
            ########
           #########
          ##########
         ###########
        ############
       #############
      ##############
     ###############
    ################
   #################
  ##################
 ###################
####################

Challenge 3

Generate this:

                   #
                  ###
                 #####
                #######
               #########
              ###########
             #############
            ###############
           #################
          ###################
         #####################
        #######################
       #########################
      ###########################
     #############################
    ###############################
   #################################
  ###################################
 #####################################
#######################################

Before you see my solutions, please think for a few minutes. While #1 and #2 are trivial, #3 can make you think for a few minutes. Also, when you use PowerShell, you are not inhibited by the limitation of traditional constructs that bog you down. Try it out for yourself before you scroll down to see my answers.

Scroll down for my solutions

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

Challenge 1 solution

In all of these solutions, I can take away a lot of the spaces and brackets to produce the same result but I leave them in for clarity.

1..20 | %{$('#'*$_)}

My fellow group member and friend Alan pointed out that the enclosing $() was not necessary, so the simpler version is below and I will drop the $ in my other solutions too.

1..20 | %{('#'*$_)}

Where:

  • “1..20” generates an array of elements 1 to 20. Consider it  shorthand for For ($i=0; $i -le 20; $i++) {$i}
  • “%” is the same as “foreach”
  • “(‘#’*$_)” is string multiplication where ‘#’ is multiplied by current loop value to produce N characters of #

Said another way, it is the same as saying

For ($i=0; $i -le 20; $i++) {(‘#’ * $i)}

Challenge 2 solution

1..20 | %{(' '*(20-$_)) + ('#'*$_)}

Where:

This is similar to Challenge #1 except, we also add spaces at the beginning of each line with (‘ ‘*(20-$_))

Challenge 3 solution

This is a little tricky. I do not know if you noticed the “requirement” but until you do, you will not get the solution no matter how hard you try. If you noticed the result of the #3 challenge, the tree builds up with “odd” number of #’s in each line. i.e., it goes 1,3,5,7,9 etc. If you try to do it with even numbers, you cannot get the same tree. Once you notice this fact, the solution becomes easy.

1..40 | ?{($_%2) -gt 0} |  %{(' '*((40-($_+1))/2)) + ('#'*$_)}

Where:

  • ? is the same as where-object
  • ?{($_%2) -gt 0} filters out the even numbers
  • (‘ ‘*((40-($_+1))/2)) generates the space needed in the front. Need half the space as previous solution

While working on #3, I accidentally created this solution which I think produced a better Xmas tree (the intent, given the season but NOT the requirement). The flaw is that it uses both even and odd numbers from 1 to 20 instead of just 20 lines of odd values.

1..20 | %{(' '*((20-$_)/2)) + ('#'*$_)}

          #
         ##
        ###
        ####
        #####
       ######
      #######
      ########
      #########
     ##########
    ###########
    ############
    #############
   ##############
  ###############
  ################
  #################
 ##################
###################
####################

Conclusion

We all do write very complex programs at work but in the end, it is the simple games like these that rejuvenate and refresh the mind while having fun doing it. There was a mention about more of these in the future which got me thinking. If you had a memory dump of a computer as a file and you had to build a dictionary of passwords from it, how would you do it in PowerShell? This was something that came-up in our discussion in light of the Meltdown and Spectre chip security flaws.

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