
The Chairman is back with another set of math-related challenges. These types of challenges are a good way to get your brain looking at a problem from a different angle. This challenge includes 2 problems. One aimed more for intermediate level PowerShell scripters and one that might be a little more advanced.
Intermediate Level
Given a value like this:
$i = 2568
What is the total sum of all the individual integers? That is to say
2+5+6+8
Write a function that will accept a value and return the sum.
Bonus Elements
- Limit the length of the number to 10 digits
- Include Verbose output to show the operation
- Write a variation on the function that writes an object to the pipeline that shows the original number, the number of individual elements and the sum.
Advanced Level
Given a value like this:
$a = 2,5,6
What are all the unique non-sums using all possible combinations. For example:
2
5
6
2+5 = 7
2+5+6 = 13
2+6 = 8
5+6 = 11
Write a PowerShell function or set of functions that will return a unique, and ordered list of sums based on the array. The array should have no more than 9 elements and use values 1-9 in the array.
Bonus Elements
- Add parameter validation on the array length
- Include Verbose output to show the operation
As usual, please submit links to your work and solutions and not the answer itself.
Intermediate solution
I’ve completed the intermediate challenge. You can see my solution and examples at https://gist.github.com/thedavecarroll/ccece8563e9fe995ef8c760c2d3c6abe.
While working with the advanced challenge, I went down a rabbit hole with validation in the pipeline and hit an open issue. I ran out of steam and will update the gist when I can get back to this, but with pipeline support.
*without pipeline support.
Intermediate Challenge solution:
https://pcautomations.com/2021/02/10/maths-challenge/
I believe this meets all the requirements. I had a hard time creating the combinations for the last part. Had to borrow from online, credited in function. Validations will output a normal error. Hope that is OK.
https://github.com/sboggs1/IronScripter/blob/master/Another%20PowerShell%20Math%20Challenge%20-%20Feb%202021.ps1
Here’s what I came up with. https://jdhitsolutions.com/blog/powershell/8236/solving-another-powershell-math-challenge/
Intermediate and advanced solution:
https://gist.github.com/nmbell/0961a062091a266c1435087905de284e