Skip to content

Instantly share code, notes, and snippets.

@tomstuart
Created August 9, 2014 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomstuart/9f5de3cd41cd9897c4d4 to your computer and use it in GitHub Desktop.
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
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