r""" Python module for OEIS sequence number A052008. 'n with digits sorted ascending' + 'n with digits sorted descending'. Examples of use. ----------------------------------------------------------------------- >>> from a052008 import * >>> print a052008_list(16) [2, 4, 6, 8, 10, 12, 14, 16, 18, 11, 22, 33, 44, 55, 66, 77] >>> print a052008_offset 0 >>> for x in a052008_list_pairs(6): ... print x ... (0, 2) (1, 4) (2, 6) (3, 8) (4, 10) (5, 12) >>> print a052008(5) 12 ----------------------------------------------------------------------- """ from itertools import islice, izip, count __all__ = ('a052008_offset', 'a052008_list', 'a052008_list_pairs', 'a052008', 'a052008_gen') __author__ = 'Nick Hobson ' a052008_offset = offset = 0 def a052008_gen(): """Generator function for OEIS sequence A052008.""" for n in count(1): z = [] while n > 0: n, d = divmod(n, 10) z.append(d) z.sort() x = reduce(lambda s, t: 10*s + t, z) + reduce(lambda s, t: 10*s + t, reversed(z)) yield x def a052008_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(a052008_gen(), n)) def a052008_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), a052008_gen())) def a052008(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(a052008_gen(), n-offset, n-offset+1)).pop()