Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function New-Planet
- {
- param
- (
- [string]$Name,
- [int]$Density,
- [int]$Radius
- )
- $planet = New-Object -TypeName PSObject
- $planet | Add-Member -MemberType NoteProperty -Name Name -Value $Name
- $planet | Add-Member -MemberType NoteProperty -Name Density -Value $Density
- $planet | Add-Member -MemberType NoteProperty -Name Radius -Value $Radius
- $planet | Add-Member -MemberType NoteProperty -Name Volume -Value ((4/3) * [math]::pi * [math]::pow($radius, 3))
- $planet | Add-Member -MemberType NoteProperty -Name Mass -Value ($planet.Volume * $planet.Density)
- $planet
- }
- Function Get-GravitationalForce
- {
- param
- (
- [double]$G = 6.67E-11,
- [Parameter(Mandatory=$true)][double]$mass,
- [Parameter(Mandatory=$true, ValueFromPipeline=$true)][PSObject]$Planet
- )
- # Format the result of the equation with no more than 3 decimal places
- $gravitationalForce = "{0:N3}" -f
- ($G * (($mass * $Planet.Mass) / [math]::pow($Planet.Radius, 2)))
- Write-Output "$($Planet.Name): $gravitationalForce"
- }
- Function SolveForReddit
- {
- param
- (
- $inputString
- )
- $lines = $inputString -split '[\r\n]'
- if($lines.Count -lt 3)
- {
- Write-Error "Must have at least 3 lines."
- }
- # Foreach planet line, split the inputs to create a new planet,
- # and feed that planet into the gravitational force function.
- $lines[2..$lines.Count] |
- % {
- $input = $_ -split ", "
- New-Planet -Name $input[0] -Radius $input[1] -Density $input[2] |
- % { Get-GravitationalForce -mass $lines[0] -Planet $_ }
- }
- }
Add Comment
Please, Sign In to add comment