Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Temperature-conversion/Jq/temperature-conversion-1.jq
Normal file
37
Task/Temperature-conversion/Jq/temperature-conversion-1.jq
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# round(keep) takes as input any jq (i.e. JSON) number and emits a string.
|
||||
# "keep" is the desired maximum number of numerals after the decimal point,
|
||||
# e.g. 9.999|round(2) => 10.00
|
||||
def round(keep):
|
||||
tostring
|
||||
| (index("e") | if . then . else index("E") end) as $e
|
||||
| if $e then (.[0:$e] | round(keep)) + .[$e+1:]
|
||||
else index(".") as $ix
|
||||
| if $ix == null then .
|
||||
else .[0:$ix + 1] as $head
|
||||
| .[$ix+1:$ix+keep+2] as $tail
|
||||
| if ($tail|length) <= keep then $head + $tail
|
||||
else ($tail | .[length-1:] | tonumber) as $last
|
||||
| if $last < 5 then $head + $tail[0:$tail|length - 1]
|
||||
else (($head + $tail) | length) as $length
|
||||
| ($head[0:-1] + $tail)
|
||||
| (tonumber + (if $head[0:1]=="-" then -5 else 5 end))
|
||||
| tostring
|
||||
| .[0: ($ix+1+length-$length)] + "." + .[length-keep-1:-1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end;
|
||||
|
||||
def k2c: . - 273.15;
|
||||
def k2f: . * 1.8 - 459.67;
|
||||
def k2r: . * 1.8;
|
||||
|
||||
# produce a stream
|
||||
def cfr:
|
||||
if . >= 0
|
||||
then "Kelvin: \(.)", "Celsius: \(k2c|round(2))",
|
||||
"Fahrenheit: \(k2f|round(2))", "Rankine: \(k2r|round(2))"
|
||||
else error("cfr: \(.) is an invalid temperature in degrees Kelvin")
|
||||
end;
|
||||
|
||||
cfr
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
$ jq -M -r -f Temperature_conversion.jq
|
||||
21
|
||||
Kelvin: 21
|
||||
Celsius: -252.15
|
||||
Fahrenheit: -421.87
|
||||
Rankine: 37.80
|
||||
|
||||
-1
|
||||
jq: error: cfr: -1 is an invalid temperature in degrees Kelvin
|
||||
Loading…
Add table
Add a link
Reference in a new issue