#iterations is a parameter that controls how many times
#the morphism is iterated. For sufficiently large values
#of iterations (depending on n), this procedure will generate
#the first n terms of the sequence
def firstNTerms(n, iterations):
#define the morphism
morph = dict()
morph['0'] = '001'
morph['1'] = '110'
#construct the fixed point
word = '0'
for i in xrange(0, iterations):
word2 = ''
for c in word:
word2 += morph[c]
word = word2
#find the abelian complexity
terms = []
for i in xrange(1, n + 1):
absubwords = set([])
for j in xrange(0, len(word) - i + 1):
subword = list(word[j:j + i])
subword.sort()
absubwords.add(''.join(subword))
terms.append(len(absubwords))
return terms