Created
April 8, 2021 10:12
-
-
Save xpqz/20c1248347a8014e0a4b1cdabc730ddb to your computer and use it in GitHub Desktop.
Python attempt at Problem 8, Phase II of the Dyalog APL Problem Solving Competition, 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from itertools import chain, combinations | |
from collections import Counter | |
def balance(data): | |
total = sum(data) | |
if total % 2: | |
return None | |
target = total // 2 | |
for subset in chain.from_iterable(combinations(data, r) for r in range(len(data) + 1)): | |
if sum(subset) == target: | |
return [list(subset), list((Counter(data) - Counter(subset)).elements())] | |
return None | |
def f(): | |
return balance([10, 81, 98, 27, 28, 5, 1, 46, 63, 99, 25, 39, 84, 87, 76, 85, 78, 64, 41, 93]) | |
print(timeit.timeit(f, number=3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runs in 40-50ms on my machine.