from typing import List
import logging


def get_sentences_by_scores(n_sentences: int, scores, sentences: List[str], maximise_score: bool):
    """
    Return the sentences which maximise/minimise the given scores preserving their original order
    :param n_sentences: number of sentences to include in the output list
    :param scores: scores associated with the given sentences
    :param sentences: List of sentences to evaluate
    :param maximise_score: whether to choose sentence that maximise or minimise the given scores
    :return:
    """
    logging.info("get_sentences_by_scores >>>")
    # Create a dictionary for storing sentences and their position in the given text
    sentences_with_index = {}
    for i, sentence in enumerate(sentences):
        sentences_with_index[sentence] = i
    # Order sentences by their score
    ranked_sentences = sorted(((scores[i], s) for i, s in enumerate(sentences)),
                              reverse=maximise_score)
    # Keep only most meaningful sentences
    ranked_sentences = ranked_sentences[0:n_sentences]
    # Get the indexes of these meaningful sentences
    summary_sentences_index = []
    for ranked_sentence in ranked_sentences:
        current_sentence = ranked_sentence[1]
        current_sentence_index = sentences_with_index[current_sentence]
        summary_sentences_index.append(current_sentence_index)
    # Sort indexes in ascending order: in this way we will maintain article coherence
    summary_sentences_index.sort()
    summary = [sentences[i] for i in summary_sentences_index]
    logging.info("get_sentences_by_scores <<<")
    return summary