Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Created February 22, 2010 13:39
Show Gist options
  • Save NicolasT/311074 to your computer and use it in GitHub Desktop.
Save NicolasT/311074 to your computer and use it in GitHub Desktop.
SENTINEL = chr(ord('$'))
def rotations(s):
for i in xrange(len(s)):
yield '%s%s' % (s[i:], s[:i])
def bwt(s):
if SENTINEL in s:
raise ValueError('Input can\'t contain chr(%d)' % ord(SENTINEL))
return ''.join(row[-1]
for row in sorted(rotations('%s%s' % (s, SENTINEL))))
def ibwt(s):
rows = [''] * len(s)
for _ in xrange(len(s)):
for i in xrange(len(s)):
rows[i] = '%s%s' % (s[i], rows[i])
rows.sort()
row = rows[0]
idx = row.index(SENTINEL)
return '%s%s' % (row[idx + 1:], row[:idx])
for line in 'BANANA', 'SIX MIXED PIXIES SIFT SIXTY PIXIE DUST BOXES':
print line
t = bwt(line)
print 'BWT:', t
print 'IBWT:', ibwt(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment