Python tips

Some quick details about python.

self.

If you are new to python, there may be some weird features to observe. Python uses the variable self to refer to member variables and methods within a class, so you will see it everywhere within a class. If you are familiar with the this keyword in Java or C++, it is a similar idea. If you want to modify or refer to a member variable, just remember to use the self. prefix.

self.start = "S" #set start variable

result = self.matchedSym("a") # call the matchedSym method with argument 'a'
# result is saved in local variable result.

0 indexing

Remember that Python (and C, and C++, and Java) start indexing arrays at 0, so the first character of string w is w[0]. You can use len() to find the length of any sequence (list, string, etc.). Note that in math notation and the psuedocode above, we use 1 indexing, so the first character of string w is w1. Adjust accordingly.

dictionaries

I recommend storing the table element denoted V[i, j] in a python dictionary. Below are some useful snippets that may help you with the syntax and semantics of dictionaries.

d = {} # create empty dictionary

i=1
j=2

#store the value 1 in the dictionary with the key (1,2)
d[(i,j)] = 1

#print the value associated with a key (i,j)
#if the key does not already exists, it will throw an error
print(d[(i,j)])

"""
you can store any object as a value. A list may be handy.
Python dictionary keys must be immutable, so a tuple (i,j) is fine,
but a list [i,j] is not a valid key.
"""

in

You can use the keyword in to check if an item is in a list or a key is in a dictionary.

if "a" in "apple":
  print("Yes!")

range

You can use the range(i,j) function to generate a list of values between i and j-1 inclusive. This is helpful for python for loops.

#print 1 through 9 inclusive
for i in range(1,10):
  print(i)

If i is ommited from the range() function, it is assumed to be 0.