Volver a los desafíos
Fácil 0 resueltos 0% de aceptación

Temperature Sensor Adapter

Adapt readings from Fahrenheit, Kelvin and Celsius sensors to one Celsius interface.

Lo que aprenderás

  • Wrap different formats behind one interface
  • Convert Fahrenheit and Kelvin to Celsius
  • Parse a unit prefix and a value

Problema

Ver traducción

The adapter pattern lets code that expects one interface work with objects that provide another. Here, a dashboard only understands degrees Celsius, but the sensors report in different units.

Each reading is a unit letter, a colon and a value:

  • C:<value>: already Celsius.
  • F:<value>: Fahrenheit, converted with (F − 32) × 5 / 9.
  • K:<value>: Kelvin, converted with K − 273.15.

Implement sensor_adapter(readings).

Input

readings: The raw sensor readings.

Output

Return every reading in Celsius, in order. Answers within 1e-6 are accepted.

Restricciones

1 ≤ number of readings ≤ 1,000

−1,000 ≤ value ≤ 1,000

Ejemplos

Ejemplo 1
Entrada:
readings = ["C:21.5", "F:212", "K:273.15"]

Salida esperada:
expected = [21.5, 100.0, 0.0]
Entrada:
std::vector<std::string> readings = {"C:21.5", "F:212", "K:273.15"};

Salida esperada:
std::vector<double> expected = {21.5, 100.0, 0.0};
Entrada:
string[] readings = new string[] {"C:21.5", "F:212", "K:273.15"};

Salida esperada:
double[] expected = new double[] {21.5, 100.0, 0.0};

Explicación

21.5 °C stays; 212 °F is (212 − 32) × 5 / 9 = 100 °C; 273.15 K is 0 °C.

Ejemplo 2
Entrada:
readings = ["F:-40"]

Salida esperada:
expected = [-40.0]
Entrada:
std::vector<std::string> readings = {"F:-40"};

Salida esperada:
std::vector<double> expected = {-40.0};
Entrada:
string[] readings = new string[] {"F:-40"};

Salida esperada:
double[] expected = new double[] {-40.0};

Explicación

−40 is the same in both scales: (−40 − 32) × 5 / 9 = −40.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

The letter before the colon tells you the unit.

Pista 2

Convert every reading to Celsius with its own formula.

Pista 3

Parse the number after the colon as a decimal, including negative values.

Complejidad esperada

O(number of readings) time, O(number of readings) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.