r""" Python module for OEIS sequence number A118248. Write down the binary numbers 1, 10, ... , but omit any number (such as 11 or 101) that has appeared as a string earlier in the sequence. A118248 is the decimal conversion of the binary sequence. Examples of use. ----------------------------------------------------------------------- >>> from a118248 import * >>> print a118248_list(16) [0, 1, 2, 4, 7, 8, 11, 16, 18, 21, 22, 25, 29, 31, 32, 35] >>> print a118248_offset 0 >>> for x in a118248_list_pairs(6): ... print x ... (0, 0) (1, 1) (2, 2) (3, 4) (4, 7) (5, 8) >>> print a118248_list_upto(21) [0, 1, 2, 4, 7, 8, 11, 16, 18, 21] >>> print a118248(3) 4 ----------------------------------------------------------------------- """ from itertools import islice, izip, takewhile, count __all__ = ('a118248_offset', 'a118248_list', 'a118248_list_pairs', 'a118248_list_upto', 'a118248', 'a118248_gen') __author__ = 'Nick Hobson ' a118248_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 a118248_gen(): """Generator function for OEIS sequence A118248.""" st = '0' yield 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 yield n def a118248_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(a118248_gen(), n)) def a118248_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), a118248_gen())) def a118248_list_upto(m): """Returns a list of all terms not exceeding m > 0.""" return list(takewhile(lambda t: t <= m, a118248_gen())) def a118248(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(a118248_gen(), n-offset, n-offset+1)).pop()