For many people, it is back-to-school time. This time of years always makes The Chairman nostalgic, so he has commissioned a new set of PowerShell scripting challenges. These tasks are aimed at beginner level PowerShell scripters, with a few bonuses targeted toward intermediate users. Although everyone is welcome and encouraged to try their hand.
The Challenges
You need to create simple PowerShell functions to meet the following goals. Each task should result in a separate function.
- Given 2 lengths of a right triangle, calculate the missing length. You will need to let the user specify which sides (traditionally A,B and C) of the triangle they are specifying.
- Calculate the area of a circle with a given diameter.
- Calculate the volume of a sphere with a given diameter.
- Calculate the volume of a cylinder from a given diameter and height.
- Calculate the factorial of an integer.
Intermediate Additions
For those of you with a bit more PowerShell experience, your functions should include parameter validation and error handling. Your function should have an option to write a rich object to the pipeline. For example, the first challenge might have you write an object with properties of A, B, and C. Finally, you should be able to create a simple module with a manifest.
As with all challenges, please submit links to your work in the comments and not the code itself.
And it is OK to ask your kids for help. 😉
The first requirement is not possible. Two side of a triangle are not enough to define a unique triangle.
I may not have been clear. Traditionally, the sides are A and B and the hypotenuse is C. If you know C and A you can calculate B. If you know A and B you can calculate C. That’s the challenge.
Jeff,
Thank you for your response. I think the method you are referring to only works for right triangles. For acute or obtuse triangles, I think you would need to know the angle between the two sides to determine the hypotenuse.
I’ve tried to create a module and manifest, but I’ve never done it before so I’m not sure if it is right. The module loads when I import it, so I think I’m on the right track. This was really good practice!
https://github.com/PaulHenry678/IronScripter
-PH
Yes. I made an assumption about right triangles and assumed you could all read my mind. 😉
It should probably be noted that this (Pythagoras theorem) only applies to right-angled triangles?
I believe you are right. I was trying to keep it simple and made a mental assumption that people would know I meant right triangles.
Thanks for the challenge! What I have so far:
https://github.com/tjgruber/ironscripterchallenges/blob/master/IronScripterChallenge_2020-08-31.ps1
Hopefully I find more time to add more intermediate additions to it.
Awesome! I never knew about `[ValidateRange(“Positive”)]` – thank you!
I really enjoyed this challenge! However, hate to admit how hard it was for me to remember the basic mathematics required before even attempting to convert it to PowerShell! =D
Here’s attempt:
https://github.com/Deanlw/PowerShell/blob/master/IronScripterChallenge%20-%20Back%20To%20School.ps1
I’d encourage you to come up with function names that use standard verbs.
Thanks Jeff, I was very torn on the topic of the function names!
On one hand I knew I should be using standard verbs, on the other I wanted to make the function very easy to understand for the reader…
One thing you can do is use a standard name, even if it feels a bit awkward. Then create an alias that is more user-friendly. This is useful when packaging functions into a module where you really want to follow naming standards.
Well, that was a fun walk down memories-of-math-class lane – here’s my attempt!
https://gist.github.com/JPRuskin/142d8bf376f2fcbe0f4c54089149ee1a
I blogged about my solution at https://jdhitsolutions.com/blog/powershell/7680/friday-fun-back-to-school-with-powershell/
I have been working on this solution since the challenge was presented, but wasn’t able to put a considerable chunk of effort into it at any given time.
A few of the learning points were definitely new to me, such as ValidateRangeKind and the [double]::NaN. I knew of the [math] class, but I haven’t used it that often.
Overall, this was another fun challenge.
Here is my solution: https://bit.ly/2ZEhLKM