### Navigation

- index
- modules |
- previous |
- Combinatorics »
- Developer Tools »

# Rankers

`sage.combinat.ranker.``from_list`(l)

  Returns a ranker from the list l.

  INPUT:

  - `l` - a list

  OUTPUT:

  - `[rank, unrank]` - functions

  EXAMPLES:

      sage: import sage.combinat.ranker as ranker
      sage: l = [1,2,3]
      sage: r,u = ranker.from_list(l)
      sage: r(1)
      0
      sage: r(3)
      2
      sage: u(2)
      3
      sage: u(0)
      1

`sage.combinat.ranker.``on_fly`()

  Returns a pair of enumeration functions rank / unrank.

  rank assigns on the fly an integer, starting from 0, to any object passed as argument. The object should be hashable. unrank is the inverse function; it returns None for indices that have not yet been assigned.

  EXAMPLES:

      sage: [rank, unrank] = sage.combinat.ranker.on_fly()
      sage: rank('a')
      0
      sage: rank('b')
      1
      sage: rank('c')
      2
      sage: rank('a')
      0
      sage: unrank(2)
      'c'
      sage: unrank(3)
      sage: rank('d')
      3
      sage: unrank(3)
      'd'

  Todo

  add tests as in combinat::rankers

`sage.combinat.ranker.``rank_from_list`(l)

  Returns a rank function given a list l.

  EXAMPLES:

      sage: import sage.combinat.ranker as ranker
      sage: l = [1,2,3]
      sage: r = ranker.rank_from_list(l)
      sage: r(1)
      0
      sage: r(3)
      2

`sage.combinat.ranker.``unrank`(L, i)

  Return the \(i\)-th element of \(L\).

  INPUT:

  - `L` – a list, tuple, finite enumerated set, ...
  - `i` – an int or `Integer`

  The purpose of this utility is to give a uniform idiom to recover the \(i\)-th element of an object `L`, whether `L` is a list, tuple (or more generally a `collections.Sequence`), an enumerated set, some old parent of Sage still implementing unranking in the method `__getitem__`, or an iterable (see `collections.Iterable`). See trac ticket #15919.

  EXAMPLES:

  Lists, tuples, and other `sequences`:

      sage: from sage.combinat.ranker import unrank
      sage: unrank(['a','b','c'], 2)
      'c'
      sage: unrank(('a','b','c'), 1)
      'b'
      sage: unrank(xrange(3,13,2), 1)
      5

  Enumerated sets:

      sage: unrank(GF(7), 2)
      2
      sage: unrank(IntegerModRing(29), 10)
      10

  An old parent with unranking implemented in `__getitem__`:

      sage: M = MatrixSpace(GF(3), 2, 2)
      sage: hasattr(M, "unrank")
      False
      sage: M[42]
      [1 0]
      [2 1]
      sage: unrank(M, 42)
      [1 0]
      [2 1]

  An iterable:

      sage: unrank(NN,4)
      4

  An iterator:

      sage: unrank(('a{}'.format(i) for i in range(20)), 0)
      'a0'
      sage: unrank(('a{}'.format(i) for i in range(20)), 2)
      'a2'

  Warning

  When unranking an iterator, it returns the `i`-th element beyond where it is currently at:

      sage: from sage.combinat.ranker import unrank
      sage: it = iter(range(20))
      sage: unrank(it, 2)
      2
      sage: unrank(it, 2)
      5

  TESTS:

      sage: from sage.combinat.ranker import unrank
      sage: unrank(range(3), 10)
      Traceback (most recent call last):
      ...
      IndexError: list index out of range

      sage: unrank(('a{}'.format(i) for i in range(20)), 22)
      Traceback (most recent call last):
      ...
      IndexError: index out of range

      sage: M[100]
      Traceback (most recent call last):
      ...
      IndexError: list index out of range

`sage.combinat.ranker.``unrank_from_list`(l)

  Returns an unrank function from a list.

  EXAMPLES:

      sage: import sage.combinat.ranker as ranker
      sage: l = [1,2,3]
      sage: u = ranker.unrank_from_list(l)
      sage: u(2)
      3
      sage: u(0)
      1

### Navigation

- index
- modules |
- previous |
- Combinatorics »
- Developer Tools »