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

Post History

66%
+2 −0
Challenges Find good coalitions

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 imp...

posted 2y ago by Moshi‭  ·  edited 2y ago by Moshi‭

Answer
#2: Post edited by user avatar Moshi‭ · 2021-10-04T23:08:57Z (over 2 years ago)
Added explanation
  • # [Python 3], 191 bytes
  • ```python
  • 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!][TIO-kud95h9i]
  • [Python 3]: https://docs.python.org/3/
  • [TIO-kud95h9i]: https://tio.run/##lVBBTsMwEDw3r1jlUrsEaAqpUFC4RLwi5OA6SbtSYlu2WwmhXnkAT@QjIZuKkiJx4GBrZ3Z2djXm1e@0uuub7KVvRbepBJi0kNBoCxJQgdwJVGxR5MxElhNtibZCbWvW1ooZfhXzkmMDom0ZPcwy5vYdMzcH0e5rxzi/XT1lI1WokjwUeVR89KuozpmMyE3yazzRONDFMopLXgaN1R2gr63XunWAndHWLx7zTOpug0p41Mr1vnbeQQZFMHs7RsMXKqF0mMIqOcEDElhGED53QjoC8Z@d5aUF9UZVnExU8erXorPqfqJ6GOptXaEf6vXFQLz@78DPHdNrk8lAcgzKIKAIKRAKdwwmDWbGovKMEP8G88/3j3kEzYk907z/Ag "Python 3 – Try It Online"
  • # [Python 3], 191 bytes
  • ```python
  • 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!][TIO-kud95h9i]
  • 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.
  • ```python
  • 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:
  • ```text
  • n subset > s/2 and n-1 subset <= s/2
  • ```
  • is equivalent to
  • ```text
  • (n subset <= s/2) == 0 and (n-1 subset <= s/2) == 1
  • ```
  • is equivalent to
  • ```text
  • (n-i subset <= s/2) == i) for i in [0,1]
  • ```
  • [Python 3]: https://docs.python.org/3/
  • [TIO-kud95h9i]: https://tio.run/##lVBBTsMwEDw3r1jlUrsEaAqpUFC4RLwi5OA6SbtSYlu2WwmhXnkAT@QjIZuKkiJx4GBrZ3Z2djXm1e@0uuub7KVvRbepBJi0kNBoCxJQgdwJVGxR5MxElhNtibZCbWvW1ooZfhXzkmMDom0ZPcwy5vYdMzcH0e5rxzi/XT1lI1WokjwUeVR89KuozpmMyE3yazzRONDFMopLXgaN1R2gr63XunWAndHWLx7zTOpug0p41Mr1vnbeQQZFMHs7RsMXKqF0mMIqOcEDElhGED53QjoC8Z@d5aUF9UZVnExU8erXorPqfqJ6GOptXaEf6vXFQLz@78DPHdNrk8lAcgzKIKAIKRAKdwwmDWbGovKMEP8G88/3j3kEzYk907z/Ag "Python 3 – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-10-04T22:57:41Z (over 2 years ago)
# [Python 3], 191 bytes

```python
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!][TIO-kud95h9i]

[Python 3]: https://docs.python.org/3/
[TIO-kud95h9i]: https://tio.run/##lVBBTsMwEDw3r1jlUrsEaAqpUFC4RLwi5OA6SbtSYlu2WwmhXnkAT@QjIZuKkiJx4GBrZ3Z2djXm1e@0uuub7KVvRbepBJi0kNBoCxJQgdwJVGxR5MxElhNtibZCbWvW1ooZfhXzkmMDom0ZPcwy5vYdMzcH0e5rxzi/XT1lI1WokjwUeVR89KuozpmMyE3yazzRONDFMopLXgaN1R2gr63XunWAndHWLx7zTOpug0p41Mr1vnbeQQZFMHs7RsMXKqF0mMIqOcEDElhGED53QjoC8Z@d5aUF9UZVnExU8erXorPqfqJ6GOptXaEf6vXFQLz@78DPHdNrk8lAcgzKIKAIKRAKdwwmDWbGovKMEP8G88/3j3kEzYk907z/Ag "Python 3 – Try It Online"