Skip to content

Instantly share code, notes, and snippets.

@h-lame
Last active August 29, 2015 14:05
Show Gist options
  • Save h-lame/37226207c8157c4a051d to your computer and use it in GitHub Desktop.
Save h-lame/37226207c8157c4a051d to your computer and use it in GitHub Desktop.
Implementation of a Burrows-Wheeler Transform, as guided by https://github.com/zetter/burrows-wheeler-transform
class BWT
def encode(string)
chars = "#{string}$".chars
rotations = []
chars.length.times.
inject(chars) do |new_chars, _|
rotations << new_chars
new_chars.rotate
end
rotations.
sort.
map(&:last).
join
end
def decode(encoded_string)
encoded_string.length.
times.
inject(encoded_string.length.times.map {|_| '' }) do |strings, _|
strings.map.with_index do |str, idx|
str.prepend(encoded_string[idx])
end.sort
end.
detect { |string| string[-1] == '$' }[0..-2]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment