source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
3,166
Write a Python function to find the symmetric difference between two sets using the symmetric difference operator (`^`). The symmetric difference of two sets is defined as the set of elements which are in either of the sets but not in both. The function should handle input validation to ensure that both inputs are sets...
Here is the Python code to solve the given problem in the markdown block: ```python def find_symmetric_difference(set1: set, set2: set) -> set: """ Finds the symmetric difference between two sets using the symmetric difference operator (^). Args: set1 (set): The first set. set2 (set): The s...
code_exercise_27k
3,170
Write a Python function to create a set from a given list and then find the intersection of this set with another set. The function should return a new set containing only the elements that are present in both sets. The code should start with: ```python from typing import List, Set def create_and_intersect_sets(lst: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Set def create_and_intersect_sets(lst: List[int], other_set: Set[int]) -> Set[int]: """ Creates a set from a given list and finds the intersection with another set. Args: lst (List[int]): T...
code_exercise_27k
3,172
Write a Python function to find the Greatest Common Divisor (GCD) of three numbers using the Euclidean algorithm. The function should be able to handle both positive and negative integers. The code should start with: ```python def gcd_of_three_numbers(num1: int, num2: int, num3: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def gcd_of_three_numbers(num1: int, num2: int, num3: int) -> int: """ Calculates the Greatest Common Divisor (GCD) of three numbers using the Euclidean algorithm. Args: num1 (int): The first number. num2 (in...
code_exercise_27k
3,174
Write a Python function to find the sum of all elements in a given set of numeric elements. The function should handle sets containing integers and floating-point numbers. The function should raise a `TypeError` if the input is not a set or if any element in the set is not numeric. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_set_elements(a_set: set) -> int or float: """ Calculates the sum of all numeric elements in a given set. Args: a_set (set): The set of numeric elements. Returns: int or float: The sum of all ...
code_exercise_27k
3,178
Write a Python function to remove all occurrences of a specific delimiter from a given string. If the delimiter does not exist in the string, the function should return the original string unchanged. The function should handle any type of delimiter, including special characters and spaces. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_delimiter(string: str, delimiter: str) -> str: """ Removes all occurrences of a specific delimiter from a given string. Args: string (str): The input string from which the delimiter should be removed. ...
code_exercise_27k
3,179
Write a Python function to calculate the mean of all elements in a given set of numbers. The function should handle the following cases: 1. If the set is empty, raise a `ValueError` with the message "The data_set is empty." 2. If the set contains any non-numeric values, raise a `ValueError` with the message "The data_s...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_mean(data_set: set) -> float: """ Calculates the mean of all numeric elements in a set. Args: data_set (set): The set of numbers for which the mean needs to be calculated. Returns: float: ...
code_exercise_27k
3,180
Write a Python function to find the maximum length of a palindromic subarray in a given list. A palindromic subarray is a contiguous subarray that reads the same forwards and backwards. The function should return the maximum length of such a subarray. 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 longest_palindromic_subarray(arr: List[int]) -> int: """ Find the maximum length of a palindromic subarray in a given list. Args: arr (List[int]): The input list of integers. Return...
code_exercise_27k
3,182
Write a Python function to find the median of a set of numbers. The function should handle sets with both even and odd numbers of elements and should return the median as a float. The function should also raise a `ValueError` if the input set is empty or contains non-numeric elements. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python def find_median(data: set) -> float: """ Calculates the median of a set of numbers. Args: data (set): A set of numerical values. Returns: float: The median value. Raises: ValueError: If the...
code_exercise_27k
3,183
Write a Python function to find the length of the longest substring without repeating characters in a given string. The function should efficiently handle the input string and return the length of the longest substring without repeating characters. The code should start with: ```python def longest_substring_length(s: s...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_substring_length(s: str) -> int: """ Find the length of the longest substring without repeating characters in a given string. Args: s (str): The input string. Returns: int: The length of the...
code_exercise_27k
3,184
Write a Python function to remove specified delimiters from the beginning and end of a given string. The function should handle multiple occurrences of the delimiters at both ends of the string. The code should start with: ```python def remove_delimiters(string: str, delimiter: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_delimiters(string: str, delimiter: str) -> str: """ Removes all occurrences of the specified delimiter from the beginning and end of the given string. Args: string (str): The input string from which delim...
code_exercise_27k
3,188
Write a Python function to determine if a given number is a perfect power of two. A perfect power of two is a number that can be expressed as \(2^k\) where \(k\) is a non-negative integer. The function should return `True` if the number is a perfect power of two, and `False` otherwise. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python def is_power_of_two(n: int) -> bool: """ Determines if a given number is a perfect power of two. A perfect power of two is a number that can be expressed as 2^k where k is a non-negative integer. Args: n (int):...
code_exercise_27k
3,192
Write a Python function to find the number of digits in a given number using recursion. The function should handle both positive and negative integers. The code should start with: ```python def count_digits(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_digits(n: int) -> int: """ Recursively counts the number of digits in a given number. Args: n (int): The number to count the digits of. Returns: int: The number of digits in the input number. ...
code_exercise_27k
3,200
Write a Python function to separate negative and positive numbers in a given list while preserving their respective order. The function should return a new list with all negative numbers followed by all positive numbers, maintaining the order in which they appeared in the original list. 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 separate_negative_positive(numbers: List[int]) -> List[int]: """ Separate negative and positive numbers in a list, preserving their respective order. Args: numbers (List[int]): A list of...
code_exercise_27k
3,205
Write a Python function to find the intersection of multiple sets. The function should take a list of sets as input and return a set containing the elements that are common to all the sets in the list. The function should handle edge cases such as an empty list of sets or a list containing non-set elements. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_intersection(sets: List[set]) -> set: """ Finds the intersection of multiple sets. Args: sets (List[set]): A list of sets. Returns: set: The intersection of all the set...
code_exercise_27k
3,207
Write a Python function to calculate the sum of the first n terms of the Fibonacci sequence. The function should handle the input validation and ensure that the input `n` is a positive integer. If `n` is not a positive integer, the function should raise a `ValueError`. The code should start with: ```python def calcula...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_fib_sum(n: int) -> int: """ Calculate the sum of the first n terms of the Fibonacci sequence. Args: n (int): The number of terms to sum. Returns: int: The sum of the first n terms of the F...
code_exercise_27k
3,217
Write a Python function to find the maximum number of intervals that overlap at any point in a given list of intervals. Each interval is represented as a tuple containing two integers, representing the start and end points of the interval. The function should return the maximum number of overlapping intervals at any po...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def max_intervals_overlap(intervals: List[Tuple[int, int]]) -> int: """ Calculates the maximum number of intervals that overlap at any point in a list. Args: intervals (List[Tuple[int...
code_exercise_27k
3,218
Write a Python function to find the intersection of two sets using the `&` operator. The function should handle cases where the input sets are empty or contain non-overlapping elements. The function should also raise a `ValueError` if either of the inputs is not a set. The code should start with: ```python def find_in...
Here is the Python code to solve the given problem in the markdown block: ```python def find_intersection(set1: set, set2: set) -> set: """ Find the intersection of two sets. Args: set1 (set): The first set. set2 (set): The second set. Returns: set: A new set containing the int...
code_exercise_27k
3,219
Write a Python function to find the leftmost occurrence of a specific delimiter in a given string. If the delimiter is not found in the string, the function should return -1. The function should be efficient and handle edge cases such as empty strings and delimiters longer than the string. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def find_leftmost_occurrence(string: str, delimiter: str) -> int: """ Find the leftmost occurrence of a specific delimiter in a given string. Args: string (str): The string to search in. delimiter (str): The...
code_exercise_27k
3,222
Write a Python function to determine if a given list is a valid subsequence of another list. A valid subsequence means that all elements of the subsequence appear in the main sequence in the same order, but not necessarily consecutively. The code should start with: ```python from typing import List def is_valid_subse...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_valid_subsequence(sequence: List[int], subsequence: List[int]) -> bool: """ Determines if the given subsequence is a valid subsequence of the main sequence. Args: sequence (List[int])...
code_exercise_27k
3,223
Write a Python function to find the symmetric difference between two sets. The symmetric difference between two sets is defined as the set of elements which are in either of the sets but not in both. The function should use the `^` operator to compute the symmetric difference. The code should start with: ```python def...
Here is the Python code to solve the given problem in the markdown block: ```python def symmetric_difference(set1: set, set2: set) -> set: """ Computes the symmetric difference between two sets. The symmetric difference between two sets is the set of elements which are in either of the sets but not in both...
code_exercise_27k
3,224
Write a Python function to determine if a given string is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once. The function should ignore case and non-alphabetic characters. The code should start with: ```python def is_pangram(text: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(text: str) -> bool: """ Determine if a given string is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once. This function ignores case and non-alphabetic characters...
code_exercise_27k
3,225
Write a Python function to remove the intersection of two sets using the `difference` method. The function should return a new set containing elements that are in the first set but not in the second set. The code should start with: ```python def remove_intersection_with_difference(set1: set, set2: set) -> set: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_intersection_with_difference(set1: set, set2: set) -> set: """ Removes the intersection of two sets using the `difference` method. Args: set1 (set): The first set. set2 (set): The second set. ...
code_exercise_27k
3,227
Write a Python function to create a set of unique characters from a given string, while preserving the order of their first occurrence in the string. The function should start with: ```python from typing import Set def create_ordered_set_from_string(string: str) -> Set[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Set def create_ordered_set_from_string(string: str) -> Set[str]: """ Creates a set of unique characters from the given string, preserving the order of their first occurrence. Args: string (str): ...
code_exercise_27k
3,231
Write a Python function to find the maximum and minimum values in a given set of numbers. The function should return a tuple containing the maximum and minimum values. If the input set is empty, the function should raise a `ValueError`. The code should start with: ```python def find_max_min(num_set: set) -> tuple: ```...
Here is the Python code to solve the given problem in the markdown block: ```python def find_max_min(num_set: set) -> tuple: """ Find the maximum and minimum values in a given set of numbers. Args: num_set (set): The input set of numbers. Returns: tuple: A tuple containing the maximum ...
code_exercise_27k
3,232
Write a Python function to determine if a list of strings can be concatenated to form a palindrome. A palindrome is a string that reads the same forward and backward. The function should check if the concatenation of the strings in the list can form a palindrome by rearranging the characters if necessary. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def can_form_palindrome(strings: List[str]) -> bool: """ Determine if a list of strings can be concatenated to form a palindrome. Args: strings (List[str]): A list of strings to be checked. ...
code_exercise_27k
3,234
Write a Python function to find the maximum and minimum values in a given set of numbers using a loop. The function should handle an empty set by raising a `ValueError`. The code should start with: ```python def find_max_min(nums: set) -> tuple: ```
Here is the Python code to solve the given problem in the markdown block: ```python def find_max_min(nums: set) -> tuple: """ Find the maximum and minimum values in a given set of numbers using a loop. Args: nums (set): A set of numbers. Returns: tuple: A tuple containing the maximum a...
code_exercise_27k
3,238
Write a Python function to find all the divisors of a given number efficiently. The function should return a list of all divisors in ascending order. The code should start with: ```python from typing import List def find_divisors(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def find_divisors(n: int) -> List[int]: """ Find all the divisors of a given number efficiently. Parameters: n (int): The number to find divisors for. Returns: List[int]: A ...
code_exercise_27k
3,239
Write a Python function to find the minimum number of jumps required to reach the end of a list where each element represents the maximum number of steps that can be jumped from that position. If it is not possible to reach the end, return -1. The code should start with: ```python from typing import List def min_jump...
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: """ Find the minimum number of jumps required to reach the end of a list. Args: arr (List[int]): A list of non-negative integers where each eleme...
code_exercise_27k
3,247
Write a Python function to calculate the sum of the series \(1 + \frac{1}{2} + \frac{1}{3} + \ldots + \frac{1}{n}\) for a given positive integer \(n\). The function should handle invalid inputs by raising a `ValueError`. The code should start with: ```python def calculate_harmonic_series_sum(n: int) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_harmonic_series_sum(n: int) -> float: """ Calculate the sum of the harmonic series 1 + 1/2 + 1/3 + ... + 1/n. Args: n (int): The number of terms in the series. Returns: float: The sum of t...
code_exercise_27k
3,255
Write a Python function to find the smallest prime factor of a given number. The function should handle numbers greater than or equal to 2. If the number is less than 2, the function should raise a `ValueError`. The function should start with: ```python def find_smallest_prime_factor(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def find_smallest_prime_factor(n: int) -> int: """ Find the smallest prime factor of a given number. Args: n (int): The number for which the smallest prime factor needs to be found. Returns: int: The sm...
code_exercise_27k
3,264
Write a Python function to find the intersection of two sets without using the built-in `intersection()` method. The function should iterate through the elements of the first set and check if each element is present in the second set. If an element is found in both sets, it should be added to the result set. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python def find_intersection_manual(set1: set, set2: set) -> set: """ Find the intersection of two sets without using the built-in intersection() method. Args: set1 (set): The first set. set2 (set): The second ...
code_exercise_27k
3,268
Write a Python function to find the symmetric difference between two sets using the symmetric_difference() method. The symmetric difference of two sets is the set of elements which are in either of the sets but not in both. The function should take two sets as input and return the symmetric difference set. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python def find_symmetric_difference(set1: set, set2: set) -> set: """ Find the symmetric difference between two sets using the symmetric_difference() method. Args: set1 (set): The first set. set2 (set): The second...
code_exercise_27k
3,269
Write a Python function to find the maximum sum of absolute differences between adjacent elements in a given list of integers. The function should return the maximum sum of absolute differences. The code should start with: ```python from typing import List def max_sum_of_differences(arr: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_sum_of_differences(arr: List[int]) -> int: """ Calculate the maximum sum of absolute differences between adjacent elements in a list. Args: arr (List[int]): The input list of integer...
code_exercise_27k
3,272
Write a Python function to double all the elements in a given list. The function should return a new list with all the elements from the input list doubled. The code should start with: ```python from typing import List def double_list_elements(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 double_list_elements(lst: List[int]) -> List[int]: """ Doubles all the elements in a given list and returns a new list with the doubled values. Args: lst (List[int]): The list of integer...
code_exercise_27k
3,273
Write a Python function to find the symmetric difference between two sets. The symmetric difference between two sets is defined as the set of elements which are in either of the sets but not in both. The function should handle any iterable as input and convert it to a set before performing the symmetric difference oper...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Iterable, Set def symmetric_difference(iterable1: Iterable, iterable2: Iterable) -> Set: """ Finds the symmetric difference between two iterables. The symmetric difference is defined as the set of elemen...
code_exercise_27k
3,274
Write a Python function to square all the elements in a given list. The function should handle lists containing integers, floats, and negative numbers. The function should return a new list with the squared values. The code should start with: ```python from typing import List def square_elements(lst: List[float]) -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def square_elements(lst: List[float]) -> List[float]: """ Squares all the elements in a given list. Args: lst (List[float]): A list of numeric values (integers or floats). Returns: ...
code_exercise_27k
3,279
Write a Python function to find the length of each element in a given list and return a new list with those lengths. The function should handle lists containing elements of various types, including strings, numbers, and other objects. If an element is not a string or a collection (like a list or tuple), its length shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def get_element_lengths(lst: List[Any]) -> List[int]: """ Calculate the length of each element in a given list and return a new list with the lengths. Args: lst (List[Any]): The inp...
code_exercise_27k
3,280
Write a Python function to find the Cartesian product of two sets. The Cartesian product of two sets \( A \) and \( B \) is the set of all ordered pairs \( (a, b) \) where \( a \) is in \( A \) and \( b \) is in \( B \). The function should return a set of tuples representing the Cartesian product. The code should sta...
Here is the Python code to solve the given problem in the markdown block: ```python def cartesian_product(set1: set, set2: set) -> set: """ Calculate the Cartesian product of two sets. Args: set1 (set): The first set. set2 (set): The second set. Returns: set: The Cartesian prod...
code_exercise_27k
3,282
Write a Python function to reverse the order of elements in a given list. The function should handle lists of any type of elements, including nested lists. The function should also raise a `TypeError` if the input is not a list. The code should start with: ```python from typing import List, Any def reverse_list(lst: L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def reverse_list(lst: List[Any]) -> List[Any]: """ Reverses the order of elements in a given list. Args: lst (List[Any]): The list to be reversed. Returns: List[Any]: The r...
code_exercise_27k
3,284
Write a Python function to check if a given set is a proper subset of another set. A set `set1` is considered a proper subset of `set2` if all elements of `set1` are present in `set2`, but `set1` is not equal to `set2`. The function should return `True` if `set1` is a proper subset of `set2`, and `False` otherwise. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def is_proper_subset(set1: set, set2: set) -> bool: """ Check if set1 is a proper subset of set2. Args: set1 (set): The first set to check. set2 (set): The second set to check against. Returns: ...
code_exercise_27k
3,287
Write a Python function to remove all duplicates from a given list while preserving the order of the elements. The function should return a new list with all unique elements in the order they first appeared in the original list. The code should start with: ```python from typing import List def remove_duplicates_prese...
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) -> List: """ Remove all duplicates from a given list while preserving the order of the elements. Args: lst (List): The list containing duplica...
code_exercise_27k
3,288
Write a Python function to find the elements present in one set but not in another, and vice versa. The function should return a tuple containing two sets: the first set should contain elements present in the first set but not in the second set, and the second set should contain elements present in the second set but n...
Here is the Python code to solve the given problem in the markdown block: ```python def find_elements_in_sets(set1: set, set2: set) -> tuple: """ Find the elements present in one set but not in another, and vice versa. Args: set1 (set): The first set. set2 (set): The second set. Return...
code_exercise_27k
3,289
Write a Python function to calculate the sum of the series \(1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \ldots + \frac{(-1)^n}{n}\). The function should take an integer `n` as input and return the sum of the first `n` terms of the series. The function should handle edge cases such as when `n` is 0 or negative by rai...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_series_sum(n: int) -> float: """ Calculates the sum of the series 1 - 1/2 + 1/3 - ... + (-1)^n/n. Args: n (int): The number of terms in the series. Returns: float: The sum of the series. ...
code_exercise_27k
3,290
Write a Python function to count the number of overlapping pairs of delimiters in a given string. The function should accept a string and a list of delimiter pairs, where each pair consists of a start delimiter and an end delimiter. The function should return the count of overlapping pairs found in the string. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def count_overlapping_delimiters(string: str, delimiter_pairs: List[Tuple[str, str]]) -> int: """ Counts the number of overlapping pairs of delimiters in a given string. Args: string ...
code_exercise_27k
3,295
Write a Python function to find the elements that are present in both sets and return them as a new set. The function should handle input validation to ensure that both inputs are of type `set`. If either input is not a set, the function should raise a `TypeError`. The code should start with: ```python def find_common...
Here is the Python code to solve the given problem in the markdown block: ```python def find_common_elements(set1: set, set2: set) -> set: """ Find the elements present in both sets. Args: set1 (set): The first set. set2 (set): The second set. Returns: set: A set containing the...
code_exercise_27k
3,296
Write a Python function to multiply corresponding elements of two given lists and return a new list with the results. The function should handle lists of different lengths by taking the minimum length of the two lists for the multiplication. The function should also raise a `ValueError` if either of the input lists is ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def multiply_lists(list1: List[int], list2: List[int]) -> List[int]: """ Multiplies corresponding elements of two given lists and returns a new list with the results. Args: list1 (List[int])...
code_exercise_27k
3,298
Write a Python function to filter out all even numbers from a given list and return a new list with only odd numbers. The function should handle lists of integers and return an empty list if no odd numbers are present. The code should start with: ```python from typing import List def filter_odd_numbers(numbers: List[i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_odd_numbers(numbers: List[int]) -> List[int]: """ Filters out even numbers from a given list and returns a new list with only odd numbers. Args: numbers (List[int]): A list of int...
code_exercise_27k
3,299
Write a Python function to find the elements present in either of the given sets, but not in both. This is known as the symmetric difference of two sets. The function should take two sets as input and return a new set containing the elements that are in either of the sets, but not in both. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def symmetric_difference(set1: set, set2: set) -> set: """ Find the elements present in either of the given sets, but not in both. Parameters: set1 (set): The first set. set2 (set): The second set. Returns: ...
code_exercise_27k
3,300
Write a Python function to find the second largest number in a given list of integers. If the list contains less than 2 elements, the function should raise a `ValueError`. The function should be efficient and handle edge cases such as duplicate numbers and negative numbers. 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 find_second_largest(numbers: List[int]) -> int: """ Find the second largest number in a given list of integers. Args: numbers (List[int]): A list of integers. Returns: int: ...
code_exercise_27k
3,305
Write a Python function to remove the first and last elements from each list in a given list of lists and return a new list of modified lists. If a list has fewer than three elements, it should be replaced with an empty list in the result. The code should start with: ```python from typing import List def remove_first...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_first_and_last_elements(list_of_lists: List[List[int]]) -> List[List[int]]: """ Remove the first and last elements from each list in a given list of lists. If a list has fewer than three ...
code_exercise_27k
3,308
Write a Python function to calculate the sum of the divisors of a given number efficiently. The function should handle both small and large numbers, and it should be optimized to avoid unnecessary computations. The code should start with: ```python def sum_of_divisors_efficient(num: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_divisors_efficient(num: int) -> int: """ Calculate the sum of the divisors of a given number efficiently. Args: num (int): The number for which the sum of divisors is to be calculated. Returns: ...
code_exercise_27k
3,312
Write a Python function to flatten a given nested list structure and return it as a flat list. The function should handle lists nested at any level and should not use any external libraries. The code should start with: ```python from typing import List, Any def flatten_nested_list(nested_list: List[Any]) -> List[Any]:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def flatten_nested_list(nested_list: List[Any]) -> List[Any]: """ Recursively flattens a given nested list structure and returns it as a flat list. Args: nested_list (List[Any]): A nest...
code_exercise_27k
3,315
Write a Python function to rotate the elements of a given list to the right by a specified 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 code should start with: ```python from typing import List def rotate_list(...
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: """ Rotates the elements of a given list to the right by a specified number of positions. Args: lst (List): The list to be rotated. ...
code_exercise_27k
3,321
Write a Python function to split a given list into two lists at a specified position and return a new list with those two lists. The function should handle edge cases such as an empty list, a position that is out of bounds, and a position that is exactly at the start or end of the list. The code should start with: ```p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def split_list_at_position(input_list: List[Any], position: int) -> List[List[Any]]: """ Split a given list into two lists at a specified position and return a new list with those two lists. Ar...
code_exercise_27k
3,325
Write a Python function to find the difference between corresponding elements of two given lists and return a new list with the results. The function should handle lists of different lengths by padding the shorter list with zeros. The code should start with: ```python from typing import List def find_difference_with_p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_difference_with_padding(list1: List[int], list2: List[int]) -> List[int]: """ Calculate the difference between corresponding elements of two given lists and return a new list with the results. ...
code_exercise_27k
3,327
Write a Python function to merge a list of dictionaries into a single dictionary. If there are duplicate keys, the value from the last dictionary in the list should overwrite the previous values. The function should raise a `TypeError` if the input is not a list of dictionaries. 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, Dict def merge_dicts(list_of_dicts: List[Dict]) -> Dict: """ Merge a list of dictionaries into a single dictionary. Args: list_of_dicts (List[Dict]): A list of dictionaries to be merged. ...
code_exercise_27k
3,328
Write a Python function to calculate the sum of the digits of a given number using recursion. The function should not use any loops or built-in functions that directly solve the problem. The function should handle positive integers only and raise a `ValueError` for non-positive integers. The code should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_digits(number: int) -> int: """ Calculates the sum of the digits of a given number using recursion. Args: number (int): The number for which the sum of its digits needs to be found. Returns: ...
code_exercise_27k
3,330
Write a Python function to count the number of occurrences of each element in a given list and return a dictionary with those counts. The function should handle lists containing any hashable elements, such as integers, strings, and tuples. The code should start with: ```python from typing import List, Dict, Any def co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Any def count_element_occurrences(lst: List[Any]) -> Dict[Any, int]: """ Count the number of occurrences of each element in a given list. Args: lst (List[Any]): The list of elements t...
code_exercise_27k
3,335
Write a Python function to remove empty strings from a given list of strings and return a new list without empty strings. The function should handle lists containing various types of strings, including empty strings, whitespace-only strings, and non-empty strings. The function should also handle edge cases such as an e...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_empty_strings(strings: List[str]) -> List[str]: """ Removes empty strings and strings containing only whitespace from a given list of strings. Args: strings (List[str]): A list of...
code_exercise_27k
3,339
Write a Python function to generate all possible permutations of elements in a given list. The function should return a list of lists, where each inner list represents a unique permutation of the original list. The code should start with: ```python from typing import List def generate_permutations(lst: List) -> List[L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_permutations(lst: List) -> List[List]: """ Generate all possible permutations of elements in a given list. Args: lst (List): The list of elements to generate permutations from. ...
code_exercise_27k
3,349
Write a Python function to move all zeroes to the end of a given list while preserving the order of other elements. The function should modify the list in place and return the modified list. The code should start with: ```python from typing import List def move_zeros_to_end(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 move_zeros_to_end(lst: List[int]) -> List[int]: """ Move all zeroes to the end of a given list while preserving the order of other elements. Args: lst (List[int]): The input list con...
code_exercise_27k
3,361
Write a Python function to check if a given set is a proper subset of multiple sets. A proper subset is a subset that is not equal to the set itself. The function should return `True` if the given set is a proper subset of all the sets in the list, and `False` otherwise. The code should start with: ```python from typi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Set def is_proper_subset(main_set: Set[int], other_sets: List[Set[int]]) -> bool: """ Checks if a set is a proper subset of multiple sets. Args: main_set (Set[int]): The set to check if it ...
code_exercise_27k
3,363
Write a Python function to find the top N elements in a given list based on their frequency and return a new list with those elements. If there are ties in frequency, the elements should be returned in the order they first appear in the input list. The function should handle cases where `n` is greater than the number o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from collections import Counter def find_top_n_elements(lst: List, n: int) -> List: """ Find the top N elements in a given list based on their frequency and return a new list with those elements. Ar...
code_exercise_27k
3,365
Write a Python function to check if two numbers are not equal. The function should handle both integers and floating-point numbers. The code should start with: ```python def check_if_not_equal(num1: float, num2: float) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def check_if_not_equal(num1: float, num2: float) -> bool: """ Check if two numbers are not equal. Args: num1 (float): The first number. num2 (float): The second number. Returns: bool: True if th...
code_exercise_27k
3,367
Write a Python function to calculate the cumulative sum of numbers in a given list and return a new list with those sums. The function should handle lists of integers and floats, and should return a list of the same type as the input list. The code should start with: ```python from typing import List, Union def cumula...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def cumulative_sum(numbers: List[Union[int, float]]) -> List[Union[int, float]]: """ Calculate the cumulative sum of numbers in a given list. Args: numbers (List[Union[int, float]]): ...
code_exercise_27k
3,370
Write a Python function to find the maximum product of two distinct elements from a given list of positive integers. The function should handle lists with at least two elements and should return the maximum product efficiently. The code should start with: ```python from typing import List def max_product_of_two(lst: L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_product_of_two(lst: List[int]) -> int: """ Finds the maximum product of two distinct elements from a given list of positive integers. Args: lst (List[int]): A list of positive intege...
code_exercise_27k
3,371
Write a Python function to find the elements present in a main set but not in any of the other provided sets. The function should take a main set and a variable number of other sets as arguments and return a set containing the elements that are unique to the main set. The code should start with: ```python def find_uni...
Here is the Python code to solve the given problem in the markdown block: ```python def find_unique_elements(main_set: set, *other_sets: set) -> set: """ Finds the elements present in the main_set but not in any of the other_sets. Args: main_set (set): The set to compare against. *other_set...
code_exercise_27k
3,374
Write a Python function to find the second largest delimiter in a given string. The function should return the second largest delimiter based on the frequency of its occurrence in the string. If there is no second largest delimiter, the function should return `None`. The function should handle an empty string by raisin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_second_largest_delimiter(string: str, delimiters: List[str]) -> Optional[str]: """ Find the second largest delimiter in a given string based on its frequency of occurrence. Args: ...
code_exercise_27k
3,377
Write a Python function to calculate the sum of all the elements in a given list, but with a twist: the function should also handle nested lists, i.e., lists that contain other lists as elements. The function should recursively sum all the elements, regardless of their nesting level. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def recursive_sum(numbers: List[Union[int, List]]) -> int: """ Calculate the sum of all elements in a given list, including elements in nested lists. Args: numbers (list): A list of i...
code_exercise_27k
3,380
Write a Python function to find the common elements present in all sets within a given list of sets. The function should return a set containing these common elements. If the input list of sets is empty, the function should raise a `ValueError`. The code should start with: ```python from typing import List def find_c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(sets: List[set]) -> set: """ Find the elements present in all sets within the given list of sets. Args: sets (List[set]): A list of sets. Returns: set: ...
code_exercise_27k
3,381
Write a Python function to find the maximum number in a given list of numbers. If the list is empty, raise a `ValueError` with a specific message. The function should be efficient and handle various types of input lists, including lists with negative numbers, duplicates, and a single element. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_maximum_number(numbers: List[int]) -> int: """ Finds the maximum number in a list of integers. Args: numbers (List[int]): A list of integers. Returns: int: The maximum ...
code_exercise_27k
3,385
Write a Python function to find the maximum number in a list. The function should handle both integers and floating-point numbers. If the list is empty, the function should raise a `ValueError` with the message "List cannot be empty." The code should start with: ```python from typing import List, Union def find_maximu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_maximum(numbers: List[Union[int, float]]) -> Union[int, float]: """ Finds the maximum number in a list of integers and/or floating-point numbers. Args: numbers (List[Union[in...
code_exercise_27k
3,388
Write a Python function to find the average of all the elements in a given list of numbers. If the list is empty, the function should return `None`. The function should handle lists containing integers and floating-point numbers. The code should start with: ```python from typing import List, Optional def find_average(...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_average(lst: List[float]) -> Optional[float]: """ Calculate the average of all the elements in a given list of numbers. Args: lst (List[float]): The list of numbers for wh...
code_exercise_27k
3,395
Write a Python function to determine if a given number is a prime number. The function should return `True` if the number is prime and `False` otherwise. The function should handle edge cases such as negative numbers, 0, and 1, which are not prime numbers. The function should also be efficient by checking divisibility ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Determine if a given number is a prime number. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. """ ...
code_exercise_27k
3,397
Write a Python function to check if a list contains any duplicate elements. The function should return `True` if there are duplicates and `False` otherwise. The function should be efficient and should handle lists of various types of elements, including integers, strings, and other objects. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def has_duplicates(lst: List[Any]) -> bool: """ Checks if a list contains any duplicate elements. Args: lst (List[Any]): The input list to check for duplicates. Returns: bo...
code_exercise_27k
3,404
Write a Python function to reverse a list in-place without using any additional data structures. The function should modify the original list and return `None`. The code should start with: ```python from typing import List, Any def reverse_list_in_place(lst: List[Any]) -> None: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def reverse_list_in_place(lst: List[Any]) -> None: """ Reverse a list in-place. This function takes a list as input and reverses its elements in-place without using any additional data structur...
code_exercise_27k
3,407
Write a Python function to sort a list of integers in ascending order using the merge sort algorithm. The merge sort algorithm is a divide-and-conquer algorithm that recursively divides the list into halves until each sublist contains a single element, then merges these sublists back together in sorted order. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(lst: List[int]) -> List[int]: """ Sorts a given list of integers in ascending order using the merge sort algorithm. Args: lst (List[int]): The list of integers to be sorted. ...
code_exercise_27k
3,409
Write a Python function to sort a list in descending order using a custom sorting algorithm. The function should implement the QuickSort algorithm to achieve this. The code should start with: ```python from typing import List def sort_list_descending(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 sort_list_descending(lst: List[int]) -> List[int]: """ Sorts a given list in descending order using the QuickSort algorithm. Args: lst (List[int]): The list to be sorted. Returns: ...
code_exercise_27k
3,413
Write a Python function to remove duplicates and sort a list in ascending order. The function should take a list of integers as input and return a new list with duplicate elements removed and sorted in ascending order. The code should start with: ```python from typing import List def remove_duplicates_and_sort(lst: Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_sort(lst: List[int]) -> List[int]: """ Removes duplicates and sorts a list of integers in ascending order. Args: lst (List[int]): The input list of integers. R...
code_exercise_27k
3,420
Write a Python function to split a given list of integers into two separate lists: one containing all even numbers and the other containing all odd numbers. The function should return a tuple with the two lists. The code should start with: ```python from typing import List, Tuple def split_list_into_even_odd(lst: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def split_list_into_even_odd(lst: List[int]) -> Tuple[List[int], List[int]]: """ Splits a given list of integers into two separate lists: one containing all even numbers and the other containing a...
code_exercise_27k
3,422
Write a Python function to count the number of elements in a list that satisfy a given condition. The condition should be defined as a lambda function. The function should return the count of elements that meet the condition. The code should start with: ```python from typing import List, Callable def count_elements_w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Callable def count_elements_with_condition(lst: List, condition: Callable[[any], bool]) -> int: """ Count the number of elements in a list that satisfy a given condition. Args: lst (list): ...
code_exercise_27k
3,425
Write a Python function to check if a given list of integers is a palindrome. A list is considered a palindrome if it reads the same backward as forward. The function should return `True` if the list is a palindrome and `False` otherwise. The code should start with: ```python from typing import List def is_palindrome_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_palindrome_list(lst: List[int]) -> bool: """ Check if a list of integers is a palindrome. Args: lst (List[int]): The list of integers to check. Returns: bool: True if the...
code_exercise_27k
3,426
Write a Python function to normalize a string by removing excess delimiters and any leading/trailing whitespace from each element. The function should also handle multiple consecutive delimiters by treating them as a single delimiter. The code should start with: ```python def normalize_string(string: str, delimiter: st...
Here is the Python code to solve the given problem in the markdown block: ```python def normalize_string(string: str, delimiter: str = ',') -> str: """ Normalizes a string by removing excess delimiters and any leading/trailing whitespace from each element. Args: string (str): The input string to be...
code_exercise_27k
3,427
Write a Python function to check if a given string ends with any of the characters in a specified set of characters. The function should return a boolean indicating whether the string ends with any of the characters in the set. The code should start with: ```python def check_string_ends_with_any(string: str, char_set:...
Here is the Python code to solve the given problem in the markdown block: ```python def check_string_ends_with_any(string: str, char_set: set) -> bool: """ Check if a string ends with any of the characters in a specified set. Args: string (str): The input string to check. char_set (set): A ...
code_exercise_27k
3,429
Write a Python function to remove all occurrences of a specific element from a given list. The function should return a new list with all occurrences of the specified element removed. The code should start with: ```python from typing import List, Any def remove_all_occurrences(lst: List[Any], element: Any) -> List[Any...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def remove_all_occurrences(lst: List[Any], element: Any) -> List[Any]: """ Remove all occurrences of a specific element from a given list. Args: lst (List[Any]): The list from which ele...
code_exercise_27k
3,431
Write a Python function to concatenate two lists and return the result. The function should handle lists of any type, including nested lists, and should not modify the original lists. The code should start with: ```python from typing import List, Any def concatenate_lists(list1: List[Any], list2: List[Any]) -> List[An...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def concatenate_lists(list1: List[Any], list2: List[Any]) -> List[Any]: """ Concatenates two lists and returns the result. Args: list1 (List[Any]): The first list to concatenate. ...
code_exercise_27k
3,435
Write a Python function to merge two sorted lists into one sorted list. The function should efficiently combine the elements from both lists while maintaining the sorted order. The code should start with: ```python from typing import List def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]: """ Merge two sorted lists into one sorted list. Args: list1 (List[int]): The first sorted list. list2 (List[...
code_exercise_27k
3,440
Write a Python function to find the second-largest number in a given list of integers. The function should handle lists with at least two elements and raise a `ValueError` if the list does not meet this requirement. The code should start with: ```python from typing import List def find_second_largest(numbers: List[int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_second_largest(numbers: List[int]) -> int: """ Find the second-largest number in a list of integers. Args: numbers (List[int]): A list of integers. Returns: int: The se...
code_exercise_27k
3,444
Write a Python function to check if one set is a subset of another set. The function should return `True` if all elements of the first set are present in the second set, and `False` otherwise. The function should handle sets of any size and type of elements. The code should start with: ```python def is_subset(set_1: s...
Here is the Python code to solve the given problem in the markdown block: ```python def is_subset(set_1: set, set_2: set) -> bool: """ Check if set_1 is a subset of set_2. Args: set_1 (set): The first set to check. set_2 (set): The second set that set_1 is being compared to. Returns: ...
code_exercise_27k
3,451
Write a Python function to check if a given string contains both uppercase and lowercase letters. The function should return `True` if the string contains at least one uppercase letter and at least one lowercase letter, and `False` otherwise. The code should start with: ```python def contains_both_cases(string: str) ->...
Here is the Python code to solve the given problem in the markdown block: ```python def contains_both_cases(string: str) -> bool: """ Check if a given string contains both uppercase and lowercase letters. Args: string (str): The input string to be checked. Returns: bool: True if the st...
code_exercise_27k
3,452
Write a Python function to find the intersection of two sets, ensuring that the function handles cases where one or both sets are empty. The function should return a new set containing the elements that are common to both sets. The code should start with: ```python def get_intersection(set1: set, set2: set) -> set: ```...
Here is the Python code to solve the given problem in the markdown block: ```python def get_intersection(set1: set, set2: set) -> set: """ Return the intersection of two sets. Args: set1 (set): The first input set. set2 (set): The second input set. Returns: set: A new set conta...
code_exercise_27k
3,454
Write a Python function to check if a given string contains only digits. The function should return `True` if the string consists entirely of digits, and `False` otherwise. The code should start with: ```python def is_string_only_digits(input_string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_string_only_digits(input_string: str) -> bool: """ Checks if a given string contains only digits. Args: input_string (str): The string to be checked. Returns: bool: True if the string contains on...
code_exercise_27k
3,455
Write a Python function to check if one list is a subset of another list. The function should return `True` if all elements of the first list are present in the second list, and `False` otherwise. The function should handle lists with any type of elements, including duplicates. 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 List, Any def is_subset(list1: List[Any], list2: List[Any]) -> bool: """ Checks if all elements of list1 are present in list2. Args: list1 (List[Any]): The first list to check if it is a subset. ...
code_exercise_27k
3,456
Write a Python function to find the symmetric difference between two sets. The symmetric difference of two sets is defined as the set of elements which are in either of the sets but not in both. The function should return a new set containing the symmetric difference. The code should start with: ```python def symmetri...
Here is the Python code to solve the given problem in the markdown block: ```python def symmetric_difference(set1: set, set2: set) -> set: """ Finds the symmetric difference between two sets. The symmetric difference of two sets is the set of elements which are in either of the sets but not in both. A...
code_exercise_27k
3,460
Write a Python function to determine if two sets are disjoint (have no common elements). The function should return `True` if the sets are disjoint and `False` otherwise. The code should start with: ```python def are_disjoint(set1: set, set2: set) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def are_disjoint(set1: set, set2: set) -> bool: """ Determines if two sets are disjoint (have no common elements). Args: set1 (set): The first set. set2 (set): The second set. Returns: bool: Tru...
code_exercise_27k
3,464
Write a Python function to remove a specific element from a given set. If the element is not found in the set, the function should return the original set without any changes. The function should not raise any exceptions. The code should start with: ```python def remove_element_from_set(s: set, element) -> set: ```
Here is the Python code to solve the given problem in the markdown block: ```python def remove_element_from_set(s: set, element) -> set: """ Remove a specific element from a given set. If the element is not found, the set remains unchanged. Args: s (set): The input set from which the element is to ...
code_exercise_27k
3,466
Write a Python function to determine if a given year is a leap year but not a multiple of 100. A leap year is defined as a year that is divisible by 4, except for years that are divisible by 100 but not by 400. The function should return `True` if the year is a leap year and not a multiple of 100, and `False` otherwise...
Here is the Python code to solve the given problem in the markdown block: ```python def is_leap_year_but_not_hundred(year: int) -> bool: """ Determine if a given year is a leap year but not a multiple of 100. Args: year (int): The year to check. Returns: bool: True if the year is a lea...
code_exercise_27k
3,468
Write a Python function to find the symmetric difference between two lists. The symmetric difference between two lists is defined as the set of elements which are in either of the lists, but not in both. The function should return the symmetric difference as a list, with elements in ascending order. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def symmetric_difference(list1: List[int], list2: List[int]) -> List[int]: """ Finds the symmetric difference between two lists. The symmetric difference is defined as the set of elements which are ...
code_exercise_27k
3,474
Write a Python function to count the number of elements in a given set that are divisible by a specified divisor. The function should be flexible enough to handle any positive integer divisor. The code should start with: ```python def count_divisible_elements(input_set: set, divisor: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_divisible_elements(input_set: set, divisor: int) -> int: """ Count the number of elements in a set that are divisible by a specified divisor. Args: input_set (set): The set containing the elements to be ch...
code_exercise_27k
3,475
Write a Python function to check if a given string contains both uppercase and lowercase letters. The function should return `True` if the string contains at least one uppercase letter and at least one lowercase letter, and `False` otherwise. The code should start with: ```python def has_upper_and_lower(s: str) -> bool...
Here is the Python code to solve the given problem in the markdown block: ```python def has_upper_and_lower(s: str) -> bool: """ Check if a string contains both uppercase and lowercase letters. Args: s (str): The string to be checked. Returns: bool: True if the string contains both upp...