The money trail

Description

Discuss (7 comments)
This puzzle is a follow up on the Currency converter puzzle. The goal is still to convert one currency into another however with some added difficulty.

Input Specifications

Your program must take one and only one command line argument: the input file.
The input file is formatted as follows:
  1. the first line contains the number n of currencies exchange rates
  2. the n following lines each contain a currency equivalence of the form
    rate1 currency1 = rate2 currency2
  3. the next line contains the number p of conversions queries
  4. the p following lines each contain a currency conversion query of the form
    amount source_currency => target_currency

Important: There is no default currency, and consequently it is not always possible to convert a currency into another.

Example:
4
1 Euro = 1.4025 Dollar
2.5 Dollar = 70.5482 Ruble
5.5 Rupee = 3.4749 Ruble
0.7 Pound = 1.7516 Real
4
10 Dollar => Euro
25 Euro => Rupee
1.5 Ruble => Real
5.50 Real => Pound

Output Specifications

You must print your output to the standard output (printf, etc...).
It should contain p lines, one for each currency conversion query giving the amount in the target currency, if the currency cannot be converted then the value 0 should be printed instead.

Important: The output result must be printed with exactly 4 fractional digit accuracy. The only exception is for the 0 indicating that a conversion is not possible.

For the previous example, the solution is:
7.1301
1566.0628
0
2.1980
Example explanation:
  • The value 7.1301 is simply computed by using the first exchange rate (Euro -> Dollar)
  • The value 1566.0628 is computed by combining the first 3 exchange rates (Euro -> Dollar -> Ruble -> Rupee).
  • The value 0 reflects the fact that with the given exchange rates, it's impossible to convert from Ruble to Real

Scoring

The number of points is determined by the tests that are passed. You can see in the Unit Tests section for how many percent each test success contributes to the final score.
© CoderCharts - All Rights Reserved
Type Puzzle Pass 165 Fail 59
Scoring Generic Level Medium Points 900
Tags arithmetic, conversion, recursion

Source code editor

Unit Tests

Name CPU Unit Memory Unit

Small

(10% of the score) 12 currencies, 99 conversions
1s
250 MB

Medium

(20% of the score) 50 currencies, 501 conversions
1s
250 MB

Large

(30% of the score) 100 currencies, 10000 conversions
1s
250 MB

Extreme

(40% of the score) 500 currencies, 10000 conversions
1s
250 MB

Confirm that you want to switch languages
Your current edits will be lost.

Confirm that you wish to load a previous submission
Your current edits will be lost.