Created
August 9, 2014 10:09
-
-
Save tomstuart/9f5de3cd41cd9897c4d4 to your computer and use it in GitHub Desktop.
An implementation of the Burrows–Wheeler transform that satisfies https://github.com/zetter/burrows-wheeler-transform
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
class BWT | |
def encode(string) | |
chars = string.chars + ['$'] | |
chars.each_index.map(&chars.method(:rotate)).sort.map(&:last).join | |
end | |
def decode(string) | |
chars = string.chars | |
chars.inject([]) { |table| chars.zip(table).sort }. | |
map(&:join).detect { |s| s.end_with?('$') }.chop | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment