Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Find good coalitions

+3
−0

As you might know, there were elections in Germany, and now the parties have to form a government coalition. Let's help them with it!

A good coalition has the strict majority of seats (that is, more seats than the opposition). Moreover you don't want more parties than necessary in a coalition, that is, if you still have a majority after removing one of the parties, it's not a good coalition.

You are given a list of parties and their number of seats. Your task is to output a list of good coalitions, where each coalition is given by a list of its members. For this task, a coalition may also consist of a single party.

Input and output may be in any suitable form (e.g. they need not be lists, but can be sets, dictionaries, XML, …). Note that the order of the parties in each coalition does not matter, nor does the order of coalitions. For example, the output [["nano", "vi"], ["nano", Emacs"], ["vi", "Emacs"]] is equivalent to [["vi", Emacs"], ["Emacs", "nano"], ["nano", "vi"]].

This is code-golf, the shortest code wins.

Test cases:

{"nano": 25}
 → [["nano"]]

{"vi": 20, "Emacs": 21}
 → [["Emacs"]]

{"vi": 20, "Emacs": 20}
 → [["vi", "Emacs"]]

{"nano": 20, "vi": 15, "Emacs": 12}
 → [["nano", "vi"], ["nano", Emacs"], ["vi", "Emacs"]]

{"nano": 25, "vi": 14, "Emacs": 8, "gedit": 6}
 → [["nano", "vi"], ["nano", "Emacs"], ["nano", "gedit"], ["vi", "Emacs", "gedit"]]

{"nano": 16, "vi": 14, "Emacs": 8, "gedit": 6}
 → [["nano", "vi"], ["nano", "Emacs"], ["vi", "Emacs", "gedit"]]

{"nano": 10, "vi": 10, "Emacs": 5, "gedit": 5}
 → [["nano", "vi"], ["nano", "Emacs", "gedit"], ["vi", "Emacs", "gedit]]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+3
−0

Haskell, 141 bytes

import Control.Monad
f a=map(map fst)$filter(\c->let{g=map
snd;e=sum(g a)`div`2;f=g c;d=sum
f}in d>e&&all((<=e).(d-))f)$filterM(pure[1<0..])a

Try it online!

Explanation: we generate all possible coalitions and then filter out the bad ones. filterM(pure[1<0..])a is the powerset of input.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Python 3, 191 bytes

lambda p:[c for c in chain(*[C(p,r)for r in range(len(p)+1)])if all(all(i==(sum(p.values())/2>=sum(p[n]for n in d))for d in C(c,len(c)-i))for i in[0,1])]
from itertools import*;C=combinations

Try it online!

Yay for quadruple nested list compressions...

Explanation

First we take the powerset:

lambda p:[c for c in chain(*[C(p,r)for r in range(len(p)+1)])

Then we filter.

if all(all(i==(sum(p.values())/2>=sum(p[n]for n in d))for d in C(c,len(c)-i))
for i in[0,1])]
from itertools import*;C=combinations # Imports and aliases

There's a neat little trick: we want to have the set sum greater than s/2 (the average), while having the n-1 sized subsets' sums less than or equal to s/2. By realizing that the full set is equal to the n sized subset, we can combine both of them:

n subset > s/2 and n-1 subset <= s/2

is equivalent to

(n subset <= s/2) == 0 and (n-1 subset <= s/2) == 1

is equivalent to

(n-i subset <= s/2) == i) for i in [0,1]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »