import math


def dcg(labels, at=None):
  """Compute DCG given labels in order."""
  result = 0.0
  position = 2
  for i in labels:
    if i != 0:
      result += 1 / math.log2(position)
    position += 1
    if at is not None and (position >= at + 2):
      break
  return result