Created
February 22, 2010 13:39
-
-
Save NicolasT/311074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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