- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 temp[x] * z > temp[y] is non-zero, then

- 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

- Related Articles
- Program to find out the dot product of two sparse vectors in Python
- Program to find out the length between two cities in shortcuts in Python
- Program to find out the k-th largest product of elements of two arrays in Python
- Program to find out the number of accepted invitations in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to Find Out the Strings of the Same Size in Python
- Program to find out if two expression trees are equivalent using Python
- Program to find out distance between two nodes in a binary tree in Python
- Program to Find Out Currency Arbitrage in Python
- Python Program to find out the sum of values in hyperrectangle cells
- Program to find out the value of a given equation in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to find out the value of a power of 2 in Python
- Program to find out the number of pairs of equal substrings in Python
- Program to find out the efficient way to study in Python