def compute_precision_at_k(real_gains, predicted_gains, k=5):
  """This function computes precision-at-k, i.e. the proportion of recommended items in the top-k set 
  that are relevant (belong to real_gains).

  NOTE THAT IT IS NOT AS STRICT AS compute_r_precision!
  """
  relevant_documents = [elem[0] for elem in real_gains]
  retrieved_documents = [elem[0] for elem in sorted(predicted_gains, key = lambda x:x[1], reverse=True)][:k]
  if relevant_documents and retrieved_documents:
    return len(set(relevant_documents) & set(retrieved_documents))/len(retrieved_documents)
  return float('nan')