Author: Franco Saliola <saliola at gmail.com>
Sage/Python includes a builtin datastructure from strings.
There are several ways to input strings. You can input a string using single quotes (‘) or double quotes (”):
You can also input a string using three quotes (“”” or ‘’‘). This is useful if you want to use both ” and ‘ in your string, or you want your string to span multiple lines:
Exercises
Create and print the following string
\ | ( | ) / /
_________________
| |
| |
| I <3 Coffee! /--\
| | |
\ /\--/
\___________/
Without using cut-and-paste(!) replace the substring I <3 Coffee! with the substring I <3 Tea!.
Print a copy of your string with all the letters capitalized (upercase).
Strings behave very much like lists. The table below summarizes their common operations.
Operation Syntax for lists Syntax for strings Accessing a letter list[3] string[3] Slicing list[3:17:2] string[3:17:2] Concatenation list1 + list2 string1 + sting2 A copy list[:] string[:] A reversed copy list[::-1] string[::-1] Length len(list) len(string)
Exercises
The factors of length 2 of ‘rhubarb’ are
rhhuubbaarrb
Write a function called factors that returns a list of the factors of length l of s , and list all the factors of length 3 of ‘rhubarb’.
What happens if you apply your function factors to the list [0,1,1,0,1,0,0,1] ? If it doesn’t work for both lists and strings, go back and modify your function so that it does work for both.
The string
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
begins with W 12 times, then B once, then W 12 times, then B 3 times, then W 24 times, then B once and then W 14 times. Thus, it can be encoded by the tuples:
(W, 12), (B, 1), (W, 12), (B, 3), (W, 24), (B, 1), (W, 14)
This is called the run-length encoding of the string.
Exercises
Write a function that returns the run-length encoding of a string. Does your function work for lists as well as strings? If not, then can you make it so that it works for both strings and lists? Use your function to compute the run-length encoding of the list:
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
The rotations of the string ‘bananas’ are:
bananasananasbnanasbaanasbannasbanaasbanansbanana
and if we sort these alphabetically, then we get:
ananasbanasbanasbananbananasnanasbanasbanasbanana
Exercises
Define a function print_sorted_rotations that sorts all the rotations of a string and prints them in an array as above. Print the sorted rotations of the strings ‘ananas’ and ‘cocomero’.
The Burrows-Wheeler Transform (BWT) of a string s sorts all the rotations of s and then returns the last column.
For example, if we sort the rotations of ‘bananas’:
ananasbanasbanasbananbananasnanasbanasbanasbanana
then the last column is bnnsaaa , so the BWT of bananas is bnnsaaa.
Exercises
Write a function that returns the BWT of a string. Compute the BWT of bananas , ananas and cocomero . (Hint: You can return you answer as a list, but if you want to return a string, then you might want to use the join method for strings.)
Combine the functions you defined above to create an @interact object that takes a string s and prints:
(Hint: String formatting can be done using the % operator. Here is an example:
If you are familiar with C then you will notice that string formating is very similar to C ‘s sprintf statement.)
Use your interact object to explore this transformation, and to answer the following questions.
By comparing the BWT of a string with the first column of the array of sorted rotations of a string s , devise and implement an algorithm that reconstructs the first column of the array from the BWT of s .
By examining the first two columns of the array, devise and implement an algorithm that reconstructs the first two columns of the array from the BWT of a string. ( Hint: compare the last and first column with the first two columns.)
By examining the first three columns of the array, devise and implement an algorithm that reconstructs the first three columns of the array from the BWT of a string.
Write a function that reconstructs the entire array of sorted rotations of a string from the BWT of the string.
A Lyndon word is a word w that comes first in alphabetical order among all its rotations. Is the BWT invertible on Lyndon words?
Explain how one can modify the BWT to make it invertible on arbitrary words. Implement your modified transformation and the inverse transformation.