Triangular currency arbitrage scanner
A trading desk in Zurich watches quoted exchange rates between currencies. A triangular arbitrage exists when converting A to B, then B to C, then C back to A returns more than you started with, that is when the product of the three rates is greater than 1.
Task: the first line is n, the number of quotes. Each of the next n lines is FROM TO rate: one unit of FROM buys rate units of TO (a decimal number). A pair that is not quoted cannot be used, and a quote in one direction says nothing about the other direction. Consider every cycle of three different currencies A->B->C->A where all three quotes exist. Write each cycle starting from its alphabetically smallest code (so EUR->USD->GBP->EUR, never USD->GBP->EUR->USD for the same cycle). Find the cycle with the largest product, computed exactly with the decimal module. If several cycles tie, take the one whose text is alphabetically first. If the best product is greater than 1, print A->B->C->A +G% where G is (product - 1) * 100 with 4 decimals, halves up. Otherwise print NONE.
Sample tests
Input
6 USD EUR 0.92 EUR GBP 0.86 GBP USD 1.27 USD GBP 0.79 GBP EUR 1.16 EUR USD 1.09
Expected output
EUR->GBP->USD->EUR +0.4824%
Input
3 CHF USD 1.12 USD JPY 150.5 JPY CHF 0.0059
Expected output
NONE
Your answer is also checked against 4 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
