Skip to content

Instantly share code, notes, and snippets.

@tomstuart
Created August 9, 2014 10:09
Show Gist options
  • Select an option

  • Save tomstuart/9f5de3cd41cd9897c4d4 to your computer and use it in GitHub Desktop.

Select an option

Save tomstuart/9f5de3cd41cd9897c4d4 to your computer and use it in GitHub Desktop.

Revisions

  1. tomstuart created this gist Aug 9, 2014.
    12 changes: 12 additions & 0 deletions bwt.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    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