簡単 0件解決済み 正答率0%

Temperature Sensor Adapter

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

1 ≤ number of readings ≤ 1,000

−1,000 ≤ value ≤ 1,000

例

例 1
入力:
readings = ["C:21.5", "F:212", "K:273.15"]

期待される出力:
expected = [21.5, 100.0, 0.0]
入力:
std::vector<std::string> readings = {"C:21.5", "F:212", "K:273.15"};

期待される出力:
std::vector<double> expected = {21.5, 100.0, 0.0};
入力:
string[] readings = new string[] {"C:21.5", "F:212", "K:273.15"};

期待される出力:
double[] expected = new double[] {21.5, 100.0, 0.0};

解説

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

例 2
入力:
readings = ["F:-40"]

期待される出力:
expected = [-40.0]
入力:
std::vector<std::string> readings = {"F:-40"};

期待される出力:
std::vector<double> expected = {-40.0};
入力:
string[] readings = new string[] {"F:-40"};

期待される出力:
double[] expected = new double[] {-40.0};

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

The letter before the colon tells you the unit.

ヒント 2

Convert every reading to Celsius with its own formula.

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。