aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/abigail/python/ch-2.py
blob: 3e9424fb181322744e513d60b6aa4e700ecbd60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/opt/local/bin/python

#
# See ../READ.md
#

#
# Run as python ch-2.py -s SIZE < input-file
#

import fileinput
import sys
import getopt

#
# Parse and validate options
#
size = -1
opts, args = getopt . getopt (sys . argv [1:], 's:')
for opt, val in opts:
    if opt == "-s":
        size = int (val)

sys . argv [1:] = []

if size < 0:
    sys . stderr . write ("Argument -s SIZE 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 ():
    sections = len (line) / size
    sum = 0
    for i in range (size):
        zeros = 0
        for j in range (sections):
            index = j * size + i
            if line [index : index + 1] == "0":
                zeros += 1
        ones = sections - zeros
        sum += min (zeros, ones)
    print (sum)