Given a list of conversion items between symbols, write an algorithm to return the conversion ratio for each pair of symbols.
HardCalculate 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
Watch Michael, a software engineer at Google, answer the question "Given a list of conversion items between different symbols, write an algorithm that returns the conversion ratio value for each pair of symbols."
Related questions
Given an array of points on a plane (x,y), write a function to return the distance between the closest pair of points.Given a list of items with different sizes, write an algorithm to pack these items into the minimum number of batches without exceeding the capacity of each batch.Given a list of trades, iterate to return buy/sell pairs and overall profit.Related courses


