Skip to content

Instantly share code, notes, and snippets.

@ngtk
Created August 25, 2015 11:16
Show Gist options
  • Select an option

  • Save ngtk/f6d812435abf951e50c9 to your computer and use it in GitHub Desktop.

Select an option

Save ngtk/f6d812435abf951e50c9 to your computer and use it in GitHub Desktop.
block sortable
module BlockSortable
def encode
[
matrix.map(&:last).join,
matrix.index(self)
]
end
def matrix
str = self.dup
matrix = []
matrix << str
(str.length-1).times do
str = str.left_rotate
matrix << str
end
matrix.sort
end
def left_rotate
str = self.dup
chars = str.chars
left = chars.shift
chars << left
chars.join
end
def last
self[-1]
end
end
class String
prepend BlockSortable
end
puts "payapa".encode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment