You are given a non-negative floating point number rounded to two decimal places celsius
, that denotes the temperature in Celsius.
You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit]
.
Return the array ans
. Answers within 10-5
of the actual answer will be accepted.
Note that:
Kelvin = Celsius + 273.15
Fahrenheit = Celsius * 1.80 + 32.00
Example 1:
Input: celsius = 36.50 Output: [309.65000,97.70000] Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
Example 2:
Input: celsius = 122.11 Output: [395.26000,251.79800] Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
Constraints:
0 <= celsius <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for temperature conversion involves directly applying the conversion formulas. We'll calculate both Celsius and Kelvin temperatures from the given Fahrenheit temperature using their respective formulas. Finally, we'll return both converted temperatures.
Here's how the algorithm would work step-by-step:
def convert_the_temperature(fahrenheit_temperature):
# Apply the Fahrenheit to Celsius conversion formula
celsius_temperature = (fahrenheit_temperature - 32) * 5 / 9
# Apply the Fahrenheit to Kelvin conversion formula
kelvin_temperature = (fahrenheit_temperature - 32) * 5 / 9 + 273.15
# Return Celsius and Kelvin temperatures
return [celsius_temperature, kelvin_temperature]
The problem asks us to convert a temperature from Celsius to Kelvin and Fahrenheit. We can solve this by applying the defined conversion formulas directly. It's a straightforward application of basic arithmetic.
Here's how the algorithm would work step-by-step:
def convert_temperature(celsius_temperature):
# Calculate Kelvin. This is a direct conversion.
kelvin_temperature = celsius_temperature + 273.15
# Calculate Fahrenheit. This is a direct conversion.
fahrenheit_temperature = (celsius_temperature * 1.8) + 32
# Return both converted temperatures
return [kelvin_temperature, fahrenheit_temperature]
Case | How to Handle |
---|---|
Input temperature is null or not a number | Return an appropriate error message or throw an exception to indicate invalid input. |
Input temperature is negative (below absolute zero) | Check if the temperature is physically possible and handle it by returning an error or an empty result. |
Input temperature is extremely large, potentially causing overflow | Use appropriate data types (e.g., double) and check for potential overflow during calculations. |
Input temperature is exactly 0 (Celsius or Fahrenheit) | The conversion formulas should correctly handle zero input according to the defined standard. |
Floating-point precision errors during conversion | Consider rounding the results to a specified number of decimal places to avoid misleading very small differences. |
The temperature scale is not valid or unrecognized | Raise error, use default value if acceptable, or attempt to resolve by other means (configuration, etc). |
Very small non-zero input temperatures | Ensure the calculation does not lead to divide-by-zero errors or significant loss of precision. |
Conversion to Kelvin yields zero or negative results | If converting to Kelvin ensure the answer is >=0, returning an error otherwise because this is physically impossible. |