diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-06-05 00:10:09 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-06-05 00:10:09 -0500 |
| commit | c9c7142570ee4f82f6c0998e6ecfae6f032fe463 (patch) | |
| tree | 4aa5bf856c3e2d832ca8374cbff82d7527ef76aa /challenge-115/abigail/python/ch-2.py | |
| parent | c18fb21257876cc864357fa7a2237995c1f6b169 (diff) | |
| parent | 373a97f7ee737cbd8d3e8d87a96b6ae0dec388b5 (diff) | |
| download | perlweeklychallenge-club-c9c7142570ee4f82f6c0998e6ecfae6f032fe463.tar.gz perlweeklychallenge-club-c9c7142570ee4f82f6c0998e6ecfae6f032fe463.tar.bz2 perlweeklychallenge-club-c9c7142570ee4f82f6c0998e6ecfae6f032fe463.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-115/abigail/python/ch-2.py')
| -rw-r--r-- | challenge-115/abigail/python/ch-2.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-115/abigail/python/ch-2.py b/challenge-115/abigail/python/ch-2.py new file mode 100644 index 0000000000..a3b040b832 --- /dev/null +++ b/challenge-115/abigail/python/ch-2.py @@ -0,0 +1,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) |
