diff options
| author | Abigail <abigail@abigail.be> | 2021-01-27 18:01:19 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-27 18:02:27 +0100 |
| commit | 46fdef8d9c84252e197f529b12987980e1cf6ff5 (patch) | |
| tree | 4b4293978a578c4c5a0968fa8d698508ff3b3459 /challenge-097/abigail/python/ch-2.py | |
| parent | 9a417c9df1e355f0535a038f32a3952eb44ffba8 (diff) | |
| download | perlweeklychallenge-club-46fdef8d9c84252e197f529b12987980e1cf6ff5.tar.gz perlweeklychallenge-club-46fdef8d9c84252e197f529b12987980e1cf6ff5.tar.bz2 perlweeklychallenge-club-46fdef8d9c84252e197f529b12987980e1cf6ff5.zip | |
Python solution for week 97, part 2
Diffstat (limited to 'challenge-097/abigail/python/ch-2.py')
| -rw-r--r-- | challenge-097/abigail/python/ch-2.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-097/abigail/python/ch-2.py b/challenge-097/abigail/python/ch-2.py new file mode 100644 index 0000000000..828c6fcf12 --- /dev/null +++ b/challenge-097/abigail/python/ch-2.py @@ -0,0 +1,45 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-2.py -s SECTIONS < input-file +# + +import fileinput +import sys +import getopt + +# +# Parse and validate options +# +shift = -1 +opts, args = getopt . getopt (sys . argv [1:], 's:') +for opt, val in opts: + if opt == "-s": + sections = int (val) + +sys . argv [1:] = [] + +if sections < 0: + sys . stderr . write ("Argument -s SECTIONS is required\n") + sys . exit (1) + +# +# Iterate over the input. For each position, count the number of 0s, +# and calculate the number of 1s. Sum the minimum of those numbers. +# +for line in fileinput . input (): + s_len = len (line) / sections + sum = 0 + for i in range (s_len): + zeros = 0 + for j in range (sections): + index = j * s_len + i + if line [index : index + 1] == "0": + zeros += 1 + ones = sections - zeros + sum += min (zeros, ones) + print (sum) |
