def calc_rank(someset, n):
  """
  Calculates the rank of a subset `someset` of {1, 2, ..., `n`} 
  in the ordering given by grey sequenccce of characteristic vectors.
  """
  assoc_seq = [k + 1 in someset for k in range(n)]
  bit = False
  rank = 0
  for k, x in enumerate(assoc_seq):
    bit ^= x
    rank += bit * 2**(n - k - 1)
  return rank