class_docstr: A Gray code is essentially a Hamiltonian walk on
  a n-dimensional cube with edge length of one.
  The vertices of the cube are represented by vectors
  whose values are binary. The Hamilton walk visits
  each vertex exactly once. The Gray code for a 3d
  cube is ['000','100','110','010','011','111','101',
  '001'].
  
  A Gray code solves the problem of sequentially
  generating all possible subsets of n objects in such
  a way that each subset is obtained from the previous
  one by either deleting or adding a single object.
  In the above example, 1 indicates that the object is
  present, and 0 indicates that its absent.
  
  Gray codes have applications in statistics as well when
  we want to compute various statistics related to subsets
  in an efficient manner.
  
  Examples
  ========
  
  >>> from sympy.combinatorics import GrayCode
  >>> a = GrayCode(3)
  >>> list(a.generate_gray())
  ['000', '001', '011', '010', '110', '111', '101', '100']
  >>> a = GrayCode(4)
  >>> list(a.generate_gray())
  ['0000', '0001', '0011', '0010', '0110', '0111', '0101', '0100',     '1100', '1101', '1111', '1110', '1010', '1011', '1001', '1000']
  
  References
  ==========
  
  .. [1] Nijenhuis,A. and Wilf,H.S.(1978).
         Combinatorial Algorithms. Academic Press.
  .. [2] Knuth, D. (2011). The Art of Computer Programming, Vol 4
         Addison Wesley
class_docstr_tok: ["A","Gray","code","is","essentially","a","Hamiltonian","walk","on","a","n-dimensional","cube","with","edge","length","of","one",".","The","vertices","of","the","cube","are","represented","by","vectors","whose","values","are","binary",".","The","Hamilton","walk","visits","each","vertex","exactly","once",".","The","Gray","code","for","a","3d","cube","is","[","'000","'",",","'100","'",",","'110","'",",","'010","'",",","'011","'",",","'111","'",",","'101","'",",","'001","'","]",".","A","Gray","code","solves","the","problem","of","sequentially","generating","all","possible","subsets","of","n","objects","in","such","a","way","that","each","subset","is","obtained","from","the","previous","one","by","either","deleting","or","adding","a","single","object",".","In","the","above","example",",","1","indicates","that","the","object","is","present",",","and","0","indicates","that","its","absent",".","Gray","codes","have","applications","in","statistics","as","well","when","we","want","to","compute","various","statistics","related","to","subsets","in","an","efficient","manner",".","Examples","========",">",">",">","from","sympy.combinatorics","import","GrayCode",">",">",">","a","=","GrayCode","(","3",")",">",">",">","list","(","a.generate_gray","(",")",")","[","'000","'",",","'001","'",",","'011","'",",","'010","'",",","'110","'",",","'111","'",",","'101","'",",","'100","'","]",">",">",">","a","=","GrayCode","(","4",")",">",">",">","list","(","a.generate_gray","(",")",")","[","'0000","'",",","'0001","'",",","'0011","'",",","'0010","'",",","'0110","'",",","'0111","'",",","'0101","'",",","'0100","'",",","'1100","'",",","'1101","'",",","'1111","'",",","'1110","'",",","'1010","'",",","'1011","'",",","'1001","'",",","'1000","'","]","References","==========","..","[","1","]","Nijenhuis",",","A",".","and","Wilf",",","H.S.","(","1978",")",".","Combinatorial","Algorithms",".","Academic","Press",".","..","[","2","]","Knuth",",","D.","(","2011",")",".","The","Art","of","Computer","Programming",",","Vol","4","Addison","Wesley"]
docstr: Unranks an n-bit sized Gray code of rank k. This method exists
  so that a derivative GrayCode class can define its own code of
  a given rank.
  
  The string here is generated in reverse order to allow for tail-call
  optimization.
  
  Examples
  ========
  
  >>> from sympy.combinatorics import GrayCode
  >>> GrayCode(5, rank=3).current
  '00010'
  >>> GrayCode.unrank(5, 3)
  '00010'
  
  See Also
  ========
  
  rank
docstr_tok: ["Unranks","an","n-bit","sized","Gray","code","of","rank","k.","This","method","exists","so","that","a","derivative","GrayCode","class","can","define","its","own","code","of","a","given","rank",".","The","string","here","is","generated","in","reverse","order","to","allow","for","tail-call","optimization",".","Examples","========",">",">",">","from","sympy.combinatorics","import","GrayCode",">",">",">","GrayCode","(","5",",","rank=3",")",".current","'00010'",">",">",">","GrayCode.unrank","(","5",",","3",")","'00010'","See","Also","========","rank"]
code: def unrank(self, n, rank):
      """
      Unranks an n-bit sized Gray code of rank k. This method exists
      so that a derivative GrayCode class can define its own code of
      a given rank.
  
      The string here is generated in reverse order to allow for tail-call
      optimization.
  
      Examples
      ========
  
      >>> from sympy.combinatorics import GrayCode
      >>> GrayCode(5, rank=3).current
      '00010'
      >>> GrayCode.unrank(5, 3)
      '00010'
  
      See Also
      ========
  
      rank
      """
  
      def _unrank(k, n):
          if n == 1:
              return str(k % 2)
          m = 2 ** (n - 1)
          if k < m:
              return "0" + _unrank(k, n - 1)
          return "1" + _unrank(m - (k % m) - 1, n - 1)
  
      return _unrank(rank, n)
  
file_path: sympy/sympy/combinatorics/graycode.py
imports: from sympy.core import Basic, Integer
  import random
label_desc: Use image node_id 9 for calling the GrayCode obj's underlying member method code with example usage: obj.unrank(n, rank) and returns: _unrank, unknown, str, unknown