Skip to main content

Conversion Ratios

HardPremium

Calculate Conversion Ratios. Given a list of conversion pairs between different units of measurement, write an algorithm that returns the conversion ratio from one unit to another, either directly or through a series of conversions. If no conversion is possible, the algorithm should return None.

For instance, if you have the following conversions: ("USD", "EUR", 0.88), ("EUR", "JPY", 129.53), and ("GBP", "USD", 1.39), the algorithm should be able to deduce the conversion rate from "USD" to "JPY" as well as from "GBP" to "EUR".

Examples:

conversions = [ ('USD', 'EUR', 0.88), ('EUR', 'JPY', 129.53), ('GBP', 'USD', 1.39) ] source = 'USD' destination = 'JPY' output: 114.0064 # 0.88 * 129.53 source = 'GBP' destination = 'EUR' output: 1.2232 # 1.39 / 0.88 source = 'EUR' destination = 'GBP' output: None # No direct or indirect conversion available