easy +5 pts

Speed Converter

Convert speeds between km/h and m/s with a simple function.

Write a function `convert_speed(value, from_unit, to_unit)` that converts a numeric speed value between two units: `kmh` (kilometers per hour) and `ms` (meters per second). The function should return the converted value rounded to 2 decimal places. If the units are the same, return the original value (still rounded to 2 decimals). Conversion factors: - 1 km/h = 1000 m / 3600 s = 1/3.6 m/s - 1 m/s = 3.6 km/h The input units are always one of `'kmh'` or `'ms'`. You may assume the value is a non-negative number. The function must return a float.

Constraints

0 ≤ value ≤ 10^6. from_unit and to_unit are always `'kmh'` or `'ms'`. Time complexity O(1).

Example

>>> convert_speed(36, 'kmh', 'ms')
10.0
>>> convert_speed(10, 'ms', 'kmh')
36.0
>>> convert_speed(72, 'kmh', 'kmh')
72.0
>>> convert_speed(0, 'ms', 'kmh')
0.0
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

First compare from_unit and to_unit: if equal, just round.
Convert to a base unit (m/s) then convert to the target unit.
Use round(result, 2) to get the required precision.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.