0
\$\begingroup\$

Problem statement:

Write a program that converts all given temperatures from a given input temperature scale to a given output temperature scale. The temperature scales to be supported are Kelvin, Celsius, Fahrenheit, Rankine, Delisle, Newton, Rømer, Réaumur.

Synopsis: tempconv INPUT_SCALE OUTPUT_SCALE [TEMPERATURE]...

The INPUT_SCALE and OUTPUT_SCALE shall be given as follows:

  • K for Kelvin
  • C for Celsius
  • F for Fahrenheit
  • R for Rankine
  • D for Delisle
  • N for Newton
  • for Rømer
  • for Réaumur.

Example:

tempconv K C 0 273.15 373.15
-273.15
0.0
100.0

My solution in Typescript:

class TempConv {
    readonly toKelvin: (other: number) => number;
    readonly fromKelvin: (kelvin: number) => number;

    static readonly converters = new Map<String, TempConv>();

    static readonly KELVIN     = new TempConv(kelvin     => kelvin,                           kelvin => kelvin,                            "K", "k", "Kelvin");
    static readonly CELSIUS    = new TempConv(celsius    => celsius + 273.15,                 kelvin => kelvin - 273.15,                   "°C", "C", "c", "Celsius");
    static readonly DELISLE    = new TempConv(delisle    => 373.15 - delisle * 2 / 3,         kelvin => (373.15 - kelvin) * 3 / 2,         "°De", "De", "DE", "de", "Delisle");
    static readonly FAHRENHEIT = new TempConv(fahrenheit => (fahrenheit + 459.67) * 5 / 9,    kelvin => kelvin * 9 / 5 - 459.67,           "°F", "F", "f", "Fahrenheit");
    static readonly NEWTON     = new TempConv(newton     => newton * 100 / 33 + 273.15,       kelvin => (kelvin - 273.15) * 33 / 100,      "°N", "N", "n", "Newton");
    static readonly RANKINE    = new TempConv(rankine    => rankine * 5 / 9,                  kelvin => kelvin * 9 / 5,                    "°R", "R", "r", "Rankine");
    static readonly RÉAUMUR    = new TempConv(réaumur    => réaumur * 5 / 4 + 273.15,         kelvin => (kelvin - 273.15) * 4 / 5,         "°Ré", "°Re", "Ré", "RÉ", "ré", "Re", "RE", "re", "Réaumur");
    static readonly RØMER      = new TempConv(rømer      => (rømer - 7.5) * 40 / 21 + 273.15, kelvin => (kelvin - 273.15) * 21 / 40 + 7.5, "°Rø", "°Ro", "Rø", "RØ", "rø", "Ro", "RO", "ro", "Rømer");

    constructor(toKelvin: (other: number) => number, fromKelvin: (kelvin: number) => number, ...names: String[]) {
        this.toKelvin = toKelvin;
        this.fromKelvin = fromKelvin;
        for (const name of names)
            TempConv.converters.set(name, this);
    }
}

const toKelvin = TempConv.converters.get(process.argv[2]).toKelvin;
const fromKelvin = TempConv.converters.get(process.argv[3]).fromKelvin;
process.argv.slice(4).forEach(temp => process.stdout.write(fromKelvin(toKelvin(parseFloat(temp))).toString() + "\n"));
\$\endgroup\$
1
  • \$\begingroup\$ Looks good to me, three tiny additions: 1) use string instead of String, 2) maybe ...names: [ string, ...string[] ] to guarantee at least one name, 3) maybe args validation? \$\endgroup\$
    – alx
    Sep 11, 2022 at 23:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.