Tiny PowerShell Projects
This is the code repository for Tiny PowerShell Projects.
I ported it from the Manning Publications book, Tiny Python Projects, by Ken Youens-Clark:
- The Python repo is here: https://github.com/kyclark/tiny_python_projects
- The Python book is here: https://www.manning.com/books/tiny-python-projects?a_aid=youens&a_bid=b6485d52
One of the many cool aspects of this not only the approach to learning PowerShell
this way, additionally, you can head over to the Python
repo and see how it is done in that language. Could be you are here because you're a Pythonista, and want to see how PoShers do it.
Either way, it's the same puzzles implemented in both languages, and you can use the tests provided, to do Test Driven Development and prove these and your solutions work, as you make changes.
The approach
There is a directory for each chapter of the book.
There is a README to describe each exercise.
Each directory contains a test.ps1
program you can use with Invoke-Pester
to check that you have written the program correctly.
In addition, each directory has two PowerShell
scripts, one called AllTest.ps1
, and the other solution1.ps1
.
AllTest
AllTest.ps1
does something very beneficial. It looks in that chapters directory for all files that match the wildcard solution*.ps1
, and one by one copies it to a ps1
that is expected in the test.ps1
, and the last step, it calls Invoke-Pester
.
This allows you to keep the original solution supplied, and you can then create as many solutions as you'd like to test different ways to solve the puzzle. This lets you experiment with different ways to use a regex, arrays, hash tables or other approaches, in order to figure out better ways to handle this. The different solutions can sit side by side with the others and the test.ps1
can be automatically run against all.
Testing is Integral
The testing step is integral to writing and solving these challenges.
Using a "test-driven development" mentality, where you write tests before you write code, is very much recommended.
The tests should define what it means for a program to be correct, and then you write programs to satisfy the tests.
All the tests have been written for you, and you should write your own functions and tests. Practice is key.
You should run the test suite after every change to your program to ensure you are making progress!
Chapters
-
Chapter 1: How to write and test a PowerShell program How to create a PowerShell program that prints a string and takes a parameter.
-
Chapter 2: Crow's Nest: How to write a PowerShell program that accepts a single, positional argument and creates a newly formatted output string.
-
Chapter 3: Picnic Writing a PowerShell program that accepts multiple string arguments and formats the results depending on the number of items.
-
Chapter 4: Jump The Fivep): Writing a PowerShell program to encode the numerals in a given text using an algorithm called "Jump The Five." Use of a dictionary as a lookup table, characters not in the dictionary remain unchanged. Introduction to encoding/decoding text, basic idea of encryption.
-
Chapter 5: Howler Writing a PowerShell program that can process input text either from the command line or from a file.The output prints either to STDOUT or to a file. Learning about how to read/write the contents of a file.
-
Chapter 6: Word Count Writing a PowerShell program to emulate a word count program. Validates and processes multiple file inputs as well as STDIN and creates output of the counts of lines, words, and characters for each file.
-
Chapter 7: Gashlycrumb: Writing a PowerShell program that processes an input file to build a lookup table (dictionary) that is used with multiple positional arguments to translate to the values from the file.
-
Chapter 8: Apples and Bananas Writing a PowerShell program to find and replace elements in a string.
-
Chapter 9: Abuse Writing a PowerShell program to generate Shakespearean insults by randomly combining some number of adjectives with a randomly chosen noun. Learning about randomness, seeds, and testing.
-
Chapter 10: Telephone Using probabilistic and deterministic approaches to randomly mutate a string.
-
Chapter 11: Bottles of Beer Writing a PowerShell program to produce the verse to the "99 Bottles of Beer" song from a given starting point. Learning to count down, format strings, algorithm design. A focus on writing a function and unit test, exploring ways to incorporate our function to generate the verses from for loops.
-
Chapter 12: Ransom Writing a PowerShell program that will randomly capitalize letters in a given piece of text for the nefarious purpose of creating a ransom note.
-
Chapter 13: Twelve Days of Christmas Writing a PowerShell program to create the verses for "The Twelve Days of Christmas" from a given day. Learning how to write a function and the test for it, then using the function in a list to generate the output.
-
Chapter 14: The Rhymer: Writing a PowerShell program that can split off any initial consonants from a word and append a list of prefixes to create new rhyming "words." Exploration of regular expressions to handle words with no initial consonants, with one or more leading consonants, and nothing but consonants.
-
Chapter 15: The Kentucky Friar In this chapter we delve further into regular expressions, first learning how to split a string using a regex so we can separate things that look like "words" from non-words like punctuation and whitespace. Then we try to identify the word "you" (case-insensitive) to turn into "y'all" and any 2-syllable words ending in "-ing" so we can replace the final "g" with an apostrophe so that "cooking" becomes "cookin'" but "swing" would remain "swing." We then apply this to an entire body of text to Kentucky fry the words with amusing results.
-
Chapter 16: The Scrambler Writing a PowerShell program to find each "word" in a body of text and then scramble the letters such that the first and last letters remain in place, then reconstructing the text for output. Using regular expressions to split text, using
Sort-Object { Get-Random }
.