source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
8,559
Write a Python function to unpack the keys of a given dictionary into separate variables and create a new list containing these keys. The function should handle dictionaries with any number of key-value pairs. The code should start with: ```python from typing import Dict, List def unpack_keys_to_list(dictionary: Dict)...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def unpack_keys_to_list(dictionary: Dict) -> List[str]: """ Unpacks the keys of a given dictionary into a new list. Args: dictionary (dict): The dictionary containing key-value pairs. ...
code_exercise_27k
8,561
Write a Python function to remove elements from a list that have duplicate values. The function should return a new list containing only 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]: """ Remove elements from a list that have duplicate values and return a new list containing only the first occurrence of each element. Args: ...
code_exercise_27k
8,563
Write a Python function to unpack the elements of a tuple into separate variables and count the number of occurrences of a specific value. The function should handle tuples of any size and return a tuple containing the unpacked variables and the count of occurrences of the specified value. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, Any def unpack_and_count(tup: Tuple[Any, ...], value: Any) -> Tuple[Any, ...]: """ Unpacks the elements of a tuple into separate variables and counts the number of occurrences of a specific value. ...
code_exercise_27k
8,566
Write a Python function to unpack the values of a dictionary into separate variables and calculate their product. The function should handle dictionaries with numeric values only and raise appropriate errors for empty dictionaries or non-numeric values. The code should start with: ```python def calculate_product(dicti...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_product(dictionary: dict) -> int or float: """ Calculate the product of the values in a dictionary. Args: dictionary (dict): The dictionary containing the values to be unpacked and multiplied. Ret...
code_exercise_27k
8,568
Write a Python function to remove consecutive duplicate elements from a given list. The function should return a new list with all consecutive duplicates removed, preserving the order of the remaining elements. The code should start with: ```python from typing import List def remove_consecutive_duplicates(lst: List[in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_consecutive_duplicates(lst: List[int]) -> List[int]: """ Removes consecutive duplicate elements from the given list. Args: lst (List[int]): The input list from which consecutive d...
code_exercise_27k
8,574
Write a Python function to check if all elements in a given tuple are unique. The function should unpack the tuple into separate variables and then determine if all values are unique. The code should start with: ```python def are_all_elements_unique(my_tuple: tuple) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def are_all_elements_unique(my_tuple: tuple) -> bool: """ Checks if all elements in the given tuple are unique. Args: my_tuple (tuple): The input tuple to check for unique elements. Returns: bool: True ...
code_exercise_27k
8,578
Write a Python function to unpack the values of a given dictionary into separate variables and find the smallest value. The function should handle dictionaries with any number of key-value pairs and return the smallest value. The code should start with: ```python from typing import Dict, Any def find_smallest_value(di...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any def find_smallest_value(dictionary: Dict[Any, Any]) -> Any: """ Unpacks the values of a dictionary into separate variables and finds the smallest value. Args: dictionary (Dict[Any, Any]...
code_exercise_27k
8,588
Write a Python function to find the intersection of two sets and calculate the product of the common elements using recursion. The function should return both the intersection set and the product of the common elements. The code should start with: ```python from typing import Set, Tuple def find_intersection_and_prod...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Set, Tuple def find_intersection_and_product(set1: Set[int], set2: Set[int]) -> Tuple[Set[int], int]: """ Find the intersection of two sets and calculate the product of the common elements using recursion. ...
code_exercise_27k
8,590
Write a Python function to calculate the number of microseconds since midnight for a given datetime object. The function should take a datetime object as input and return the number of microseconds since midnight. The code should start with: ```python from datetime import datetime def microseconds_since_midnight(dt: d...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def microseconds_since_midnight(dt: datetime) -> int: """ Calculate the number of microseconds since midnight for a given datetime object. Args: dt (datetime): The datetime object for ...
code_exercise_27k
8,592
Write a Python function to find the intersection of two sets using recursion and another function to check if one set is a proper subset of another set. The intersection function should return the set of elements common to both sets, and the proper subset function should return `True` if the first set is a proper subse...
Here is the Python code to solve the given problem in the markdown block: ```python def intersection_recursive(set1: set, set2: set) -> set: """ Find the intersection of two sets using recursion. Args: set1 (set): The first set. set2 (set): The second set. Returns: set: The int...
code_exercise_27k
8,597
Write a Python function to find the second smallest value in a given tuple. The function should handle tuples with at least two elements and should return the second smallest value. If the tuple has fewer than two elements, the function should raise a `ValueError`. The code should start with: ```python def find_second...
Here is the Python code to solve the given problem in the markdown block: ```python def find_second_smallest(t: tuple) -> any: """ Find the second smallest value in the given tuple. Args: t (tuple): A tuple of values. Returns: any: The second smallest value in the tuple. Raises: ...
code_exercise_27k
8,598
Write a Python function to calculate the number of milliseconds since midnight for a given datetime object. The function should take the year, month, day, hour, minute, second, and microsecond as input parameters and return the number of milliseconds since midnight. The code should start with: ```python import datetim...
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def milliseconds_since_midnight(year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0) -> int: """ Calculate the number of milliseconds since midnight for a given...
code_exercise_27k
8,603
Write a Python function to find the intersection of two sets and another function to find the largest common subset of two sets using recursion. The first function should return the intersection of the two sets, and the second function should return the largest common subset of the two sets. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python def intersection(a: set, b: set) -> set: """ Find the intersection of two sets. Args: a (set): The first set. b (set): The second set. Returns: set: The intersection of the two sets. """ ...
code_exercise_27k
8,607
Write a Python function to unpack the elements of a tuple into separate variables and check if all values are greater than a specific number. The function should return `True` if all values in the tuple are greater than the specified number, and `False` otherwise. The code should start with: ```python def check_tuple_v...
Here is the Python code to solve the given problem in the markdown block: ```python def check_tuple_values(tuple_variable: tuple, specific_number: int) -> bool: """ Unpacks the elements of a tuple into separate variables and checks if all values are greater than a specific number. Args: tuple_varia...
code_exercise_27k
8,610
Write a Python function to remove duplicate sublists from a given nested list. Each sublist within the nested list should be considered unique based on its elements, regardless of their order. The function should return a new nested list containing only the unique sublists. The code 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 remove_duplicate_sublists(nested_lst: List[List[int]]) -> List[List[int]]: """ Remove duplicate sublists from a nested list, considering sublists unique based on their elements regardless of order. ...
code_exercise_27k
8,611
Write a Python function to find the largest positive value in a given dictionary. The function should unpack the values of the dictionary into separate variables and then determine the largest positive value, ignoring any negative values. If no positive values are found, the function should raise a `ValueError`. The c...
Here is the Python code to solve the given problem in the markdown block: ```python def find_largest_positive_value(dictionary: dict) -> int: """ Finds the largest positive value in a given dictionary. Args: dictionary (dict): The dictionary containing key-value pairs. Returns: int: Th...
code_exercise_27k
8,614
Write a Python function to unpack the keys of a given dictionary into separate variables and count the number of keys that contain only numeric characters. The function should return the count of such keys. The code should start with: ```python from typing import Dict def count_numeric_keys(dictionary: Dict[str, any])...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_numeric_keys(dictionary: Dict[str, any]) -> int: """ Counts the number of keys in a dictionary that contain only numeric characters. Args: dictionary (Dict[str, any]): The dictiona...
code_exercise_27k
8,617
Write a Python function to find the intersection of two sets and check if the first set is a proper superset of the second set using recursion. The function should return a tuple containing the intersection of the two sets and a boolean indicating whether the first set is a proper superset of the second set. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python def recursive_intersection_and_superset(set1: set, set2: set) -> tuple: """ Recursively finds the intersection of two sets and checks if the first set is a proper superset of the second set. Args: set1 (set): The fi...
code_exercise_27k
8,618
Write a Python function to find the median value of a given tuple of numbers. The function should unpack the elements of the tuple into separate variables and then calculate the median. The median is defined as the middle value in a sorted list of numbers. If the list has an even number of elements, the median is the a...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, List def find_median(t: Tuple[float]) -> float: """ Find the median value of a tuple by unpacking its elements into separate variables. Args: t (Tuple[float]): The input tuple from which t...
code_exercise_27k
8,621
Write a Python function to calculate the product of the values in a given dictionary, ignoring any zero values. The function should unpack the dictionary values into separate variables and then compute the product of the non-zero values. The code should start with: ```python from typing import Dict def calculate_produ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def calculate_product_of_non_zero_values(dictionary: Dict[str, int]) -> float: """ Calculate the product of the non-zero values in a given dictionary. Args: dictionary (Dict[str, int]): A di...
code_exercise_27k
8,622
Write a Python function to remove elements from a list that have repeating patterns. A repeating pattern is defined as a substring that, when repeated a certain number of times, equals the entire string. For example, "abcabc" has a repeating pattern "abc", and "xyzxyzxyz" has a repeating pattern "xyz". The function sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_elements_with_repeating_patterns(lst: List[str]) -> List[str]: """ Remove elements from a list that have repeating patterns. Args: lst (List[str]): The input list of strings to pr...
code_exercise_27k
8,631
Write a Python function to unpack the values of a dictionary into separate variables and calculate the average of all values greater than 100. The function should return the average as a float. If there are no values greater than 100, the function should raise a `ValueError`. 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 Dict, List, Union def calculate_average_greater_than_100(data: Dict[str, int]) -> Union[float, None]: """ Calculate the average of values in a dictionary that are greater than 100. Args: data (Di...
code_exercise_27k
8,632
Write a Python function to find the intersection of two sets and check if it is non-empty using recursion. The function should return a tuple where the first element is the intersection set and the second element is a boolean indicating whether the intersection is non-empty. The code should start with: ```python def f...
Here is the Python code to solve the given problem in the markdown block: ```python def find_intersection_and_check_non_empty(set1: set, set2: set) -> tuple: """ Recursive function to find the intersection of two sets and check if it is non-empty. Args: set1 (set): The first set. set2 (set)...
code_exercise_27k
8,635
Write a Python function to count the number of dictionary keys that contain only alphabetic characters and unpack the keys into separate variables. The function should return the count of such keys. The code should start with: ```python from typing import Dict, List def count_and_unpack_alpha_keys(dictionary: Dict[str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def count_and_unpack_alpha_keys(dictionary: Dict[str, any]) -> (int, List[str]): """ Counts the number of dictionary keys that contain only alphabetic characters and unpacks the keys into a list. ...
code_exercise_27k
8,641
Write a Python function to unpack the values of a given dictionary into separate variables and calculate the square of their sum. The function should handle dictionaries with any number of key-value pairs, where the values are numeric. The code should start with: ```python from typing import Dict def calculate_square_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def calculate_square_of_sum(dictionary: Dict[str, int]) -> int: """ Unpacks the values of a given dictionary into separate variables and calculates the square of their sum. Args: dictionary ...
code_exercise_27k
8,644
Write a Python function to remove duplicate elements from a given list while preserving the order of the first occurrence of each element. The function should return a new list with the duplicates removed. The code should start with: ```python from typing import List def remove_duplicates_preserve_order(input_list: 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(input_list: List[int]) -> List[int]: """ Remove duplicate elements from a given list while preserving the order of the first occurrence of each element. Args: ...
code_exercise_27k
8,645
Write a Python function to determine if a given year is a leap year. The function should use the `datetime` module to create a date object and check if February 29 exists in that year. The function should return `True` if the year is a leap year and `False` otherwise. The code should start with: ```python import datet...
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def is_leap_year(year: int) -> bool: """ Determine if a given year is a leap year. Args: year (int): The year to be checked. Returns: bool: True if the year is a leap year, False otherw...
code_exercise_27k
8,646
Write a Python function to compute the sum of absolute values of the elements in a tuple. The function should unpack the tuple into separate variables and then calculate the sum of their absolute values. The code should start with: ```python from typing import Tuple def sum_of_absolute_values(t: Tuple[int, int, int, i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple def sum_of_absolute_values(t: Tuple[int, int, int, int]) -> int: """ Compute the sum of absolute values of the elements in a tuple. Args: t (Tuple[int, int, int, int]): Input tuple containi...
code_exercise_27k
8,650
Write a Python function to find the date of the first business day of a given year. The function should return the date object representing the first business day (Monday to Friday) of the specified year. If the first day of the year is a weekend (Saturday or Sunday), the function should return the date of the followin...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import date, timedelta def get_first_business_day(year: int) -> date: """ Returns the date of the first business day (Monday to Friday) of the specified year. Args: year (int): The year for which to f...
code_exercise_27k
8,658
Write a Python function to unpack the keys of a given dictionary into separate variables and create a new dictionary with lowercase keys only. The function should also ensure that the new dictionary does not contain any duplicate keys after converting them to lowercase. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any def transform_dictionary(dictionary: Dict[str, Any]) -> Dict[str, Any]: """ Unpacks the keys of a dictionary into separate variables and creates a new dictionary with lowercase keys only, ensuri...
code_exercise_27k
8,661
Write a Python function to calculate the sum of values at even indices in a given tuple. The function should unpack the elements of the tuple into separate variables and then compute the sum of values at even indices. The code should start with: ```python from typing import Tuple def sum_even_indices(tuple_data: Tuple...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple def sum_even_indices(tuple_data: Tuple[int]) -> int: """ Calculate the sum of values at even indices in a given tuple. Args: tuple_data (Tuple[int]): A tuple of integer values. Returns...
code_exercise_27k
8,669
Write a Python function to remove multiple elements from a given set. The function should take a set and a list of elements to be removed as input and return the modified set after removing the specified elements. The function should handle cases where the elements to be removed are not present in the set. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_elements_from_set(s: set, elements: list) -> set: """ Removes multiple elements from a given set. Args: s (set): The original set from which elements will be removed. elements (list): The list of ...
code_exercise_27k
8,671
Write a Python function to subtract a specified number of days from a given date. The function should accept a date string in the format 'YYYY-MM-DD' and an integer representing the number of days to subtract. The function should return the resulting date string after subtracting the specified number of days. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def subtract_days_from_date(date: str, num_days: int) -> str: """ Subtracts a specified number of days from a given date. Args: date (str): The date string in the format 'YY...
code_exercise_27k
8,672
Write a Python function to remove all odd numbers from a given set of integers and return a new set containing only the even numbers. The function should handle an empty set gracefully and should not modify the original set. The code should start with: ```python def remove_odd_numbers(numbers: set) -> set: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_odd_numbers(numbers: set) -> set: """ Remove all odd numbers from the input set and return a new set containing only the even numbers. Args: numbers (set): A set of integers from which to remove odd numbe...
code_exercise_27k
8,674
Write a Python function to add a specified number of days to a given date. The function should take a date string in the format 'YYYY-MM-DD' and an integer representing the number of days to add. The function should return a string representing the date after adding the specified number of days, also in the format 'YYY...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def add_days(date_string: str, days: int) -> str: """ Adds a specified number of days to a given date. Args: date_string (str): A string representing the date in the format ...
code_exercise_27k
8,675
Write a Python function to remove all even numbers from a given set of integers. The function should return a new set containing only the odd numbers. The code should start with: ```python def remove_even_numbers(numbers: set) -> set: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_even_numbers(numbers: set) -> set: """ Remove all even numbers from a given set of integers and return a new set containing only the odd numbers. Args: numbers (set): A set of integers from which to remov...
code_exercise_27k
8,678
Write a Python function to remove the common elements between two sets and return a new set containing the elements from the first set that are not in the second set. The function should handle sets of any size and ensure that the original sets are not modified. The code should start with: ```python def remove_common_e...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_common_elements(set1: set, set2: set) -> set: """ Remove the common elements between two sets and return a new set containing the elements from the first set that are not in the second set. Args: set1 (se...
code_exercise_27k
8,682
Write a Python function to remove elements from a set until a specific condition is met. The condition is defined by a function that takes an element as input and returns a boolean value. The function should continue removing elements from the set until the condition function returns `False` for all elements in the set...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_elements_until_condition(set_of_elements: set, condition_func: callable) -> set: """ Remove elements from a set until a specific condition is met. The function continues to remove elements from the set as long as...
code_exercise_27k
8,687
Write a Python function to unpack the values of a given dictionary into separate variables and calculate the sum of their exponential values. The exponential value of each dictionary value should be calculated as the square of the value. The function should return the sum of these exponential values. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def calculate_exponential_sum(dictionary: Dict[str, int]) -> float: """ Unpacks the values of a dictionary into separate variables and calculates the sum of their exponential values. The exponential...
code_exercise_27k
8,691
Write a Python function to filter strings from a given list that can be converted to a palindrome. A string can be converted to a palindrome if rearranging its characters can form a palindrome. For example, "aabb" can be rearranged to form "abba", which is a palindrome. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from collections import Counter def can_be_palindrome(string: str) -> bool: """ Check if a string can be rearranged to form a palindrome. Args: string (str): The input string to check. ...
code_exercise_27k
8,698
Write a Python function to unpack the keys of a given dictionary into separate variables and count the number of keys that contain only uppercase characters. The function should return the count of such keys. The code should start with: ```python from typing import Dict, List def unpack_and_count_uppercase_keys(dictio...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def unpack_and_count_uppercase_keys(dictionary: Dict[str, any]) -> int: """ Unpacks the keys of a dictionary into separate variables and counts the number of keys containing only uppercase characte...
code_exercise_27k
8,700
Write a Python function to remove all negative values from a given set of integers. The function should return a new set containing only the non-negative values from the original set. The code should start with: ```python def remove_negative_values(s: set) -> set: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_negative_values(s: set) -> set: """ Removes all negative values from a given set of integers. Args: s (set): The input set of integers. Returns: set: A new set containing only the non-negativ...
code_exercise_27k
8,702
Write a Python function to remove all positive values from a given set. The function should return a new set containing only the non-positive values (i.e., zero and negative values) from the original set. The function should be efficient and handle sets of any size. The code should start with: ```python def remove_pos...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_positive_values(s: set) -> set: """ Removes all positive values from a given set and returns a new set containing only non-positive values. Args: s (set): The set from which positive values are to be remo...
code_exercise_27k
8,717
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. The code should start with: ```python import string def convert_to_uppercase(input_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python import string def convert_to_uppercase(input_string: str) -> str: """ Converts a given string to uppercase. Args: input_string (str): The input string that needs to be converted to uppercase. Returns: ...
code_exercise_27k
8,719
Write a Python function to filter sublists from a nested list that have the same length as the first sublist. The function should return a new list containing only those sublists that match the length of the first sublist in the input nested list. The code should start with: ```python from typing import List def filt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_sublists_by_length(nested_list: List[List[any]]) -> List[List[any]]: """ Filter sublists from a nested list that have the same length as the first sublist. Args: nested_list (List...
code_exercise_27k
8,721
Write a Python function to unpack the keys of a dictionary into separate variables and count the number of keys containing a specific substring. The function should return a tuple containing the count of keys with the substring and a list of the keys that contain the substring. The code should start with: ```python fr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def count_and_list_keys_with_substring(d: Dict[str, any], substr: str) -> Tuple[int, List[str]]: """ Unpacks the keys of a dictionary into separate variables and counts the number of keys co...
code_exercise_27k
8,724
Write a Python function to remove elements from a list that contain a specific set of vowels. The function should be case-insensitive and should return a new list with the filtered elements. The code should start with: ```python from typing import List, Set def remove_elements_with_vowels(lst: List[str], vowels: Set[s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Set def remove_elements_with_vowels(lst: List[str], vowels: Set[str]) -> List[str]: """ Remove elements from a list that contain a specific set of vowels. Args: lst (List[str]): The input l...
code_exercise_27k
8,726
Write a Python function to generate a list of dates between two specified dates, inclusive. The function should handle date inputs in the format "YYYY-MM-DD" and return a list of dates in the same format. The function should also handle cases where the start date is the same as the end date. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta from typing import List def generate_date_range(start_date: str, end_date: str) -> List[str]: """ Generates a list of dates between two specified dates, inclusive. Args: sta...
code_exercise_27k
8,736
Write a Python function to filter elements from a given list that contain a specific substring pattern. The function should return a new list containing only the elements that include the specified substring. The code should start with: ```python from typing import List def filter_elements_by_pattern(lst: List[str], p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_elements_by_pattern(lst: List[str], pattern: str) -> List[str]: """ Filters elements from a list that contain a specific substring pattern. Args: lst (List[str]): The list of stri...
code_exercise_27k
8,753
Write a Python function to count the frequency of each element in a given list and return the result as a dictionary. The function should handle lists of any data type, including integers, strings, and mixed types. The code should start with: ```python from typing import List, Dict, Any import collections def count_el...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Any import collections def count_element_frequency(lst: List[Any]) -> Dict[Any, int]: """ Counts the frequency of each element in the given list and returns the result as a dictionary. Args: ...
code_exercise_27k
8,762
Write a Python function to generate all possible combinations of a given list of elements, where each combination includes exactly k elements. The function should use the `itertools` module to achieve this efficiently. The code should start with: ```python from typing import List import itertools def generate_combinat...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import itertools def generate_combinations(lst: List[int], k: int) -> List[tuple]: """ Generate all possible combinations of a given list of elements, where each combination includes exactly k elements. ...
code_exercise_27k
8,767
Write a Python function to find the N largest items in a list using the `heapq` module. The function should return the N largest items in descending order. The code should start with: ```python from typing import List import heapq def find_n_largest(lst: List[int], n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import heapq def find_n_largest(lst: List[int], n: int) -> List[int]: """ Find the N largest items in a list using the heapq module. Args: lst (List[int]): The input list of integers. ...
code_exercise_27k
8,775
Write a Python function to create a random 2D array of integers within a specified range using the `numpy` module. The function should take three parameters: the number of rows, the number of columns, and a tuple representing the range of integers (inclusive). The function should return the generated 2D array. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_random_int_array(rows: int, cols: int, range_tuple: tuple) -> np.ndarray: """ Creates a random 2D array of integers within a specified range using the numpy module. Args: rows (int): N...
code_exercise_27k
8,776
Write a Python function to unpack the keys of a given dictionary into separate variables, shuffle the keys, and then calculate the sum of the ASCII values of the characters in the shuffled keys. The function should return the sum of these ASCII values. The code should start with: ```python from typing import Dict def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict import random def sum_of_shuffled_ascii_values(dictionary: Dict[str, any]) -> int: """ Unpacks the keys of a dictionary into separate variables, shuffles them, and calculates the sum of ASCII values of t...
code_exercise_27k
8,780
Write a Python function to convert a given list of strings into a single string, separated by a specific character. The function should handle cases where the list is empty or contains only one element. Additionally, the function should be able to handle different types of separators, including special characters and s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def convert_list_to_string(lst: List[str], separator: str) -> str: """ Converts a given list of strings into a single string, separated by the specified separator. Args: lst (List[str]): The...
code_exercise_27k
8,782
Write a Python function to calculate the number of seconds between two specified dates and times. The function should accept two strings representing the dates and times in the format 'YYYY-MM-DD HH:MM:SS'. The function should return the number of seconds between these two dates and times as an integer. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def calculate_seconds(start_datetime: str, end_datetime: str) -> int: """ Calculate the number of seconds between two specified dates and times. Args: start_datetime (str): The startin...
code_exercise_27k
8,786
Write a Python function to calculate the area of a rectangle given the length and width. The function should handle non-negative inputs and return the area as a float. The code should start with: ```python def calculate_area(length: float, width: float) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_area(length: float, width: float) -> float: """ Calculate the area of a rectangle given its length and width. Parameters: length (float): The length of the rectangle. Must be non-negative. width (float...
code_exercise_27k
8,790
Write a Python function to find all prime numbers within a given range efficiently using the Sieve of Eratosthenes algorithm. The function should return a list of prime numbers within the specified range. The code should start with: ```python from typing import List def find_primes_in_range(start: int, end: int) -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_primes_in_range(start: int, end: int) -> List[int]: """ Find all prime numbers within a given range using the Sieve of Eratosthenes algorithm. Args: start (int): The starting number...
code_exercise_27k
8,794
Write a Python function to generate the Fibonacci sequence up to a given number of terms. The function should handle invalid inputs by raising appropriate exceptions. The code should start with: ```python def generate_fibonacci_sequence(num_terms: int) -> list: ```
Here is the Python code to solve the given problem in the markdown block: ```python def generate_fibonacci_sequence(num_terms: int) -> list: """ Generate the Fibonacci sequence up to a given number of terms. Args: num_terms (int): The number of terms to generate in the Fibonacci sequence. Retu...
code_exercise_27k
8,804
Write a Python function to determine if a given number is a perfect square. The function should use a binary search approach to efficiently check if the number is a perfect square. The code should start with: ```python def is_perfect_square(num: int) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_perfect_square(num: int) -> bool: """ Determine if a given number is a perfect square using a binary search approach. Args: num (int): The number to be checked. Returns: bool: True if the number ...
code_exercise_27k
8,815
Write a Python function to shuffle a given list in place and return the shuffled list. The function should use the `random` module to perform the shuffling. The code should start with: ```python from typing import List import random def shuffle_list(items: List[any]) -> List[any]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def shuffle_list(items: List[any]) -> List[any]: """ Shuffles a given list in place and returns the shuffled list. Args: items (List[any]): The list of items to be shuffled. ...
code_exercise_27k
8,818
Write a Python function to calculate the hypotenuse of a right-angled triangle given the lengths of the other two sides. The function should handle non-negative inputs and return the hypotenuse as a float. The code should start with: ```python def calculate_hypotenuse(side1: float, side2: float) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_hypotenuse(side1: float, side2: float) -> float: """ Calculate the hypotenuse of a right-angled triangle given the lengths of the other two sides. Parameters: side1 (float): Length of the first side of...
code_exercise_27k
8,820
Write a Python function to unpack the keys of a given dictionary into separate variables and count the number of keys that contain only numeric characters and dashes. The function should return the count of such keys. The code should start with: ```python from typing import Dict def count_numeric_dash_keys(dictionary...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_numeric_dash_keys(dictionary: Dict[str, any]) -> int: """ Counts the number of keys in the given dictionary that contain only numeric characters and dashes. Args: dictionary (Dict[...
code_exercise_27k
8,822
Write a Python function to convert a given decimal number to its binary representation. The function should handle only positive integers and return the binary representation as a string. The function should raise a `ValueError` if the input is not a positive integer. The code should start with: ```python def decimal_...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_binary(decimal: int) -> str: """ Convert a decimal number to its binary representation. Args: decimal (int): The decimal number to be converted to binary. Returns: str: The binary represe...
code_exercise_27k
8,825
Write a Python function to unpack the elements of a tuple into separate variables and check if the sum of these values is a power of 2. The function should return a boolean value indicating whether the sum is a power of 2. The function should also raise a `TypeError` if the input is not a tuple or if any element in the...
Here is the Python code to solve the given problem in the markdown block: ```python def is_sum_power_of_2(t: tuple) -> bool: """ Unpacks the elements of a tuple into separate variables and checks if the sum of values is a power of 2. Args: t (tuple): A tuple containing integer values. Returns:...
code_exercise_27k
8,826
Write a Python function to convert a given binary number represented as a string to its decimal equivalent. The function should raise a `ValueError` if the input string contains characters other than '0' and '1'. The code should start with: ```python def binary_to_decimal(binary_number: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_decimal(binary_number: str) -> int: """ Convert a binary number represented as a string to its decimal equivalent. Parameters: binary_number (str): The binary number to be converted. It should contain ...
code_exercise_27k
8,831
Write a Python function to combine two tuples into a single tuple, ensuring that the resulting tuple maintains the order of elements from both input tuples. The function should handle tuples of any length, including empty tuples. The code should start with: ```python from typing import Tuple def combine_tuples(tuple1:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple def combine_tuples(tuple1: Tuple, tuple2: Tuple) -> Tuple: """ Combines two tuples into a single tuple. Args: tuple1 (Tuple): The first tuple to combine. tuple2 (Tuple): The second ...
code_exercise_27k
8,832
Write a Python function to convert a given `datetime` object to a string representation in the format "Day/Month/Year Hour:Minute:Second". The function should handle the input validation to ensure that the provided object is indeed an instance of the `datetime` class. If the input is not a `datetime` object, the functi...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def format_datetime(datetime_obj: datetime) -> str: """ Convert a datetime object to a string representation in the format "Day/Month/Year Hour:Minute:Second". Args: datetime_obj (date...
code_exercise_27k
8,835
Write a Python function to remove duplicate elements from a given tuple and return a new tuple with unique elements. The function should maintain the order of elements as they appear in the original tuple. The code should start with: ```python from typing import Tuple def remove_duplicates_with_order(elements: Tuple) ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple def remove_duplicates_with_order(elements: Tuple) -> Tuple: """ Removes duplicate elements from a tuple while maintaining the order of elements. Args: elements (Tuple): The tuple containing...
code_exercise_27k
8,837
Write a Python function to reverse the elements of a given tuple and return a new tuple with the elements in reverse order. The function should handle tuples of any length, including empty tuples. The code should start with: ```python def reverse_tuple(input_tuple: tuple) -> tuple: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_tuple(input_tuple: tuple) -> tuple: """ Reverses the elements in a tuple and returns a new tuple. Args: input_tuple (tuple): The input tuple to be reversed. Returns: tuple: A new tuple with ...
code_exercise_27k
8,840
Write a Python function to determine if a given number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding the number itself). The function should return `True` if the number is a perfect number, and `False` otherwise. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python def is_perfect_number(num: int) -> bool: """ Determine if a given number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding the number itself). Args: ...
code_exercise_27k
8,841
Write a Python function to find the maximum and minimum values in a given tuple of numeric values. The function should handle tuples containing integers and floats, and it should raise a `ValueError` if the input is an empty tuple or doesn't contain any numeric values. The code should start with: ```python def find_ma...
Here is the Python code to solve the given problem in the markdown block: ```python def find_max_min(numbers: tuple) -> tuple: """ Finds the maximum and minimum values in a given tuple of numeric values. Args: numbers (tuple): The input tuple containing numeric values. Returns: tuple: ...
code_exercise_27k
8,844
Write a Python function to generate a random list of integers within a given range. The function should take three parameters: the starting value of the range (inclusive), the ending value of the range (exclusive), and the length of the list to be generated. The function should return a list of random integers within t...
Here is the Python code to solve the given problem in the markdown block: ```python import random def generate_random_list(start: int, end: int, length: int) -> list: """ Generates a random list of integers within a given range. Args: start (int): The starting value of the range (inclusive). ...
code_exercise_27k
8,845
Write a Python function to convert a tuple into a list. The function should handle tuples of any length, including empty tuples. The function should also handle nested tuples, converting them into nested lists. The code should start with: ```python from typing import Tuple, List, Any def convert_tuple_to_list(t: Tuple...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, List, Any def convert_tuple_to_list(t: Tuple[Any, ...]) -> List[Any]: """ Converts a tuple into a list, handling nested tuples by converting them into nested lists. Args: t (Tuple[Any, ......
code_exercise_27k
8,848
Write a Python function to calculate the number of days between two given dates. The function should handle date strings in the format 'YYYY-MM-DD' and return the number of days between the start date and the end date. If the start date is later than the end date, the function should return a negative number. The funct...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import date def calculate_days_between(start_date: str, end_date: str) -> int: """ Calculate the number of days between two given dates. Args: start_date (str): The start date in the format 'YYYY-MM-D...
code_exercise_27k
8,849
Write a Python function to check if an element exists within a tuple. The function should return `True` if the element is found in the tuple, and `False` otherwise. The code should start with: ```python def check_element_in_tuple(element, tup) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def check_element_in_tuple(element, tup) -> bool: """ Check if an element exists within a tuple. Args: element: The element to check. tup: The tuple to search within. Returns: bool: True if the ...
code_exercise_27k
8,850
Write a Python function to generate all possible combinations of a given list using the `itertools.combinations` function. The function should return a list of tuples, where each tuple represents a combination of the input list. The function should handle lists of varying lengths and ensure that all possible combinatio...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import itertools def generate_all_combinations(lst: List[int]) -> List[Tuple[int, ...]]: """ Generate all possible combinations of a given list using itertools.combinations. Args: lst...
code_exercise_27k
8,851
Write a Python function to convert a tuple into a string, where each element of the tuple is separated by a specified delimiter. The function should handle tuples containing various data types, including integers, strings, and floats. The code should start with: ```python from typing import Tuple, Any def tuple_to_str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, Any def tuple_to_string(data: Tuple[Any], delimiter: str = ', ') -> str: """ Converts a tuple into a string with elements separated by a specified delimiter. Args: data (Tuple[Any]): The i...
code_exercise_27k
8,857
Write a Python function to find the prime factorization of a given number. The function should return a list of prime factors in ascending order. The code should start with: ```python from typing import List def prime_factorization(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def prime_factorization(n: int) -> List[int]: """ Finds the prime factorization of a given number. Args: n (int): The number to factorize. Returns: List[int]: The prime factors ...
code_exercise_27k
8,859
Write a Python function to find the N largest elements from a given dataset efficiently. The dataset is a list of integers. The function should handle cases where N is greater than the length of the dataset or less than or equal to 0 by returning an empty list. 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 import heapq def get_n_largest_items(dataset: List[int], n: int) -> List[int]: """ Returns the N largest items from the given dataset. Args: dataset (list): The dataset from which to extract...
code_exercise_27k
8,860
Write a Python function to perform element-wise multiplication of two tuples. The function should take two tuples as input and return a new tuple where each element is the product of the corresponding elements from the input tuples. If the input tuples have different lengths, the function should raise a `ValueError`. ...
Here is the Python code to solve the given problem in the markdown block: ```python def element_wise_multiplication(a: tuple, b: tuple) -> tuple: """ Perform element-wise multiplication of two tuples. Args: a (tuple): The first input tuple. b (tuple): The second input tuple. Returns: ...
code_exercise_27k
8,865
Write a Python function to calculate the sum of numeric values in a given tuple. The function should handle tuples containing integers, floats, and a mix of both. The code should start with: ```python from typing import Tuple def calculate_sum(nums: Tuple[float]) -> float: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import Tuple def calculate_sum(nums: Tuple[float]) -> float: """ Calculates the sum of numeric values in a tuple. Args: nums (Tuple[float]): A tuple containing numeric values (integers or floats). ...
code_exercise_27k
8,866
Write a Python function to calculate the mean of each row in a 2D array using the `numpy` module. The function should return a 1D array where each element is the mean of the corresponding row in the input 2D array. The code should start with: ```python import numpy as np def calculate_row_means(arr: np.ndarray) -> np....
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def calculate_row_means(arr: np.ndarray) -> np.ndarray: """ Calculate the mean of each row in a 2D array using numpy. Parameters: arr (numpy.ndarray): The input 2D array. Returns: numpy.ndar...
code_exercise_27k
8,867
Write a Python function to determine the day of the week for a given date. The function should accept a date in string format (e.g., "2023-10-05") or as a `datetime` object. The function should return the day of the week as a string (e.g., "Thursday"). The code should start with: ```python from datetime import datetim...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def get_day_of_week(date): """ Determine the day of the week for a given date. Args: date (str or datetime): The date for which the day of the week needs to be determined. ...
code_exercise_27k
8,868
Write a Python function to determine if a given string is a valid palindrome permutation. A string is considered a valid palindrome permutation if it can be rearranged to form a palindrome. The function should ignore spaces and be case-insensitive. The code should start with: ```python def is_palindrome_permutation(s:...
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome_permutation(s: str) -> bool: """ Determine if a given string is a valid palindrome permutation. A string is considered a valid palindrome permutation if it can be rearranged to form a palindrome. The f...
code_exercise_27k
8,869
Write a Python function to check if all elements of a given tuple are the same. The function should return `True` if all elements are identical, and `False` otherwise. The code should start with: ```python def all_elements_same(t: tuple) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def all_elements_same(t: tuple) -> bool: """ Check if all elements of a given tuple are the same. Args: t (tuple): The input tuple to be checked. Returns: bool: True if all elements are the same, False ...
code_exercise_27k
8,872
Write a Python function to sort a list of datetime objects in chronological order. The function should handle lists of varying lengths and ensure that all elements in the input list are datetime objects. If any element is not a datetime object, the function should raise a `TypeError`. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime from typing import List def sort_datetime_list(datetime_list: List[datetime]) -> List[datetime]: """ Sort a list of datetime objects in chronological order. Args: datetime_list (List[d...
code_exercise_27k
8,874
Write a Python function to generate all possible combinations of a given list of elements. Each combination should be represented as a tuple, and the function should return a list of all such tuples. The function should handle lists of varying lengths and ensure that all possible combinations, including the empty combi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple from itertools import combinations def generate_all_combinations(elements: List[any]) -> List[Tuple[any]]: """ Generate all possible combinations of a given list of elements. Args: el...
code_exercise_27k
8,882
Write a Python function to concatenate two tuples and return the resulting tuple. The function should handle the case where the input is not a tuple by raising a `TypeError`. The code should start with: ```python def concatenate_tuples(t1: tuple, t2: tuple) -> tuple: ```
Here is the Python code to solve the given problem in the markdown block: ```python def concatenate_tuples(t1: tuple, t2: tuple) -> tuple: """ Concatenates two tuples and returns the resulting tuple. Args: t1 (tuple): The first tuple to be concatenated. t2 (tuple): The second tuple to be co...
code_exercise_27k
8,883
Write a Python function to calculate the factorial of a given non-negative integer using the `math` module. The function should handle the case where the input number is negative by raising a `ValueError`. The code should start with: ```python import math def calculate_factorial(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import math def calculate_factorial(n: int) -> int: """ Calculate the factorial of a given non-negative integer. Args: n (int): The non-negative integer for which the factorial needs to be calculated. Returns:...
code_exercise_27k
8,884
Write a Python function to calculate the date and time after adding a specified number of minutes to a given datetime object. The function should handle both positive and negative minute values. The code should start with: ```python from datetime import datetime, timedelta def add_minutes_to_datetime(dt: datetime, min...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def add_minutes_to_datetime(dt: datetime, minutes: int) -> datetime: """ Calculate the date and time after adding a specified number of minutes to a given datetime object. Args: ...
code_exercise_27k
8,885
Write a Python function to generate a list of tuples, where each tuple contains a random number and its square. The function should accept two parameters: the number of tuples to generate (`num_tuples`) and the range for the random numbers (`start_range` and `end_range`). The function should return a list of tuples, wh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import random def generate_random_number_tuples(num_tuples: int, start_range: int, end_range: int) -> List[Tuple[int, int]]: """ Generate a list of tuples, where each tuple contains a random numbe...
code_exercise_27k
8,889
Write a Python function to remove the first and last elements from a given tuple and return a new tuple containing the remaining elements. The function should handle tuples of various lengths, including empty tuples and tuples with only one element. The code should start with: ```python def remove_first_and_last_elemen...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_first_and_last_elements(input_tuple: tuple) -> tuple: """ Removes the first and last elements from a tuple and returns a new tuple. Args: input_tuple (tuple): The input tuple from which the elements will ...
code_exercise_27k
8,891
Write a Python function to rotate a given list by a specific number of positions. The rotation can be either to the left or to the right, depending on the sign of the number of positions. The function should handle cases where the number of positions is greater than the length of the list by using modulo operation. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def rotate_list(lst: List, positions: int) -> List: """ Rotate a given list by a specific number of positions. Args: lst (List): The input list to rotate. positions (int): The number...
code_exercise_27k
8,892
Write a Python function to calculate the date and time after adding a specified number of weeks to a given datetime object. The function should return the new datetime object and print the result. The code should start with: ```python import datetime def add_weeks_to_date(date_time: datetime.datetime, weeks: int) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def add_weeks_to_date(date_time: datetime.datetime, weeks: int) -> datetime.datetime: """ Calculate the date and time after adding a specified number of weeks to a given datetime object. Args: date_...
code_exercise_27k
8,894
Write a Python function to reverse the order of words in a given sentence. The function should handle sentences with multiple spaces between words and leading/trailing spaces. The code should start with: ```python def reverse_sentence(sentence: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_sentence(sentence: str) -> str: """ Reverses the order of words in a given sentence. Args: sentence (str): The input sentence to be reversed. Returns: str: The reversed sentence where the wo...
code_exercise_27k
8,895
Write a Python function to calculate the number of days between two dates, considering leap years and different months. The function should handle date inputs in the format `YYYY-MM-DD` and return the absolute number of days between the two dates. If the end date is earlier than the start date, the function should rais...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def days_between_dates(start_date: str, end_date: str) -> int: """ Calculate the number of days between two dates. Args: start_date (str): The start date in the format 'YYYY-MM-DD'. ...
code_exercise_27k
8,897
Write a Python function to subtract one month from a given `datetime` object accurately, considering the varying number of days in different months and leap years. The function should handle edge cases such as subtracting from January and leap years. The code should start with: ```python from datetime import datetime,...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def subtract_one_month(dt: datetime) -> datetime: """ Subtract one month from a given datetime object accurately, considering varying days in months and leap years. Args: dt...
code_exercise_27k
8,898
Write a Python function to determine if two given tuples have at least one common element. The function should return `True` if there is at least one common element between the two tuples, and `False` otherwise. The code should start with: ```python def have_common_elements(tuple1: tuple, tuple2: tuple) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def have_common_elements(tuple1: tuple, tuple2: tuple) -> bool: """ Determine if two tuples have at least one common element. Args: tuple1 (tuple): The first tuple to compare. tuple2 (tuple): The second tupl...
code_exercise_27k
8,900
Write a Python function to check if a given number is divisible by both 3 and 5 simultaneously. The function should return `True` if the number is divisible by both 3 and 5, and `False` otherwise. The code should start with: ```python def is_divisible_by_3_and_5(number: int) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_divisible_by_3_and_5(number: int) -> bool: """ Check if a given number is divisible by both 3 and 5 simultaneously. Args: number (int): The number to check for divisibility. Returns: bool: True i...