def words_before_index(text, idx):
    """Counts the number of words in the text before idx"""
    while text[idx] != ' ':
        idx -= 1
    if idx <= 0:
        return 0
    n_words = len(text[:idx].split(' '))
    return n_words