aboutsummaryrefslogtreecommitdiff
path: root/challenge-102/abigail/ruby/ch-2.rb
blob: 9a4301117e0956c5c2b71e0b1bb7a4f4c5a35f95 (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
#!/usr/bin/ruby

#
# See ../README.md
#
 
#
# Run as: ruby ch-2.rb < input-file
#

#
# Working from the end of the required string backwards, we alternate
# placing a hash, and placing a number. We place them in an array @out,
# and at the end, print out said array in reverse order.
#

ARGF . each_line do |_|
    index = _ . to_i
    out = Array . new
    hash = false
    while index > 0 do
        if hash = !hash
            out . push ("#")
            index -= 1
        else
            out . push ((index + 1) . to_s)
            index -=    (index + 1) . to_s . length
        end
    end
    puts (out . reverse . join)
end