0 out of 464 challenges solved
Write a Python function `remove_length` that removes all words of a specified length `k` from a given string. #### Function Signature ```python [main.nopy] def remove_length(input_string: str, k: int) -> str: ``` #### Parameters - `input_string` (str): The input string containing words separated by spaces. - `k` (int): The length of words to be removed. #### Returns - `str`: A string with all words of length `k` removed. #### Example Usage ```python [main.nopy] remove_length("The person is most value tet", 3) # Output: "person is most value" remove_length("If you told me about this ok", 4) # Output: "If you me about ok" remove_length("Forces of darkeness is come into the play", 4) # Output: "Forces of darkeness is the" ```
def remove_length(input_string: str, k: int) -> str: """ Remove all words of length k from the input string. Args: input_string (str): The input string containing words. k (int): The length of words to remove. Returns: str: The modified string with specified words removed. """ # Split the input string into words words = input_string.split() # Filter out words of length k filtered_words = [word for word in words if len(word) != k] # Join the filtered words back into a string result = ' '.join(filtered_words) return result