r""" Python module for OEIS sequence number A048991. Write down the numbers 1, 2, 3, ... , but omit any number (such as 12 or 101) which has appeared as a string earlier in the sequence. Examples of use. ----------------------------------------------------------------------- >>> from a048991 import * >>> print a048991_list(15) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16] >>> print a048991_offset 1 >>> for x in a048991_list_pairs(6): ... print x ... (1, 1) (2, 2) (3, 3) (4, 4) (5, 5) (6, 6) >>> print a048991_list_upto(10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> print a048991(5) 5 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a048991_offset', 'a048991_list', 'a048991_list_pairs', 'a048991_list_upto', 'a048991', 'a048991_gen') __author__ = 'Nick Hobson ' a048991_offset = offset = 1 def a048991_gen(): """Generator function for OEIS sequence A048991.""" st = '' for n in count(1): t = str(n) if st.find(t) == -1: st += t yield n def a048991_list(n): """Returns a list of the first n >= 0 terms.""" if n < 0: raise ValueError, 'Input must be a non-negative integer' return list(islice(a048991_gen(), n)) def a048991_list_pairs(n): """Returns a list of tuples (n, a(n)) of the first n >= 0 terms.""" if n < 0: raise ValueError, 'Input must be a non-negative integer' return list(izip(xrange(offset, n+offset), a048991_gen())) def a048991_list_upto(m): """Returns a list of all terms not exceeding m > 0.""" if m < 1: raise ValueError, 'Input must be a positive integer' return list(takewhile(lambda t: t <= m, a048991_gen())) def a048991(n): """Returns the term with index n >= 1; offset 1.""" if n < offset: raise ValueError, 'Input must be an integer >= offset = ' + str(offset) return list(islice(a048991_gen(), n-offset, n-offset+1)).pop()