Last active
May 13, 2020 23:22
-
-
Save MatthewJDavis/d9aae55f4bf8833bdc1036c8a7b1b358 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #region Beginner | |
| <# | |
| The challenge is simple: get the sum of the even numbers between 1 and 100. | |
| You should be able to do this in at least 3 different ways. Show all 3 ways. You don’t need to write any functions or scripts. | |
| #> | |
| # Method 1, foreach loop | |
| $sum = 0 | |
| foreach ($n in 1..100) { | |
| if($n %2 -eq 0){ | |
| $sum = $sum + $n | |
| } | |
| } | |
| $sum | |
| # Method 2, while loop | |
| $i = 0 | |
| $sum = 0 | |
| while ($i -lt 101) { | |
| if($i %2 -eq 0) | |
| { | |
| $sum = $sum + $i | |
| } | |
| $i++ | |
| } | |
| $sum | |
| # Method 3, PowerShell pipeline with ForEach-Object cmdlet | |
| 1..100 | ForEach-Object {if($_ %2 -eq 0){ $_ }} | Measure-Object -Sum | Select-Object -Property sum | |
| #endregion | |
| #region Intermediate | |
| <# | |
| After you’ve solved the beginner level, take what you’ve learned and create a PowerShell function to get the sum and | |
| average of every X number between 1 and a user specified maximum. | |
| For example, the function should produce a result that shows the sum and average of every 4th number between 1 and 512. | |
| #> | |
| function Get-Number { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory)] | |
| [Int] | |
| $Max, | |
| [Parameter(Mandatory)] | |
| [Int] | |
| $Interval | |
| ) | |
| Write-Verbose $Max | |
| Write-Verbose $Interval | |
| $numList = [system.collections.generic.list[int]]::new() | |
| for ($i = 1; $i -lt $Max; $i = $i + $Interval) { | |
| $numList.add($i) | |
| } | |
| $numList | Measure-Object -Average -Sum | Select-Object -Property Average, Sum | |
| } | |
| #endregion | |
| #region Bonus Challenge | |
| <# | |
| If you want a bit more challenge add the following features to your function. | |
| Limit the interval to a value between 1 and 10 | |
| Add a property to your output that includes all the matching number | |
| Let the user specify the starting and stopping numbers | |
| #> | |
| function Get-NumberBonus { | |
| [CmdletBinding()] | |
| param ( | |
| # The minimum number to start from. Must be less the the max number value. | |
| [Parameter(Mandatory)] | |
| [ValidateNotNullOrEmpty()] | |
| [Int] | |
| $Min, | |
| # The maximum number where to stop. Must be greater that the min value. | |
| [Parameter(Mandatory)] | |
| [Int] | |
| $Max, | |
| # The interval to increment by. Between 1 and 10 | |
| [Parameter(Mandatory)] | |
| [ValidateRange(1,10)] | |
| [Int] | |
| $Interval | |
| ) | |
| Write-Verbose $Max | |
| Write-Verbose $Interval | |
| if($Min -ge $Max){ | |
| throw 'Min cannot be equal or greater than Max' | |
| } | |
| $numList = [system.collections.generic.list[int]]::new() | |
| for ($i = $Min; $i -lt $Max; $i = $i + $Interval) { | |
| $numList.add($i) | |
| } | |
| $result = [PSCustomObject]@{ | |
| 'Average' = ($numList | Measure-Object -Average).Average | |
| 'Sum' = ($numList | Measure-Object -Sum).Sum | |
| 'MatchedNumbers' = $numList | |
| } | |
| $result | |
| } | |
| #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment