import torch


def accuracy(scores: torch.Tensor, targets: torch.Tensor, k: int) -> float:
    """
    Compute the top-k accuracy.

    Parameters
    ----------
    scores : torch.Tensor
        Scores from the model

    targets : torch.Tensor
        True labels

    k : int
        k in top-k accuracy

    Returns
    -------
    accuracy : float
        Top-k accuracy
    """
    batch_size = targets.size(0)

    # return the indices of the top-k elements along the first dimension (along every row of a 2D Tensor), sorted
    _, ind = scores.topk(k, 1, True, True)

    # The target tensor is the same for each of the top-k predictions (words). Therefore, we need to expand it to
    # the same shape as the tensor (ind)
    # (double every label in the row --> so every row will contain k elements/k columns)
    correct = ind.eq(targets.view(-1, 1).expand_as(ind))

    # sum up the correct predictions --> we will now have one value (the sum)
    correct_total = correct.view(-1).float().sum()  # 0D tensor

    # devide by the batch_size and return the percentage
    return correct_total.item() * (100.0 / batch_size)