Last active
January 10, 2023 23:15
-
-
Save thedavecarroll/458429ec442b109ea8d6ba46b9ed7b18 to your computer and use it in GitHub Desktop.
IronScripter Challenge - November 15, 2019 - Beginner PowerShell Function
This file contains 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
function ConvertTo-Celsius { | |
param($Fahrenheit) | |
($Fahrenheit - 32) * 5/9 | |
} | |
function ConvertTo-Fahrenheit { | |
param($Celsius) | |
($Celsius * 9/5) + 32 | |
} |
This file contains 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
function ConvertTo-Celsius { | |
param($Fahrenheit) | |
process { | |
[PsCustomObject]@{ | |
Fahrenheit = $Fahrenheit | |
Celsius = ($Fahrenheit - 32) * 5/9 | |
} | |
} | |
} | |
function ConvertTo-Fahrenheit { | |
param($Celsius) | |
process { | |
[PsCustomObject]@{ | |
Celsius = $Celsius | |
Fahrenheit = ($Celsius * 9/5) + 32 | |
} | |
} | |
} |
This file contains 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
function ConvertTo-Celsius { | |
param( | |
[Parameter(ValueFromPipeline,Position=0)] | |
[decimal[]]$Fahrenheit, | |
[ValidateRange(0,28)] | |
[int]$Precision = 1 | |
) | |
process { | |
foreach ($Temperature in $Fahrenheit) { | |
if ($Temperature -ge -459.67) { | |
[PsCustomObject]@{ | |
Fahrenheit = $Temperature | |
Celsius = [math]::Round(($Temperature - 32) * 5/9,$Precision) | |
} | |
} else { | |
'{0} {1}F is below absolute zero. Please try again.' -f $Temperature,[char]176 | Write-Warning | |
} | |
} | |
} | |
} | |
function ConvertTo-Fahrenheit { | |
param( | |
[Parameter(ValueFromPipeline,Position=0)] | |
[decimal[]]$Celsius, | |
[ValidateRange(0,28)] | |
[int]$Precision = 1 | |
) | |
process { | |
foreach ($Temperature in $Celsius) { | |
if ($Temperature -ge -273.15) { | |
[PsCustomObject]@{ | |
Celsius = $Temperature | |
Fahrenheit = [math]::Round(($Temperature * 9/5) + 32,$Precision) | |
} | |
} else { | |
'{0} {1}F is below absolute zero. Please try again.' -f $Temperature,[char]176 | Write-Warning | |
} | |
} | |
} | |
} |
This file contains 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
class Temperature { | |
[decimal]$Value | |
[ValidateSet('F','C','K')] | |
[string]$Unit | |
hidden [string]$Comment | |
Temperature () {} | |
Temperature ( $Value, $Unit) { | |
$BelowAbsoluteZero = switch ($Unit) { | |
'F' { if ($Value -lt -459.67) { $true } } | |
'C' { if ($Value -lt -273.15) { $true } } | |
'K' { if ($Value -lt 0) { $true } } | |
default { $false } | |
} | |
if ($BelowAbsoluteZero) { | |
if ($Unit -match 'F|C' ) { | |
throw ('{0} {1}{2} is below absolute zero. Please try again.' -f $Value,[char]176,$Unit) | |
} else { | |
throw ('{0} {1} is below absolute zero. Please try again.' -f $Value,$Unit) | |
} | |
} | |
$this.Unit = $Unit | |
$this.Value = $Value | |
$this.Comment = $this.GetStateChangeComment() | |
} | |
[string] ToString () { | |
if ($this.Unit -match 'F|C') { | |
return '{0} {1}{2}' -f $this.Value,[char]176,$this.Unit | |
} else { | |
return '{0} {1}' -f $this.Value,$this.Unit | |
} | |
} | |
[decimal] ToFahrenheit () { | |
$ToFahrenheit = switch ($this.Unit) { | |
'C' { ($this.Value * 9/5) + 32 } | |
'K' { ($this.Value - 273.15) * 9/5 + 32 } | |
default { $this.Value } | |
} | |
return $ToFahrenheit | |
} | |
[decimal] ToFahrenheit ([int]$Precision) { | |
$ToFahrenheit = switch ($this.Unit) { | |
'C' { ($this.Value * 9/5) + 32 } | |
'K' { ($this.Value - 273.15) * 9/5 + 32 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToFahrenheit,$Precision) | |
} | |
[string] ToFahrenheitString () { | |
return '{0} {1}{2}' -f $this.ToFahrenheit(),[char]176,'F' | |
} | |
[string] ToFahrenheitString ([int]$Precision) { | |
return '{0} {1}{2}' -f $this.ToFahrenheit($Precision),[char]176,'F' | |
} | |
[decimal] ToCelsius () { | |
$ToCelsius = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 } | |
'K' { $this.Value - 273.15 } | |
default { $this.Value } | |
} | |
return $ToCelsius | |
} | |
[decimal] ToCelsius ([int]$Precision) { | |
$ToCelsius = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 } | |
'K' { $this.Value - 273.15 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToCelsius,$Precision) | |
} | |
[string] ToCelsiusString () { | |
return '{0} {1}{2}' -f $this.ToCelsius(),[char]176,'C' | |
} | |
[string] ToCelsiusString ([int]$Precision) { | |
return '{0} {1}{2}' -f $this.ToCelsius($Precision),[char]176,'C' | |
} | |
[decimal] ToKelvin () { | |
$ToKelvin = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 + 273.15 } | |
'C' { $this.Value + 273.15 } | |
default { $this.Value } | |
} | |
return $ToKelvin | |
} | |
[decimal] ToKelvin ([int]$Precision) { | |
$ToKelvin = switch ($this.Unit) { | |
'F' { ($this.Value - 32) * 5/9 + 273.15 } | |
'C' { $this.Value + 273.15 } | |
default { $this.Value } | |
} | |
return [decimal]::Round($ToKelvin,$Precision) | |
} | |
[string] ToKelvinString () { | |
return '{0} {1}' -f $this.ToKelvin(),'K' | |
} | |
[string] ToKelvinString ([int]$Precision) { | |
return '{0} {1}' -f $this.ToKelvin($Precision),'K' | |
} | |
[object] Convert () { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit() | |
Celsius = $this.ToCelsius() | |
Kelvin = $this.ToKelvin() | |
Comment = $this.Comment | |
} | |
} | |
[object] Convert ([int]$Precision) { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit($Precision) | |
Celsius = $this.ToCelsius($Precision) | |
Kelvin = $this.ToKelvin($Precision) | |
Comment = $this.Comment | |
} | |
} | |
[object] ConvertWithString () { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit() | |
FahrenheitString = $this.ToFahrenheitString() | |
Celsius = $this.ToCelsius() | |
CelsiusString = $this.ToCelsiusString() | |
Kelvin = $this.ToKelvin() | |
KelvinString = $this.ToKelvinString() | |
Comment = $this.Comment | |
} | |
} | |
[object] ConvertWithString ([int]$Precision) { | |
return [PsCustomObject]@{ | |
Fahrenheit = $this.ToFahrenheit($Precision) | |
FahrenheitString = $this.ToFahrenheitString($Precision) | |
Celsius = $this.ToCelsius($Precision) | |
CelsiusString = $this.ToCelsiusString($Precision) | |
Kelvin = $this.ToKelvin($Precision) | |
KelvinString = $this.ToKelvinString($Precision) | |
Comment = $this.Comment | |
} | |
} | |
[string] GetStateChangeComment () { | |
if ($this.ToCelsius() -eq -273.15) { | |
$GetStateChangeComment = 'Absolute Zero' | |
} else { | |
$Celsius = $this.ToCelsius(0) | |
$GetStateChangeComment = switch ($Celsius) { | |
0 { 'Freezing Point' ; break } | |
3 { 'Near Freezing Point' ; break } | |
100 { 'Boiling Point' ; break } | |
233 { 'Fahrenheit 451 – the temperature at which book paper catches fire, and burns...' ; break } | |
5505 { 'Surface of the Sun' ; break } | |
} | |
} | |
return $GetStateChangeComment | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Catching a value below absolute zero is a great call. I wouldn't have thought of that.