source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
14,645
Write a Python function to calculate the median of a list of numbers. The median is the middle value in a sorted list of numbers. If the list has an even number of elements, the median is the average of the two middle numbers. The function should handle lists of any length, including empty lists. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_median(numbers: List[int]) -> float: """ Calculate the median of a list of numbers. Args: numbers (List[int]): A list of integers for which the median is to be calculated. Retu...
code_exercise_27k
14,657
Write a Python function to count the number of words in a given string, considering various edge cases such as multiple spaces, leading and trailing spaces, and empty strings. The function should handle these cases gracefully and return the correct word count. The code should start with: ```python def count_words(inpu...
Here is the Python code to solve the given problem in the markdown block: ```python def count_words(input_string: str) -> int: """ Count the number of words in a given string, handling various edge cases. Args: input_string (str): The input string to count the words from. Returns: int:...
code_exercise_27k
14,661
Write a Python function to find the k-th smallest element in a given list of numbers. The function should handle duplicate values by considering unique elements only. If the list has fewer than k elements, the function should raise a `ValueError`. The code should start with: ```python from typing import List, Union d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_kth_smallest(numbers: List[Union[int, float]], k: int) -> Union[int, float]: """ Find the k-th smallest element in a given list of numbers, considering unique elements only. Args: ...
code_exercise_27k
14,664
Write a Python function to round a given number to a specified number of decimal places. The function should take two parameters: the number to be rounded and the number of decimal places to round to. The function should return the rounded value of the given number. The code should start with: ```python def round_to_d...
Here is the Python code to solve the given problem in the markdown block: ```python def round_to_decimal_places(number: float, decimal_places: int) -> float: """ Round a given number to a specified number of decimal places. Args: number (float): The number to be rounded. decimal_places (int...
code_exercise_27k
14,670
Write a Python function to convert a given string to lowercase. The function should handle strings with alphanumeric characters, special characters, and spaces. The code should start with: ```python def convert_to_lowercase(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def convert_to_lowercase(s: str) -> str: """ Convert a given string to lowercase. Args: s (str): The string to be converted. Returns: str: The converted string in lowercase. Example: >>> co...
code_exercise_27k
14,671
Write a Python function to convert a given string to uppercase. The function should handle any string input, including those with special characters and numbers, and should return the string with all alphabetic characters converted to uppercase. The code should start with: ```python def convert_to_uppercase(string: st...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_to_uppercase(string: str) -> str: """ Convert a given string to uppercase. This function takes a string as input and returns a new string with all alphabetic characters converted to uppercase. Non-alphabetic...
code_exercise_27k
14,672
Write a Python function to validate if a given string is a valid phone number according to the following rules: 1. The phone number must contain exactly 10 digits. 2. The phone number must not contain any non-digit characters. 3. The first digit of the phone number must not be '0' or '1'. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_phone_number(phone_number: str) -> bool: """ Validate if a given string is a valid phone number. Args: phone_number (str): The phone number to be validated. Returns: bool: True if the phone...
code_exercise_27k
14,679
Write a Python function to check if a given string starts with any of the characters in a specified set of characters. The function should return a boolean indicating whether the string starts with any of the characters in the set. The code should start with: ```python def starts_with_any_character(string: str, charac...
Here is the Python code to solve the given problem in the markdown block: ```python def starts_with_any_character(string: str, characters: set) -> bool: """ Check if a given string starts with any of the characters in a specified set. Args: string (str): The string to check. characters (set...
code_exercise_27k
14,684
Write a Python function to determine if a given number is a perfect square. The function should handle both integer and floating-point inputs. The function should return `True` if the number is a perfect square and `False` otherwise. The code should start with: ```python def is_perfect_square(num: float) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_perfect_square(num: float) -> bool: """ Determine if a given number is a perfect square. Parameters: num (float): The number to be checked. Returns: bool: True if the number is a perfect square, False ot...
code_exercise_27k
14,685
Write a Python function to check if a given string ends with any of the characters in a specified set of characters. The function should return a boolean indicating whether the string ends with any of the characters in the set. The code should start with: ```python def ends_with_any(string: str, char_set: set) -> bool...
Here is the Python code to solve the given problem in the markdown block: ```python def ends_with_any(string: str, char_set: set) -> bool: """ Check if a given string ends with any of the characters in a specified set of characters. Args: string (str): The input string to be checked. char_s...
code_exercise_27k
14,687
Write a Python function to determine if a given number is a power of two. A number is considered a power of two if it can be expressed as \(2^k\) where \(k\) is a non-negative integer. The function should return `True` if the number is a power of two, and `False` otherwise. The code should start with: ```python def is...
Here is the Python code to solve the given problem in the markdown block: ```python def is_power_of_two(num: int) -> bool: """ Determine if a given number is a power of two. Args: num (int): The number to be checked. Returns: bool: True if the number is a power of two, False otherwise....
code_exercise_27k
14,688
Write a Python function to check if a given number is even. The function should handle both integers and floating-point numbers, ensuring that the input is non-negative. If the input is not a non-negative integer or float, the function should raise a `ValueError`. The code should start with: ```python def is_even(num)...
Here is the Python code to solve the given problem in the markdown block: ```python def is_even(num): """ Check if a given number is even. Parameters: num (int or float): The number to be checked. Returns: bool: True if the number is even, False otherwise. Raises: ValueError: If the i...
code_exercise_27k
14,690
Write a Python function to find the minimum and maximum values in a list of numeric values. The function should handle empty lists and lists containing non-numeric values by raising appropriate exceptions. The code should start with: ```python from typing import List, Tuple def find_min_max(lst: List[float]) -> Tuple[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_min_max(lst: List[float]) -> Tuple[float, float]: """ Find the minimum and maximum values in a list of numeric values. Args: lst (List[float]): A list of numeric values. ...
code_exercise_27k
14,691
Write a Python function to convert a given binary number represented as a string to its decimal equivalent. The function should handle binary numbers of any length and should return the decimal equivalent as an integer. The code should start with: ```python def binary_to_decimal(binary: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_decimal(binary: str) -> int: """ Converts a binary number represented as a string to its decimal equivalent. Args: binary (str): The binary number to convert. It should consist only of '0' and '1'. ...
code_exercise_27k
14,692
Write a Python function to find the maximum value from a given list of integers or floats. The function should raise a `ValueError` if the input list is empty. The code should start with: ```python from typing import List, Union def find_maximum_value(lst: List[Union[int, float]]) -> Union[int, float]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_maximum_value(lst: List[Union[int, float]]) -> Union[int, float]: """ Find the maximum value from a given list of integers or floats. Args: lst (List[Union[int, float]]): The...
code_exercise_27k
14,702
Write a Python function to find the maximum value from a given list of numeric values. The function should handle empty lists by raising a `ValueError` and non-numeric values by raising a `TypeError`. The code should start with: ```python from typing import List, Union def find_maximum_value(lst: List[Union[float, int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_maximum_value(lst: List[Union[float, int]]) -> Union[float, int]: """ Find and return the maximum value from a given list of numeric values. Parameters: lst (List[Union[float, in...
code_exercise_27k
14,704
Write a Python function to separate even and odd numbers from a given list of integers and return them as two separate lists. The function should handle an empty list and return empty lists for both even and odd numbers. The code should start with: ```python from typing import List, Tuple def separate_even_odd(numbers...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def separate_even_odd(numbers_list: List[int]) -> Tuple[List[int], List[int]]: """ Separates even and odd numbers from a given list of integers and returns them as two separate lists. Args: ...
code_exercise_27k
14,706
Write a Python function to extract all the numbers from a given string using regular expressions. The function should return a list of all the numbers found in the string. The code should start with: ```python import re from typing import List def extract_numbers(string: str) -> List[str]: ```
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[str]: """ Extracts all the numbers from a given string using regular expressions. Args: string (str): The input string from which to extrac...
code_exercise_27k
14,708
Write a Python function to remove all white spaces from a given string, including spaces, tabs, and newline characters. The function should handle various types of white spaces efficiently. The code should start with: ```python def remove_all_whitespace(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_all_whitespace(string: str) -> str: """ Removes all white spaces, including spaces, tabs, and newline characters, from a given string. Args: string (str): The input string from which all white ...
code_exercise_27k
14,710
Write a Python function to validate a given URL using a regular expression. The function should return `True` if the URL is valid and `False` otherwise. The URL should match the following criteria: - It must start with `http://` or `https://`. - It must contain a domain name that starts with a letter or number and can ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def validate_url(url: str) -> bool: """ Validate a given URL using a regular expression. Args: url (str): The URL to be validated. Returns: bool: True if the URL is valid, False otherwise. ...
code_exercise_27k
14,711
Write a Python function to extract all words starting with a capital letter from a given sentence. The function should use regular expressions to identify and extract these words efficiently. The code should start with: ```python import re from typing import List def extract_capital_words(sentence: str) -> List[str]: ...
Here is the Python code to solve the given problem in the markdown block: : ```python import re from typing import List def extract_capital_words(sentence: str) -> List[str]: """ Extracts all words starting with a capital letter from the given sentence. Args: sentence (str): The input sentence fro...
code_exercise_27k
14,712
Write a Python function to sort a given list in descending order using a custom sorting algorithm. The function should implement the QuickSort algorithm to achieve this. The code should start with: ```python from typing import List def sort_list_descending(input_list: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def sort_list_descending(input_list: List[int]) -> List[int]: """ Sorts a given list in descending order using the QuickSort algorithm. Args: input_list (List[int]): The list to be sorted....
code_exercise_27k
14,713
Write a Python function to calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. The function should handle both positive and negative integers. The code should start with: ```python def gcd(a: int, b: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def gcd(a: int, b: int) -> int: """ Calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. Args: a (int): The first number. b (int): The second number. Returns: ...
code_exercise_27k
14,718
Write a Python function to find the first non-repeating character in a given string. If no non-repeating character is found, the function should return `None`. The function should be efficient and handle various edge cases, such as an empty string or a string with all repeating characters. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def find_first_non_repeating_character(string: str) -> str: """ Find the first non-repeating character in a given string. Args: string (str): The input string. Returns: str: The first non-repeating char...
code_exercise_27k
14,721
Write a Python function to check if a given string is a valid IPv4 address. The function should validate the format and range of each octet in the IP address. The code should start with: ```python def is_valid_ipv4(ip_address: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv4(ip_address: str) -> bool: """ Check if a given string is a valid IPv4 address. Args: ip_address (str): The string representation of an IP address. Returns: bool: True if the given stri...
code_exercise_27k
14,724
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_order(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_order(input_string: str) -> str: """ Reverse the order of words in a given string. Args: input_string (str): The input string to reverse the order of words. Returns: str: The string wi...
code_exercise_27k
14,726
Write a Python function to calculate the sum of all elements in a given list of numeric values. The function should handle lists containing integers and floating-point numbers. The function should raise a `TypeError` if the input is not a list or if any element in the list is not a numeric value. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def calculate_sum(lst: List[Union[int, float]]) -> Union[int, float]: """ Calculate the sum of all elements in a given list of numeric values. Args: lst (List[Union[int, float]]): The...
code_exercise_27k
14,730
Write a Python function to check if a given list is empty. The function should return `True` if the list is empty and `False` otherwise. The code should start with: ```python from typing import List def is_list_empty(lst: List[int]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_list_empty(lst: List[int]) -> bool: """ Check if a given list is empty. Args: lst (List[int]): The list to check for emptiness. Returns: bool: True if the list is empty, ...
code_exercise_27k
14,733
Write a Python function to extract all the hashtags from a given tweet using regular expressions. The function should return a list of all the extracted hashtags. The code should start with: ```python import re from typing import List def extract_hashtags(tweet: str) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python import re from typing import List def extract_hashtags(tweet: str) -> List[str]: """ Extracts all the hashtags from a given tweet using regular expressions. Args: tweet (str): The tweet to extract hashtags from. ...
code_exercise_27k
14,734
Write a Python function to check if a given element exists in a list and return the index of the first occurrence of the element if it exists, otherwise return -1. The function should handle both basic data types (integers, strings, etc.) and complex data types (lists, tuples, etc.). The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any def find_element_index(element: Any, lst: list) -> int: """ Find the index of the first occurrence of a given element in a list. Args: element (Any): The element to search for. lst (l...
code_exercise_27k
14,739
Write a Python function to calculate the sum of all the digits in a given number using recursion. The function should handle both positive and negative integers. The code should start with: ```python def sum_of_digits(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def sum_of_digits(n: int) -> int: """ Calculates the sum of all the digits in a given number using recursion. Args: n (int): The number for which the sum of digits is to be calculated. Returns: int: T...
code_exercise_27k
14,741
Write a Python function to count the number of occurrences of each element in a given list. The function should return a dictionary where the keys are the unique elements in the list and the values are the number of occurrences of each element. The code should start with: ```python from typing import List, Dict def co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_occurrences(lst: List[int]) -> Dict[int, int]: """ Count the number of occurrences of each element in a list. Args: lst (List[int]): The input list of integers. Returns:...
code_exercise_27k
14,743
Write a Python function to check if a given string is a valid email address. The function should use a regular expression to validate the email address according to the following rules: 1. The email address must contain exactly one "@" symbol. 2. The part before the "@" symbol can contain letters, digits, underscores, ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_email(email: str) -> bool: """ Check if a given string is a valid email address. Args: email (str): The email address to be checked. Returns: bool: True if the email address is v...
code_exercise_27k
14,745
Write a Python function to determine if a given string contains at least one digit using regular expressions. The function should return `True` if the string contains at least one digit, and `False` otherwise. The code should start with: ```python import re def contains_digit(text: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def contains_digit(text: str) -> bool: """ Determine if a given string contains at least one digit using regular expressions. Args: text (str): The string to be checked for the presence of at least one di...
code_exercise_27k
14,748
Write a Python function to extract all HTML tags from a given HTML document string. The function should return a list of all HTML tags found in the document. The function should handle various HTML tags, including those with attributes and self-closing tags. The code should start with: ```python import re from typing i...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_html_tags(html_str: str) -> List[str]: """ Extracts all HTML tags from a given HTML document. Args: html_str (str): The input HTML document as a string. Returns: ...
code_exercise_27k
14,750
Write a Python function to convert a given string into a list of its characters, while also handling special cases such as an empty string and a string with only whitespace characters. The function should return an empty list if the input string is empty or contains only whitespace characters. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def string_to_list_with_validation(string: str) -> List[str]: """ Convert a given string to a list of characters, handling special cases. This function checks if the input string is empty or contain...
code_exercise_27k
14,751
Write a Python function to reverse a given list in-place without using any built-in functions or slicing. The function should modify the original list and return it. The code should start with: ```python from typing import List def reverse_list_in_place(lst: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def reverse_list_in_place(lst: List[int]) -> List[int]: """ Reverse a given list in-place without using built-in functions or slicing. Args: lst (List[int]): The list to be reversed. ...
code_exercise_27k
14,753
Write a Python function to check if a given string is a valid IPv6 address. An IPv6 address consists of eight groups of four hexadecimal digits, separated by colons. Each group must contain exactly four hexadecimal digits, and the entire address must be exactly 39 characters long (including colons). The function should...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv6(address: str) -> bool: """ Check if a given string is a valid IPv6 address. An IPv6 address consists of eight groups of four hexadecimal digits, separated by colons. Each group must contain exactly fou...
code_exercise_27k
14,755
Write a Python function to find and extract all the dates in the format `MM/DD/YYYY` from a given text document. The function should return a list of all the dates found in the text. The code should start with: ```python import re from typing import List def extract_dates(text: str) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_dates(text: str) -> List[str]: """ Extracts all dates in the format MM/DD/YYYY from the given text. Args: text (str): The text to search for dates. Returns: ...
code_exercise_27k
14,759
Write a Python function to convert a given decimal number to its binary representation. The function should handle non-negative integers and return the binary representation as a string. The function should also raise a `ValueError` if the input is not a non-negative integer. The code should start with: ```python def ...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_binary(decimal: int) -> str: """ Converts a decimal number to its binary representation. Args: decimal (int): The decimal number to convert. Must be a non-negative integer. Returns: str: ...
code_exercise_27k
14,763
Write a Python function to count the frequency of a specific element in a given list. The function should be able to handle lists of any type of elements, including integers, strings, and other objects. The code should start with: ```python from typing import List, Any def count_frequency(lst: List[Any], element: Any)...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def count_frequency(lst: List[Any], element: Any) -> int: """ Count the frequency of a specific element in a given list. Args: lst (List[Any]): The list in which to count the frequency....
code_exercise_27k
14,764
Write a Python function to calculate the sum of all the numbers in a given list using recursion. The function should handle lists of integers and floats. The code should start with: ```python from typing import List, Union def recursive_sum(numbers: List[Union[int, float]]) -> Union[int, float]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def recursive_sum(numbers: List[Union[int, float]]) -> Union[int, float]: """ Calculates the sum of all numbers in a given list using recursion. Args: numbers (List[Union[int, float]]...
code_exercise_27k
14,768
Write a Python function to find the mode(s) of a given list of numbers. The mode is the number(s) that appear most frequently in the list. If there are multiple numbers with the same highest frequency, the function should return all of them. The function should handle an empty list by raising a `ValueError`. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_mode(numbers: List[int]) -> List[int]: """ Find the mode(s) of a given list of numbers. The mode is the number(s) that appear most frequently in the list. If there are multiple numbers ...
code_exercise_27k
14,769
Write a Python function to check if a given string is a valid username based on specific rules. The function should return `True` if the username is valid and `False` otherwise. The rules for a valid username are: - The username must contain only alphanumeric characters (a-z, A-Z, 0-9) and underscores (_). - The userna...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_username(username: str) -> bool: """ Check if a given string is a valid username based on specific rules. Parameters: username (str): The username to be checked. Returns: bool: True if t...
code_exercise_27k
14,774
Write a Python function to check if a given string contains only numeric characters, including handling strings with leading and trailing spaces. The function should return `True` if the string contains only numeric characters after stripping the spaces, and `False` otherwise. The code should start with: ```python def...
Here is the Python code to solve the given problem in the markdown block: ```python def is_numeric_string_with_spaces(string: str) -> bool: """ Check if a given string contains only numeric characters after stripping leading and trailing spaces. Args: string (str): The string to be checked. Re...
code_exercise_27k
14,777
Write a Python function to determine if a given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. The code should start with: ```python def is_armstron...
Here is the Python code to solve the given problem in the markdown block: ```python def is_armstrong_number(number: int) -> bool: """ Determine if a given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits. F...
code_exercise_27k
14,779
Write a Python function to validate if a given string contains only uppercase letters and digits. The function should use a regular expression to perform the validation. The code should start with: ```python import re def is_valid_string(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_string(string: str) -> bool: """ Validate if a given string contains only uppercase letters and digits. Args: string (str): The string to be validated. Returns: bool: True if the...
code_exercise_27k
14,783
Write a Python function to extract all phone numbers formatted as `(XXX) XXX-XXXX` from a given text string. The function should use regular expressions to identify and extract the phone numbers. The code should start with: ```python import re from typing import List def extract_phone_numbers(text: str) -> List[str]: ...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_phone_numbers(text: str) -> List[str]: """ Extracts all phone numbers of the format (XXX) XXX-XXXX from a given text string. Args: text (str): The input text string whe...
code_exercise_27k
14,785
Write a Python function to validate whether a given string consists of only letters and digits using a regular expression. The function should return `True` if the string matches the criteria, and `False` otherwise. The code should start with: ```python import re def is_valid_string(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_string(string: str) -> bool: """ Validate whether a given string consists of only letters and digits. Args: string (str): The string to be checked. Returns: bool: True if the str...
code_exercise_27k
14,788
Write a Python function to extract all valid email addresses from a given text document using regular expressions. The function should handle various email formats and return a list of all found email addresses. The code should start with: ```python import re from typing import List def extract_emails(text: str) -> Li...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_emails(text: str) -> List[str]: """ Extracts all valid email addresses from the given text document using regular expressions. Args: text (str): The text document to se...
code_exercise_27k
14,790
Write a Python function to remove all special characters from a given string, leaving only alphanumeric characters and spaces. The function should handle various edge cases, such as empty strings, strings with only special characters, and strings with no special characters. The code should start with: ```python import ...
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 given string, leaving only alphanumeric characters and spaces. Args: string (str): The input string from whic...
code_exercise_27k
14,792
Write a Python function to calculate the product of all the numbers in a given list, handling both integers and floating-point numbers. The function should return the product as a floating-point number. The code should start with: ```python from typing import List def calculate_product(numbers: List[float]) -> float: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_product(numbers: List[float]) -> float: """ Calculates the product of all the numbers in a given list. Args: numbers (List[float]): A list of numbers (integers or floating-poin...
code_exercise_27k
14,804
Write a Python function to check if a given string contains at least one special character from a predefined set of special characters (!@#$%^&*). The function should return `True` if the string contains at least one special character, and `False` otherwise. The code should start with: ```python import re def contains...
Here is the Python code to solve the given problem in the markdown block: ```python import re def contains_special_character(string: str) -> bool: """ Check if the given string contains at least one special character from the set (!@#$%^&*). Args: string (str): The input string to be checked. ...
code_exercise_27k
14,817
Write a Python function to convert a decimal number to its corresponding Roman numeral representation. The function should handle decimal numbers in the range from 1 to 3999. The code should start with: ```python def decimal_to_roman(decimal_num: int) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_roman(decimal_num: int) -> str: """ Converts a decimal number to its corresponding Roman numeral representation. Args: decimal_num (int): The decimal number to be converted. Must be between 1 and 3999...
code_exercise_27k
14,820
Write a Python function to check if a given string is a valid UUID (Universally Unique Identifier) version 4. A valid UUID version 4 should match the pattern `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`, where `x` is any hexadecimal digit (0-9, a-f) and `y` is one of `8`, `9`, `a`, or `b`. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_uuid_v4(uuid: str) -> bool: """ Checks if a given string is a valid UUID version 4. Parameters: uuid (str): The string to be checked. Returns: bool: True if the string is a valid UUID ve...
code_exercise_27k
14,821
Write a Python function to extract all valid email addresses from a given text document. The email addresses should follow the standard format of `username@domain.extension`. The function should handle common variations in email formats, such as different domain extensions and subdomains. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_email_addresses(text: str) -> List[str]: """ Extracts all valid email addresses from the given text. Args: text (str): The text document to search for email addresses. ...
code_exercise_27k
14,824
Write a Python function to validate if a given string consists only of letters (both uppercase and lowercase), digits, and underscores. The function should return `True` if the string is valid and `False` otherwise. The code should start with: ```python import re def is_valid_string(input_string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_string(input_string: str) -> bool: """ Validate if the given string consists only of letters, digits, and underscores. Args: input_string (str): The string to be validated. Returns: ...
code_exercise_27k
14,827
Write a Python function to determine if a given number is a happy number. A happy number is defined by the following process: - Starting with any positive integer, replace the number by the sum of the squares of its digits. - Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_happy_number(n: int) -> bool: """ Determine if a given number is a happy number. Args: n (int): The number to check. Returns: bool: True if the number is a happy number, False otherwise. A h...
code_exercise_27k
14,830
Write a Python function to check if a given string contains at least one non-alphanumeric character using regular expressions. The function should return `True` if the string contains at least one non-alphanumeric character, and `False` otherwise. The code should start with: ```python import re def contains_non_alphan...
Here is the Python code to solve the given problem in the markdown block: ```python import re def contains_non_alphanumeric(string: str) -> bool: """ Check if the given string contains at least one non-alphanumeric character. Args: string (str): The string to be checked. Returns: bool...
code_exercise_27k
14,833
Write a Python function to remove all vowels from a given string. 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 need to be removed. Returns: str: The string with all vowels rem...
code_exercise_27k
14,837
Write a Python function to calculate the average of all the numeric values in a given dictionary. The function should handle dictionaries with numeric values only and raise a `ValueError` if the dictionary is empty or contains non-numeric values. The code should start with: ```python def calculate_average(dictionary: ...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_average(dictionary: dict) -> float: """ Calculate the average of all the numeric values in a dictionary. Args: dictionary (dict): The dictionary containing numeric values. Returns: float: ...
code_exercise_27k
14,840
Write a Python function to determine if a given string contains at least one repeating character using a regular expression. The function should return `True` if the string contains at least one repeating character, and `False` otherwise. The code should start with: ```python import re def has_repeating_char(string: s...
Here is the Python code to solve the given problem in the markdown block: ```python import re def has_repeating_char(string: str) -> bool: """ Determines if a given string contains at least one repeating character. Args: string (str): The input string to be checked for repeating characters. R...
code_exercise_27k
14,841
Write a Python function to extract all words from a given sentence that start with a number. The function should use regular expressions to identify and return these words. The code should start with: ```python import re from typing import List def extract_words_starting_with_number(sentence: str) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_words_starting_with_number(sentence: str) -> List[str]: """ Extracts all the words starting with a number in a given sentence. Args: sentence (str): The input sentence ...
code_exercise_27k
14,843
Write a Python function to validate if a given string consists of exactly four digits using regular expressions. The function should return `True` if the string matches the pattern and `False` otherwise. The code should start with: ```python import re def is_four_digit_string(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_four_digit_string(string: str) -> bool: """ Validates if the given string consists of exactly four digits. Args: string (str): The string to be validated. Returns: bool: True if the st...
code_exercise_27k
14,847
Write a Python function to extract all the image URLs from a given HTML document using regular expressions. The function should handle various forms of image tags and ensure that only valid URLs are extracted. The code should start with: ```python import re from typing import List def extract_image_urls(html: str) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_image_urls(html: str) -> List[str]: """ Extracts all the image URLs from the given HTML document using regular expressions. Args: html (str): The HTML document from whi...
code_exercise_27k
14,848
Write a Python function to calculate the total travel distance given a list of distances and travel modes (e.g., car, train, bus, airplane). The function should handle invalid travel modes and ensure that the number of distances matches the number of travel modes. The code should start with: ```python from typing impor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_total_distance(distances: List[float], modes: List[str]) -> float: """ Calculate the total travel distance given a list of distances and travel modes. Args: distances (List[flo...
code_exercise_27k
14,859
Write a Python function to validate if a given string consists of exactly three uppercase letters using regular expressions. The function should return `True` if the string matches the criteria, otherwise `False`. The code should start with: ```python import re def is_three_uppercase_letters(s: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_three_uppercase_letters(s: str) -> bool: """ Validates if a given string consists of exactly three uppercase letters. Args: s (str): The string to be validated. Returns: bool: True if ...
code_exercise_27k
14,860
Write a Python function to extract all the Twitter handles (@username) from a given tweet using regular expressions. The function should return a list of all the extracted Twitter handles. The code should start with: ```python import re from typing import List def extract_twitter_handles(tweet: str) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re from typing import List def extract_twitter_handles(tweet: str) -> List[str]: """ Extracts all the Twitter handles (@username) from a given tweet. Args: tweet (str): The tweet from which Twitter handles n...
code_exercise_27k
14,867
Write a Python function to implement a modified linear search algorithm that stops after a certain number of comparisons. The function should return the index of the target element if found within the specified number of comparisons, otherwise it should return -1. The code should start with: ```python from typing impor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def modified_linear_search(arr: List[Any], target: Any, max_comparisons: int = 100) -> int: """ Perform a modified linear search on a given list, stopping after a certain number of comparisons. ...
code_exercise_27k
14,870
Write a Python function to count the maximum number of consecutive occurrences of a specified element in a given list. The function should return the maximum count of consecutive occurrences of the specified element. The code should start with: ```python from typing import List, Any def max_consecutive_occurrences(ls...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def max_consecutive_occurrences(lst: List[Any], element: Any) -> int: """ Counts the maximum number of consecutive occurrences of a specified element in a list. Args: lst (List[Any]): T...
code_exercise_27k
14,871
Implement a binary search algorithm using the linear search approach. The task is to write a Python function that performs a binary search on a sorted list of numbers. The function should return the index of the target element if it is found in the list, otherwise it should return -1. The function should use a linear s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def binary_search_linear(arr: List[int], target: int) -> int: """ Perform binary search on a sorted list using the linear search approach to find the midpoint. Args: arr (List[int]): A sorte...
code_exercise_27k
14,875
Write a Python function to find the kth largest element in a given list of integers using a linear search approach. The function should handle edge cases such as an empty list or an invalid k value. The code should start with: ```python from typing import List def find_kth_largest_element(nums: List[int], k: int) -> i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_kth_largest_element(nums: List[int], k: int) -> int: """ Find the kth largest element in a list using a linear search approach. Args: nums (List[int]): The list of integers. ...
code_exercise_27k
14,879
Write a Python function to calculate the sum of all even Fibonacci numbers up to a given limit. The function should efficiently generate Fibonacci numbers and sum only the even ones. The code should start with: ```python def sum_even_fibonacci(limit: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_even_fibonacci(limit: int) -> int: """ Calculates the sum of all even Fibonacci numbers up to a given limit. Args: limit (int): The upper limit for the Fibonacci numbers. Returns: int: The sum o...
code_exercise_27k
14,882
Write a Python function to remove all duplicates from a given list without using any built-in functions. The function should maintain the order of the first occurrence of each element. The code should start with: ```python from typing import List def remove_duplicates(lst: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates(lst: List[int]) -> List[int]: """ Removes duplicates from a list without using any built-in functions. Args: lst (List[int]): A list containing elements with possib...
code_exercise_27k
14,887
Write a Python function to find the median of a given list of numeric elements using the linear search approach. The function should handle both even and odd lengths of the list and return the median as a float. If the input list is empty, the function should raise a `ValueError`. The code should start with: ```python...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_median(lst: List[int]) -> float: """ Find the median of a list using the linear search approach. Args: lst (List[int]): The input list containing numeric elements. Returns: ...
code_exercise_27k
14,889
Write a Python function to find the length of the longest consecutive sequence of numbers in a given list. A consecutive sequence is defined as a sequence of numbers where each number is exactly one more than the previous number. The function should efficiently handle the input list and return the length of the longest...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_consecutive_sequence(nums: List[int]) -> int: """ Finds the length of the longest consecutive sequence of numbers in a given list. Args: nums (List[int]): A list of integers. ...
code_exercise_27k
14,890
Write a Python function to perform a linear search on a list and return the index of the target value. If the target value is not found, the function should return -1. The function should handle lists of any type and should be efficient for small to moderately sized lists. The code should start with: ```python from ty...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, List def linear_search(lst: List[Any], target: Any) -> int: """ Perform a linear search on a list and return the index of the target value. Args: lst (List[Any]): A list of elements to searc...
code_exercise_27k
14,893
Write a Python function to perform binary search on a sorted list and return the index of the target value. If the target value is not found, return -1. The function should also handle the case where the input list is not sorted by raising a `ValueError`. The code should start with: ```python from typing import List, ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def binary_search(arr: List[Any], target: Any) -> int: """ Performs binary search on a sorted list and returns the index of the target value. Args: arr (List[Any]): A sorted list of ele...
code_exercise_27k
14,896
Write a Python function to generate all possible combinations of a given list of numbers. Each combination should be a list of numbers, and the function should return a list of all such combinations. The function should start with: ```python from typing import List def generate_combinations(numbers: List[int]) -> List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from itertools import combinations def generate_combinations(numbers: List[int]) -> List[List[int]]: """ Generate all possible combinations of a given list of numbers. Args: numbers (List[in...
code_exercise_27k
14,898
Write a Python function to find the index of the first occurrence of a target value in a given list. If the target value is not present in the list, the function should return -1. The function should be efficient and handle edge cases such as an empty list or a list where the target value is not present. The code shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def find_first_occurrence(lst: List[Any], target: Any) -> int: """ Find the index of the first occurrence of a target value in a list. Args: lst (List[Any]): The input list to search. ...
code_exercise_27k
14,901
Write a Python function to find the index of the last occurrence of a target value in a given list. If the target value is not found in the list, the function should return -1. The function should be efficient and handle edge cases such as an empty list or a list that does not contain the target value. The code should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def find_last_occurrence(lst: List[Any], target: Any) -> int: """ Finds the index of the last occurrence of a target value in a list. Args: lst (List[Any]): The input list in which to s...
code_exercise_27k
14,906
Write a Python function to find the maximum value in a list of numeric values. The function should handle the case where the list is empty by raising a `ValueError`. The code should start with: ```python from typing import List, Union def find_maximum(lst: List[Union[int, float]]) -> Union[int, float]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_maximum(lst: List[Union[int, float]]) -> Union[int, float]: """ Finds the maximum value in a list of numeric values. Args: lst (List[Union[int, float]]): A list of numeric va...
code_exercise_27k
14,909
Write a Python function to find the maximum value in a list. The function should handle empty lists by returning `None` and should raise a `TypeError` if the input is not a list. The code should start with: ```python def find_maximum(lst): ```
Here is the Python code to solve the given problem in the markdown block: ```python def find_maximum(lst): """ Finds the maximum value in a list. Args: lst (list): The list of values to find the maximum from. Returns: float or int: The maximum value in the list. If the list is empty, N...
code_exercise_27k
14,911
Write a Python function to calculate the factorial of a given number using memoization. The function should store previously computed factorial values in a dictionary to avoid redundant calculations and improve performance. The code should start with: ```python def factorial_memoized(n: int, memo: dict = {}) -> int: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial_memoized(n: int, memo: dict = {}) -> int: """ Calculates the factorial of a given number using memoization. Args: n (int): The number to calculate the factorial for. memo (dict, optional): A di...
code_exercise_27k
14,914
Write a Python function to determine if a given list of strings are rotations of each other. A rotation of a string is defined as a new string that is formed by shifting characters from the beginning of the string to the end. For example, "abc" and "bca" are rotations of each other. The function should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def are_rotations(strings: List[str]) -> bool: """ Determine if a given list of strings are rotations of each other. Args: strings (List[str]): The list of strings to check. Returns: ...
code_exercise_27k
14,915
Write a Python function to perform interpolation search on a sorted list to find the index of a target value. The function should return the index of the target value if it exists in the list, otherwise it should return -1. The code should start with: ```python from typing import List def interpolation_search(arr: Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def interpolation_search(arr: List[int], target: int) -> int: """ Perform interpolation search on a sorted list to find the index of a target value. Parameters: arr (List[int]): Sorted list of i...
code_exercise_27k
14,919
Write a Python function to find the length of the longest substring without repeating characters in a given string. The function should efficiently handle the input string and return the length of the longest substring. The code should start with: ```python def longest_substring_length(s: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def longest_substring_length(s: str) -> int: """ Finds the length of the longest substring without repeating characters in the given string. Args: s (str): The input string. Returns: int: The length of ...
code_exercise_27k
14,921
Write a Python function to perform jump search on a sorted list to find the index of a target value. The function should return the index of the target value if it exists in the list, otherwise it should return -1. The code should start with: ```python from typing import List import math def jump_search(arr: List[int]...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def jump_search(arr: List[int], target: int) -> int: """ Perform jump search to find the index of the target value in a sorted list. Args: arr (List[int]): A sorted list of integ...
code_exercise_27k
14,927
Write a Python function to find the closest value to a target value in a given list of numeric values. The function should handle empty lists and lists containing non-numeric values by raising appropriate exceptions. The code should start with: ```python from typing import List, Union def find_closest_value(lst: List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_closest_value(lst: List[Union[float, int]], target: Union[float, int]) -> Union[float, int]: """ Finds the closest value to a target value in a list of numeric values. Args: ...
code_exercise_27k
14,930
Write a Python function to find the index of a target value in a nearly sorted list. A nearly sorted list is defined as a list where each element is at most one position away from its sorted position. The function should use a modified binary search to efficiently find the target value. The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def nearly_sorted_search(arr: List[int], target: int) -> int: """ Finds the index of a target value in a nearly sorted list using a modified binary search. Args: arr (List[int]): A nearly so...
code_exercise_27k
14,934
Write a Python function to find the kth smallest element in a given list of integers. The function should handle edge cases such as an empty list, invalid k values (less than 1 or greater than the length of the list), and should return the correct kth smallest element efficiently. The code should start with: ```python...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_kth_smallest(lst: List[int], k: int) -> int: """ Find the kth smallest element in a list of integers. Args: lst (List[int]): The list of integers from which to find the kth smallest...
code_exercise_27k
14,935
Write a Python function to convert a given decimal number to its corresponding hexadecimal representation. The function should handle both positive and negative decimal numbers, returning the hexadecimal representation prefixed with '0x' for positive numbers and '-0x' for negative numbers. The function should also hand...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_hexadecimal(decimal_num: int) -> str: """ Convert a decimal number to its corresponding hexadecimal representation. Parameters: decimal_num (int): The decimal number to convert. Returns: str: The...
code_exercise_27k
14,937
Write a Python function to perform a ternary search on a sorted list and return the index of the target value. If the target value is not found, return -1. The function should be efficient and leverage the properties of a sorted list to minimize the number of comparisons. The code should start with: ```python from typ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def ternary_search(arr: List[Any], target: Any) -> int: """ Searches for a target value in a sorted list using ternary search algorithm. Args: arr (List[Any]): A sorted list of elements...
code_exercise_27k
14,945
Write a Python function to search for a target value in a sorted matrix and return the index of the target value. The matrix is a list of lists where each row and each column is sorted in ascending order. If the target value is not found, the function should return `None`. The code should start with: ```python from ty...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Optional def search_sorted_matrix(matrix: List[List[int]], target: int) -> Optional[Tuple[int, int]]: """ Searches for a target value in a sorted matrix and returns its index. The matrix is ...
code_exercise_27k
14,966
Write a Python function to perform a breadth-first search (BFS) on an undirected graph starting from a given node. The graph is represented as an adjacency list, where the keys are nodes and the values are sets of neighboring nodes. The function should return the list of nodes visited during the BFS traversal in the or...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set, List from collections import deque def bfs(graph: Dict[int, Set[int]], start_node: int) -> List[int]: """ Perform a breadth-first search (BFS) on an undirected graph. Args: graph (Dict...
code_exercise_27k
14,970
Write a Python function to find the shortest path between two nodes in an undirected graph using Breadth-First Search (BFS). The graph is represented as a dictionary where keys are nodes and values are lists of adjacent nodes. The function should return the shortest path as a list of nodes from the start node to the en...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque from typing import Dict, List, Union def shortest_path_bfs(graph: Dict[str, List[str]], start_node: str, end_node: str) -> List[str]: """ Find the shortest path between two nodes in an undirected g...
code_exercise_27k
14,974
Write a Python function to count the number of connected components in an undirected graph using depth-first search (DFS). The graph is represented as an adjacency list. The function should start with: ```python from typing import Dict, List def count_connected_components(graph: Dict[int, List[int]]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def count_connected_components(graph: Dict[int, List[int]]) -> int: """ Count the number of connected components in an undirected graph using depth-first search. Args: graph (Dict[int,...
code_exercise_27k
14,998
Write a Python function to calculate the average of a list of numbers and handle the `ZeroDivisionError` if the list is empty. If the list is empty, the function should return `None` instead of raising an error. The code should start with: ```python from typing import List, Optional def calculate_average(numbers: Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def calculate_average(numbers: List[float]) -> Optional[float]: """ Calculate the average of a list of numbers. If the list is empty, return None. Args: numbers (List[float]): A li...
code_exercise_27k
15,007
Write a Python function to perform an iterative depth-first search (DFS) on a given graph starting from a specified node. The graph is represented as an adjacency list using a dictionary where each key is a node and its value is a list of adjacent nodes. The function should return a list of nodes in the order they were...
Here is the Python code to solve the given problem in the markdown block: ```python from collections import deque from typing import Dict, List, Any def iterative_dfs(graph: Dict[Any, List[Any]], start_node: Any) -> List[Any]: """ Perform an iterative depth-first search (DFS) on a graph starting from a given n...
code_exercise_27k
15,027
Write a Python function to compute the shortest paths between all pairs of nodes in a weighted directed graph using the Floyd-Warshall algorithm. The graph is represented as an adjacency matrix, where `graph[i][j]` represents the weight of the edge from node `i` to node `j`. If there is no edge between nodes `i` and `j...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def floyd_warshall(graph: List[List[float]]) -> List[List[float]]: """ Computes the shortest paths between all pairs of nodes using the Floyd-Warshall algorithm. Args: graph: A weighted dire...