r"""
Python module for OEIS sequence number A105391.

Numbers m such that there is an equal number of numbers <= m that
are contained and that are not contained in the concatenation of
terms <= m in A048991.

Examples of use.
-----------------------------------------------------------------------
>>> from a105391 import *
>>> print a105391_list(6)
[740, 1260, 1262, 5230, 15804, 15814]
>>> print a105391_offset
1
>>> for x in a105391_list_pairs(6):
...     print x
...
(1, 740)
(2, 1260)
(3, 1262)
(4, 5230)
(5, 15804)
(6, 15814)
>>> print a105391(3)
1262
-----------------------------------------------------------------------
"""

from itertools import islice, izip, count

__all__ = ('a105391_offset', 'a105391_list', 'a105391_list_pairs', 'a105391', 'a105391_gen')
__author__ = 'Nick Hobson <nickh@qbyte.org>'

a105391_offset = offset = 1

def a105391_gen():
    """Generator function for OEIS sequence A105391."""
    st, c = '', 0
    for n in count(1):
        t = str(n)
        if st.find(t) == -1:
            st += t
            c += 1
        if c == n - c:
            yield n

def a105391_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(a105391_gen(), n))

def a105391_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), a105391_gen()))

def a105391(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(a105391_gen(), n-offset, n-offset+1)).pop()