source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
12,839
Write a Python function to compute the sum of the squares of the numbers in a given list. The function should return the sum of the squares of all the numbers in the list. The code should start with: ```python from typing import List def sum_of_squares(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_squares(numbers: List[int]) -> int: """ Computes the sum of the squares of the numbers in the given list. Args: numbers (List[int]): The list of integers for which the sum of squa...
code_exercise_27k
12,856
Write a Python function to calculate the median value of a given list of numbers. The function should handle both even and odd lengths of the list and return the median as a float. If the list is empty, the function should raise a `ValueError`. The code should start with: ```python from typing import List def find_me...
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 value of a list of numbers. Args: numbers (List[int]): A list of integers for which the median is to be calculated. ...
code_exercise_27k
12,862
Write a Python function to determine if a given string is a palindrome. The function should ignore non-alphanumeric characters and be case-insensitive. The function should start with: ```python def is_palindrome(word: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(word: str) -> bool: """ Check if a given string is a palindrome, ignoring non-alphanumeric characters and case. Parameters: word (str): The string to check. Returns: bool: True if the string i...
code_exercise_27k
12,864
Write a Python function to sort a list of numbers in descending order using the merge sort algorithm. The function should return a new list with the numbers sorted in descending order. The merge sort algorithm should be implemented from scratch without using Python's built-in sorting functions. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort_descending(numbers: List[int]) -> List[int]: """ Sorts the given list of numbers in descending order using the merge sort algorithm. Args: numbers (List[int]): A list of numbe...
code_exercise_27k
12,869
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 any string input, including empty strings. The code should start with: ```python def are_all_characters_unique(string: str...
Here is the Python code to solve the given problem in the markdown block: ```python def are_all_characters_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: T...
code_exercise_27k
12,874
Write a Python function to find the longest substring without repeating characters in a given string. The function should return the longest substring that contains no repeated characters. The code should start with: ```python def longest_substring_without_repeating_chars(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def longest_substring_without_repeating_chars(s: str) -> str: """ Finds the longest substring without repeating characters in a given string. Args: s (str): The input string. Returns: str: The longest s...
code_exercise_27k
12,877
Create a Python function to calculate the number of unique Binary Search Trees (BSTs) that can be formed with `n` nodes using a memoization technique. The function should be decorated with a memoization decorator to cache the results of previously computed values, thereby optimizing the performance. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Callable def memoize(func: Callable) -> Callable: """A memoization function decorator that caches the results of a function.""" cache = {} def wrapper(*args): if args not in cache: ca...
code_exercise_27k
12,882
Write a Python function to calculate the number of unique Binary Search Trees (BSTs) that can be formed with a given number of nodes `n` using memoization to optimize performance. The function should start with: ```python def num_unique_bsts_memoized(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def num_unique_bsts_memoized(n: int) -> int: """ Calculates the number of unique Binary Search Trees (BSTs) that can be formed with 'n' nodes using memoization. Args: n (int): The number of nodes. Returns: ...
code_exercise_27k
12,886
Write a Python function to find the length of the longest common subarray between two given arrays using dynamic programming. The function should take two lists as input and return the length of the longest common subarray. The code should start with: ```python from typing import List def longest_common_subarray_lengt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_common_subarray_length(arr1: List[int], arr2: List[int]) -> int: """ Finds the length of the longest common subarray between two arrays using dynamic programming. Args: arr1 (Lis...
code_exercise_27k
12,891
Write a Python function to find the common elements between two lists of numbers and return them in a new list. The function should handle lists of any length, including empty lists, and should not modify the original lists. The code should start with: ```python from typing import List def find_common_elements(list1: ...
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]: """ Find the common elements between two lists of numbers. Args: list1 (List[int]): The first list of numbers. ...
code_exercise_27k
12,898
Write a Python function to calculate the length of the longest common subarray between two given arrays using memoization. The function should avoid redundant calculations by storing intermediate results in a memoization table. The code should start with: ```python from typing import List def longest_common_subarray_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_common_subarray_length(arr1: List[int], arr2: List[int]) -> int: """ Calculate the length of the longest common subarray between arr1 and arr2 using memoization. Args: arr1 (List...
code_exercise_27k
12,901
Write a Python function to find the minimum number of jumps required to reach the end of an array using dynamic programming. Each element in the array represents the maximum number of steps that can be jumped from that position. The function should start with: ```python from typing import List def min_jumps_to_end(arr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def min_jumps_to_end(arr: List[int]) -> int: """ Calculate the minimum number of jumps required to reach the end of an array using dynamic programming. Args: arr (List[int]): An array of non...
code_exercise_27k
12,907
Write a Python function to calculate the sum of all even numbers in a given list of integers. The function should handle an empty list by returning 0. The code should start with: ```python from typing import List def sum_of_even_numbers(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_even_numbers(numbers: List[int]) -> int: """ Calculates the sum of all even numbers in a given list of integers. Args: numbers (List[int]): A list of integers. Returns: ...
code_exercise_27k
12,909
Write a Python function to count the number of strings in a given list that have more than three characters. The function should be efficient and handle various edge cases, such as an empty list or a list containing strings of varying lengths. The code should start with: ```python from typing import List def count_st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_strings_with_more_than_three_chars(strings: List[str]) -> int: """ Count the number of strings in a list that have more than three characters. Args: strings (List[str]): A list of ...
code_exercise_27k
12,915
Write a Python function to determine if a given string is a palindrome. The function should ignore non-alphanumeric characters and be case-insensitive. The code should start with: ```python def is_palindrome(input_string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(input_string: str) -> bool: """ Check if a given string is a palindrome, ignoring non-alphanumeric characters and case. Args: input_string (str): The string to check. Returns: bool: Tr...
code_exercise_27k
12,919
Write a Python function to filter out prime numbers from a given list of integers. The function should return a new list containing only the prime numbers from the input list. The code should start with: ```python from typing import List def filter_primes(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_primes(numbers: List[int]) -> List[int]: """ Returns a new list containing only the prime numbers from the input list of integers. Args: numbers (List[int]): A list of integers fr...
code_exercise_27k
12,923
Write a Python function to solve the rod cutting problem using memoization. The rod cutting problem involves determining the maximum revenue that can be obtained by cutting a rod of a given length into pieces, where each piece has a different price based on its length. The function should use a memoization technique to...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def rod_cutting_memoized(n: int, price_list: List[int], memo: Dict[int, int] = None) -> int: """ Solves the rod cutting problem using memoization to find the maximum revenue obtainable. Args: ...
code_exercise_27k
12,927
Write a Python function to calculate the product of all the numbers in a given list of integers. If the input list is empty, raise a `ValueError` with the message "Input list cannot be empty." The function should start with: ```python from typing import List def product_of_numbers(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def product_of_numbers(numbers: List[int]) -> int: """ Calculates the product of all the numbers in the given list. Args: numbers (List[int]): A list of integers. Returns: int: ...
code_exercise_27k
12,936
Write a Python function to calculate the number of ways to partition a set of size `n` into `k` subsets using memoization. The function should be efficient and avoid redundant calculations by storing previously computed results in a memoization table. The code should start with: ```python def partition_ways_memoized(n...
Here is the Python code to solve the given problem in the markdown block: ```python def partition_ways_memoized(n: int, k: int) -> int: """ Calculates the number of ways to partition a set of size n into k subsets using memoization. Args: n (int): The size of the set. k (int): The number of...
code_exercise_27k
12,944
Write a Python function to return a new list containing only the unique elements from a given list of integers. The function should maintain the order of elements as they appear in the input list. The code should start with: ```python from typing import List def get_unique_elements(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 get_unique_elements(input_list: List[int]) -> List[int]: """ Returns a new list with only the unique elements from the input list, maintaining the order of their first appearance. Args: ...
code_exercise_27k
12,947
Write a Python function to reverse the order of words in a given string. The function should handle multiple spaces between words and leading/trailing spaces. The code should start with: ```python def reverse_words_in_string(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_in_string(input_string: str) -> str: """ Reverses the order of words in a given string. This function takes a string as input and returns a new string with the words in the original string reversed. It...
code_exercise_27k
12,949
Write a Python function to reverse a list of strings while preserving the order of characters within each string. The function should handle lists of varying lengths, including empty lists, and should not modify the original list. The code should start with: ```python from typing import List def reverse_list_of_string...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list_of_strings(strings: List[str]) -> List[str]: """ Reverse a list of strings while preserving the order of characters within each string. Args: strings (List[str]): A list of ...
code_exercise_27k
12,950
Write a Python function to calculate the length of the longest palindromic subsequence in a given string using memoization. The function should avoid redundant calculations by storing intermediate results in a dictionary. The code should start with: ```python def longest_palindromic_subsequence_length(s: str) -> int: ...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_palindromic_subsequence_length(s: str) -> int: """ Calculate the length of the longest palindromic subsequence in a string using memoization. Args: s (str): The input string. Returns: int: T...
code_exercise_27k
12,959
Write a Python function to determine if a given sentence is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once. The function should return `True` if the sentence is a pangram and `False` otherwise. The function should be case-insensitive and should ignore non-alphabetic characte...
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(sentence: str) -> bool: """ Determines whether a given sentence is a pangram. Args: sentence (str): The sentence to be checked. Returns: bool: True if the sentence is a pangram, False oth...
code_exercise_27k
12,961
Create a Python function to calculate the number of ways to tile a floor of given dimensions using a 1x1 tile. The function should use a memoization decorator to optimize the calculation by caching the results of previously computed floor dimensions. The code should start with: ```python from functools import wraps fro...
Here is the Python code to solve the given problem in the markdown block: ```python from functools import wraps from typing import Callable def memoize(func: Callable) -> Callable: """ This is a memoization decorator that caches the results of a function. The decorator stores the result of each function c...
code_exercise_27k
12,966
Write a Python function to calculate the number of ways to tile a floor of size n x 2 using 2 x 1 tiles. The function should use memoization to optimize the computation by storing previously computed results. The code should start with: ```python def count_ways_to_tile_floor(n: int, cache: dict = None) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_ways_to_tile_floor(n: int, cache: dict = None) -> int: """ Calculate the number of ways to tile a floor of size n x 2 using 2 x 1 tiles. Args: n (int): The width of the floor. cache (dict, optional...
code_exercise_27k
12,972
Write a Python function to find the minimum number of coins required to make a given sum using memoization. The function should take a list of coin denominations and a target sum as input and return the minimum number of coins needed to achieve the target sum. If it is not possible to make the target sum with the given...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def min_coins_to_sum(coins: List[int], amount: int) -> int: """ Calculate the minimum number of coins required to make the given sum using memoization. Args: coins (List[int]): List of coin ...
code_exercise_27k
12,976
Write a Python function to determine if there is at least one student with a GPA above a specified threshold in a given list of tuples representing students' names and GPAs. The function should return `True` if such a student exists, and `False` otherwise. 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, Tuple def has_student_above_threshold(students: List[Tuple[str, float]], threshold: float) -> bool: """ Check if there is at least one student with a GPA above a specified threshold. Args: ...
code_exercise_27k
12,978
Write a Python function to calculate the minimum number of coins required to make a given sum using a memoized approach. The function should take a tuple of coin denominations and a target sum as inputs and return the minimum number of coins needed to achieve the target sum. The function should use memoization to optim...
Here is the Python code to solve the given problem in the markdown block: ```python from functools import lru_cache def min_coins_memoized(coins: tuple, total: int) -> int: """ Calculates the minimum number of coins required to make a given sum using a memoized approach. Args: coins (tuple): A tup...
code_exercise_27k
12,980
Write a Python function to concatenate all strings in a given list into a single string, ensuring that the input is a list of strings and handling edge cases such as empty lists and lists with non-string elements. The function should raise a `TypeError` if the input is not a list or if any element in the list is not a ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def concatenate_strings(strings: List[str]) -> str: """ Concatenate all strings in a given list into a single string. Args: strings (List[str]): A list of strings to be concatenated. Re...
code_exercise_27k
12,982
Write a Python function to find the maximum length of the increasing subsequence in a given array using dynamic programming. The function should efficiently compute the result by storing intermediate results in a table to avoid redundant calculations. The code should start with: ```python from typing import List def ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_length_increasing_subsequence(nums: List[int]) -> int: """ Find the maximum length of the increasing subsequence in a given array using dynamic programming. Args: nums (List[int]): T...
code_exercise_27k
12,985
Write a Python function to determine if one string is a rotation of another string. A string `str2` is considered a rotation of `str1` if it can be obtained by rotating `str1` by any number of positions. For example, "cdeab" is a rotation of "abcde" by rotating it 3 positions to the right. The function should start wi...
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 str2 is a rotation of str1. Parameters: str1 (str): The first string. str2 (str): The second string to check if it is a rotation of str1. Returns...
code_exercise_27k
12,989
Write a Python function to sort a list of integers in descending order, but with all even numbers appearing before odd numbers. The function should return a new list without modifying the original list. The code should start with: ```python from typing import List def sort_even_odd_descending(numbers: List[int]) -> Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_even_odd_descending(numbers: List[int]) -> List[int]: """ Sorts a list of integers in descending order with even numbers before odd numbers. Args: numbers (List[int]): A list of int...
code_exercise_27k
12,994
Write a Python function to calculate the maximum length of an increasing subsequence in a given list of numbers using memoization. The function should efficiently store and reuse previously computed results to avoid redundant calculations. The code should start with: ```python from typing import List def max_increasi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_increasing_subsequence(nums: List[int]) -> int: """ Calculate the maximum length of an increasing subsequence in a given list of numbers using memoization. Args: nums (List[int]): A ...
code_exercise_27k
13,002
Write a Python function to find the names of the oldest individuals from a given list of tuples, where each tuple contains a name (string) and an age (integer). If there are multiple individuals with the same maximum age, return the names of all such individuals. The function should raise a `ValueError` if the input li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def get_oldest_names(individuals: List[Tuple[str, int]]) -> List[str]: """ Returns the names of the oldest individuals from a list of tuples (name, age). Args: individuals (List[Tuple...
code_exercise_27k
13,011
Write a Python function to validate if a given string is a valid email address according to a specified regular expression pattern. The function should return `True` if the email is valid and `False` otherwise. The code should start with: ```python import re def is_valid_email(email: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_email(email: str) -> bool: """ Validate if the given string is a valid email address. Args: email (str): The string to be checked for email validity. Returns: bool: True if the e...
code_exercise_27k
13,013
Write a Python function to calculate the number of ways to reach the top of a staircase with a given number of steps using memoization. The function should be able to handle up to 100 steps efficiently. The function should start with: ```python def count_ways_memoized(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_ways_memoized(n: int) -> int: """ Calculate the number of ways to reach the top of a staircase with 'n' steps using memoization. This function uses a helper function with a memoization dictionary to store intermed...
code_exercise_27k
13,015
Write a Python function to determine if all the strings in a given list are unique. The function should return `True` if all strings are unique and `False` otherwise. The code should start with: ```python from typing import List def are_all_strings_unique(lst: List[str]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def are_all_strings_unique(lst: List[str]) -> bool: """ Determines if all strings in the given list are unique. Args: lst (List[str]): A list of strings to be checked for uniqueness. Re...
code_exercise_27k
13,021
Write a Python function to reverse a given string without using any built-in functions or methods. The function should handle strings of any length, including empty strings. The code should start with: ```python def reverse_string(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(input_string: str) -> str: """ Reverses a given string without using any built-in functions or methods. Args: input_string (str): The string to be reversed. Returns: str: The reversed...
code_exercise_27k
13,024
Write a Python function to determine if a given list of integers is a sequence of consecutive numbers. The function should return `True` if the list is a sequence of consecutive numbers, and `False` otherwise. The function should handle lists of any length, including empty lists and lists with a single element. The co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_consecutive_sequence(numbers: List[int]) -> bool: """ Determines if a given list of integers is a sequence of consecutive numbers. Args: numbers (List[int]): A list of integers. ...
code_exercise_27k
13,027
Write a Python function to determine if there are any duplicate numbers in a given list of integers. The function should return `True` if there is at least one duplicate number, and `False` otherwise. The code should start with: ```python from typing import List def has_duplicate(numbers: List[int]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_duplicate(numbers: List[int]) -> bool: """ Checks if there are any duplicate numbers in a list of integers. Args: numbers (List[int]): A list of integers. Returns: bool:...
code_exercise_27k
13,033
Write a Python function to validate whether a given string is a valid ISBN-10 number. An ISBN-10 (International Standard Book Number) consists of 10 characters: the first 9 characters are digits, and the last character can be a digit or 'X' (which represents the number 10). The ISBN-10 is valid if the sum of the digits...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_isbn_10(isbn: str) -> bool: """ Check if a given string is a valid ISBN-10 number. Parameters: isbn (str): The string to be validated as ISBN-10. Returns: bool: True if the string is a valid ISBN-1...
code_exercise_27k
13,034
Write a Python function that accepts a decorator and applies it to a given function. The function should return the decorated version of the given function. The decorator should perform some actions before and after calling the original function. The code should start with: ```python def apply_decorator(decorator): ``...
Here is the Python code to solve the given problem in the markdown block: ```python def apply_decorator(decorator): """ Apply a given decorator to a function. Args: decorator: The decorator function to be applied. Returns: The decorated version of the function. Example Usage: ...
code_exercise_27k
13,040
Write a Python function to determine if all the numbers in a given list of integers are positive and adjacent to each other. The function should return `True` if all numbers in the list are positive and each number is exactly one more than the previous number, and `False` otherwise. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def are_all_positive_and_adjacent(nums: List[int]) -> bool: """ Determine if all integers in a list are positive and adjacent to each other. Args: nums (List[int]): A list of integers. ...
code_exercise_27k
13,045
Create a Python decorator to cache the results of a function for faster repeated execution. The decorator should store the cached results in a dictionary, where the key is a combination of the function name, its positional arguments, and its keyword arguments. The decorator should be designed to work with any function,...
Here is the Python code to solve the given problem in the markdown block: ```python from functools import wraps def cache_decorator(func): """ A decorator that caches the results of a function for faster repeated execution. The cache is stored as an attribute of the decorated function itself. Args: ...
code_exercise_27k
13,047
Write a Python function to check if all strings in a given list contain at least one uppercase letter and one lowercase letter. The function should return `True` if all strings meet this criterion, and `False` otherwise. The code should start with: ```python from typing import List def check_strings_case(lst: List[str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def check_strings_case(lst: List[str]) -> bool: """ Check if all strings in a list contain at least one uppercase letter and one lowercase letter. Args: lst (List[str]): A list of strings to...
code_exercise_27k
13,048
Create a Python decorator that logs the execution time of a function and counts the number of times it has been called. The decorator should print the execution time in seconds and the call count each time the function is executed. The code should start with: ```python from functools import wraps import time def log_e...
Here is the Python code to solve the given problem in the markdown block: ```python from functools import wraps import time def log_execution_and_count(func): """ Decorator that logs the execution time of a function and counts the number of times it has been called. Args: func: The function being ...
code_exercise_27k
13,060
Create a Python decorator to memoize the results of a function using a dictionary. The decorator should cache the results of function calls and return the cached result for subsequent calls with the same arguments, thus optimizing the function's performance. The code should start with: ```python def memoize(func): ```
Here is the Python code to solve the given problem in the markdown block: ```python def memoize(func): """ A decorator function that memoizes the results of a function using a dictionary. Args: func (callable): The function to be memoized. Returns: callable: The memoized version of the...
code_exercise_27k
13,064
Write a Python function to determine if all strings in a given list are different 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, the rotations of the string "abc" are "abc", "bca", and "cab". The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def are_all_rotations(strings: List[str]) -> bool: """ Determines if all strings in the given list are different rotations of each other. Args: strings (List[str]): A list of strings to chec...
code_exercise_27k
13,069
Write a Python function to determine if a given list of integers contains three consecutive odd numbers. The function should return `True` if such a sequence exists, and `False` otherwise. The code should start with: ```python from typing import List def has_three_consecutive_odds(numbers: List[int]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_three_consecutive_odds(numbers: List[int]) -> bool: """ Determines if a list of integers contains three consecutive odd numbers. Args: numbers (List[int]): A list of integers to chec...
code_exercise_27k
13,073
Write a Python function to validate a phone number based on a specific format. The phone number should be in the format "XXX-XXX-XXXX", where 'X' is a digit. The function should return `True` if the phone number is valid and `False` otherwise. The phone number must meet the following criteria: 1. It must be exactly 12 ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def validate_phone_number(phone_number: str) -> bool: """ Validate a phone number in the format "XXX-XXX-XXXX". Args: phone_number (str): The phone number to be validated. Returns: bool: True...
code_exercise_27k
13,078
Write a Python function to count the number of integers in a given list that are multiples of both 3 and 4, but not multiples of 5. The function should handle input validation to ensure the input is a list of integers. The code should start with: ```python from typing import List def count_multiples(numbers: List[int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_multiples(numbers: List[int]) -> int: """ Counts the number of integers in the given list that are multiples of both 3 and 4, but not multiples of 5. Args: numbers (List[int]): The...
code_exercise_27k
13,082
Write a Python function to count the number of words in a given string that are entirely in uppercase. The function should handle various punctuation marks and multiple spaces between words. The code should start with: ```python def count_uppercase_words(string: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_uppercase_words(string: str) -> int: """ Count the number of words in a string that are entirely in uppercase. Args: string (str): The input string containing words. Returns: int: The count of...
code_exercise_27k
13,095
Write a Python function to validate if a given string is a valid credit card number using Luhn's algorithm. The function should return `True` if the credit card number is valid and `False` otherwise. The function should handle various formats of credit card numbers, including those with spaces or hyphens. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_credit_card(card_number: str) -> bool: """ Validate if a given string is a valid credit card number using Luhn's algorithm. Args: card_number (str): The string representing the credit card number. ...
code_exercise_27k
13,103
Write a Python function to determine if there are at least two strings in a given list that contain the same set of characters, regardless of their order. The function should return `True` if such duplicate strings exist, and `False` otherwise. The code should start with: ```python from typing import List def has_dupl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_duplicate_strings(lst: List[str]) -> bool: """ Determines if there are at least two strings in the list that contain the same set of characters. Args: lst (List[str]): A list of stri...
code_exercise_27k
13,114
Write a Python function to validate 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 have exactly eight groups. The function should return `True` if th...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv6(ipv6_address: str) -> bool: """ Validates if the 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 ...
code_exercise_27k
13,125
Write a Python function to determine if all the numbers in a given list of integers are perfect squares. A perfect square is a number that can be expressed as the square of an integer. The function should return `True` if all numbers in the list are perfect squares, and `False` otherwise. The code should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def are_all_perfect_squares(numbers: List[int]) -> bool: """ Determine if all numbers in the given list are perfect squares. Args: numbers (List[int]): A list of integers to chec...
code_exercise_27k
13,144
Write a Python function to find the names of individuals whose scores are higher than the average score from a given list of tuples, where each tuple contains a name and a score. The function should return a list of these names. The code should start with: ```python from typing import List, Tuple def get_higher_than_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def get_higher_than_average_names(scores: List[Tuple[str, float]]) -> List[str]: """ Returns the names of individuals who have scores higher than the average score. Args: scores (List...
code_exercise_27k
13,147
Write a Python function to determine if there are at least two strings in a given list that contain the same characters, regardless of their order. The function should return `True` if such pairs exist, and `False` otherwise. The code should start with: ```python from typing import List def has_duplicate_characters(st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_duplicate_characters(strings: List[str]) -> bool: """ Determines if there are at least two strings in the given list that contain the same characters, regardless of their order. Args: ...
code_exercise_27k
13,185
Write a Python function to determine if there is a string in a given list that can be obtained by rearranging the characters of another string in the list. The function should return `True` if such a string exists, and `False` otherwise. The code should start with: ```python from typing import List def has_rearranged_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from collections import Counter def has_rearranged_string(strings: List[str]) -> bool: """ Determines if there is a string in the given list that can be obtained by rearranging the characters of another ...
code_exercise_27k
13,205
Write a Python function to determine if a list of strings contains any pair of strings that are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. The function should start with: ```python from t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_anagram(strings: List[str]) -> bool: """ Determines if a list of strings contains any pair of strings that are anagrams of each other. Args: strings (List[str]): A list of strings to...
code_exercise_27k
13,210
Create a Python context manager class that raises a specific exception when a condition is not met inside the code block. The context manager should accept a condition and an exception type as arguments. If the condition is not satisfied when entering the context, the specified exception should be raised. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python class ConditionCheckContext: """ A context manager that raises a specified exception if a given condition is not met when entering the context. Args: condition (callable): A callable that returns a boolean indicatin...
code_exercise_27k
13,232
Write a Python function to determine if there is a string in a given list that can be formed by concatenating the characters of another string multiple times, in any order. The function should return `True` if such a string exists, and `False` otherwise. The code should start with: ```python from typing import List d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_repeated_string(lst: List[str]) -> bool: """ Determines if there is a string in the list that can be formed by concatenating the characters of another string multiple times, in any order. ...
code_exercise_27k
13,240
Write a Python function to determine if there is a string in a given list that can be obtained by rearranging the characters of another string multiple times, in any order. The function should return `True` if such a string exists, and `False` otherwise. The code should start with: ```python from typing import List de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_rearranged_string(strings: List[str]) -> bool: """ Determine if there is a string in the list that can be obtained by rearranging the characters of another string multiple times, in any o...
code_exercise_27k
13,243
Write a Python function to determine if there exists a number in a given list of integers that can be obtained by adding any two distinct numbers from the list, without using the addition operator. The function should return `True` if such a number exists, and `False` otherwise. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def can_add_without_operator(numbers: List[int]) -> bool: """ Determines if there exists a number in the given list that can be obtained by adding any two distinct numbers from the list, without usin...
code_exercise_27k
13,254
Write a Python function to validate if a given string is a valid HTML document with a specific structure. The function should return `True` if the string meets the following criteria: 1. Starts with the opening HTML tag: `<html>`. 2. Contains a head section with the opening and closing tags: `<head>` and `</head>`. 3. ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_html_document(html_string: str) -> bool: """ Check if a given string is a valid HTML document with a specific structure. The function takes a string as input and returns True if it is a valid HTML document ...
code_exercise_27k
13,255
Write a Python function that calculates the sum of two numbers, where the second number is a default argument set to 10. The function should handle both integer and floating-point inputs. The code should start with: ```python def calculate_sum(a, b=10): ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_sum(a, b=10): """ Calculates the sum of two numbers, with the second number having a default value of 10. Parameters: a (int or float): The first number. b (int or float, optional): The second number. ...
code_exercise_27k
13,257
Write a Python function that takes a string and an optional integer argument, and returns the given string repeated by the specified number of times. If the integer argument is not provided, the string should be repeated twice by default. The function should handle cases where the integer argument is less than or equal...
Here is the Python code to solve the given problem in the markdown block: ```python def repeat_string(string: str, times: int = 2) -> str: """ Repeats the given string by the specified number of times. Args: string (str): The string to be repeated. times (int, optional): The number of times...
code_exercise_27k
13,262
Write a Python function to calculate the power of a number with a default argument of 2. The function should handle both integer and floating-point numbers as the base and an integer as the exponent. The function should also include type checking to ensure that the inputs are of the correct types. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_power(base: int | float, exponent: int = 2) -> int | float: """ Calculates the power of a number with a default exponent of 2. Parameters: base (int | float): The base number to be raised to the power. ...
code_exercise_27k
13,266
Write a Python function that checks if a given number is within a specified range, with default lower and upper bounds of 0 and 100, respectively. The function should return `True` if the number is within the range (inclusive), and `False` otherwise. The code should start with: ```python def is_within_range(num: int, ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_within_range(num: int, lower_bound: int = 0, upper_bound: int = 100) -> bool: """ Checks if a given number is within a specified range. Parameters: - num (int): The number to check. - lower_bound (int): The l...
code_exercise_27k
13,269
Write a Python function that concatenates two strings, with one of the strings being a default argument. The function should handle cases where the default string is used, as well as cases where a custom string is provided. The function should also ensure that the input strings are properly concatenated and returned as...
Here is the Python code to solve the given problem in the markdown block: ```python def concatenate_strings(string1: str, string2: str = "default") -> str: """ Concatenates two strings, with the second string having a default value of "default". Args: string1 (str): The first string to be concatena...
code_exercise_27k
13,273
Write a Python function to calculate the sum of a list of numbers, ensuring that the default argument is a new empty list each time the function is called. The function should handle cases where the input list is empty or contains non-numeric values. The code should start with: ```python from typing import List, Union ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def calculate_sum(numbers: List[Union[int, float]] = None) -> float: """ Calculate the sum of a list of numbers. This function takes a list of numbers as input and returns their sum. If no ar...
code_exercise_27k
13,275
Write a Python function to check if a given string is a palindrome, considering only alphanumeric characters and ignoring case. The function should have a default argument of an empty string. The code should start with: ```python def is_palindrome(string: str = '') -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(string: str = '') -> bool: """ Checks if a given string is a palindrome, considering only alphanumeric characters and ignoring case. Args: string (str, optional): The string to be checked. Defaults...
code_exercise_27k
13,279
Write a Python function to find the maximum of three numbers, with the third number having a default argument of 0. The function should return the maximum value among the three numbers. The code should start with: ```python def max_of_three(num1: int, num2: int, num3: int = 0) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def max_of_three(num1: int, num2: int, num3: int = 0) -> int: """ Returns the maximum of three numbers. Parameters: num1 (int or float): The first number to compare. num2 (int or float): The second number to compare...
code_exercise_27k
13,282
Write a Python function to determine if a given number is prime, starting the divisibility check from a specified divisor. The function should handle edge cases and validate the input arguments. The code should start with: ```python def is_prime(number: int, divisor: int = 2) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(number: int, divisor: int = 2) -> bool: """ Determines if a number is prime, starting the divisibility check from a specified divisor. Args: number (int): The number to be checked for primality. ...
code_exercise_27k
13,284
Write a Python function to calculate the area of a rectangle with default arguments for length and width. The function should handle negative values by raising a `ValueError`. The code should start with: ```python def calculate_rectangle_area(length: float = 0, width: float = 0) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_rectangle_area(length: float = 0, width: float = 0) -> float: """ Calculate the area of a rectangle given its length and width. Args: length (float, optional): The length of the rectangle. Defaults to ...
code_exercise_27k
13,288
Write a Python function to calculate the average of a list of numbers, ensuring that the function handles various edge cases such as empty lists, non-numeric elements, and non-iterable inputs. The function should return 0.0 for an empty list and raise a `TypeError` for non-numeric elements or non-iterable inputs. The ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union, List, Tuple def calculate_average(numbers: Union[List[Union[int, float]], Tuple[Union[int, float]]] = []) -> float: """ Calculates the average of a list of numbers. Args: numbers (Union[Li...
code_exercise_27k
13,289
Write a Python function that retrieves the value associated with a given key from a dictionary, with a default value of `None`. If the key is not found in the dictionary, the function should return the default value. The function should handle cases where the dictionary or key might be `None`. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python def get_value_with_default(dictionary, key, default=None): """ Retrieve the value associated with a given key from the given dictionary, with a default value of None. Args: dictionary (dict): The dictionary from whi...
code_exercise_27k
13,294
Write a Python function to filter out even numbers from a given list of integers. The function should handle an optional default argument of an empty list. The function should return a new list containing only the even numbers from the input list. The code should start with: ```python from typing import List def get_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_even_numbers(numbers: List[int] = None) -> List[int]: """ Filters out even numbers from a given list of integers. Args: numbers (List[int], optional): A list of integers. Defaults to...
code_exercise_27k
13,297
Write a Python function to determine the length of a string, with a default argument of an empty string. The function should handle both provided and default arguments correctly and return the length of the string. The code should start with: ```python def string_length(string: str = '') -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def string_length(string: str = '') -> int: """ Returns the length of the given string. If no string is provided, it defaults to an empty string. Args: string (str, optional): The string whose length needs to be det...
code_exercise_27k
13,301
Write a Python function to calculate the area of a circle, with a default argument of 1 for the radius. The function should handle the case where the radius is negative by raising a `ValueError`. The code should start with: ```python import math def calculate_area_of_circle(radius: float = 1) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import math def calculate_area_of_circle(radius: float = 1) -> float: """ Calculate the area of a circle given its radius. Args: radius (float, optional): The radius of the circle. Defaults to 1. Returns: ...
code_exercise_27k
13,302
Write a Python function to reverse a given string, with a default argument of an empty string. The function should handle both non-empty and empty strings efficiently. The code should start with: ```python def reverse_string(text: str = '') -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(text: str = '') -> str: """ Reverses the given string. Args: text (str, optional): The string to be reversed. Defaults to an empty string. Returns: str: The reversed string. Exam...
code_exercise_27k
13,311
Write a Python function to check if a number is divisible by another number, with the default argument of 1. The function should handle input validation and raise appropriate errors for invalid inputs. The code should start with: ```python def is_divisible(number: int, divisor: int = 1) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_divisible(number: int, divisor: int = 1) -> bool: """ Checks if a given number is divisible by another number, with a default divisor of 1. Args: number (int): The number to be checked for divisibility. ...
code_exercise_27k
13,312
Write a Python function to calculate the area of a rectangle, with default arguments for length and width. The function should handle cases where only one of the dimensions is provided, assuming the other dimension to be 0. Additionally, if no arguments are provided, the function should return 0, assuming both dimensio...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_rectangle_area(length=0, width=0) -> float: """ Calculate the area of a rectangle. Args: length (float, optional): The length of the rectangle. Defaults to 0. width (float, optional): The width...
code_exercise_27k
13,315
Write a Python function to remove duplicate elements from a given list, ensuring that the function handles the default argument correctly and does not modify the original list. The function should return a new list with unique elements in the order they first appear. The code should start with: ```python from typing i...
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] = None) -> List[int]: """ Removes duplicate elements from a list while preserving the order of their first appearance. Args: lst (List[int], optional): T...
code_exercise_27k
13,318
Write a Python function to find the minimum value in a list of numbers, with a default argument of an empty list. If the list is empty, the function should return `None`. The function should handle lists containing integers, floats, and a mix of both. The code should start with: ```python from typing import List, Union...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_minimum(numbers: List[Union[int, float]] = []) -> Union[int, float, None]: """ Finds the minimum value in a list of numbers. Args: numbers (List[Union[int, float]], optional)...
code_exercise_27k
13,322
Write a Python function that adds a given element to the end of a list, with a default argument of an empty list. The function should handle cases where the list is not provided or is explicitly set to `None`. Additionally, the function should raise a `ValueError` if the provided `lst` is not a list. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, List, Optional def add_element_to_list(element: Any, lst: Optional[List[Any]] = None) -> List[Any]: """ Adds a given element to the end of a list. Args: element (Any): The element to be adde...
code_exercise_27k
13,323
Write a Python function to calculate the sum of all even numbers in a given list of integers. The function should handle an empty list by returning 0. The code should start with: ```python from typing import List def sum_even_numbers(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_even_numbers(numbers: List[int]) -> int: """ Calculates the sum of all even numbers in a given list of integers. Args: numbers (List[int]): A list of integers. Returns: ...
code_exercise_27k
13,324
Write a Python function to check if a list is sorted in ascending order. The function should handle an empty list as a default argument and return `True` if the list is empty. The function should also handle lists with duplicate elements and return `True` if the list is sorted with duplicates. 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 is_ascending(lst: List[int] = []) -> bool: """ Checks if a list is sorted in ascending order. Args: lst (List[int], optional): The list to be checked. Defaults to an empty list. Ret...
code_exercise_27k
13,327
Write a Python function to remove all duplicates from a given list while preserving the order of the elements. The function should take a list as input and return a new list with all duplicate elements removed, maintaining the first occurrence of each element. The code should start with: ```python from typing import L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_preserve_order(lst: List[int]) -> List[int]: """ Removes duplicates from the input list while preserving the order of the elements. Args: lst (List[int]): The input lis...
code_exercise_27k
13,330
Write a Python function to remove all occurrences of a specified element from a given list. The function should handle the case where the list is empty or the element to remove is not present in the list. The function should also handle the case where the element to remove is `None`. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def remove_element(list_to_modify: List[Any], element_to_remove: Any = None) -> List[Any]: """ Removes all occurrences of a specified element from a given list. Args: list_to_modify (Li...
code_exercise_27k
13,331
Write a Python function to extract unique numbers from a given list of numbers and return them in the order of their first appearance. The function should handle lists of integers and floats. The code should start with: ```python from typing import List, Union def get_unique_numbers(numbers: List[Union[int, float]]) -...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def get_unique_numbers(numbers: List[Union[int, float]]) -> List[Union[int, float]]: """ Extracts unique numbers from a given list of numbers while maintaining the order of their first appearance....
code_exercise_27k
13,332
Write a Python function to sort a list of strings in alphabetical order, but with a twist: the function should also allow for a custom sorting key. The custom sorting key should be a function that takes a string and returns a value based on which the strings will be sorted. If no custom key is provided, the function sh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Callable, Optional def sort_strings_with_key(strings: List[str], key: Optional[Callable[[str], any]] = None) -> List[str]: """ Sorts a list of strings in alphabetical order with an optional custom sorti...
code_exercise_27k
13,333
Write a Python function to reverse a given string using a stack data structure. The function should start with: ```python from typing import List def reverse_string_with_stack(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def reverse_string_with_stack(input_string: str) -> str: """ Reverses a given string using a stack data structure. Args: input_string (str): The string to be reversed. Returns: ...
code_exercise_27k
13,334
Write a Python function to sort a list of strings in alphabetical order. The function should handle an optional argument for the list of strings, defaulting to an empty list if not provided. The function should return a new list with the strings sorted in alphabetical order. The code should start with: ```python from ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def sort_strings(string_list: Optional[List[str]] = None) -> List[str]: """ Sorts a list of strings in alphabetical order. Args: string_list (Optional[List[str]], optional): A list...
code_exercise_27k
13,337
Write a Python function to determine if a given number is a perfect square. The function should have a default argument of 1. The function should return `True` if the number is a perfect square and `False` otherwise. The function should handle negative numbers by returning `False`. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_perfect_square(num: int = 1) -> bool: """ Determines if a given number is a perfect square. Args: num (int): The number to check. Defaults to 1. Returns: bool: True if the number is ...
code_exercise_27k
13,339
Write a Python function to filter out prime numbers from a given list of integers. The function should return a new list containing only the prime numbers from the input list. The function should be efficient and handle edge cases such as an empty list or a list containing non-prime numbers. 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 is_prime(number: int) -> bool: """ Check if a given number is prime. Args: number (int): The number to check for primality. Returns: bool: True if the number is prime, False...
code_exercise_27k
13,340
Write a Python function to calculate the area of a triangle given its base and height. The function should have default arguments for both base and height, set to 1. The function should return the calculated area as a float. Additionally, provide a function to calculate the perimeter of the triangle given its three sid...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_triangle_area(base: float = 1, height: float = 1) -> float: """ Calculate the area of a triangle. Args: base (float): The base length of the triangle (default is 1). height (float): The height ...
code_exercise_27k
13,342
Write a Python function to convert a list of strings to uppercase and return a new list with the converted strings. The function should handle an empty list and ensure that each string in the input list is converted to uppercase. The code should start with: ```python from typing import List def convert_to_uppercase(st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def convert_to_uppercase(strings: List[str]) -> List[str]: """ Converts a list of strings to uppercase and returns a new list with the converted strings. Args: strings (List[str]): A list of...
code_exercise_27k
13,345
Write a Python function to remove an element from a specific index in a list. The function should handle the case where the index is out of range by returning the original list unchanged. The function should not raise any exceptions. The code should start with: ```python from typing import List def remove_element_fro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_element_from_list(lst: List[any], index: int) -> List[any]: """ Remove an element from a specific index in a list. If the index is out of range, return the original list unchanged. Ar...