Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:22:21+00:00 2026-05-13T23:22:21+00:00

class FxRate { string Base { get; set; } string Target { get; set;

  • 0
 class FxRate {
     string Base { get; set; }
     string Target  { get; set; }
     double Rate { get; set; }
 }

 private IList<FxRate> rates = new List<FxRate> {
          new FxRate {Base = "EUR", Target = "USD", Rate = 1.3668},
          new FxRate {Base = "GBP", Target = "USD", Rate = 1.5039},
          new FxRate {Base = "USD", Target = "CHF", Rate = 1.0694},
          new FxRate {Base = "CHF", Target = "SEK", Rate = 8.12}
          // ...
 };

Given a large yet incomplete list of exchange rates where all currencies appear at least once (either as a target or base currency): What algorithm would I use to be able to derive rates for exchanges that aren’t directly listed?

I’m looking for a general purpose algorithm of the form:

 public double Rate(string baseCode, string targetCode, double currency)
 {
      return ...
 }

In the example above a derived rate would be GBP->CHF or EUR->SEK (which would require using the conversions for EUR->USD, USD->CHF, CHF->SEK)

Whilst I know how to do the conversions by hand I’m looking for a tidy way (perhaps using LINQ) to perform these derived conversions perhaps involving multiple currency hops, what’s the nicest way to go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:22:21+00:00Added an answer on May 13, 2026 at 11:22 pm

    First construct a graph of all your currencies:

    private Dictionary<string, List<string>> _graph
    public void ConstructGraph()
    {
        if (_graph == null) {
            _graph = new Dictionary<string, List<string>>();
            foreach (var rate in rates) {
                if (!_graph.ContainsKey(rate.Base))
                    _graph[rate.Base] = new List<string>();
                if (!_graph.ContainsKey(rate.Target))
                    _graph[rate.Target] = new List<string>();
    
                _graph[rate.Base].Add(rate.Target);
                _graph[rate.Target].Add(rate.Base);
            }
        }
    }
    

    Now traverse that graph using recursion:

    public double Rate(string baseCode, string targetCode)
    {
        if (_graph[baseCode].Contains(targetCode)) {
            // found the target code
            return GetKnownRate(baseCode, targetCode);
        }
        else {
            foreach (var code in _graph[baseCode]) {
                // determine if code can be converted to targetCode
                double rate = Rate(code, targetCode);
                if (rate != 0) // if it can than combine with returned rate
                    return rate * GetKnownRate(baseCode, code);
            }
        }
    
        return 0; // baseCode cannot be converted to the targetCode
    }
    public double GetKnownRate(string baseCode, string targetCode) 
    {
        var rate = rates.SingleOrDefault(fr => fr.Base == baseCode && fr.Target == targetCode);
        var rate_i rates.SingleOrDefault(fr => fr.Base == targetCode && fr.Target == baseCode));
        if (rate == null)
            return 1 / rate_i.Rate
        return rate.Rate;
    }
    

    Disclaimer: This is untested. Further, I’m sure this isn’t the most performant approach to solve the problem (O(n) I think), but I believe it will work. There are a number of things you could add to improve the performance (e.g. saving every new combined rate calculation would eventually turn this into an effective O(1))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Class A { string name; IList<A> minorList = new List<A>(); } IList<A> majorList =
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class Employee{ // salary variable is a private static variable private static double salary;
class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
class Main { public static void main(String[] args) { new Cloned().clone(); } } class
class Program { static void Main(string[] args) { Father objFather = new Son(); //Ok
class Program { static object test = new object(); static void Main(string[] args) {
class X { int i; public: X() { i = 0; } void set(int
class Test { public static void main(String[] args) throws Exception { Test t =
{ class Program { static void Main(string[] args) { int id = 0, stock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.