# Program to find out the conversion rate of two currencies in Python

Suppose we are given three arrays; curr_a, curr_b, and conv_rate. The first array contains some currency names and so does the second one, and the array conv_rate contains the rates of conversion within an item curr_a[i] to cuur_b[i]. The item of conv_rate[i] is the conversion rate between curr_a[i] and curr_b[i]. Now, we are given two currencies src and dest. We have to find out the rate of conversion from src to dest. We return the value as output, and if it is not possible we return 0.

So, if the input is like src = "INR", dest = "JPY", curr_a = ["INR", "GBP", "EUR"], curr_b = ["GBP", "EUR", "JPY"], conv_rate = [0.009, 1.17, 129.67], then the output will be 1.3654250999999997

To solve this, we will follow these steps −

• temp := a new map that contains 0 as default value
• temp[src] := 1
• i := 0
• p := True
• while p and i <= size of temp, do
• p := False
• for each x in curr_a, y in curr_b, and z in conv_rate, do
• if temp[x] * z > temp[y] is non-zero, then
• temp[y] := temp[x] * z
• p := True
• i := i + 1
• if i <= size of temp, then
• return temp[dest]
• otherwise,
• return -1

## Example

Let us see the following implementation to get better understanding −

from collections import defaultdict

def solve(src, dest, curr_a, curr_b, conv_rate):
temp = defaultdict(int)
temp[src] = 1
i = 0
p = True
while p and i <= len(temp):
p = False
for x, y, z in zip(curr_a, curr_b, conv_rate):
if temp[x] * z > temp[y]:
temp[y] = temp[x] * z
p = True
i += 1
return temp[dest] if i <= len(temp) else -1

print(solve("INR", "JPY", ["INR", "GBP", "EUR"], ["GBP", "EUR", "JPY"], [0.009, 1.17, 129.67]))

## Input

"INR", "JPY", ["INR", "GBP", "EUR"], ["GBP", "EUR", "JPY"], [0.009,
1.17, 129.67]

## Output

1.3654251