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

Currency conversion is a common problem in financial applications. Given three arrays containing source currencies, destination currencies, and their conversion rates, we need to find the conversion rate between any two currencies using the available exchange paths.

The problem provides curr_a (source currencies), curr_b (destination currencies), and conv_rate (conversion rates). We need to find the conversion rate from a source currency to a destination currency, returning 0 if no path exists.

Problem Analysis

If the input is src = "INR", dest = "JPY", curr_a = ["INR", "GBP", "EUR"], curr_b = ["GBP", "EUR", "JPY"], conv_rate = [0.009, 1.17, 129.67], then the conversion path is:

  • INR ? GBP: 1 × 0.009 = 0.009
  • GBP ? EUR: 0.009 × 1.17 = 0.01053
  • EUR ? JPY: 0.01053 × 129.67 = 1.3654

Algorithm Steps

To solve this, we will follow these steps ?

  • Create a dictionary to store maximum conversion rates for each currency
  • Initialize the source currency with rate 1
  • Iteratively update rates by finding better conversion paths
  • Continue until no improvements are found or maximum iterations reached
  • Return the rate for destination currency

Implementation

from collections import defaultdict

def solve(src, dest, curr_a, curr_b, conv_rate):
    # Dictionary to store maximum conversion rates
    temp = defaultdict(int)
    temp[src] = 1
    
    i = 0
    p = True
    
    # Continue until no improvements or max iterations
    while p and i <= len(temp):
        p = False
        
        # Check all currency pairs for better rates
        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 conversion rate or -1 if not possible
    return temp[dest] if i <= len(temp) else -1

# Test the function
result = solve("INR", "JPY", ["INR", "GBP", "EUR"], ["GBP", "EUR", "JPY"], [0.009, 1.17, 129.67])
print(f"Conversion rate from INR to JPY: {result}")
Conversion rate from INR to JPY: 1.3654251

How It Works

The algorithm uses a modified Bellman-Ford approach:

  1. Initialization: Set the source currency rate to 1
  2. Iteration: For each currency pair, check if going through the current path gives a better rate
  3. Update: If a better rate is found, update it and mark that improvements were made
  4. Termination: Stop when no improvements are found or maximum iterations reached

Edge Cases

# Case 1: Direct conversion
print(solve("USD", "EUR", ["USD"], ["EUR"], [0.85]))

# Case 2: No path exists
print(solve("USD", "INR", ["USD", "GBP"], ["EUR", "JPY"], [0.85, 150]))

# Case 3: Same source and destination
print(solve("USD", "USD", ["USD"], ["EUR"], [0.85]))
0.85
0
1

Conclusion

This algorithm efficiently finds currency conversion rates by exploring all possible paths between currencies. It handles complex multi-step conversions and returns the optimal rate, making it useful for financial applications requiring currency exchange calculations.

Updated on: 2026-03-26T16:52:56+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements