0 out of 464 challenges solved
Write a Python function `find_words_starting_with_p(words)` that takes a list of strings as input. Each string contains multiple words separated by spaces. The function should return the first two words from the first string in the list that start with the letter 'P' (case-sensitive). If no such pair of words exists, return `None`. #### Example Usage ```python [main.nopy] find_words_starting_with_p(["Python PHP", "Java JavaScript", "C C++"]) # Output: ('Python', 'PHP') find_words_starting_with_p(["Python Programming", "Java Programming"]) # Output: ('Python', 'Programming') find_words_starting_with_p(["Pqrst Pqr", "qrstuv"]) # Output: ('Pqrst', 'Pqr') find_words_starting_with_p(["Java JavaScript", "C C++"]) # Output: None ```
def find_words_starting_with_p(words): """ Finds the first two words starting with 'P' from the first string in the list. Args: words (list of str): A list of strings containing words. Returns: tuple or None: A tuple of the first two words starting with 'P', or None if not found. """ # Iterate through the list of strings for string in words: # Split the string into words word_list = string.split() # Filter words starting with 'P' p_words = [word for word in word_list if word.startswith('P')] # Check if there are at least two such words if len(p_words) >= 2: return (p_words[0], p_words[1]) # Return None if no such pair is found return None