Created
August 25, 2015 11:16
-
-
Save ngtk/f6d812435abf951e50c9 to your computer and use it in GitHub Desktop.
block sortable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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