aboutsummaryrefslogtreecommitdiff
path: root/challenge-115/abigail/python/ch-2.py
blob: a3b040b832e6d6603473304163c748e500661a03 (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
46
47
48
49
#!/opt/local/bin/python

#
# See ../README.md
#

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

import fileinput

NR_OF_DIGITS = 10

for line in fileinput . input ():
    #
    # Parse the input, count digits
    #
    digits = []
    for d in range (NR_OF_DIGITS):
        digits . append (0)
    for d in line . split ():
        d = int (d)
        digits [d] = digits [d] + 1

    #
    # Find the smallest even number
    #
    last = -1

    for d in range (NR_OF_DIGITS - 2, -1, -2):
        if digits [d] > 0:
            last = d

    #
    # If we don't have an even number, skip
    #
    if last < 0:
        continue
    digits [last] = digits [last] - 1

    #
    # Print the rest of the digits, highest to lowest
    #
    for d in range (NR_OF_DIGITS - 1, 0, -1):
        for i in range (digits [d]):
            print (d, end = '')

    print (last)