aboutsummaryrefslogtreecommitdiff
path: root/challenge-116/abigail/ruby/ch-1.rb
blob: dfd15809e535ac56144a5810348291ed5ae770df (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
#!/usr/bin/ruby

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

def make_sequence (string, start)
    if   string == start
    then return [start]
    end
    if   0 == string . index(start)
    then tail   = string [start . length, string . length]
         result = make_sequence(tail, (start . to_i + 1) . to_s) ||
                  make_sequence(tail, (start . to_i - 1) . to_s)
         if   result
         then return result . unshift (start)
         end
    end
    return nil

end

ARGF . each_line do
    |line|
    line . strip!
    for i in 1 .. line . length do
        start  = line [0, i]
        result = make_sequence(line, start)
        if   result
        then puts (result . join (","))
             break
        end
    end
end