algoadvance

You are given a non-negative floating point number celsius, that denotes the temperature in Celsius. Your task is to convert the temperature from Celsius to both Kelvin and Fahrenheit, and return in a list [kelvin, fahrenheit].

The conversions are defined as follows:

Clarifying Questions

  1. Input Range: What is the range for the input celsius?
    • Response: The input celsius is a non-negative floating point number.
  2. Output Precision: Is there a specific precision required for the output values?
    • Response: The problem statement does not specify required precision, so we can assume standard floating point accuracy.
  3. Edge Cases: Are there any specific edge cases we should be aware of?
    • Response: Considering the input is non-negative, edge cases could include celsius = 0 and very large values of celsius.

Strategy

To implement the solution:

  1. Read the input value celsius.
  2. Convert celsius to kelvin using the formula kelvin = celsius + 273.15.
  3. Convert celsius to fahrenheit using the formula fahrenheit = celsius * 1.80 + 32.00.
  4. Return the result as a list [kelvin, fahrenheit].

By following these steps, we can ensure accurate conversion from Celsius to both Kelvin and Fahrenheit.

Code

def convertTemperature(celsius: float) -> list:
    kelvin = celsius + 273.15
    fahrenheit = celsius * 1.80 + 32.00
    return [kelvin, fahrenheit]

# Example usage:
print(convertTemperature(0))        # Expected output: [273.15, 32.00]
print(convertTemperature(100))      # Expected output: [373.15, 212.00]
print(convertTemperature(37.5))     # Expected output: [310.65, 99.50]

Time Complexity

The time complexity for this problem is O(1) because the operations we are performing (addition and multiplication) are constant time operations. We are performing a fixed number of operations regardless of the size of the input. Thus, the solution is effectively constant time.

Try our interview co-pilot at AlgoAdvance.com