blob: e1e41a91fd3fb39a4772fde41857bf72d02f8148 (
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
50
51
52
53
54
55
56
57
|
#!/usr/bin/ruby
#
# See ../README.md
#
#
# Run as: ruby ch-2.rb < input-file
#
nr_of_digits = 10
ARGF . each_line do
|line|
#
# Read the input and count the digits
#
digits = []
for d in 0 .. nr_of_digits - 1 do
digits [d] = 0
end
line . split() . each do
|d|
digits [d . to_i] += 1
end
#
# Find the lowest even number
#
last = -1
for i in 1 .. nr_of_digits do
d = nr_of_digits - i
if d % 2 == 0 && digits [d] > 0
then last = d
end
end
#
# Skip if the input does not contain an even number
#
if last < 0
then next
end
digits [last] -= 1
#
# Print the digits, highest to lowest
#
for i in 1 .. nr_of_digits do
d = nr_of_digits - i
for j in 1 .. digits [d] do
print (d)
end
end
puts (last)
end
|