Given two integers diesel (liters of fuel used) and distance (kilometers driven), calculate the fuel consumption in liters per 100 kilometers.
The fuel consumption is calculated as (diesel / distance) * 100.
If the inputs are invalid (negative diesel, or zero or negative distance), return -1.0. Otherwise, return the result rounded to 2 decimal places.
Function Signature
def fuel_consumption(diesel: int, distance: int) -> float:
Rules
-
The input is invalid when
diesel < 0
or
distance <= 0
. Return
-1.0
in that case.
-
diesel = 0
with a positive
distance
is valid and gives
0.0
.
-
Round the exact value of
(diesel / distance) * 100
to the nearest hundredth. A value exactly halfway between two hundredths rounds up, so
0.125
becomes
0.13
.
-
Return a float equal to the rounded value, for example
10.0
or
2.33
.
Constraints
-
diesel
and
distance
can be any integer value (positive, zero, or negative).
Examples
Input: diesel = 50, distance = 500
Output: 10.0
(50 / 500) * 100 = 10, so the car uses 10.0 liters per 100 km.
Input: diesel = 30, distance = -10
Output: -1.0
A negative distance is invalid, so the function returns -1.0 without dividing.
Input: diesel = 1, distance = 800
Output: 0.13
(1 / 800) * 100 = 0.125, which sits exactly halfway between 0.12 and 0.13. The half-up rule gives 0.13.