from typing import Union
from typing import List
import torch


def sequence_accuracy(sequences: Union[List[str], torch.Tensor],
                      target_sequences: Union[List[str], torch.Tensor]) -> float:
    """

    What percentage out of the given sequences match the target sequences:
        1 if A == B else 0 with A and B being strings or list of tokens

    :param sequences: list of strings or tensor
    :param target_sequences: list of strings or tensor
    :return: mean accuracy over all pairs
    """
    if isinstance(sequences, torch.Tensor):
        sequences = sequences.tolist()
    if isinstance(target_sequences, torch.Tensor):
        target_sequences = target_sequences.tolist()
    equal = [
        sequence == target_sequence
        for sequence, target_sequence in zip(sequences, target_sequences)
    ]
    return sum(equal) / max(len(equal), 1)