32
6
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!
Rules:
- Shortest code wins... this is code-golf after all...
- Please provide your output of the major test case with your code answer...
1In the "ultimate test", shouldn't there be a semicolon after
JP:4100
? – Mathieu Rodic – 2014-03-27T08:53:43.523@MathieuRodic Thanks for the sharp eye to detail... +1 to you, my friend... – WallyWest – 2014-03-27T09:34:56.290
@WallyWest: the sharp eye is not mine... but my script's, when it failed at parsing! – Mathieu Rodic – 2014-03-27T09:39:18.770
Merci bien, all the same... – WallyWest – 2014-03-27T09:48:30.473
9I can't help but wonder if this is a very clever way of having a homework assignment completed for you. If so, you deserve it. – mkingston – 2014-03-27T11:30:57.330
2Yeah, if you did this will real numbers you'd notice an astonishing contradiction. The sum of all surpluses and deficits will be negative. – Cruncher – 2014-03-27T12:57:08.300
seems like this could be solved with linear programming (such as AMPL), although I've never seen it used for a code golf problem. – smcg – 2014-03-27T14:06:27.537
@Tobia I'm not sure what country you live in where you have "private banks" printing currency. Probably not a financially stable one... – qwr – 2014-03-27T19:46:09.277
3Actually, it's not homework in disguise... It was inspired from my weekly poker tournament with my friends... Trying to figure out a quicker way of working out the winnings for each player ;) – WallyWest – 2014-03-27T20:31:38.150
1@WallyWest LOL ;) btw, apologies for the OT comments, but it's a matter I'm quite passionate about. Now let's get back to some fun coding and forget the woes of the world... – Tobia – 2014-03-27T20:33:48.987
@Tobia, Forgiven! I'd love to see an entry from you for this question... – WallyWest – 2014-03-28T03:45:01.800
Ok, will do an APL one over the weekend! – Tobia – 2014-03-28T08:51:41.860
@JBentley If you want to talk economics, go elsewhere... This question was not to incite opinions about global economics...
Please keep comments like this out of this question or I'll start plucking these comments out like stray grey hairs... – WallyWest – 2014-03-28T22:39:43.060
1@WallyWest I was responding to an existing series of comments. But I shall not post further on the subject. – JBentley – 2014-03-29T04:59:48.790