source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
391
Write a Python function named `swap_case` that takes a string as input and returns the string with the case of each letter swapped (lowercase letters become uppercase and vice versa). The function should handle any string, including those with non-alphabetic characters, and should not modify these characters. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_case(string: str) -> str: """ Swaps the case of each letter in a given string. Parameters: string (str): The input string. Returns: str: The string with the case of each letter swapped. """ # H...
code_exercise_27k
392
Write a Python function to remove all punctuation characters from a given string. The function should handle various types of punctuation and return the cleaned string. The code should start with: ```python import string def remove_punctuation(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation(input_string: str) -> str: """ Removes all punctuation characters from a given string. Args: input_string (str): The input string from which punctuation characters are to be rem...
code_exercise_27k
393
Write a Python function named `extract_numbers` that takes a string as input and returns a list of all the numbers (integer or float) found in the string. The function should handle both positive and negative numbers, as well as numbers in scientific notation. The code should start with: ```python import re from typing...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_numbers(string: str) -> List[float]: """ Extracts all numbers (integer or float) from a given string and returns them as a list of floats. Args: string (str): The input...
code_exercise_27k
394
Write a Python function named `remove_vowels` that takes a string as input and returns the string with all vowels removed. The function should handle both uppercase and lowercase vowels. The code should start with: ```python def remove_vowels(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_vowels(string: str) -> str: """ Removes all vowels from a given string. Args: string (str): The input string from which vowels are to be removed. Returns: str: The string with all vowels remo...
code_exercise_27k
396
Write a Python function to reverse the order of words in a given string. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with leading or trailing spaces. The function should return the string with the order of words reversed, maintaining the original spaci...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(string: str) -> str: """ Reverses the order of words in a given string. Args: string (str): The input string to reverse the order of words. Returns: str: The string with the order of w...
code_exercise_27k
398
Write a Python function named `remove_non_alphabetic` that takes a string as input and returns the string with all non-alphabetic characters removed. The function should only keep alphabetic characters (both uppercase and lowercase) and spaces. The code should start with: ```python def remove_non_alphabetic(string: str...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_non_alphabetic(string: str) -> str: """ Removes all non-alphabetic characters from a given string, keeping only alphabetic characters and spaces. Args: string (str): The input string from which non-alphab...
code_exercise_27k
400
Write a Python function to count the number of words in a given string, considering various edge cases such as empty strings, strings with multiple spaces, and strings with punctuation. The function should handle these cases gracefully and return the correct word count. The code should start with: ```python def count_...
Here is the Python code to solve the given problem in the markdown block: ```python def count_words(string: str) -> int: """ Counts the number of words in a given string. This function handles various edge cases such as empty strings, strings with multiple spaces, and strings with punctuation. It norma...
code_exercise_27k
402
Write a Python function named `extract_emails` that takes a string as input and returns a list of all the email addresses found in the string. The function should use a regular expression to identify and extract valid email addresses. The code should start with: ```python import re from typing import List def extract_...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_emails(string: str) -> List[str]: """ Extracts all email addresses from a given string and returns them as a list. Args: string (str): The input string from which to ex...
code_exercise_27k
405
Write a Python function named `remove_lowercase` that takes a string as input and returns the string with all lowercase letters removed. The function should handle an empty string and strings with no lowercase letters. The code should start with: ```python def remove_lowercase(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_lowercase(string: str) -> str: """ Removes all lowercase letters from a given string. Args: string (str): The input string from which to remove lowercase letters. Returns: str: The string wit...
code_exercise_27k
406
Write a Python function to extract all hashtags from a given string. A hashtag is defined as a word or phrase preceded by the '#' symbol. The function should return a list of all the hashtags found in the string. The code should start with: ```python from typing import List def extract_hashtags(string: str) -> List[st...
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List import re def extract_hashtags(string: str) -> List[str]: """ Extracts all hashtags from a given string and returns them as a list. A hashtag is defined as a word or phrase preceded by the '#' symb...
code_exercise_27k
407
Write a Python function named `remove_uppercase` that takes a string as input and returns the string with all uppercase letters removed. The function should be case-sensitive and only remove uppercase letters, leaving lowercase letters, digits, and special characters unchanged. The code should start with: ```python de...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_uppercase(string: str) -> str: """ Removes all uppercase letters from a given string. Args: string (str): The input string from which to remove uppercase letters. Returns: str: The string wit...
code_exercise_27k
408
Write a Python function named `extract_phone_numbers` that takes a string as input and returns a list of all the phone numbers found in the string. A phone number is defined as a sequence of digits, optionally separated by hyphens or spaces. The function should be able to handle various formats of phone numbers, such a...
Here is the Python code to solve the given problem in the markdown block: ```python import re def extract_phone_numbers(string: str) -> list: """ Extracts all phone numbers from a given string and returns them as a list. A phone number is defined as a sequence of digits, optionally separated by hyphens or ...
code_exercise_27k
409
Write a Python function named `remove_non_alphanumeric` that takes a string as input and returns the string with all non-alphanumeric characters removed. Non-alphanumeric characters include any characters that are not letters (both uppercase and lowercase) or digits. The function should handle empty strings and strings...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_non_alphanumeric(string: str) -> str: """ Removes all non-alphanumeric characters from a given string. Args: string (str): The input string from which non-alphanumeric characters will be remove...
code_exercise_27k
410
Write a Python function named `extract_dates` that takes a string as input and returns a list of all the dates found in the string. A date is defined as a sequence of digits representing day, month, and year, separated by slashes (`/`) or hyphens (`-`). The function should be able to handle various date formats, includ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def extract_dates(string: str) -> list: """ Extracts all dates from a given string and returns them as a list. A date is defined as a sequence of digits representing day, month, and year, separated by slashes or h...
code_exercise_27k
412
Write a Python function named `extract_credit_card_numbers` that takes a string as input and returns a list of all the credit card numbers found in the string. A credit card number is defined as a sequence of 16 digits, optionally separated by hyphens or spaces in groups of 4 digits. The function should handle various ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def extract_credit_card_numbers(string: str) -> list: """ Extracts all credit card numbers from a given string and returns them as a list. A credit card number is defined as a sequence of 16 digits, optionally sep...
code_exercise_27k
413
Write a Python function named `remove_repeated_characters` that takes a string as input and returns the string with any repeated characters removed. Only the first occurrence of each character should be kept. The function should handle an empty string and maintain the order of characters as they appear in the input str...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_repeated_characters(string: str) -> str: """ Removes any repeated characters from a given string. Only the first occurrence of each character is kept. Args: string (str): The input string from which r...
code_exercise_27k
414
Write a Python function to extract all valid IP addresses from a given string. An IP address is defined as a sequence of four numbers separated by periods, where each number is between 0 and 255. The function should validate the IP addresses to ensure they are within the valid range. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import re def extract_valid_ip_addresses(string: str) -> List[str]: """ Extracts all valid IP addresses from a given string and returns them as a list. An IP address is defined as a sequence of four ...
code_exercise_27k
416
Write a Python function to extract all mentions from a given string. A mention is defined as a word or phrase preceded by the '@' symbol. The function should handle various cases, including multiple mentions, mentions with underscores, and mentions with numbers. The code should start with: ```python import re from typi...
Here is the Python code to solve the given problem in the markdown block: : ```python import re from typing import List def extract_mentions(string: str) -> List[str]: """ Extracts all mentions from a given string and returns them as a list. A mention is defined as a word or phrase preceded by the '@' symb...
code_exercise_27k
417
Write a Python function named `remove_digits` that takes a string as input and returns the string with all numeric digits removed. The function should handle various edge cases, such as an empty string, a string with no digits, and a string consisting entirely of digits. The function should also be efficient and avoid ...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_digits(string: str) -> str: """ Removes all numeric digits from a given string. Args: string (str): The input string from which to remove numeric digits. Returns: str: The string with all num...
code_exercise_27k
420
Write a Python function to extract all emoticons from a given string. An emoticon is defined as a sequence of characters representing a facial expression, typically starting with one of the characters `:`, `;`, or `=` followed by an optional `-` and ending with one or more of the characters `)`, `(`, `|`, `D`, `P`, `O`...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_emoticons(string: str) -> List[str]: """ Extracts all emoticons from a given string and returns them as a list. An emoticon is defined as a sequence of characters representing a...
code_exercise_27k
421
Write a Python function named `reverse_string` that takes a string as input and returns the reverse of the input string. The function should handle an empty string and should not use Python's built-in slicing or reverse methods directly. Instead, implement the reversal using a loop or recursion. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(input_string: str) -> str: """ Reverses the input string without using built-in slicing or reverse methods. Args: input_string (str): The string to be reversed. Returns: str: The reve...
code_exercise_27k
422
Write a Python function named `count_vowels` that takes a string as input and returns the number of vowels (a, e, i, o, u) in the input string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels. The code should start with: ```python def count_vowels(input_string: str) ...
Here is the Python code to solve the given problem in the markdown block: ```python def count_vowels(input_string: str) -> int: """ Counts the number of vowels (a, e, i, o, u) in the input string. Args: input_string (str): The string to count vowels from. Returns: int: The number of vo...
code_exercise_27k
423
Write a Python function named `remove_duplicates` that takes a string as input and returns a new string with all duplicate characters removed, while maintaining the original order of characters. The function should handle an empty string and should not use any built-in functions or methods that directly solve the probl...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_duplicates(input_string: str) -> str: """ Removes duplicate characters from the input string while maintaining the original order of characters. Args: input_string (str): The string from which to remove d...
code_exercise_27k
428
Write a Python function named `reverse_words` that takes a string as input and returns a new string with the words in reverse order. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with leading or trailing spaces. The code should start with: ```python def ...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(input_string: str) -> str: """ Reverses the order of words in the given string. Args: input_string (str): The string to reverse the order of words. Returns: str: The new string with th...
code_exercise_27k
429
Write a Python function named `remove_whitespace` that takes a string as input and returns a new string with all whitespace characters (spaces, tabs, and newlines) removed. The function should handle various types of whitespace characters and return the modified string without any whitespace. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespace(input_string: str) -> str: """ Removes all whitespace characters (spaces, tabs, and newlines) from the input string. Args: input_string (str): The string from which to remove whitespace charact...
code_exercise_27k
433
Write a Python function named `remove_punctuation` that takes a string as input and returns a new string with all punctuation characters removed. The function should handle various types of punctuation and should not modify the original string. The code should start with: ```python import string def remove_punctuation...
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation(input_string: str) -> str: """ Removes all punctuation characters from the given input string. Args: input_string (str): The string from which punctuation characters are to be r...
code_exercise_27k
434
Write a Python function named `title_case` that takes a string as input and returns a new string with the first letter of each word capitalized and all other letters in lowercase. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with punctuation. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python def title_case(input_string: str) -> str: """ Converts the input string to title case, where the first letter of each word is capitalized and all other letters are in lowercase. Args: input_string (str): The string ...
code_exercise_27k
436
Write a Python function named `count_occurrences` that takes two strings as input: a main string and a substring. The function should return the number of non-overlapping occurrences of the substring in the main string. The function should be case-sensitive. The code should start with: ```python def count_occurrences(...
Here is the Python code to solve the given problem in the markdown block: ```python def count_occurrences(main_string: str, substring: str) -> int: """ Counts the number of non-overlapping occurrences of a substring in a main string. Args: main_string (str): The main string in which to search for o...
code_exercise_27k
439
Write a Python function named `reverse_words_case_insensitive` that takes a string as input and returns a new string with the words in reverse order, while ignoring the case of the characters. The function should handle various edge cases, such as empty strings, strings with only one word, and strings with multiple spa...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_case_insensitive(input_string: str) -> str: """ Reverses the order of words in a given string while ignoring the case of the characters. Args: input_string (str): The input string to reverse the or...
code_exercise_27k
441
Write a Python function named `remove_whitespace_except_newlines` that takes a string as input and returns a new string with all whitespace characters (spaces and tabs) removed, except for newlines. The function should handle various edge cases, such as empty strings, strings with only newlines, and strings with no whi...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespace_except_newlines(input_string: str) -> str: """ Removes all whitespace characters (spaces and tabs) from the input string, except for newlines. Args: input_string (str): The string from which to...
code_exercise_27k
446
Write a Python function named `remove_punctuation_except_spaces` that takes a string as input and returns a new string with all punctuation characters removed, except for spaces. The function should handle various punctuation marks and ensure that spaces are preserved. The code should start with: ```python import stri...
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation_except_spaces(input_string: str) -> str: """ Removes all punctuation characters from the input string, except for spaces. Args: input_string (str): The string from which punctua...
code_exercise_27k
449
Write a Python function named `count_occurrences_case_insensitive` that takes two strings as input: a main string and a substring. The function should return the number of occurrences of the substring in the main string, ignoring the case of the characters. The function should handle edge cases such as empty strings an...
Here is the Python code to solve the given problem in the markdown block: ```python def count_occurrences_case_insensitive(main_string: str, substring: str) -> int: """ Counts the number of occurrences of a substring in a main string, ignoring case. Args: main_string (str): The main string in which...
code_exercise_27k
451
Write a Python function named `remove_duplicates_case_insensitive` that takes a string as input and returns a new string with all duplicate characters removed, ignoring the case of the characters. The original order of characters should be maintained. The function should handle edge cases such as an empty string or a s...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_duplicates_case_insensitive(input_string: str) -> str: """ Removes duplicate characters from the input string, ignoring case, and maintains the original order of characters. Args: input_string (str): The ...
code_exercise_27k
452
Write a Python function named `reverse_words_case_insensitive` that takes a string as input and returns a new string with the words in reverse order, ignoring the case of the characters. The function should handle various edge cases, such as empty strings, strings with only one word, and strings with multiple spaces be...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_case_insensitive(input_string: str) -> str: """ Reverses the order of words in a given string, ignoring the case of the characters. Args: input_string (str): The input string to reverse the order o...
code_exercise_27k
454
Write a Python function named `remove_whitespace_except_newlines_and_tabs` that takes a string as input and returns a new string with all whitespace characters (spaces and tabs) removed, except for newlines and tabs. The function should handle various edge cases, such as empty strings, strings with only newlines or tab...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespace_except_newlines_and_tabs(input_string: str) -> str: """ Removes all whitespace characters (spaces and tabs) from the input string, except for newlines and tabs. Args: input_string (str): The in...
code_exercise_27k
459
Write a Python function named `remove_punctuation_except_spaces_and_tabs` that takes a string as input and returns a new string with all punctuation characters removed, except for spaces and tabs. The function should handle various edge cases, such as empty strings, strings with only punctuation, and strings with no pu...
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation_except_spaces_and_tabs(input_string: str) -> str: """ Removes all punctuation characters from the input string, except for spaces and tabs. Args: input_string (str): The string ...
code_exercise_27k
472
Write a Python function named `remove_punctuation_except_spaces_and_tabs_and_newlines` that takes a string as input and returns a new string with all punctuation characters removed, except for spaces, tabs, and newlines. The function should handle various edge cases, such as empty strings, strings with only punctuation...
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation_except_spaces_and_tabs_and_newlines(input_string: str) -> str: """ Removes all punctuation characters from the input string, except for spaces, tabs, and newlines. Args: input_s...
code_exercise_27k
475
Write a Python function named `count_occurrences_case_insensitive_and_whitespace_agnostic_and_punctuation_agnostic` that takes two strings as input: a main string and a substring. The function should return the number of occurrences of the substring in the main string, ignoring the case of the characters, ignoring whit...
Here is the Python code to solve the given problem in the markdown block: ```python import string def count_occurrences_case_insensitive_and_whitespace_agnostic_and_punctuation_agnostic(main_string: str, substring: str) -> int: """ Counts the number of occurrences of a substring in a main string, ignoring case...
code_exercise_27k
486
Write a Python function named `reverse_string` that takes a string as a parameter and returns the reverse of that string. The function should handle any string input, including empty strings and strings with special characters. The code should start with: ```python def reverse_string(text: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(text: str) -> str: """ Reverses the input string. Args: text (str): The string to be reversed. Returns: str: The reversed string. """ # Ensure the input is a string if not...
code_exercise_27k
487
Write a Python function named `get_even_indices` that takes a string as a parameter and returns a new string containing only the characters at even indices (0-based) of the original string. The function should handle strings of any length, including empty strings. The code should start with: ```python def get_even_ind...
Here is the Python code to solve the given problem in the markdown block: ```python def get_even_indices(text: str) -> str: """ Returns a new string containing only the characters at even indices (0-based) of the original string. Args: text (str): The input string from which to extract characters a...
code_exercise_27k
489
Write a Python function named `get_first_n_chars` that takes two parameters: a string `text` and an integer `n`. The function should return the first `n` characters of the string. If `n` is greater than the length of the string, the function should return the entire string. If `n` is less than or equal to 0, the functi...
Here is the Python code to solve the given problem in the markdown block: ```python def get_first_n_chars(text: str, n: int) -> str: """ Returns the first n characters of the given string. Args: text (str): The input string from which to extract the first n characters. n (int): The number o...
code_exercise_27k
492
Write a Python function named `remove_whitespace` that takes a string as a parameter and returns a new string with all whitespace characters removed. The function should be able to handle various types of whitespace characters, including spaces, tabs, and newlines. The code should start with: ```python def remove_whit...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_whitespace(text: str) -> str: """ Removes all whitespace characters from the given string. Args: text (str): The input string from which whitespace characters are to be removed. Returns: ...
code_exercise_27k
494
Write a Python function named `swap_case` that takes a string as a parameter and returns a new string with the case of each letter swapped. All uppercase letters should be converted to lowercase, and all lowercase letters should be converted to uppercase. Non-alphabetic characters should remain unchanged. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_case(text: str) -> str: """ Swaps the case of each letter in the given string. Args: text (str): The input string whose case needs to be swapped. Returns: str: A new string with the case of eac...
code_exercise_27k
497
Write a Python function named `reverse_words` that takes a string as a parameter and returns a new string with the order of the words reversed. The words in the new string should be separated by a single space. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strin...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(text: str) -> str: """ Reverses the order of words in a given string and returns a new string with the words in reversed order. Args: text (str): The input string containing words to be reversed. ...
code_exercise_27k
502
Write a Python function to determine if two given strings are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. The function should be case-insensitive and should ignore spaces. The code should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(text1: str, text2: str) -> bool: """ Determines if two strings are anagrams of each other. Args: text1 (str): The first string to compare. text2 (str): The second string to compare. Retur...
code_exercise_27k
504
Write a Python function named `is_subsequence` that takes two strings as parameters: `text` and `subsequence`. The function should return `True` if `subsequence` is a subsequence of `text`, and `False` otherwise. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements witho...
Here is the Python code to solve the given problem in the markdown block: ```python def is_subsequence(text: str, subsequence: str) -> bool: """ Determines if the given subsequence is a subsequence of the text. Args: text (str): The main string to check against. subsequence (str): The strin...
code_exercise_27k
508
Write a Python function named `is_pangram` that takes a string as a parameter and returns `True` if the string is a pangram, and `False` otherwise. A pangram is a sentence that contains every letter of the alphabet at least once, ignoring spaces and capitalization. The function should be case-insensitive and should ign...
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(text: str) -> bool: """ Determines if the given string is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once, ignoring spaces and capitalization. Args: ...
code_exercise_27k
509
Write a Python function to calculate the frequency of each word in a given string. The function should return a dictionary where the keys are the unique words in the string and the values are the corresponding frequencies of those words. The function should handle punctuation and case sensitivity by converting the stri...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict import string def get_word_frequencies(text: str) -> Dict[str, int]: """ Calculate the frequency of each word in a given string. Args: text (str): The input string from which to calculate wo...
code_exercise_27k
511
Write a Python function named `get_most_frequent_word` that takes a string as a parameter and returns the most frequent word in the string. If there are multiple words with the same highest frequency, return the first occurrence. The function should handle case sensitivity and punctuation. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Tuple import string def get_most_frequent_word(text: str) -> str: """ Returns the most frequent word in the given text. If there are multiple words with the same highest frequency, returns the firs...
code_exercise_27k
516
Write a Python function named `get_words_starting_with` that takes two parameters: a string `text` and a character `start_char`. The function should return a list of words from the string that start with the given character. The order of the words in the list should match their order in the string. The function should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import string def get_words_starting_with(text: str, start_char: str) -> List[str]: """ Returns a list of words from the given text that start with the specified character. Args: text (str):...
code_exercise_27k
528
Write a Python function named `format_name` that takes in two parameters: `first_name` and `last_name`. The function should return a formatted string that combines the first name and last name with a space in between. Additionally, handle the cases where either `first_name` or `last_name` might be `None`. If either is ...
Here is the Python code to solve the given problem in the markdown block: ```python def format_name(first_name: str, last_name: str) -> str: """ Combines the first name and last name with a space in between, handling cases where either name might be None. Parameters: first_name (str): The first name. C...
code_exercise_27k
529
Write a Python function named `capitalize_words` that takes in a string as a parameter. The function should return a new string where each word in the input string is capitalized. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with punctuation. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_words(string: str) -> str: """ Capitalizes each word in the input string. Parameters: string (str): The input string. Returns: str: The new string with each word capitalized. """ # Handle...
code_exercise_27k
530
Write a Python function named `reverse_words` that takes in a string as a parameter. The function should return a new string where the order of words in the input string is reversed, but the order of characters within each word remains unchanged. For example, if the input string is "hello world", the function should re...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(string: str) -> str: """ Reverses the order of words in the input string while keeping the characters within each word unchanged. Args: string (str): The input string containing words separated by ...
code_exercise_27k
532
Write a Python function named `remove_duplicates` that takes in a string as a parameter. The function should return a new string where all duplicate characters are removed, while maintaining the original order of characters. The function should handle an empty string and strings with all unique characters. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_duplicates(string: str) -> str: """ Removes duplicate characters from the input string while maintaining the original order. Args: string (str): The input string from which to remove duplicate characters....
code_exercise_27k
533
Write a Python function named `replace_spaces` that takes in a string as a parameter. The function should return a new string where all spaces are replaced with underscores. The function should handle empty strings and strings with no spaces correctly. The code should start with: ```python def replace_spaces(string: st...
Here is the Python code to solve the given problem in the markdown block: ```python def replace_spaces(string: str) -> str: """ Replaces all spaces in the input string with underscores. Args: string (str): The input string. Returns: str: The new string with spaces replaced by underscor...
code_exercise_27k
536
Write a Python function named `swap_case` that takes in a string as a parameter. The function should return a new string where the case of each character is swapped (lowercase characters become uppercase and vice versa). The function should handle various edge cases, such as empty strings, strings with only digits, and...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_case(string: str) -> str: """ Swaps the case of each character in the input string. Parameters: string (str): The input string. Returns: str: The new string with swapped case. """ # Use the bui...
code_exercise_27k
538
Write a Python function named `remove_whitespace` that takes in a string as a parameter. The function should return a new string where all whitespace characters (spaces, tabs, and newlines) are removed. The function should handle various types of whitespace characters and return the string without any modifications oth...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespace(string: str) -> str: """ Removes all whitespace characters from the input string. Parameters: string (str): The input string from which whitespace characters are to be removed. Returns: st...
code_exercise_27k
539
Write a Python function named `split_string` that takes in a string and a delimiter as parameters. The function should return a list of substrings obtained by splitting the input string at each occurrence of the delimiter. The function should handle cases where the input string or delimiter is `None` by returning an em...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def split_string(string: str, delimiter: str) -> List[str]: """ Splits the input string at each occurrence of the delimiter and returns a list of substrings. Parameters: string (str): The input ...
code_exercise_27k
541
Write a Python function named `remove_punctuation` that takes in a string as a parameter. The function should return a new string where all punctuation characters (such as commas, periods, and exclamation marks) are removed. The function should handle various edge cases, such as an empty string or a string with no punc...
Here is the Python code to solve the given problem in the markdown block: ```python import string def remove_punctuation(input_string: str) -> str: """ Removes all punctuation characters from the input string. Args: input_string (str): The input string from which punctuation characters will be rem...
code_exercise_27k
542
Write a Python function named `capitalize_first_letters` that takes in a string as a parameter. The function should return a new string where the first letter of each word is capitalized, and all other letters are lowercase. The function should handle various edge cases, such as strings with multiple spaces, leading an...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_first_letters(string: str) -> str: """ Capitalizes the first letter of each word in the input string, and converts all other letters to lowercase. Args: string (str): The input string. Returns: ...
code_exercise_27k
543
Write a Python function named `reverse_words` that takes in a string as a parameter. The function should return a new string where the order of the words is reversed. The function should handle various edge cases, such as an empty string, a string with only one word, and a string with multiple spaces between words. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(string: str) -> str: """ Reverses the order of words in the input string. Args: string (str): The input string. If None, it is treated as an empty string. Returns: str: The new string ...
code_exercise_27k
547
Write a Python function named `replace_vowels` that takes in a string as a parameter. The function should return a new string where all vowels (a, e, i, o, u) are replaced with the character '*'. The function should be case-insensitive, meaning both uppercase and lowercase vowels should be replaced. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python def replace_vowels(string: str) -> str: """ Replaces all vowels in the input string with the character '*'. Parameters: string (str): The input string. Returns: str: The new string with vowels replaced by '*'. ...
code_exercise_27k
551
Write a Python function named `remove_digits` that takes in a string as a parameter. The function should return a new string where all digits (0-9) are removed. The function should handle empty strings and strings without digits gracefully. The code should start with: ```python def remove_digits(string: str) -> str: `...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_digits(string: str) -> str: """ Removes all digits (0-9) from the input string. Args: string (str): The input string from which digits are to be removed. Returns: str: The new string with all...
code_exercise_27k
553
Write a Python function named `remove_special_characters` that takes in a string as a parameter. The function should return a new string where all special characters (such as !, @, #, $, %, etc.) are removed. The function should handle various edge cases, including empty strings and strings with no special characters. ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_special_characters(string: str) -> str: """ Removes all special characters from the input string. Parameters: string (str): The input string from which special characters are to be removed. Re...
code_exercise_27k
555
Write a Python function named `remove_non_alphanumeric` that takes in a string as a parameter. The function should return a new string where all non-alphanumeric characters are removed. The function should handle both uppercase and lowercase letters, digits, and should not modify the original string. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_non_alphanumeric(string: str) -> str: """ Removes all non-alphanumeric characters from the input string. Args: string (str): The input string from which non-alphanumeric characters will be remo...
code_exercise_27k
557
Write a Python function named `remove_newlines` that takes in a string as a parameter. The function should return a new string where all newline characters (`\n`) are removed. The function should handle various edge cases, such as an empty string, a string with no newlines, and a string with multiple newlines. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_newlines(string: str) -> str: """ Removes all newline characters (\n) from the input string. Args: string (str): The input string from which newline characters are to be removed. Returns: str...
code_exercise_27k
558
Write a Python function named `capitalize_last_letters` that takes in a string as a parameter. The function should return a new string where the last letter of each word is capitalized, and all other letters are lowercase. The function should handle various edge cases, such as empty strings, single-letter words, and mu...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_last_letters(string: str) -> str: """ Capitalizes the last letter of each word in the input string, and converts all other letters to lowercase. Args: string (str): The input string. Returns: ...
code_exercise_27k
559
Write a Python function named `remove_trailing_spaces` that takes in a string as a parameter. The function should return a new string where all trailing spaces at the end of each line are removed. The function should handle multiple lines and ensure that only trailing spaces are removed, not spaces within the lines. T...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_trailing_spaces(string: str) -> str: """ Removes all trailing spaces at the end of each line in the input string. Args: string (str): The input string containing one or more lines. Returns: s...
code_exercise_27k
561
Write a Python function named `remove_multiple_spaces` that takes in a string as a parameter. The function should return a new string where multiple consecutive spaces are replaced with a single space. The function should handle edge cases such as empty strings and strings with no spaces. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_multiple_spaces(string: str) -> str: """ Replaces multiple consecutive spaces in the input string with a single space. Args: string (str): The input string. Returns: str: The new s...
code_exercise_27k
563
Write a Python function named `remove_leading_spaces` that takes in a string as a parameter. The function should return a new string where all leading spaces at the beginning of each line are removed. The function should handle multiple lines and preserve the line breaks in the original string. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_leading_spaces(string: str) -> str: """ Removes all leading spaces at the beginning of each line in the input string. Parameters: string (str): The input string containing one or more lines. Returns: ...
code_exercise_27k
565
Write a Python function named `remove_empty_lines` that takes in a string as a parameter. The function should return a new string where all empty lines (lines containing only whitespace characters) are removed. The function should handle various types of whitespace characters, including spaces, tabs, and newlines. The...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_empty_lines(string: str) -> str: """ Removes all empty lines (lines containing only whitespace characters) from the input string. Parameters: string (str): The input string from which to remove empty lines. ...
code_exercise_27k
590
Write a Python function named `capitalize_words_with_conditions` that takes in a string, a list of prefixes, a list of suffixes, and a list of substrings as parameters. The function should return a new string where all words that start with any of the specified prefixes, end with any of the specified suffixes, or conta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def capitalize_words_with_conditions(string: str, prefixes: List[str], suffixes: List[str], substrings: List[str]) -> str: """ Capitalizes words that start with any of the specified prefixes, end with an...
code_exercise_27k
689
Write a Python function named `reverse_words` that takes a string input and returns a new string with the words in reverse order. The function should preserve the order of characters within each word. The code should start with: ```python def reverse_words(sentence: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def reverse_words(sentence: str) -> str: """ Reverses the order of words in a given sentence while preserving the order of characters within each word. Args: sentence (str): The input sentence whose words are to b...
code_exercise_27k
690
Write a Python function named `count_vowels` that takes a string input and returns the count of vowels (a, e, i, o, u) in the string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels. The code should start with: ```python def count_vowels(string: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_vowels(string: str) -> int: """ Counts the number of vowels (a, e, i, o, u) in a given string and returns the count. Parameters: string (str): The string to count vowels in. Returns: int: The count of...
code_exercise_27k
691
Write a Python function named `capitalize_words` that takes a string input and returns the string with each word capitalized. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with punctuation. The code should start with: ```python def capitalize_words(strin...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_words(string: str) -> str: """ Capitalizes each word in a given string and returns the modified string. Args: string (str): The string to capitalize words in. Returns: str: The string wit...
code_exercise_27k
692
Write a Python function named `remove_duplicates` that takes a string input and returns the string with duplicate characters removed, while maintaining the order of the first occurrence of each character. The function should handle an empty string and return an empty string in that case. The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_duplicates(string: str) -> str: """ Removes duplicate characters from a given string and returns the modified string, maintaining the order of the first occurrence of each character. Args: string (str...
code_exercise_27k
694
Write a Python function named `split_string` that takes two string inputs: `string` and `delimiter`. The function should split the `string` into a list of substrings based on the `delimiter` and return the list. The function should handle cases where the delimiter is not found in the string by returning the entire stri...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def split_string(string: str, delimiter: str) -> List[str]: """ Splits a given string into a list of substrings based on a specified delimiter and returns the list. Args: string (str): The s...
code_exercise_27k
697
Write a Python function named `remove_whitespace` that takes a string input and returns the string with all whitespace characters removed. The function should handle various types of whitespace characters, including spaces, tabs, and newlines. The code should start with: ```python def remove_whitespace(string: str) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_whitespace(string: str) -> str: """ Removes all whitespace characters from a given string and returns the modified string. Parameters: string (str): The string to remove whitespace from. Retur...
code_exercise_27k
698
Write a Python function named `is_anagram` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` and `string2` are anagrams of each other (contain the same characters in a different order), and `False` otherwise. The function should ignore spaces and case sensitivity. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(string1: str, string2: str) -> bool: """ Checks if two strings are anagrams of each other and returns True or False. Parameters: string1 (str): The first string. string2 (str): The second string. ...
code_exercise_27k
699
Write a Python function named `capitalize_first_letter` that takes a string input and returns the string with the first letter of each word capitalized. If the input string is `None`, the function should return an empty string. The code should start with: ```python def capitalize_first_letter(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_first_letter(string: str) -> str: """ Capitalizes the first letter of each word in a given string and returns the modified string. Args: string (str): The string to capitalize the first letter of each...
code_exercise_27k
700
Write a Python function named `reverse_words` that takes a string input and returns the string with the order of words reversed. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with leading or trailing spaces. The code should start with: ```python def reve...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words(string: str) -> str: """ Reverses the order of words in a given string and returns the modified string. Args: string (str): The string to reverse the order of words. Returns: str: The ...
code_exercise_27k
706
Write a Python function named `is_rotation` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a rotation of `string2`, and `False` otherwise. A rotation occurs when one string can be obtained by rotating another string by any number of positions. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation(string1: str, string2: str) -> bool: """ Checks if string1 is a rotation of string2 and returns True or False. Parameters: string1 (str): The potential rotation. string2 (str): The string to check fo...
code_exercise_27k
707
Write a Python function named `swap_case` that takes a string input and returns the string with the case of each character swapped (lowercase characters become uppercase and vice versa). The function should handle any string input, including empty strings and strings with special characters. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_case(string: str) -> str: """ Swaps the case of each character in a given string and returns the modified string. Parameters: string (str): The string to swap the case of each character. Returns: str: ...
code_exercise_27k
710
Write a Python function named `remove_suffix` that takes two string inputs, `string` and `suffix`. The function should return the string with the `suffix` removed from the end, if it exists. If the `suffix` is not found at the end of the string, the function should return the original string. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_suffix(string: str, suffix: str) -> str: """ Removes the suffix from the end of a string, if it exists, and returns the modified string. If the suffix is not found, the original string is returned. Args: ...
code_exercise_27k
711
Write a Python function named `reverse_words_order` that takes a string input and returns the string with the order of words reversed. The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with leading or trailing spaces. The code should start with: ```python de...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_order(string: str) -> str: """ Reverses the order of words in a given string and returns the modified string. Args: string (str): The string to reverse the order of words. Returns: str...
code_exercise_27k
712
Write a Python function named `is_pangram` that takes a string input and returns `True` if the string is a pangram, and `False` otherwise. A pangram is a sentence that contains every letter of the alphabet at least once. The function should be case-insensitive and should ignore non-alphabetic characters. The code shou...
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(string: str) -> bool: """ Checks if a given string is a pangram and returns True or False. A pangram is a sentence that contains every letter of the alphabet at least once. This function is case-insensiti...
code_exercise_27k
720
Write a Python function named `is_substring` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a substring of `string2`, and `False` otherwise. The function should handle edge cases such as empty strings and should be efficient in checking for substrings. The code...
Here is the Python code to solve the given problem in the markdown block: ```python def is_substring(string1: str, string2: str) -> bool: """ Checks if string1 is a substring of string2 and returns True or False. Parameters: string1 (str): The potential substring. string2 (str): The string to check...
code_exercise_27k
721
Write a Python function named `remove_non_alphabetic` that takes a string input and returns the string with all non-alphabetic characters removed. Alphabetic characters include both uppercase and lowercase letters. The function should handle an empty string and return it as is. The code should start with: ```python de...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_non_alphabetic(string: str) -> str: """ Removes all non-alphabetic characters from a given string and returns the modified string. Parameters: string (str): The string to remove non-alphabetic characters from...
code_exercise_27k
722
Write a Python function named `is_rotation_case_insensitive` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a case-insensitive rotation of `string2`, and `False` otherwise. A rotation occurs when one string can be obtained by rotating another string by any numbe...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation_case_insensitive(string1: str, string2: str) -> bool: """ Checks if string1 is a case-insensitive rotation of string2 and returns True or False. Parameters: string1 (str): The potential rotation. str...
code_exercise_27k
723
Write a Python function named `reverse_string_case` that takes a string input and returns the string with the case of each character reversed (lowercase characters become uppercase and vice versa). The function should handle an empty string and should not modify non-alphabetic characters. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string_case(string: str) -> str: """ Reverses the case of each alphabetic character in a given string and returns the modified string. Parameters: string (str): The string to reverse the case of each charact...
code_exercise_27k
726
Write a Python function named `remove_consecutive_whitespace` that takes a string input and returns the string with consecutive whitespace characters (spaces, tabs, etc.) replaced by a single space. The function should handle various types of whitespace characters, including spaces, tabs, newlines, and carriage returns...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_consecutive_whitespace(string: str) -> str: """ Replaces consecutive whitespace characters in a given string with a single space and returns the modified string. Args: string (str): The string ...
code_exercise_27k
727
Write a Python function named `is_rotation_case_sensitive` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a case-sensitive rotation of `string2`, and `False` otherwise. A rotation occurs when one string can be obtained by rotating another string by any number of...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation_case_sensitive(string1: str, string2: str) -> bool: """ Checks if string1 is a case-sensitive rotation of string2 and returns True or False. Parameters: string1 (str): The potential rotation. string2...
code_exercise_27k
729
Write a Python function named `is_subsequence_case_insensitive` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a case-insensitive subsequence of `string2`, and `False` otherwise. A subsequence is a sequence that can be derived from another sequence by deleting s...
Here is the Python code to solve the given problem in the markdown block: ```python def is_subsequence_case_insensitive(string1: str, string2: str) -> bool: """ Checks if string1 is a case-insensitive subsequence of string2 and returns True or False. Parameters: string1 (str): The potential subsequence...
code_exercise_27k
730
Write a Python function named `remove_trailing_whitespace` that takes a string input and returns the string with trailing whitespace characters removed. The function should handle various types of trailing whitespace characters, including spaces, tabs, and newlines. The code should start with: ```python def remove_trai...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_trailing_whitespace(string: str) -> str: """ Removes trailing whitespace characters from a given string and returns the modified string. Args: string (str): The string to remove trailing whitespace from. ...
code_exercise_27k
731
Write a Python function named `is_substring_case_sensitive` that takes two string inputs, `string1` and `string2`. The function should return `True` if `string1` is a substring of `string2`, considering case-sensitivity. Return `False` otherwise. The function should handle edge cases such as empty strings and non-strin...
Here is the Python code to solve the given problem in the markdown block: ```python def is_substring_case_sensitive(string1: str, string2: str) -> bool: """ Checks if string1 is a substring of string2, considering case-sensitivity, and returns True or False. Parameters: string1 (str): The potential sub...
code_exercise_27k
768
Write a Python function named `compare_length` that takes two strings as input and returns `True` if the length of the first string is greater than the length of the second string, and `False` otherwise. The function should handle empty strings and strings of different lengths. The code should start with: ```python de...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_length(str1: str, str2: str) -> bool: """ Compare the length of two strings and return True if the length of the first string is greater than the length of the second string, and False otherwise. Args: s...
code_exercise_27k
769
Write a Python function named `compare_case` that takes two strings as input and returns `True` if the first string is in uppercase and the second string is in lowercase, and `False` otherwise. The function should handle empty strings and strings with mixed characters appropriately. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_case(str1: str, str2: str) -> bool: """ Compare the case of two strings and return True if the first string is in uppercase and the second string is in lowercase, and False otherwise. Parameters: str1 (str):...
code_exercise_27k
770
Write a Python function named `compare_startswith` that takes two strings as input and returns `True` if the first string starts with the second string, and `False` otherwise. The function should use the `startswith` method of the string class to perform the comparison. The code should start with: ```python def compar...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_startswith(str1: str, str2: str) -> bool: """ Compare if the first string starts with the second string and return True if it does, and False otherwise. Parameters: str1 (str): The first string. str2 (st...
code_exercise_27k
771
Write a Python function named `compare_endswith` that takes two strings as input and returns `True` if the first string ends with the second string, and `False` otherwise. The function should use the `endswith` method of the string object to perform the comparison. The code should start with: ```python def compare_end...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_endswith(str1: str, str2: str) -> bool: """ Compare if the first string ends with the second string and return True if it does, and False otherwise. Parameters: str1 (str): The first string. str2 (str): ...
code_exercise_27k
772
Write a Python function named `compare_contains` that takes two strings as input and returns `True` if the second string is contained within the first string, and `False` otherwise. The function should be case-insensitive and should handle empty strings appropriately. The code should start with: ```python def compare_...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_contains(str1: str, str2: str) -> bool: """ Compare if the second string is contained within the first string and return True if it is, and False otherwise. The comparison is case-insensitive. Parameters: ...