Skip to content

Instantly share code, notes, and snippets.

@astropanic
Created July 26, 2010 23:45
Show Gist options
  • Save astropanic/491460 to your computer and use it in GitHub Desktop.
Save astropanic/491460 to your computer and use it in GitHub Desktop.
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment