import re


def add_selections_to_tweet(tweet_text, selections):
    """Replace placeholder characters with correct words given a selections list.

    Args:
        tweet_text (str): A tweet template sentence from tweet_content.yaml
        selections (list): A list of the names of the segments of the day (in preference order)

    Returns:
        text_with_selection: A grammatically correct sentence with the selections substituted in

    """
    text_with_selection = tweet_text
    for selection in selections:
        text_with_selection = re.sub("NOUN", selection, text_with_selection, count=1)

    if selections[0] == "afternoon" or selections[0] == "evening":
        text_with_selection = re.sub("A\(N\)", "an", text_with_selection)
    else:
        text_with_selection = re.sub("A\(N\)", "a", text_with_selection)
    return text_with_selection