Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Compose a Vehicle from Engine and Sensor Components

Model a Vehicle using Engine and Sensor classes through composition. Implement start/stop, driving fuel consumption, and sensor reads.

Python practice15 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

Design three classes — Engine, Sensor, and Vehicle — and a helper function vehicle_report(...) that composes them to simulate starting, driving, and reading sensors. Specifications: - Engine(horsepower: float, fuel_capacity: float) - start(): marks the engine running - stop(): marks the engine stopped - consume(km: float) -> float: reduces fuel based on a consumption model and returns actual kilometers the engine could drive (may be less than requested if fuel runs out) - fuel_level() -> float: returns current fuel remaining - Sensor(kind: str, value: float) - read() -> float: returns the current sensor value - Vehicle(engine: Engine, sensors: list[Sensor]) - start(), stop() - drive(km: float) -> float: attempts to drive km when engine is running; uses engine.consume and returns actual kilometers driven - read_sensors() -> list[float]: returns a list of sensor readings in insertion order - vehicle_report(horsepower, fuel_capacity, sensors_values, drive_km) -> tuple - Creates Engine and Sensor instances, composes them into a Vehicle, starts it, attempts to drive drive_km, stops the vehicle, and returns a tuple of (remaining_fuel_rounded, distance_driven_rounded, sensor_values_list). - All numeric values in the returned tuple should be rounded to 2 decimal places. - If drive_km is negative, treat it as zero (no driving). Consumption model (use exactly this): consumption_per_km = 0.2 * (horsepower / 100). If fuel runs out while driving, the engine drives as far as remaining fuel allows and fuel reaches zero. You must use composition: Vehicle should hold an Engine instance and a list of Sensor instances. The helper vehicle_report(...) is the function used in tests. Edge cases to handle: zero fuel capacity, no sensors, negative drive distances.

Task

Use composition to build a Vehicle that coordinates an Engine and multiple Sensors. Calculate fuel consumption and expose a consolidated report of remaining fuel, distance driven, and sensor readings.

Examples

Start, drive within range, and read sensors

Input

vehicle_report(100, 50, [22.5, 35.0], 100)

Output

(30.0, 100.0, [22.5, 35.0])

With horsepower 100, consumption_per_km = 0.2. Driving 100 km consumes 20 liters from 50 -> remaining 30. Sensors returned as given.

Input format

A call to vehicle_report(horsepower: float, fuel_capacity: float, sensors_values: list[float], drive_km: float).

Output format

A tuple (remaining_fuel, distance_driven, sensor_values_list) where remaining_fuel and distance_driven are rounded to 2 decimal places and sensor_values_list preserves the input order.

Constraints

- horsepower >= 0 - fuel_capacity >= 0 - sensors_values is a list (possibly empty) of floats - drive_km can be any float; negative treated as 0 - Use composition (Vehicle contains Engine and Sensors)

Samples

Sample 1

Input

vehicle_report(80, 20, [10.0], 50)

Output

(12.0, 50.0, [10.0])

consumption_per_km = 0.16, 50 km consumes 8 liters leaving 12 liters.