समस्या
The adapter pattern code को, जो एक interface की उम्मीद करता है, ऐसे objects के साथ काम करने देता है जो दूसरा interface provide करते हैं. Here, एक dashboard केवल degrees Celsius समझता है, लेकिन sensors अलग-अलग units में report करते हैं.
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 ≤ रीडिंग की संख्या ≤ 1,000
−1,000 ≤ मान ≤ 1,000
उदाहरण
इनपुट:
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 वैसा ही रहता है; 212 °F = (212 − 32) × 5 / 9 = 100 °C; 273.15 K = 0 °C।
इनपुट:
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 दोनों पैमानों में बराबर है: (−40 − 32) × 5 / 9 = −40।
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
कोलन से पहले का अक्षर यूनिट बताता है।
संकेत 2
हर रीडिंग को उसके अपने सूत्र से सेल्सियस में बदलें।
संकेत 3
कोलन के बाद की संख्या को दशमलव की तरह पढ़ें, ऋणात्मक मान भी।
अपेक्षित जटिलता
समय O(रीडिंग की संख्या), अतिरिक्त स्थान O(रीडिंग की संख्या)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।