The Hansa votes
- O(2ⁿ) · Legendary
- Full plan
- Python
- JavaScript
- recursion
- lists
- comparisons
Problem
In Lübeck, in 1370, the cities of the Hanseatic League meet to decide whether to close their ports to a king. They don't all weigh the same: each city brings a certain number of votes, and the motion passes if the cities voting in favor add up to at least the quota. Hinrich, the clerk, suspects that more votes don't always mean more power, and wants to measure how often each city's vote is the one that decides.
You get votes, a list of whole numbers from 1 up, one per city (it may be empty), and quota, a whole number from 1 up. For each city, an alliance is any group of the other cities, from none of them to all of them; two cities with the same votes are still different cities. A city's vote decides in an alliance when the alliance alone falls short of the quota, but with that city's votes it reaches at least the quota. Return a list of whole numbers, in the same order as votes: for each city, in how many alliances of the others its vote decides.
With [4, 2, 1] and quota 4, the first city decides in all four alliances of the other two (none, the 2, the 1 and both together), because none of them reaches 4 without it. The other two never decide: without the first they fall short, and with the first the quota was already met. You return [4, 0, 0]. With [3, 2, 2] and quota 4, each city decides in 2 alliances: you return [2, 2, 2].
Examples
The first example
[4, 2, 1], 4 → [4, 0, 0]
The second example
[3, 2, 2], 4 → [2, 2, 2]
Equal votes
[1, 1, 1], 2 → [2, 2, 2]
More votes is not always more power
[5, 4, 3, 1], 7 → [4, 4, 4, 0]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def decisive(votes, quota):
passJavaScript
function decisive(votes, quota) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.