source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
974
Write a Python function named `atbash_cipher` that takes a string `message` as a parameter. The function should encrypt the message using the Atbash cipher algorithm by replacing each letter in the message with its corresponding letter in the reversed alphabet. Non-alphabet characters should remain unchanged. Return th...
Here is the Python code to solve the given problem in the markdown block: ```python def atbash_cipher(message: str) -> str: """ Encrypts the given message using the Atbash cipher algorithm. The Atbash cipher replaces each letter in the message with its corresponding letter in the reversed alphabet. Non...
code_exercise_27k
976
Write a Python function named `polybius_square_cipher` that takes a string `message` as a parameter. The function should encrypt the message using the Polybius square cipher algorithm by replacing each letter in the message with its corresponding two-digit coordinate in the Polybius square. Return the encrypted message...
Here is the Python code to solve the given problem in the markdown block: ```python def polybius_square_cipher(message: str) -> str: """ Encrypts a message using the Polybius square cipher algorithm. The Polybius square cipher replaces each letter in the message with its corresponding two-digit coordin...
code_exercise_27k
985
Write a Python function named `reverse_cipher_decrypt` that takes a string `encrypted_message` as a parameter. The function should decrypt the message by reversing the order of the characters. Return the decrypted message. The function should start with: ```python def reverse_cipher_decrypt(encrypted_message: str) -> s...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_cipher_decrypt(encrypted_message: str) -> str: """ Decrypts a message that has been encrypted using a reverse cipher by reversing the order of the characters. Args: encrypted_message (str): The encrypted...
code_exercise_27k
989
Create a Python function named `atbash_cipher_decrypt` that takes a string `encrypted_message` as a parameter. The function should decrypt the message using the Atbash cipher algorithm by replacing each letter in the encrypted message with its corresponding letter in the reversed alphabet. Return the decrypted message....
Here is the Python code to solve the given problem in the markdown block: ```python def atbash_cipher_decrypt(encrypted_message: str) -> str: """ Decrypts the given encrypted message using the Atbash cipher algorithm. The Atbash cipher replaces each letter in the encrypted message with its correspondin...
code_exercise_27k
999
Create a Python function named `hash_collision_ratio` that takes a list of strings as input and returns the ratio of hash collisions to the total number of strings. The ratio should be a decimal value rounded to two decimal places. The function should also include a helper function `hash_collisions` that calculates the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def hash_collisions(string_list: List[str]) -> Dict[int, int]: """ Calculate the number of collisions for each unique hash value in the list of strings. Args: string_list (List[str]): ...
code_exercise_27k
1,067
Write a Python function named `generate_random_string` that takes an integer `length` as input and returns a random string of lowercase letters with the given length. The function should handle edge cases such as a non-positive length by returning an empty string. The code should start with: ```python import random im...
Here is the Python code to solve the given problem in the markdown block: ```python import random import string def generate_random_string(length: int) -> str: """ Generates a random string of lowercase letters with the given length. Args: length (int): The desired length of the random string. ...
code_exercise_27k
1,069
Write a Python function to reverse a given string. The function should handle strings of varying lengths, including empty strings. The function should be named `reverse_string` and should take a single string as input and return the reversed string. The code should start with: ```python def reverse_string(string: str)...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses the given string. Args: string (str): The string to be reversed. Returns: str: The reversed string. """ # Ensure the input is a string if...
code_exercise_27k
1,070
Write a Python function named `remove_duplicates` that takes a string as input and returns a new string with all duplicate characters removed. The order of the characters should be preserved. The function should handle any string input, including empty strings and strings with all unique characters. The code should st...
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 preserving the order of the characters. Args: string (str): The input string from which duplicate characters ne...
code_exercise_27k
1,071
Write a Python function to determine if a given string is a palindrome. The function should ignore spaces, punctuation, and capitalization. The code should start with: ```python import re def is_palindrome(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_palindrome(string: str) -> bool: """ Determine if a given string is a palindrome, ignoring spaces, punctuation, and capitalization. Args: string (str): The input string to be checked for palindrome...
code_exercise_27k
1,072
Write a Python function to capitalize the first letter of each word in a given string. Words in the string are separated by spaces. The function should handle strings with multiple spaces between words and strings that start or end with spaces. The code should start with: ```python def capitalize_words(string: str) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_words(string: str) -> str: """ Capitalizes the first letter of each word in the given string. Args: string (str): The input string in which to capitalize the first letter of each word. Returns: ...
code_exercise_27k
1,075
Write a Python function to determine if two given strings are anagrams of each other. The function should be case-insensitive and should ignore spaces. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The code should start with: ```python def is_anagram(string1: str, string2:...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(string1: str, string2: str) -> bool: """ Determines if two strings are anagrams of each other. Args: string1 (str): The first string to compare. string2 (str): The second string to compare. ...
code_exercise_27k
1,076
Write a Python function named `remove_whitespace` that takes a string as input 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, newlines, and carriage returns. The code should start with: ```python def ...
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 the given string. Args: string (str): The input string from which whitespace characters need to be removed. Retur...
code_exercise_27k
1,077
Write a Python function to find the longest word in a given string. If there are multiple words with the same longest length, return the first occurrence. The function should handle various edge cases, such as an empty string or a string with only one word. The code should start with: ```python def find_longest_word(s...
Here is the Python code to solve the given problem in the markdown block: ```python def find_longest_word(string: str) -> str: """ Finds the longest word in a given string. If there are multiple words with the same longest length, returns the first occurrence. Args: string (str): The input str...
code_exercise_27k
1,078
Write a Python function to reverse the order of words in a given string. The function should handle strings with multiple spaces between words and leading/trailing spaces. The code should start with: ```python def reverse_words(string: str) -> str: ```
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 in which the order of words needs to be reversed. Returns: str: A new strin...
code_exercise_27k
1,080
Write a Python function named `remove_duplicates_ordered` that takes a string as input and returns a new string with all duplicate characters removed while preserving the order of the characters. The function should ensure that each character appears only once in the resulting string, and the first occurrence of each c...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_duplicates_ordered(string: str) -> str: """ Removes duplicate characters from the input string while preserving the order of the first occurrence of each character. Args: string (str): The input string fr...
code_exercise_27k
1,081
Write a Python function named `is_rotation` that takes two strings as input: `string1` and `string2`. The function should return `True` if `string2` is a rotation of `string1`, and `False` otherwise. A rotation occurs when a string is rotated by any number of positions and the characters wrap around to the beginning. ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation(string1: str, string2: str) -> bool: """ Determines if string2 is a rotation of string1. Args: string1 (str): The first string to compare. string2 (str): The second string to compare. Re...
code_exercise_27k
1,082
Write a Python function named `split_string` that takes a string and a delimiter as input and returns a list of substrings obtained by splitting the string at each occurrence of the delimiter. The function should handle cases where the delimiter is not found in the string, and it should also handle multiple consecutive...
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 at each occurrence of a specified delimiter and returns a list of substrings. Args: string (str): The st...
code_exercise_27k
1,083
Write a Python function named `is_subsequence` that takes two strings as input: `string1` and `string2`. The function should return `True` if `string1` is a subsequence of `string2`, and `False` otherwise. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without chan...
Here is the Python code to solve the given problem in the markdown block: ```python def is_subsequence(string1: str, string2: str) -> bool: """ Determines if string1 is a subsequence of string2. Args: string1 (str): The first string to check if it is a subsequence. string2 (str): The second...
code_exercise_27k
1,084
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 use the `string.punctuation` constant to identify and remove punctuation characters. The code should start with: ```python import string def remove_punct...
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 need to be ...
code_exercise_27k
1,085
Write a Python function to determine if all characters in a given string are unique. The function should return `True` if all characters are unique and `False` otherwise. The function should handle both uppercase and lowercase letters as distinct characters. The code should start with: ```python def is_unique(string: ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_unique(string: str) -> bool: """ Determines if all characters in the given string are unique. Args: string (str): The string to be checked for uniqueness of characters. Returns: bool: True if all...
code_exercise_27k
1,091
Write a Python function named `remove_adjacent_duplicates` that takes a string as input and returns a new string with adjacent duplicate characters removed. The function should iterate through the string and only append characters to the new string if they are not the same as the next character in the original string. ...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_adjacent_duplicates(string: str) -> str: """ Removes adjacent duplicate characters from the given string. Args: string (str): The input string from which adjacent duplicate characters are to be removed. ...
code_exercise_27k
1,093
Write a Python function named `is_substring` that takes two strings as input: `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 c...
Here is the Python code to solve the given problem in the markdown block: ```python def is_substring(string1: str, string2: str) -> bool: """ Determines if `string1` is a substring of `string2`. Args: string1 (str): The first string to check if it is a substring. string2 (str): The second s...
code_exercise_27k
1,096
Write a Python function to reverse a given string. The function should handle both normal strings and special cases such as empty strings and `None`. If the input is `None`, the function should return an empty string. The code should start with: ```python def reverse_string(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses the given string. If the input is None, returns an empty string. Args: string (str): The string to be reversed. Returns: str: The reversed string or ...
code_exercise_27k
1,097
Write a Python function to determine if a given string is a palindrome. A palindrome is a string that reads the same forwards and backwards. The function should ignore case and non-alphanumeric characters when checking for palindrome properties. The code should start with: ```python def is_palindrome(string: str) -> b...
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(string: str) -> bool: """ Determine if a given string is a palindrome, ignoring case and non-alphanumeric characters. Args: string (str): The input string to check for palindrome properties. R...
code_exercise_27k
1,099
Write a Python function to capitalize the first letter of each word in a given string. The function should handle various edge cases, such as strings with multiple spaces, leading and trailing spaces, and empty strings. The function should return a new string with the first letter of each word capitalized. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_words(string: str) -> str: """ Capitalizes the first letter of each word in the given string. Args: string (str): The input string to be processed. Returns: str: A new string with the fir...
code_exercise_27k
1,100
Write a Python function to count the number of non-overlapping occurrences of a substring in a given main string. The function should handle edge cases such as empty strings and substrings that are longer than the main string. The code should start with: ```python def count_substring(main_string: str, substring: str) ...
Here is the Python code to solve the given problem in the markdown block: ```python def count_substring(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 the...
code_exercise_27k
1,102
Write a Python function to determine if one string is a rotation of another string. A rotation occurs when one string can be obtained by rotating the other string by any number of characters. The function should return `True` if one string is a rotation of the other, and `False` otherwise. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation(str1: str, str2: str) -> bool: """ Determines if one string is a rotation of another string. Args: str1 (str): The first string. str2 (str): The second string to check if it is a rotation of ...
code_exercise_27k
1,104
Write a Python function to find the longest common prefix among a list of strings. The function should return an empty string if there is no common prefix. The code should start with: ```python from typing import List def longest_common_prefix(strings: List[str]) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_common_prefix(strings: List[str]) -> str: """ Find the longest common prefix among a list of strings. Args: strings (List[str]): A list of strings to find the common prefix from....
code_exercise_27k
1,106
Write a Python function to determine if a given string is a pangram. A pangram is a string that contains every letter of the alphabet at least once. The function should be case-insensitive and should ignore non-alphabetic characters. The code should start with: ```python def is_pangram(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(string: str) -> bool: """ Determine if the given string is a pangram. A pangram is a string that contains every letter of the alphabet at least once. This function is case-insensitive and ignores non-alph...
code_exercise_27k
1,107
Write a Python function to determine if a given string is a subsequence of another string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. The function should start with: ```python def is_subsequence(main_string...
Here is the Python code to solve the given problem in the markdown block: ```python def is_subsequence(main_string: str, subsequence: str) -> bool: """ Determines if the given subsequence is present in the main string in the same order. Args: main_string (str): The main string to search within. ...
code_exercise_27k
1,108
Write a Python function to determine if a given string has balanced parentheses, brackets, and braces. The function should return `True` if the string is balanced and `False` otherwise. A string is considered balanced if each opening symbol (parenthesis, bracket, or brace) has a corresponding closing symbol in the corr...
Here is the Python code to solve the given problem in the markdown block: ```python def is_balanced(string: str) -> bool: """ Determines if the given string has balanced parentheses, brackets, and braces. Args: string (str): The string to check for balanced symbols. Returns: bool: True...
code_exercise_27k
1,109
Write a Python function to reverse the order of words in a given string. The function should handle strings with multiple spaces between words and leading/trailing spaces. The code should start with: ```python def reverse_words(string: str) -> str: ```
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 containing words to be reversed. Returns: str: A new string with the orde...
code_exercise_27k
1,110
Write a Python function to determine if all characters in a given string are unique. The function should return `True` if all characters are unique and `False` otherwise. The function should handle an empty string as a special case and return `True` for it. The function should be case-sensitive, meaning 'A' and 'a' are...
Here is the Python code to solve the given problem in the markdown block: ```python def is_unique(string: str) -> bool: """ Determines if all characters in the given string are unique. Args: string (str): The input string to check for unique characters. Returns: bool: True if all chara...
code_exercise_27k
1,111
Write a Python function called `is_rotation_of_palindrome` that takes in a string as input and returns `True` if the string is a rotation of a palindrome, and `False` otherwise. A rotation of a palindrome is a string that can be obtained by rotating a palindrome string by any number of characters. The function should b...
Here is the Python code to solve the given problem in the markdown block: ```python def is_rotation_of_palindrome(string: str) -> bool: """ Determines if the given string is a rotation of a palindrome. Args: string (str): The input string to check. Returns: bool: True if the string is ...
code_exercise_27k
1,113
Write a Python function to validate an email address based on a specific format. The function should return `True` if the email address is valid and `False` otherwise. The email address must follow the format `username@domain.extension`, where: - `username` can contain alphanumeric characters and the special characters...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_email(email: str) -> bool: """ Validates whether the given string is a valid email address based on a specific format. Args: email (str): The email address to validate. Returns: ...
code_exercise_27k
1,115
Write a Python function to determine if a given string is a valid username based on specific criteria. The function should return `True` if the string is a valid username and `False` otherwise. The criteria for a valid username are: 1. It must contain only alphanumeric characters and underscores. 2. It must start with ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_username(username: str) -> bool: """ Determines if the given string is a valid username based on the following criteria: - Contains only alphanumeric characters and underscores. - Starts with a le...
code_exercise_27k
1,116
Write a Python function to determine if a given string is a valid URL based on specific criteria. The function should return `True` if the string is a valid URL and `False` otherwise. A valid URL must satisfy the following conditions: - Starts with either "http://" or "https://" - Contains a domain name (e.g., "example...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_url(url: str) -> bool: """ Determines if a given string is a valid URL based on specific criteria. Args: url (str): The string to be validated as a URL. Returns: bool: True if th...
code_exercise_27k
1,119
Write a Python function to determine if a given string is a valid IPv4 address. The function should return `True` if the string is a valid IPv4 address and `False` otherwise. A valid IPv4 address must meet the following criteria: - It consists of exactly four decimal numbers separated by periods (e.g., "192.168.0.1"). ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv4(ip_address: str) -> bool: """ Determines if the given string is a valid IPv4 address. Args: ip_address (str): The string to be validated as an IPv4 address. Returns: bool: True if the ...
code_exercise_27k
1,121
Write a Python function to determine if a given string is a valid binary number. A valid binary number must satisfy the following conditions: - It contains only the characters '0' and '1'. - It has a length greater than 0. The function should start with: ```python def is_valid_binary(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_binary(string: str) -> bool: """ Determines if the given string is a valid binary number. A valid binary number must satisfy the following conditions: - Contains only the characters '0' and '1'. - Has a...
code_exercise_27k
1,122
Write a Python function to determine if a given string is a valid hexadecimal number. A valid hexadecimal number must satisfy the following conditions: - Contains only hexadecimal digits (0-9, A-F, a-f) - Has a length greater than 0 The function should start with: ```python def is_valid_hexadecimal(string: str) -> boo...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_hexadecimal(string: str) -> bool: """ Determines if the given string is a valid hexadecimal number. Args: string (str): The string to be checked. Returns: bool: True if the string is a vali...
code_exercise_27k
1,123
Write a Python function called `is_valid_roman_numeral` that takes in a string as input and returns `True` if the string is a valid Roman numeral, and `False` otherwise. A valid Roman numeral must satisfy the following conditions: - Contains only the Roman numeral symbols: I, V, X, L, C, D, M - Follows the rules of Rom...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_roman_numeral(string: str) -> bool: """ Determines if the given string is a valid Roman numeral. Args: string (str): The string to be validated as a Roman numeral. Returns: bool: True if th...
code_exercise_27k
1,129
Write a Python function called `is_valid_file_name` that takes in a string as input and returns `True` if the string is a valid file name, and `False` otherwise. A valid file name must satisfy the following conditions: - Contains only alphanumeric characters, underscores (`_`), hyphens (`-`), and periods (`.`). - Start...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_file_name(string: str) -> bool: """ Checks if the given string is a valid file name. A valid file name must satisfy the following conditions: - Contains only alphanumeric characters, underscores ...
code_exercise_27k
1,131
Write a Python function called `is_valid_variable_name` that takes in a string as input and returns `True` if the string is a valid variable name according to Python's naming conventions, and `False` otherwise. A valid variable name must satisfy the following conditions: - Starts with a letter or an underscore (`_`) - ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_variable_name(string: str) -> bool: """ Determines if the given string is a valid Python variable name. Args: string (str): The string to be checked. Returns: bool: True if the s...
code_exercise_27k
1,132
Write a Python function called `is_valid_module_name` that takes in a string as input and returns `True` if the string is a valid module name, and `False` otherwise. A valid module name must satisfy the following conditions: - Starts with a letter or an underscore (`_`) - Contains only alphanumeric characters and under...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_module_name(string: str) -> bool: """ Determines if the given string is a valid module name. A valid module name must satisfy the following conditions: - Starts with a letter or an underscore (_)...
code_exercise_27k
1,133
Write a Python function called `is_valid_function_name` that takes in a string as input and returns `True` if the string is a valid function name, and `False` otherwise. A valid function name must satisfy the following conditions: - Starts with a letter or an underscore (`_`) - Contains only alphanumeric characters and...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_function_name(string: str) -> bool: """ Determines if the given string is a valid function name in Python. A valid function name must satisfy the following conditions: - Starts with a letter or a...
code_exercise_27k
1,134
Write a Python function to determine if a given string is a valid class name according to the specified rules. The function should return `True` if the string is a valid class name and `False` otherwise. A valid class name must satisfy the following conditions: - Starts with an uppercase letter. - Contains only alphanu...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_class_name(string: str) -> bool: """ Determines if the given string is a valid class name according to the specified rules. Args: string (str): The string to be validated as a class name. ...
code_exercise_27k
1,135
Write a Python function called `is_valid_identifier` that takes in a string as input and returns `True` if the string is a valid identifier, and `False` otherwise. A valid identifier must satisfy the following conditions: - Starts with a letter or an underscore (`_`) - Contains only alphanumeric characters and undersco...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_identifier(string: str) -> bool: """ Determines if the given string is a valid identifier. A valid identifier must satisfy the following conditions: - Starts with a letter or an underscore (_) ...
code_exercise_27k
1,137
Write a Python function to determine if a given string has a valid length for a credit card number. A valid credit card number length must be either 13, 15, or 16 characters. The function should return `True` if the length is valid and `False` otherwise. The code should start with: ```python def is_valid_credit_card_le...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_credit_card_length(card_number: str) -> bool: """ Determines if the given string has a valid length for a credit card number. A valid credit card number length must be either 13, 15, or 16 characters. Args...
code_exercise_27k
1,138
Write a Python function to determine if a given string has a valid length for an email address. The function should return `True` if the string's length is between 3 and 254 characters (inclusive), and `False` otherwise. The function should start with: ```python def is_valid_email_length(email: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_email_length(email: str) -> bool: """ Determines if the given string has a valid length for an email address. Args: email (str): The string to check for valid email length. Returns: bool: T...
code_exercise_27k
1,139
Write a Python function to determine if a given string has a valid length for a URL. A valid URL length must satisfy the following conditions: - The length of the string must be between 1 and 2048 characters (inclusive). The function should start with: ```python def is_valid_url_length(url: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_url_length(url: str) -> bool: """ Determines if the given string has a valid length for a URL. A valid URL length must be between 1 and 2048 characters (inclusive). Args: url (str): The string to c...
code_exercise_27k
1,141
Write a Python function called `is_valid_list_length` that takes in a list and two integers, `minimum_length` and `maximum_length`, as input. The function should return `True` if the length of the list is between the `minimum_length` and `maximum_length` (inclusive), and `False` otherwise. The function should also hand...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_valid_list_length(lst: List, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given list is within the specified range (inclusive). Args: lst (Li...
code_exercise_27k
1,142
Write a Python function called `is_valid_dict_length` that takes in a dictionary and two integers, `minimum_length` and `maximum_length`, as input. The function should return `True` if the dictionary has a length that falls within the specified range (inclusive), and `False` otherwise. The function should handle cases ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_dict_length(dictionary: dict, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given dictionary is within the specified range (inclusive). Args: dictionary (dict): ...
code_exercise_27k
1,143
Write a Python function called `is_valid_tuple_length` that takes in a tuple and two integers representing the minimum and maximum lengths, and returns `True` if the tuple has a length within the specified range (inclusive), and `False` otherwise. The function should validate the input to ensure that the minimum length...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_tuple_length(tup: tuple, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given tuple is within the specified range (inclusive). Args: tup (tuple): The tuple whose ...
code_exercise_27k
1,144
Write a Python function called `is_valid_set_length` that takes in a set and two integers as input and returns a boolean value. The function should return `True` if the length of the set is between the provided minimum and maximum lengths (inclusive), and `False` otherwise. The function should handle edge cases such as...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_set_length(set_obj: set, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given set is within the specified range (inclusive). Args: set_obj (set): The set whose le...
code_exercise_27k
1,145
Write a Python function called `is_valid_range_length` that takes in a range object and two integers representing the minimum and maximum lengths (inclusive) as input. The function should return `True` if the length of the range object falls within the specified range (inclusive), and `False` otherwise. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_range_length(range_obj, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of a given range object falls within a specified range (inclusive). Args: range_obj (range): The r...
code_exercise_27k
1,146
Write a Python function called `is_valid_sequence_length` that takes in a sequence object as input and returns `True` if the sequence has a valid length, and `False` otherwise. A valid sequence length must satisfy the following conditions: - Has a length between a minimum length and a maximum length (inclusive), where ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Sequence def is_valid_sequence_length(sequence: Sequence, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given sequence is within the specified range (inclusive). ...
code_exercise_27k
1,147
Write a Python function called `is_valid_mapping_length` that takes in a mapping object and two integers, `minimum_length` and `maximum_length`, as input. The function should return `True` if the length of the mapping object is within the specified range (inclusive), and `False` otherwise. The mapping object can be a d...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_mapping_length(mapping, minimum_length: int, maximum_length: int) -> bool: """ Determines if the length of the given mapping object is within the specified range (inclusive). Args: mapping: A mapping ob...
code_exercise_27k
1,148
Write a Python function called `is_valid_sequence` that takes in a sequence as input and returns `True` if the sequence is valid, and `False` otherwise. A valid sequence must satisfy the following conditions: - Is not empty (has at least one element) - Contains elements of the same type The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Sequence def is_valid_sequence(sequence: Sequence[Any]) -> bool: """ Determines if a sequence is valid based on the following conditions: - The sequence is not empty. - All elements in the sequen...
code_exercise_27k
1,150
Write a Python function to left-align a given string within a specified width. If the string's length is less than the specified width, pad the string with spaces on the right. If the string's length is greater than or equal to the specified width, return the string as is. The function should start with: ```python def...
Here is the Python code to solve the given problem in the markdown block: ```python def left_align(string: str, width: int) -> str: """ Left-aligns the given string within the specified width. If the string's length is less than the specified width, pad the string with spaces on the right. If the strin...
code_exercise_27k
1,155
Write a Python function named `capitalize_first_letter` that takes a string as input and returns the string with the first letter capitalized. If the input string is `None`, the function should return `None`. The function should handle empty strings by returning an empty string. The code should start with: ```python d...
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 the given string. Args: string (str): The input string to be processed. Returns: str: The input string with the first...
code_exercise_27k
1,156
Write a Python function named `count_vowels` that takes a string as input and returns the count of vowels (both uppercase and lowercase) in the string. The function should handle an empty string and ignore case differences. 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 (both uppercase and lowercase) in the given string. Args: string (str): The input string to count vowels from. Returns: int: The tot...
code_exercise_27k
1,159
Write a Python function named `split_string` that takes two strings as input: the original string and the delimiter. The function should return a list of substrings obtained by splitting the original string at each occurrence of the delimiter. If the delimiter is an empty string, the function should return a list of ch...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def split_string(original_string: str, delimiter: str) -> List[str]: """ Splits the original string by the given delimiter and returns a list of substrings. Args: original_string (str): The ...
code_exercise_27k
1,161
Write a Python function to count the occurrences of each character in a given string and return the result as a dictionary. The function should handle an empty string and ignore case sensitivity by converting all characters to lowercase. The code should start with: ```python from typing import Dict def count_character...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_characters(string: str) -> Dict[str, int]: """ Count the occurrences of each character in the given string and return the result as a dictionary. The function is case insensitive and conver...
code_exercise_27k
1,162
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 ignore case and any non-alphabetic characters. The code should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(string1: str, string2: str) -> bool: """ Determines if two strings are anagrams of each other, ignoring case and non-alphabetic characters. Args: string1 (str): The first string to compare. st...
code_exercise_27k
1,164
Write a Python function named `is_substring` that takes two strings as input and returns `True` if the second string is a substring of the first string, and `False` otherwise. The function should handle edge cases such as empty strings and should be efficient in checking for substrings. The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python def is_substring(string1: str, string2: str) -> bool: """ Determines if the second string is a substring of the first string. Args: string1 (str): The main string in which to search for the substring. string...
code_exercise_27k
1,165
Write a Python function to remove all punctuation characters from a given string. Punctuation characters include any non-alphanumeric characters (i.e., characters that are not letters or digits). The function should return the cleaned string. The code should start with: ```python def remove_punctuation(string: str) ->...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_punctuation(string: str) -> str: """ Removes all punctuation characters from the given string. Args: string (str): The input string from which punctuation characters are to be removed. Returns: ...
code_exercise_27k
1,166
Write a Python function named `swap_case` that takes a string as input and returns the string with the case of each character swapped. Uppercase characters should be converted to lowercase, and lowercase characters should be converted to uppercase. The function should handle any string input, including special characte...
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 given string. Args: string (str): The input string whose characters' cases need to be swapped. Returns: str: A new string w...
code_exercise_27k
1,167
Write a Python function named `title_case` that takes a string as input and returns the string with the first letter of each word capitalized and all other letters lowercase. The function should handle various edge cases, such as empty strings, strings with only one word, and strings with multiple spaces between words....
Here is the Python code to solve the given problem in the markdown block: ```python def title_case(string: str) -> str: """ Capitalizes the first letter of each word in the given string and converts all other letters to lowercase. Args: string (str): The input string to be processed. Returns: ...
code_exercise_27k
1,169
Write a Python function named `remove_whitespace` that takes a string as input and returns the string with leading and trailing 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_whitesp...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespace(string: str) -> str: """ Removes leading and trailing whitespace characters from the given string. Args: string (str): The input string from which to remove whitespace characters. Returns:...
code_exercise_27k
1,172
Write a Python function named `remove_consecutive_duplicates` that takes a string as input and returns a new string with consecutive duplicate characters removed. Only one occurrence of each consecutive duplicate should remain. The function should handle an empty string and strings with no consecutive duplicates. The ...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_consecutive_duplicates(string: str) -> str: """ Removes consecutive duplicate characters from the given string. Args: string (str): The input string from which consecutive duplicates are to be removed. ...
code_exercise_27k
1,174
Write a Python function named `is_alpha` that takes a string as input and returns `True` if the string contains only alphabetic characters (letters), and `False` otherwise. The function should handle edge cases such as empty strings and strings with spaces or special characters. The code should start with: ```python d...
Here is the Python code to solve the given problem in the markdown block: ```python def is_alpha(string: str) -> bool: """ Check if the given string contains only alphabetic characters (letters). Args: string (str): The string to be checked. Returns: bool: True if the string contains o...
code_exercise_27k
1,175
Write a Python function named `replace_case` that takes a string as input and returns the string with the case of each character replaced. Uppercase characters should be converted to lowercase, and lowercase characters should be converted to uppercase. The function should handle strings with any combination of alphabet...
Here is the Python code to solve the given problem in the markdown block: ```python def replace_case(string: str) -> str: """ Replaces the case of each character in the input string. Uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase. Args: stri...
code_exercise_27k
1,176
Write a Python function to validate if a given string is a valid email address. The function should return `True` if the string is a valid email address and `False` otherwise. For simplicity, assume that a valid email address consists of alphanumeric characters, followed by the "@" symbol, followed by a domain name. Th...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_email(email: str) -> bool: """ Validates if the given string is a valid email address. Args: email (str): The email address to validate. Returns: bool: True if the email is valid...
code_exercise_27k
1,177
Write a Python function to determine if a given string is a valid URL. The function should return `True` if the string starts with either "http://" or "https://", and `False` otherwise. The function should be named `is_valid_url` and should take a single string argument. The code should start with: ```python import re...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_url(url: str) -> bool: """ Determines if the given string is a valid URL. A valid URL is defined as a string that starts with either "http://" or "https://". Args: url (str): The string ...
code_exercise_27k
1,179
Write a Python function named `is_valid_username` that takes a string as input and returns `True` if the string is a valid username, and `False` otherwise. A valid username must meet the following criteria: 1. It must contain only alphanumeric characters and underscores (`_`). 2. It must start with a letter (either upp...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_username(username: str) -> bool: """ Validate if the given string is a valid username. A valid username must: 1. Contain only alphanumeric characters and underscores. 2. Start with a letter (...
code_exercise_27k
1,180
Write a Python function to validate if a given string is a valid phone number. A valid phone number is defined as a string that consists of exactly 10 digits. The function should return `True` if the string is a valid phone number and `False` otherwise. The code should start with: ```python import re def is_valid_phon...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_phone_number(phone_number: str) -> bool: """ Validates if the given string is a valid phone number consisting of exactly 10 digits. Args: phone_number (str): The string to be validated as a p...
code_exercise_27k
1,181
Write a Python function named `is_valid_date` that takes a string as input and returns `True` if the string is a valid date in the format "YYYY-MM-DD", and `False` otherwise. The function should ensure that the date string strictly follows the format with a four-digit year, a two-digit month, and a two-digit day, all s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union import re from datetime import datetime def is_valid_date(date_string: str) -> Union[bool, str]: """ Validate if the given string is a valid date in the format "YYYY-MM-DD". Args: date_stri...
code_exercise_27k
1,183
Write a Python function to validate if a given string is a valid social security number (SSN). A valid SSN is defined as a string consisting of exactly 9 digits. The function should return `True` if the string is a valid SSN and `False` otherwise. The code should start with: ```python import re def is_valid_social_sec...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_social_security_number(ssn: str) -> bool: """ Validate if the given string is a valid social security number (SSN). A valid SSN is defined as a string consisting of exactly 9 digits. Args: ...
code_exercise_27k
1,184
Write a Python function to determine if a given string is a valid zip code. A valid zip code is defined as a string consisting of exactly 5 digits. The function should return `True` if the string is a valid zip code and `False` otherwise. The code should start with: ```python def is_valid_zip_code(zip_code: str) -> boo...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_zip_code(zip_code: str) -> bool: """ Determines if the given string is a valid zip code. A valid zip code is defined as a string consisting of exactly 5 digits. Args: zip_code (str): The...
code_exercise_27k
1,185
Write a Python function named `is_valid_ipv4_address` that takes a string as input and returns `True` if the string is a valid IPv4 address, and `False` otherwise. A valid IPv4 address consists of four decimal numbers separated by periods, where each number is between 0 and 255. The function should also handle edge cas...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_ipv4_address(ip_address: str) -> bool: """ Validates if the given string is a valid IPv4 address. Args: ip_address (str): The string to be validated as an IPv4 address. Returns: ...
code_exercise_27k
1,189
Write a Python function to determine if a given string is a valid directory name. A valid directory name should consist only of alphanumeric characters, underscores, and hyphens. The function should return `True` if the string is a valid directory name, and `False` otherwise. The code should start with: ```python impo...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_directory_name(string: str) -> bool: """ Determines if the given string is a valid directory name. A valid directory name consists only of alphanumeric characters, underscores, and hyphens. Args...
code_exercise_27k
1,193
Write a Python function to count the number of words in a given string, considering words as sequences of characters separated by whitespace. The function should handle various edge cases, such as strings with multiple spaces, leading or trailing spaces, and empty strings. The code should start with: ```python def coun...
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. Args: string (str): The input string to count words from. Returns: int: The number of words in the string. """ ...
code_exercise_27k
1,195
Write a Python function named `remove_punctuation` that takes a string as input and returns a new string with all punctuation marks removed. Punctuation marks include any non-alphanumeric characters. The function should handle various types of punctuation and should not modify the original string. The code should star...
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 punctuation marks from a given string. Args: input_string (str): The input string from which punctuation marks are to be removed. Ret...
code_exercise_27k
1,199
Write a Python function named `replace_words` that takes a string, a target word, and a replacement word as inputs. The function should return a new string with all occurrences of the target word replaced by the replacement word. The function should be case-sensitive and should not replace substrings of other words tha...
Here is the Python code to solve the given problem in the markdown block: ```python import re def replace_words(string: str, target_word: str, replacement_word: str) -> str: """ Replaces all occurrences of a target word with a replacement word in a string. The replacement is case-sensitive and only whole w...
code_exercise_27k
1,200
Write a Python function named `count_vowels` that takes a string as input and returns the count of vowels (a, e, i, o, u) in the string. The count should be case-insensitive. The function should handle any string input, including empty strings and strings with no vowels. 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_vowels(string: str) -> int: """ Counts the number of vowels in a string (case-insensitive). Args: string (str): The input string. Returns: int: The count of vowels in the string. """ #...
code_exercise_27k
1,202
Write a Python function named `is_palindrome` that takes a string as input and returns `True` if the string is a palindrome, and `False` otherwise. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, disregarding spaces, punctuation, and capitalization. The...
Here is the Python code to solve the given problem in the markdown block: ```python import string def is_palindrome(input_string: str) -> bool: """ Checks if a string is a palindrome. Args: input_string (str): The input string to be checked. Returns: bool: True if the string is a pali...
code_exercise_27k
1,204
Write a Python function named `split_string` that takes a string and a delimiter as inputs. The function should split the string into a list of substrings based on the delimiter and return the list. Additionally, the function should handle cases where the delimiter is not found in the string by returning the original s...
Here is the Python code to solve the given problem in the markdown block: ```python def split_string(string: str, delimiter: str) -> list: """ Splits a string into a list of substrings based on a delimiter. If the delimiter is not found in the string, the original string is returned as a single-element list...
code_exercise_27k
1,205
Write a Python function named `remove_whitespace` that takes a string as input and returns a new string with all whitespace characters removed. The function should handle various types of whitespace characters, including spaces, tabs, and newline characters. The code should start with: ```python def remove_whitespace(s...
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 (spaces, tabs, and newlines) from the input string. Args: string (str): The input string from which whitespace characters are to be re...
code_exercise_27k
1,208
Write a Python function named `find_longest_common_prefix` that takes a list of strings as input and returns the longest common prefix among the strings. If there is no common prefix, return an empty string. The function should handle edge cases such as an empty list or a list with strings of varying lengths efficientl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_longest_common_prefix(strings: List[str]) -> str: """ Finds the longest common prefix among a list of strings. Args: strings (List[str]): The list of strings to find the common pref...
code_exercise_27k
1,209
Write a Python function named `capitalize_first_letters` that takes a string as input and returns a new string with the first letter of each word capitalized. The function should ignore leading and trailing whitespace. The code should start with: ```python def capitalize_first_letters(string: str) -> str: ```
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 a string, ignoring leading and trailing whitespace. Args: string (str): The input string. Returns: str:...
code_exercise_27k
1,210
Write a Python function named `reverse_words_order` that takes a string as input and returns a new string with the order of the words reversed. The function should ignore leading and trailing whitespace and should not modify the case of the words. The code should start with: ```python def reverse_words_order(string: st...
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 string, ignoring leading and trailing whitespace. Args: string (str): The input string. Returns: str: The new string wit...
code_exercise_27k
1,214
Write a Python function named `remove_non_alphabetic` that takes a string as input and returns a new string with all non-alphabetic characters removed. The function should preserve the order of alphabetic characters and handle both uppercase and lowercase letters. The code should start with: ```python def remove_non_a...
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 the input string. Args: string (str): The input string from which non-alphabetic characters will be removed. Returns...
code_exercise_27k
1,216
Write a Python function named `remove_special_characters` that takes a string as input and returns a new string with all special characters removed. Special characters include any non-alphanumeric characters except spaces. The function should handle various edge cases, such as empty strings, strings with only special c...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_special_characters(string: str) -> str: """ Removes special characters from a string. Special characters include any non-alphanumeric characters except spaces. Args: string (str): The input string fr...
code_exercise_27k
1,218
Write a Python function named `find_common_elements` that takes two lists as input and returns a new list containing the common elements between the two lists. The order of the elements in the new list should be the same as their first occurrence in the first list. The function should be efficient and avoid unnecessary...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(list1: List[int], list2: List[int]) -> List[int]: """ Finds the common elements between two lists and returns them in the order of their first occurrence in the first list. ...
code_exercise_27k
1,219
Write a Python function named `find_unique_elements` that takes a list as input and returns a new list containing only the unique elements from the input list. The order of the elements in the new list should be preserved. The function should be efficient and avoid using list comprehensions for clarity and performance....
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_unique_elements(lst: List[int]) -> List[int]: """ Finds the unique elements from a list while preserving the order of first appearance. Args: lst (List[int]): The input list from wh...
code_exercise_27k
1,220
Write a Python function named `find_missing_elements` that takes two lists as input: a main list and a reference list. The function should return a new list containing the elements from the reference list that are missing in the main list. The order of the elements in the new list should be preserved. The function shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_missing_elements(main_list: List[int], reference_list: List[int]) -> List[int]: """ Finds the missing elements from a reference list in a main list while preserving the order. Args: ...
code_exercise_27k
1,223
Write a Python function named `find_missing_numbers` that takes two lists of integers as input: a main list and a reference list. The function should return a new list containing the numbers from the reference list that are missing in the main list. The order of the numbers in the new list should be preserved. The func...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_missing_numbers(main_list: List[int], reference_list: List[int]) -> List[int]: """ Finds the missing numbers from a reference list in a main list while preserving the order. Args: m...
code_exercise_27k
1,228
Write a Python function named `find_max_sum_subarray` that takes a list of integers as input and returns the maximum sum of any contiguous subarray within the list. The function should be efficient and use an optimal algorithm to achieve this. The code should start with: ```python from typing import List def find_max...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_sum_subarray(nums: List[int]) -> int: """ Finds the maximum sum of any contiguous subarray within a list of integers. Args: nums (List[int]): The list of integers. Returns:...
code_exercise_27k
1,230
Write a Python function to find the length of the longest increasing subsequence (LIS) in a given list of integers. An increasing subsequence is a sequence of numbers in which each number is strictly greater than the previous number. The function should start with: ```python from typing import List def find_longest_in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_longest_increasing_subsequence(nums: List[int]) -> int: """ Finds the length of the longest increasing subsequence in a list of integers. Args: nums (List[int]): The list of integer...
code_exercise_27k
1,235
Write a Python function named `find_edit_distance` that calculates the minimum number of operations required to transform one string into another. The allowed operations are: insert a character, delete a character, or replace a character. The function should take two strings as input and return the edit distance as an ...
Here is the Python code to solve the given problem in the markdown block: ```python def find_edit_distance(string1: str, string2: str) -> int: """ Calculate the minimum number of operations required to transform one string into another. Args: string1 (str): The first input string. string2 (...