Skip to content

Instantly share code, notes, and snippets.

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