모든 챌린지
쉬움 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

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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.