import intseqdb

# a function that return a function defining a generator
# for the Catalan numbers: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ...
def catalan_gen():
	def _catalan_gen():
		i = 0
		val = 1
		while True:
			yield val
			val = val * (2 * (2*i + 1)) / (i + 2)
			i   = i + 1
        # only return the function defining the generator
	return _catalan_gen

# a function that return a function defining a generator
# for the powers of n: n^0, n^1, n^2, n^3, ...
def power_gen(n):
	def _power_gen():
		val = 1
		while True:
			yield val
			val = val * n
	# only return the function defining the generator
	return _power_gen

# create a database (only print the 10 first elements of each sequence)
db = intseqdb.Database(10)
# add some integer sequences into the database
db.add_sequence(intseqdb.Sequence("Powers of 1", power_gen(1)))
db.add_sequence(intseqdb.Sequence("Powers of 2", power_gen(2)))
db.add_sequence(intseqdb.Sequence("Powers of 4", power_gen(4)))
db.add_sequence(intseqdb.Sequence("Catalan numbers", catalan_gen()))
# print everything, i.e., all sequences stored in the database
print '=*=*=*=*=*=*=*=*=*=*=*=*='
print "number of sequences: %d" % len(db)
for sequence in db:
    print sequence
# search and print all sequences that begin as 1, 1
print '=*=*=*=*=*=*=*=*=*=*=*=*='
print 'all sequences that begin as 1, 1'
for sequence in db.search([1, 1]):
    print sequence
# search and print all sequences that begin as 1, 1, 2
print '=*=*=*=*=*=*=*=*=*=*=*=*='
print 'all sequences that begin as 1, 1, 2'
for sequence in db.search([1, 1, 2]):
    print sequence
# search and print all sequences that begin as 1, 1, 2, 3
print '=*=*=*=*=*=*=*=*=*=*=*=*='
print 'all sequences that begin as 1, 1, 2, 3'
for sequence in db.search([1, 1, 2, 3]):
    print sequence
print '=*=*=*=*=*=*=*=*=*=*=*=*='    
