The leaders of the world have met and have finally admitted that the best (and only) way of resolving global economic woes is to take stock of how much they owe each other and just pay each other off with huge cheques. They have hired you (ironically, at the lowest contract rate possible) to work out the best means of doing so.
After much deliberation, and asking someone to draw a simple example they've come up with the following spec.
Each country is represented by their ISO 3166-1 alpha-2 code:
US for the USA,
AU for Australia,
JP for Japan,
CN for China and so on...
- A ledger is drawn up as a series of country entries and the amounts owing to each country.
- Each country's entry starts off with their domain ID a colon, and how much they have in surplus/deficit (in billions of Euro), followed by a semicolon, then a coloned comma-separated list of the countries and how much (in billions of Euro) they owe.
- If a country owes nothing to another country, no mention of that country is entered after that semicolon separator.
- Deficits are indicated as negative numbers, surplus is indicated as a positive number.
- Values can also be floats.
- The ledger must be taken from STDIN. The end of the ledger is indicated by a carriage return on a blank line. The tally must be delivered to STDOUT.
An example of a ledger:
Input: AU:8;US:10,CN:15,JP:3 US:14;AU:12,CN:27,JP:14 CN:12;AU:8,US:17,JP:4 JP:10;AU:6,US:7,CN:10
The system then works out how much each country owes and is owed and determines their surplus/deficit, for example, for AU:
AU = 8 (current surplus) -10 (to US) -15 (to CN) -3 (to JP) +12 (from US) +8 (from CN) +6 (from JP) = 6
When all the computing is done, a tally must be shown:
Output: AU:6 US:-5 CN:35 JP:8
Your job is to create this system, capable of taking any number of ledger entries for any number of countries and capable of determining how much each country has in deficit/surplus when everything is paid out.
The ultimate test is for you to use your code to resolve the debt owed between the following countries in the test case below. These figures were taken from BBC News as of June 2011. (http://www.bbc.com/news/business-15748696)
For the purposes of the exercise, I have used their respective GDP as their current surplus... Please bear in mind that this is strictly an exercise in code quality assurance... there will be no talk of global economic resolution here in this question... If you want to talk economics I'm sure there's another subdomain in SE that handles it...
US:10800;FR:440.2,ES:170.5,JP:835.2,DE:414.5,UK:834.5 FR:1800;IT:37.6,JP:79.8,DE:123.5,UK:227,US:202.1 ES:700;PT:19.7,IT:22.3,JP:20,DE:131.7,UK:74.9,US:49.6,FR:112 PT:200;IT:2.9,DE:26.6,UK:18.9,US:3.9,FR:19.1,ES:65.7 IT:1200;JP:32.8,DE:120,UK:54.7,US:34.8,FR:309,ES:29.5 IE:200;JP:15.4,DE:82,UK:104.5,US:39.8,FR:23.8 GR:200;DE:15.9,UK:9.4,US:6.2,FR:41.4,PT:7.5,IT:2.8 JP:4100;DE:42.5,UK:101.8,US:244.8,FR:107.7 DE:2400;UK:141.1,US:174.4,FR:205.8,IT:202.7,JP:108.3 UK:1700;US:578.6,FR:209.9,ES:316.6,IE:113.5,JP:122.7,DE:379.3
Now, be the economic savior of the world!
- Shortest code wins... this is code-golf after all...
- Please provide your output of the major test case with your code answer...