- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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]))

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

1.3654251

- Related Questions & Answers
- 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 Strings of the Same Size in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to find out if two expression trees are equivalent using Python
- Program to find out the number of pairs of equal substrings in Python
- Program to find out the value of a power of 2 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 sum of the number of divisor of the divisors in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to Find Out Currency Arbitrage in Python
- Program to Find Out the Number of Squares in a Grid in Python

Advertisements