r""" Python module for OEIS sequence number A128291. Complement of A118248. (Write down the numbers 1, 10, ... , but omit any number (such as 11 or 101) which has appeared as a string earlier in the sequence. A118248 is the decimal equivalent of the binary sequence. A128291 is the the sequence of numbers omitted from A118248.) Examples of use. ----------------------------------------------------------------------- >>> from a128291 import * >>> print a128291_list(16) [3, 5, 6, 9, 10, 12, 13, 14, 15, 17, 19, 20, 23, 24, 26, 27] >>> print a128291_offset 0 >>> for x in a128291_list_pairs(6): ... print x ... (0, 3) (1, 5) (2, 6) (3, 9) (4, 10) (5, 12) >>> print a128291_list_upto(20) [3, 5, 6, 9, 10, 12, 13, 14, 15, 17, 19, 20] >>> print a128291(3) 9 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a128291_offset', 'a128291_list', 'a128291_list_pairs', 'a128291_list_upto', 'a128291', 'a128291_gen') __author__ = 'Nick Hobson ' a128291_offset = offset = 0 digit = {'0':'0000', '1':'0001', '2':'0010', '3':'0011', '4':'0100', '5':'0101', '6':'0110', '7':'0111', '8':'1000', '9':'1001', 'a':'1010', 'b':'1011', 'c':'1100', 'd':'1101', 'e':'1110', 'f':'1111', 'L':''} def a128291_gen(): """Generator function for OEIS sequence A128291.""" st = '0' for n in count(1): t = ''.join([digit[x] for x in hex(n)[2:]]).lstrip('0') if st.find(t) == -1: st += t else: yield n def a128291_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(a128291_gen(), n)) def a128291_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), a128291_gen())) def a128291_list_upto(m): """Returns a list of all terms not exceeding m > 0.""" return list(takewhile(lambda t: t <= m, a128291_gen())) def a128291(n): """Returns the term with index n >= 0; offset 0.""" if n < offset: raise ValueError, 'Input must be an integer >= offset = ' + str(offset) return list(islice(a128291_gen(), n-offset, n-offset+1)).pop()