astropanic
7/26/2010 - 11:45 PM

palindrome.rb

#SIGABRT
class Palindrome

  def compute(value)
    @size = value.size
    @even = @size % 2 == 0
    if @even
      @pos1 = (@size/2)-1
      @pos2 = @pos1+1
    else
      @pos1 = (((@size+1)/2)-1)
      @pos2 = @pos1
    end
    @mirrored = value[0,@pos1+1]
    @start = @even ? @pos1 : @pos2-1
    while @start > -1 do
      @mirrored << value[@start,1]
      @start = @start -1
    end
    if @mirrored <= value
      nxt(@pos1, @pos2)
    end
    puts @mirrored
  end
  def nxt(pos1, pos2)
    @pos1 = @mirrored[pos1,1]
    if pos1 < 0
      @mirrored[@size-1,1] = "1"
      @mirrored = "1"+@mirrored
    else
      if @pos1 < 9.to_s
        @mirrored[pos1,1] = @mirrored[pos2,1] = (@pos1.to_i + 1).to_s
      else
        @mirrored[pos1,1] = @mirrored[pos2,1] = 0.to_s
        nxt(pos1-1, pos2+1)
      end
    end
  end

end

t = gets.to_i
p = Palindrome.new
1.upto(t) do |z|
  k = gets.chomp.to_s
  p.compute(k)
end