diff options
| author | Abigail <abigail@abigail.be> | 2021-08-08 13:54:16 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-08-08 13:54:16 +0200 |
| commit | 8bca7f8b7d2f39e634aba151e6bd5e96ea3b720b (patch) | |
| tree | 3b8ee02f81a6e3c8b039a05dc49a17f0f7091ba1 /challenge-124/abigail/python | |
| parent | ebe0cbd2a0850a897334ad047508d2bcb316cbb5 (diff) | |
| download | perlweeklychallenge-club-8bca7f8b7d2f39e634aba151e6bd5e96ea3b720b.tar.gz perlweeklychallenge-club-8bca7f8b7d2f39e634aba151e6bd5e96ea3b720b.tar.bz2 perlweeklychallenge-club-8bca7f8b7d2f39e634aba151e6bd5e96ea3b720b.zip | |
Perl and Python solutions for week 124, part 2
Diffstat (limited to 'challenge-124/abigail/python')
| -rw-r--r-- | challenge-124/abigail/python/ch-2.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-124/abigail/python/ch-2.py b/challenge-124/abigail/python/ch-2.py new file mode 100644 index 0000000000..613634f80b --- /dev/null +++ b/challenge-124/abigail/python/ch-2.py @@ -0,0 +1,49 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + + +def init (): + global best_diff + best_diff = -1 + +def check_sets (set1, set2): + global best_diff + global best_set1 + global best_set2 + diff = abs (sum (set1) - sum (set2)) + if best_diff < 0 or diff < best_diff: + best_diff = diff + best_set1 = set1 + best_set2 = set2 + +def report (): + global best_set1 + global best_set2 + print (" " . join (map (lambda x: str (x), best_set1)) + "; " + + " " . join (map (lambda x: str (x), best_set2))) + + +def split_set (set, set1, set2): + n = len (set) + len (set1) + len (set2) + if len (set1) == n // 2: + check_sets (set1, set2 + set) + elif len (set2) == (n + 1) // 2: + check_sets (set1 + set, set2) + else: + split_set (set [1:], set1 + [set [0]], set2) + split_set (set [1:], set1, set2 + [set [0]]) + +import fileinput + +for line in fileinput . input (): + init () + set = list (map (lambda x: int (x), line . strip () . split ())) + split_set (set, [], []) + report () |
