"""
Problem 1: Raise to Power
Write a function raise_to_power(n) that takes an integer n and returns a function that takes a number x and raises it to the nth power. For example, if n is 5, the returned function should return x**5.
"""
def raise_to_power(n):
    """
    Returns a function that raises a given number to the nth power.
    
    Parameters:
    n (int or float): The exponent to which numbers will be raised.
    
    Returns:
    function: A function that takes a number and raises it to the power of n.
    """
    
    def power(x):
        """
        Raises x to the power of n.
        
        Parameters:
        x (int or float): The base number.
        
        Returns:
        int or float: The result of x raised to the power of n.
        """
        return x ** n
    
    return power


"""
Problem 2: File Writer
Write a function file_writer(filepath) that takes a string filepath and returns a function that takes a string and writes it to the file at filepath. For example, if filepath is 'path/to/file', the returned function should write text to the file at 'path/to/file'.
"""
def file_writer(filepath):
    """
    Returns a function that writes a given string to the specified file.
    
    Parameters:
    filepath (str): The path of the file where text will be written.
    
    Returns:
    function: A function that takes a string and appends it to the file.
    """
    
    def write_to_file(text):
        """
        Appends text to the file at filepath.
        
        Parameters:
        text (str): The text to write to the file.
        """
        with open(filepath, 'a') as my_file:
            my_file.write(text + '\n')
    
    return write_to_file

"""
Problem 3
"""
def word_n_of_each_line(n):
    """
    Returns a generator function that reads a file line by line and yields the nth word of each line.

    :param n: The position of the word to return (1-based index).
    :return: A generator function that takes a filepath argument.
    """
    def generator(filepath):
        with open(filepath, 'r', encoding='utf-8') as file:
            for line in file:
                words = line.strip().split()  # Split line into words
                yield words[n - 1] if len(words) >= n else None  # Yield nth word or None if line is too short

    return generator  # Return the function, not the generator itself
