Проблем Armies - Associative Arrays
Здравейте,
Имам проблем с тази задачка. На 2рия тест ми гърми и не знам от какво може да е. Предполагам, че е нещо малко.
Решение : https://pastebin.com/K288JfAP
Условие на задачата :
Armies
Write a function that stores information about an army leader and his armies. The input will be array of strings. The strings can be in some of the following formats:
"{leader} arrives" – add the leader (no army)
"{leader}: {army name}, {army count}" – add the army with its count to the leader (if he exists)
"{army name} + {army count}" – if the army exists somewhere add the count
"{leader} defeated" – delete the leader and his army (if he exists)
When finished reading the input sort the leaders by total army count in descending. Then each army should be sorted by count in descending.
Print in the following format:
"{leader one name}: {total army count}
>>> {armyOne name} - {army count}
>>> {armyTwo name} - {army count}
…
{leader two name}: {total army count}
…"
Constrains
- The new leaders will always be unique
- When adding new army to leader, the army will be unique
Example
Input |
Output |
['Rick Burr arrives', 'Fergus: Wexamp, 30245', 'Rick Burr: Juard, 50000', 'Findlay arrives', 'Findlay: Britox, 34540', 'Wexamp + 6000', 'Juard + 1350', 'Britox + 4500', 'Porter arrives', 'Porter: Legion, 55000', 'Legion + 302', 'Rick Burr defeated', 'Porter: Retix, 3205'] |
Porter: 58507 >>> Legion - 55302 >>> Retix - 3205 Findlay: 39040 >>> Britox - 39040 |
Thanks for the great answer. Fixed it and on top of that I got a great example why I should use a second parameter in the reduce function.
Cheers.