The Burrows-Wheeler transform1 (BWT) is a reversible permutation of the characters of a string. It was originally invented for use in compression. The BWT of a string is obtained by first taking all rotations of the string. For example, all rotations of the string abab$ are

abab$, bab$a, ab$ab, b$aba, $abab

These rotations are then sorted lexicographically

$abab, ab$ab, abab$, b$aba, bab$a

The BWT of the string is defined as the string resulting from concatenating the last characters of each rotated string in this lexicographical order. So BWT(abab$) = bb$aa for the example string.

Assignment

Example

>>> rotate('abab$')
'bab$a'
>>> rotate('abab$', left=False)
'$abab'

>>> rotations('abab$')
['abab$', 'bab$a', 'ab$ab', 'b$aba', '$abab']
>>> rotations('abab$', left=False)
['abab$', '$abab', 'b$aba', 'ab$ab', 'bab$a']

>>> bwm('abab$')
['$abab', 'ab$ab', 'abab$', 'b$aba', 'bab$a']

>>> bwt('abab$')
'bb$aa'