2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,25 +1,26 @@
{{omit from|Lilypond}}
There are quite a number of temperature scales. <br>
For this task we will concentrate on 4 of the perhaps best-known ones: [[wp:Kelvin|Kelvin]], [[wp:Degree Celsius|Celsius]], [[wp:Fahrenheit|Fahrenheit]] and [[wp:Degree Rankine|Rankine]].
There are quite a number of temperature scales. For this task we will concentrate on four of the perhaps best-known ones:
[[wp:Kelvin|Kelvin]], [[wp:Degree Celsius|Celsius]], [[wp:Fahrenheit|Fahrenheit]], and [[wp:Degree Rankine|Rankine]].
The Celsius and Kelvin scales have the same magnitude, but different null points.
:0 degrees Celsius corresponds to '''273.15''' kelvin.
:0 kelvin is absolute zero.
: 0 degrees Celsius corresponds to 273.15 kelvin.
: 0 kelvin is absolute zero.
The Fahrenheit and Rankine scales also have the same magnitude,
but different null points.
The Fahrenheit and Rankine scales also have the same magnitude, but different null points.
:0 degrees Fahrenheit corresponds to '''459.67''' degrees Rankine.
:0 degrees Rankine is absolute zero.
: 0 degrees Fahrenheit corresponds to 459.67 degrees Rankine.
: 0 degrees Rankine is absolute zero.
The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of '''5 : 9'''.
The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of 5 : 9.
Write code that accepts a value of kelvin, converts it
to values on the three other scales and prints the result.
For instance:
;Task
Write code that accepts a value of kelvin, converts it to values of the three other scales, and prints the result.
;Example:
<pre>
K 21.00
@ -29,3 +30,4 @@ F -421.87
R 37.80
</pre>
<br><br>

View file

@ -0,0 +1 @@
CONVERT{,(-273.15),(R-459.67),(R×9÷5)}

View file

@ -0,0 +1,2 @@
CONVERT 21
21 ¯252.15 ¯421.87 37.8

View file

@ -7,7 +7,7 @@ BEGIN {
break
}
if (K < 0) {
print("K must be > 0")
print("K must be >= 0")
continue
}
printf("K = %.2f\n",K)

View file

@ -0,0 +1,116 @@
use framework "Foundation" -- Yosemite onwards, for the toLowerCase() function
-- Kelvin to other scale
-- kelvinAs :: ScaleName -> Num -> Num
on kelvinAs(strOtherScale, n)
heatBabel(n, "Kelvin", strOtherScale)
end kelvinAs
-- More general conversion
-- heatBabel :: n -> ScaleName -> ScaleName -> Num
on heatBabel(n, strFromScale, strToScale)
set ratio to 9 / 5
set cels to 273.15
set fahr to 459.67
script reading
on lambda(x, strFrom)
if strFrom = "k" then
x as real
else if strFrom = "c" then
x + cels
else if strFrom = "f" then
(fahr + x) * ratio
else
x / ratio
end if
end lambda
end script
script writing
on lambda(x, strTo)
if strTo = "k" then
x
else if strTo = "c" then
x - cels
else if strTo = "f" then
(x * ratio) - fahr
else
x * ratio
end if
end lambda
end script
writing's lambda(reading's lambda(n, ¬
toLowerCase(text 1 of strFromScale)), ¬
toLowerCase(text 1 of strToScale))
end heatBabel
-- TEST
on kelvinTranslations(n)
script translations
on lambda(x)
{x, kelvinAs(x, n)}
end lambda
end script
map(translations, {"K", "C", "F", "R"})
end kelvinTranslations
on run
script tabbed
on lambda(x)
intercalate(tab, x)
end lambda
end script
intercalate(linefeed, map(tabbed, kelvinTranslations(21)))
end run
-- GENERIC LIBRARY FUNCTIONS
-- toLowerCase :: String -> String
on toLowerCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLowerCase
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -0,0 +1,16 @@
import System.Exit (die)
import Control.Monad (mapM_)
main = do
putStrLn "Please enter temperature in kelvin: "
input <- getLine
let kelvin = read input
if kelvin < 0.0
then die "Temp cannot be negative"
else mapM_ putStrLn $ convert kelvin
convert :: Double -> [String]
convert n = zipWith (++) labels nums
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (*1.8)]
nums = (show . ($n)) <$> conversions

View file

@ -0,0 +1,24 @@
{-# LANGUAGE LambdaCase #-}
import System.Exit (die)
import Control.Monad (mapM_)
import Control.Error.Safe (tryAssert, tryRead)
import Control.Monad.Trans (liftIO)
import Control.Monad.Trans.Except
main = putStrLn "Please enter temperature in kelvin: " >>
runExceptT getTemp >>=
\case Right x -> mapM_ putStrLn $ convert x
Left err -> die err
convert :: Double -> [String]
convert n = zipWith (++) labels nums
where labels = ["kelvin: ", "celcius: ", "farenheit: ", "rankine: "]
conversions = [id, subtract 273, subtract 459.67 . (1.8 *), (1.8 *)]
nums = (show . ($ n)) <$> conversions
getTemp :: ExceptT String IO Double
getTemp = do
t <- liftIO getLine >>= tryRead "Could not read temp"
tryAssert "Temp cannot be negative" (t>=0)
return t

View file

@ -1,18 +0,0 @@
main = do
putStrLn "Please enter temperature in kelvin: "
input <- getLine
let kelvin = read input :: Double
if
kelvin < 0.0
then
putStrLn "error"
else
let
celsius = kelvin - 273.15
fahrenheit = kelvin * 1.8 - 459.67
rankine = kelvin * 1.8
in do
putStrLn ("kelvin: " ++ show kelvin)
putStrLn ("celsius: " ++ show celsius)
putStrLn ("fahrenheit: " ++ show fahrenheit)
putStrLn ("rankine: " ++ show rankine)

View file

@ -0,0 +1,38 @@
(() => {
'use strict';
let kelvinTranslations = k => ['K', 'C', 'F', 'R']
.map(x => [x, heatBabel(k, 'K', x)]);
// heatBabel :: Num -> ScaleName -> ScaleName -> Num
let heatBabel = (n, strFromScale, strToScale) => {
let ratio = 9 / 5,
cels = 273.15,
fahr = 459.67,
id = x => x,
readK = {
k: id,
c: x => cels + x,
f: x => (fahr + x) * ratio,
r: x => x / ratio
},
writeK = {
k: id,
c: x => x - cels,
f: x => (x * ratio) - fahr,
r: x => ratio * x
};
return writeK[strToScale.charAt(0).toLowerCase()](
readK[strFromScale.charAt(0).toLowerCase()](n)
).toFixed(2);
};
// TEST
return kelvinTranslations(21)
.map(([s, n]) => s + (' ' + n)
.slice(-10))
.join('\n');
})();

View file

@ -0,0 +1,6 @@
tempConvert := proc(k)
seq(printf("%c: %.2f\n", StringTools[UpperCase](substring(i, 1)), convert(k, temperature, kelvin, i)), i in [kelvin, Celsius, Fahrenheit, Rankine]);
return NULL;
end proc:
tempConvert(21);

View file

@ -0,0 +1,12 @@
my %scale =
Celcius => { factor => 1 , offset => -273.15 },
Rankine => { factor => 1.8, offset => 0 },
Fahrenheit => { factor => 1.8, offset => -459.67 },
;
my $kelvin = +prompt "Enter a temperature in Kelvin: ";
die "No such temperature!" if $kelvin < 0;
for %scale.sort {
printf "%12s: %7.2f\n", .key, $kelvin * .value<factor> + .value<offset>;
}

View file

@ -0,0 +1,27 @@
function Convert-Kelvin
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[double]
$InputObject
)
Process
{
foreach ($kelvin in $InputObject)
{
[PSCustomObject]@{
Kelvin = $kelvin
Celsius = $kelvin - 273.15
Fahrenheit = $kelvin * 1.8 - 459.67
Rankine = $kelvin * 1.8
}
}
}
}

View file

@ -0,0 +1 @@
21, 100 | Convert-Kelvin

View file

@ -1,117 +1,116 @@
/*REXX program converts temperatures for a number of temperature scales.*/
numeric digits 120 /*be able to support huge numbers*/
parse arg tList /*get specified temperature lists*/
/*REXX program converts temperatures for a number (8) of temperature scales. */
numeric digits 120 /*be able to support some huge numbers.*/
parse arg tList /*get the specified temperature list. */
do until tList='' /*process a list of temperatures.*/
parse var tList x ',' tList /*temps are separated by commas. */
x=translate(x,'((',"[{") /*support other grouping symbols.*/
x=space(x); parse var x z '(' /*handle any comments (if any). */
parse upper var z z ' TO ' ! . /*separate the TO option from #*/
if !=='' then !='ALL'; all=!=='ALL' /*allow specification of "TO" opt*/
if z=='' then call serr 'no arguments were specified.'
_=verify(z, '+-.0123456789') /*a list of valid number thingys.*/
do until tList='' /*process the list of temperatures. */
parse var tList x ',' tList /*temps are separated by commas. */
x=translate(x,'((',"[{") /*support other grouping symbols. */
x=space(x); parse var x z '(' /*handle any comments (if any). */
parse upper var z z ' TO ' ! . /*separate the TO option from number.*/
if !=='' then !='ALL'; all=!=='ALL' /*allow specification of "TO" opt*/
if z=='' then call serr "no arguments were specified." /*oops-ay. */
_=verify(z, '+-.0123456789') /*list of valid numeral/number thingys.*/
n=z
if _\==0 then do
if _==1 then call serr 'illegal temperature:' z
n=left(z, _-1) /*pick off the number (hopefully)*/
u=strip(substr(z, _)) /*pick off the temperature unit. */
n=left(z, _-1) /*pick off the number (hopefully). */
u=strip(substr(z, _)) /*pick off the temperature unit. */
end
else u='k' /*assume kelvin as per task req.*/
else u='k' /*assume kelvin as per task requirement*/
if \datatype(n,'N') then call serr 'illegal number:' n
if \all then do /*there is a TO ααα scale.*/
call name ! /*process the TO abbreviation*/
!=sn /*assign the full name to ! */
end /* ! now contains scale full name*/
call name u /*allow alternate scale spellings*/
if \datatype(n, 'N') then call serr 'illegal number:' n
if \all then do /*is there is a TO ααα scale? */
call name ! /*process the TO abbreviation. */
!=sn /*assign the full name to ! */
end /*!: now contains temperature full name*/
call name u /*allow alternate scale (miss)spellings*/
select /*convert ──► °F temperatures. */
select /*convert ──► °Fahrenheit temperatures.*/
when sn=='CELSIUS' then F=n * 9/5 + 32
when sn=='DELISLE' then F=212 -(n * 6/5)
when sn=='DELISLE' then F=212 -(n * 6/5)
when sn=='FAHRENHEIT' then F=n
when sn=='KELVIN' then F=n * 9/5 - 459.67
when sn=='NEWTON' then F=n * 60/11 + 32
when sn=='RANKINE' then F=n - 459.67 /*a single R is taken as Rankine.*/
when sn=='RANKINE' then F=n - 459.67 /*a single R is taken as Rankine.*/
when sn=='REAUMUR' then F=n * 9/4 + 32
when sn=='ROMER' then F=(n-7.5) * 27/4 + 32
otherwise call serr 'illegal temperature scale: ' u
end /*select*/
K = (F + 459.67) * 5/9 /*compute temperature to kelvins.*/
say right(' ' x, 79, "") /*show original value &scale,sep.*/
K = (F + 459.67) * 5/9 /*compute temperature to kelvins. */
say right(' ' x, 79, "") /*show the original value, scale, sep. */
if all | !=='CELSIUS' then say $( ( F - 32 ) * 5/9 ) 'Celsius'
if all | !=='DELISLE' then say $( ( 212 - F ) * 5/6 ) 'Delisle'
if all | !=='FAHRENHEIT' then say $( F ) 'Fahrenheit'
if all | !=='KELVIN' then say $( K ) 'kelvin's(K)
if all | !=='NEWTON' then say $( ( F - 32 ) * 11/60 ) 'Newton'
if all | !=='RANKINE' then say $( F + 349.67 ) 'Rankine'
if all | !=='RANKINE' then say $( F + 459.67 ) 'Rankine'
if all | !=='REAUMUR' then say $( ( F - 32 ) * 4/9 ) 'Reaumur'
if all | !=='ROMER' then say $( ( F - 32 ) * 7/24 + 7.5 ) 'Romer'
if all | !=='ROMER' then say $( ( F - 32 ) * 4/27 + 7.5 ) 'Romer'
end /*until*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────$ subroutine────────────────────────*/
$: procedure; showDig=8 /*only show 8 significant digits.*/
_=format(arg(1), , showDig)/1 /*format # 8 digs past dec point.*/
p=pos(.,_) /*find position of decimal point.*/
/* [↓] align integers with FP #s.*/
if p==0 then _=_ || left('',5+showDig+1) /*no decimal point.*/
else _=_ || left('',5+showDig-length(_)+p) /*has " " */
return right(_,50) /*return the re-formatted arg. */
/*──────────────────────────────────name subroutine─────────────────────*/
name: parse arg y /*abbreviations ──► shortname. */
yU=translate(y,'eE',"éÉ"); upper yU /*uppercase version of temp unit.*/
if left(yU,7)=='DEGREES' then yU=substr(yU,8) /*redundant "degrees"? */
if left(yU,6)=='DEGREE' then yU=substr(yU,7) /* " "degree" ? */
yU=strip(yU) /*elide blanks at ends.*/
_=length(yU) /*obtain the yU length.*/
if right(yU,1)=='S' & _>1 then yU=left(yU,_-1) /*elide trailing plural*/
select /*abbreviations ──► shortname. */
when abbrev('CENTIGRADE' , yU) |,
abbrev('CENTRIGRADE', yU) |, /* 50% misspelled.*/
abbrev('CETIGRADE' , yU) |, /* 50% misspelled.*/
abbrev('CENTINGRADE', yU) |,
abbrev('CENTESIMAL' , yU) |,
abbrev('CELCIU' , yU) |, /* 82% misspelled.*/
abbrev('CELCIOU' , yU) |, /* 4% misspelled.*/
abbrev('CELCUI' , yU) |, /* 4% misspelled.*/
abbrev('CELSUI' , yU) |, /* 2% misspelled.*/
abbrev('CELCEU' , yU) |, /* 2% misspelled.*/
abbrev('CELCU' , yU) |, /* 2% misspelled.*/
abbrev('CELISU' , yU) |, /* 1% misspelled.*/
abbrev('CELSU' , yU) |, /* 1% misspelled.*/
abbrev('CELSIU' , yU) then sn='CELSIUS'
when abbrev('DELISLE' , yU,2) then sn='DELISLE'
when abbrev('FARENHEIT' , yU) |, /* 39% misspelled.*/
abbrev('FARENHEIGHT', yU) |, /* 15% misspelled.*/
abbrev('FARENHITE' , yU) |, /* 6% misspelled.*/
abbrev('FARENHIET' , yU) |, /* 3% misspelled.*/
abbrev('FARHENHEIT' , yU) |, /* 3% misspelled.*/
abbrev('FARINHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FARENHIGHT' , yU) |, /* 2% misspelled.*/
abbrev('FAHRENHIET' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FEHRENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERINHEIGHT', yU) |, /* 1% misspelled.*/
abbrev('FARIENHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARINHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARANHITE' , yU) |, /* 1% misspelled.*/
abbrev('FAHRENHEIT' , yU) then sn='FAHRENHEIT'
when abbrev('KALVIN' , yU) |, /* 27% misspelled.*/
abbrev('KERLIN' , yU) |, /* 18% misspelled.*/
abbrev('KEVEN' , yU) |, /* 9% misspelled.*/
abbrev('KELVIN' , yU) then sn='KELVIN'
when abbrev('NEUTON' , yU) |, /*100% misspelled.*/
abbrev('NEWTON' , yU) then sn='NEWTON'
when abbrev('RANKINE' , yU, 1) then sn='RANKINE'
when abbrev('REAUMUR' , yU, 2) then sn='REAUMUR'
when abbrev('ROEMER' , yU, 2) |,
abbrev('ROMER' , yU, 2) then sn='ROMER'
otherwise call serr 'illegal temperature scale:' y
end /*select*/
return
/*──────────────────────────────────one─liner subroutines───────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
serr: say; say '***error!***'; say; say arg(1); say; exit 13
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1)
serr: say; say '***error!***'; say; say arg(1); say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
$: procedure; showDig=8 /*only show eight significant digits.*/
_=format(arg(1), , showDig) / 1 /*format number 8 digs past dec, point.*/
p=pos(., _); L=length(_) /*find position of the decimal point. */
/* [↓] align integers with FP numbers.*/
if p==0 then _=_ || left('',5+showDig+1) /*the number has no decimal point. */
else _=_ || left('',5+showDig-L+p) /* " " " a " " */
return right(_, 50) /*return the re-formatted number (arg).*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
name: parse arg y /*abbreviations ──► shortname.*/
yU=translate(y, 'eE', "éÉ"); upper yU /*uppercase the temperature unit*/
if left(yU, 7)=='DEGREES' then yU=substr(yU, 8) /*redundant "degrees" after #? */
if left(yU, 6)=='DEGREE' then yU=substr(yU, 7) /* " "degree" " " */
yU=strip(yU) /*elide blanks at front and back*/
_=length(yU) /*obtain the yU length. */
if right(yU, 1)=='S' & _>1 then yU=left(yU, _ -1) /*elide trailing plural, if any.*/
select /*abbreviations ──► shortname.*/
when abbrev('CENTIGRADE' , yU) |,
abbrev('CENTRIGRADE', yU) |, /* 50% misspelled.*/
abbrev('CETIGRADE' , yU) |, /* 50% misspelled.*/
abbrev('CENTINGRADE', yU) |,
abbrev('CENTESIMAL' , yU) |,
abbrev('CELCIU' , yU) |, /* 82% misspelled.*/
abbrev('CELCIOU' , yU) |, /* 4% misspelled.*/
abbrev('CELCUI' , yU) |, /* 4% misspelled.*/
abbrev('CELSUI' , yU) |, /* 2% misspelled.*/
abbrev('CELCEU' , yU) |, /* 2% misspelled.*/
abbrev('CELCU' , yU) |, /* 2% misspelled.*/
abbrev('CELISU' , yU) |, /* 1% misspelled.*/
abbrev('CELSU' , yU) |, /* 1% misspelled.*/
abbrev('CELSIU' , yU) then sn='CELSIUS'
when abbrev('DELISLE' , yU,2) then sn='DELISLE'
when abbrev('FARENHEIT' , yU) |, /* 39% misspelled.*/
abbrev('FARENHEIGHT', yU) |, /* 15% misspelled.*/
abbrev('FARENHITE' , yU) |, /* 6% misspelled.*/
abbrev('FARENHIET' , yU) |, /* 3% misspelled.*/
abbrev('FARHENHEIT' , yU) |, /* 3% misspelled.*/
abbrev('FARINHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FARENHIGHT' , yU) |, /* 2% misspelled.*/
abbrev('FAHRENHIET' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIGHT', yU) |, /* 2% misspelled.*/
abbrev('FEHRENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERENHEIT' , yU) |, /* 2% misspelled.*/
abbrev('FERINHEIGHT', yU) |, /* 1% misspelled.*/
abbrev('FARIENHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARINHEIT' , yU) |, /* 1% misspelled.*/
abbrev('FARANHITE' , yU) |, /* 1% misspelled.*/
abbrev('FAHRENHEIT' , yU) then sn='FAHRENHEIT'
when abbrev('KALVIN' , yU) |, /* 27% misspelled.*/
abbrev('KERLIN' , yU) |, /* 18% misspelled.*/
abbrev('KEVEN' , yU) |, /* 9% misspelled.*/
abbrev('KELVIN' , yU) then sn='KELVIN'
when abbrev('NEUTON' , yU) |, /*100% misspelled.*/
abbrev('NEWTON' , yU) then sn='NEWTON'
when abbrev('RANKINE' , yU, 1) then sn='RANKINE'
when abbrev('REAUMUR' , yU, 2) then sn='REAUMUR'
when abbrev('ROEMER' , yU, 2) |,
abbrev('ROMER' , yU, 2) then sn='ROMER'
otherwise call serr 'illegal temperature scale:' y
end /*select*/
return