astropanic
7/27/2010 - 12:41 PM

palindrome.rb

class String

  def compute
    @size = self.size
    @even = @size % 2 == 0
    if @even
      @pos1 = (@size/2)-1
      @pos2 = @pos1+1
    else
      @pos1 = (((@size+1)/2)-1)
      @pos2 = @pos1
    end
    @mirrored = self[0,@pos1+1]
    @start = @even ? @pos1 : @pos2-1
    while @start > -1 do
      @mirrored << self[@start,1]
      @start = @start -1
    end
    if @mirrored <= self
      nxt(@pos1, @pos2)
    end
    @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
1.upto(t) do |z|
  k = gets.chomp.to_s
  puts k.compute
end