Start Your Journey with Linux Command Line
4.3.1.10 LAB |
A car's fuel consumption may be expressed in many different ways. For example, in Europe, it is shown as the amount of fuel consumed per 100 kilometers.
In the USA, it is shown as the number of miles traveled by a car using one gallon of fuel.
Your task is to write a pair of functions converting l/100km into mpg "mile per gallon", and vice versa.
| Sandbox |
are named liters_100km_to_miles_gallon and miles_gallon_to_liters_100km respectively;
take one argument (the value corresponding to their names)
Complete the code in the editor.
Run your code and check whether your output is the same as ours.
Here is some information to help you:
1 American mile = 1609.344 metres; 1 mile = "1.60934" meters
1 American gallon = 3.785411784 litres.
=================================================================================
LAB 4.3.1.10 asks you to convert between two common ways of expressing a car's fuel consumption. In Europe fuel use is reported as liters consumed per 100 kilometers, while in the United States it is shown as miles traveled per gallon of fuel, abbreviated mpg. Converting between these units requires knowing the relationships between meters and miles, and between liters and gallons: one American mile equals 1609.344 meters, and one American gallon equals 3.785411784 liters.
The lab asks for two functions. The first, liters_100km_to_miles_gallon, takes the liters per 100 km and returns the equivalent fuel economy in miles per gallon. The second, miles_gallon_to_liters_100km, does the reverse, taking an mpg value and returning the liters consumed per 100 km. Writing these two reciprocals is the core exercise in defining, using, and testing functions.
Conceptually, both conversions begin by finding how far a car travels on a fixed quantity of fuel. In the first function, the liters are converted to gallons by dividing by 3.785411784. The distance for 100 km in miles is computed as 100 * 1000 / 1609.344, since 100 km equals 100,000 meters and dividing by the number of meters in a mile gives the miles. The miles per gallon is then the miles divided by the gallons, which is returned as the final result.
In the reverse function, the miles are first converted back into a 100 km equivalent by multiplying by 1609.344 and 1000 and dividing by the 100 km scale, then the constant 3.785411784 liters per gallon is divided by that value to recover the liters per 100 km. Testing the functions against the expected outputs confirms the math, and because the two relationships are exact reciprocals, they transitively verify each other.
liters_100km_to_miles_gallon and miles_gallon_to_liters_100km.The European side reports liters consumed per 100 kilometers, and the US side reports miles traveled per gallon. The two functions move a value from one side to the other.
Because meters, miles, liters, and gallons are independent units, you must apply the exact factors 1609.344 meters per mile and 3.785411784 liters per gallon to get correct numbers.
Call each function with the sample values from the lab and confirm the printed outputs match the expected results. Because the functions perform inverse operations, a correct pair will also convert a value there and back unchanged.
Each function is designed to take exactly one argument, the value that matches its name. Passing the liters per 100 km into the first function and the mpg into the second produces the correct converted results.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.