source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 4,122 | Write a Python function to find the difference between two dictionaries. The function should return a new dictionary that contains the differences between the two input dictionaries. The difference is defined as follows:
- If a key exists in one dictionary but not in the other, the difference should include that key wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def find_dict_difference(dict1: Dict[Any, Any], dict2: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Finds the difference between two dictionaries.
Args:
dict1 (Dict[Any, Any]): The first dict... |
code_exercise_27k | 4,123 | Write a Python function to find the maximum value from a given set of numbers. The function should handle the case where the set is empty by raising a `ValueError`. The code should start with:
```python
def find_max(numbers: set) -> int or float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_max(numbers: set) -> int or float:
"""
Find the maximum value from a set of numbers.
Args:
numbers (set): A set of numbers.
Returns:
int or float: The maximum value from the set of numbers.
... |
code_exercise_27k | 4,125 | Write a Python function to find the intersection of two dictionaries. The intersection of two dictionaries is defined as a new dictionary that contains only the key-value pairs that are present in both dictionaries with the same values. The function should handle cases where the values in the dictionaries are not of th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def find_intersection(d1: Dict, d2: Dict) -> Dict:
"""
Returns the intersection of two dictionaries based on keys and values.
The intersection of two dictionaries is a new dictionary that contains o... |
code_exercise_27k | 4,126 | Write a Python function to find the intersection of two lists using a dictionary. The function should return a new list containing the intersection of the two input lists, ensuring that each element in the intersection appears only once. The code should start with:
```python
from typing import List
def find_intersecti... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_dict(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the intersection of two lists using a dictionary.
Args:
list1 (List[int]): The first list.
... |
code_exercise_27k | 4,127 | Write a Python function to perform a bitwise left shift on a given integer `x` by a specified number of positions `n` and return the result. The function should handle cases where `n` is negative by performing a bitwise right shift instead. The function should also handle cases where `n` is zero by returning the origin... | Here is the Python code to solve the given problem in the markdown block:
```python
def bitwise_shift(x: int, n: int) -> int:
"""
Performs a bitwise shift on a given integer 'x' by a specified number of positions 'n'.
If 'n' is positive, performs a bitwise left shift.
If 'n' is negative, performs a bitw... |
code_exercise_27k | 4,129 | Write a Python function to extract a substring from a given string `text` starting from a specified index `start_index` to the end of the string. The function should handle cases where `start_index` is out of bounds by returning an empty string if `start_index` is greater than the length of the string, or the entire st... | Here is the Python code to solve the given problem in the markdown block:
```python
def extract_substring(text: str, start_index: int) -> str:
"""
Extracts a substring from the given string 'text' starting from the specified index 'start_index' to the end of the string.
Args:
text (str): The input ... |
code_exercise_27k | 4,131 | Write a Python function to find the union of two dictionaries. The union of two dictionaries is a new dictionary that contains all the key-value pairs from both dictionaries. If a key exists in both dictionaries, the value from the second dictionary should overwrite the value from the first dictionary.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def dict_union(dict1: Dict, dict2: Dict) -> Dict:
"""
Combine two dictionaries into a single dictionary by taking the union.
Parameters:
dict1 (Dict): The first dictionary.
dict2 (Dict): The... |
code_exercise_27k | 4,132 | Write a Python function to determine if a given 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(... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_subset(subset: set, superset: set) -> bool:
"""
Determines if a given set is a subset of another set.
Args:
subset (set): The set to be checked if it is a subset.
superset (set): The set to be checked... |
code_exercise_27k | 4,136 | Write a Python function to remove items from set A that are not common to both set A and set B. The function should return the modified set A. The code should start with:
```python
def remove_non_common_items(set_a: set, set_b: set) -> set:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_non_common_items(set_a: set, set_b: set) -> set:
"""
Remove items from set A that are not common to both set A and set B.
Args:
set_a (set): The first set from which to remove non-common items.
se... |
code_exercise_27k | 4,137 | Write a Python function to update the values of three variables based on the following conditions:
1. If the value of `x` is greater than or equal to 10, assign the value of `y` to `x`.
2. If the value of `y` is less than 20, assign the value of `z` to `y`.
The function should return a tuple containing the updated val... | Here is the Python code to solve the given problem in the markdown block:
```python
def update_values(x: int, y: int, z: int) -> tuple:
"""
Update the values of x, y, and z based on the following conditions:
1. If x is greater than or equal to 10, assign the value of y to x.
2. If y is less than 20, ass... |
code_exercise_27k | 4,138 | Write a Python function to find the elements that appear more than once in a given list. The function should return a set containing the duplicate elements. The code should start with:
```python
from typing import List, Set
def find_duplicates_in_list(lst: List[int]) -> Set[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Set
def find_duplicates_in_list(lst: List[int]) -> Set[int]:
"""
Find the elements that appear more than once in a given list.
Args:
lst (List[int]): The input list of integers.
Return... |
code_exercise_27k | 4,139 | Write a Python function to find the intersection of two lists while excluding certain elements. The function should take two lists and an optional list of elements to exclude. The function should return the intersection of the two lists after removing the specified elements from both lists.
The code should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_intersection_excluding(list1: List[int], list2: List[int], exclude_elements: Optional[List[int]] = None) -> List[int]:
"""
Finds the intersection of two lists while excluding certain e... |
code_exercise_27k | 4,141 | Write a Python function to generate a list of the first `n` prime numbers. The function should use a helper function to check if a number is prime. The code should start with:
```python
from typing import List
def generate_prime_numbers(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to be checked.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
code_exercise_27k | 4,142 | Write a Python function to perform a logical NOT operation on a given boolean variable and return the result. The function should handle both `True` and `False` values and return the opposite boolean value.
The code should start with:
```python
def logical_not(flag: bool) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def logical_not(flag: bool) -> bool:
"""
Performs a logical NOT operation on the given boolean variable and returns the result.
Args:
flag (bool): The boolean variable to which the NOT operation is applied.
Ret... |
code_exercise_27k | 4,143 | Write a Python function to find the intersection of two lists, considering the order and count of elements. The function should return a list containing elements that are present in both lists, maintaining the order of their first appearance in the first list and considering the count of each element.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_order_and_count(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the intersection of two lists, considering the order and count of elements.
Args:
list1 ... |
code_exercise_27k | 4,144 | Write a Python function to check if two sets have at least one element in common. The function should return `True` if there is at least one common element between the two sets, and `False` otherwise. The function should also handle cases where the input is not a set by raising a `TypeError`.
The code should start wit... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_common_element(set1: set, set2: set) -> bool:
"""
Check if two sets have at least one element in common.
Args:
set1 (set): The first set to compare.
set2 (set): The second set to compare.
Retu... |
code_exercise_27k | 4,146 | Write a Python function to extract a substring from a given string based on specified start and end indices. The function should handle cases where the start or end index is out of bounds by adjusting them to the nearest valid index. The function should also handle cases where the start index is greater than the end in... | Here is the Python code to solve the given problem in the markdown block:
```python
def extract_substring(text: str, start: int, end: int) -> str:
"""
Extracts a substring from the given string 'text' based on the specified start and end indices.
Args:
text (str): The original string from which to ... |
code_exercise_27k | 4,149 | Write a Python function to find the common keys between two dictionaries. The function should return a set of keys that are present in both dictionaries. The code should start with:
```python
def find_common_keys(dict1: dict, dict2: dict) -> set:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_common_keys(dict1: dict, dict2: dict) -> set:
"""
Find the common keys between two dictionaries.
Args:
dict1 (dict): The first dictionary.
dict2 (dict): The second dictionary.
Returns:
... |
code_exercise_27k | 4,151 | Write a Python function to find the intersection of two lists, excluding duplicates from the second list. The function should return a new list containing elements that are present in both `list1` and `list2`, but only once per element from `list2`. The code should start with:
```python
from typing import List
def int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def intersection_exclude_duplicates(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the intersection of two lists, excluding duplicates from the second list.
Args:
list1 (List[int... |
code_exercise_27k | 4,156 | Write a Python function to sort a dictionary by its values in descending order. The function should return a new dictionary with the same keys as the original dictionary, but with the values sorted in descending order. The code should start with:
```python
from typing import Dict
def sort_dict_by_values(dictionary: Di... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_dict_by_values(dictionary: Dict[str, int]) -> Dict[str, int]:
"""
Sorts a dictionary by its values in descending order.
Args:
dictionary (Dict[str, int]): The dictionary to be sorte... |
code_exercise_27k | 4,157 | Write a Python function to round a given floating-point number to the nearest integer. The function should handle both positive and negative numbers correctly. The code should start with:
```python
def round_to_nearest_integer(number: float) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def round_to_nearest_integer(number: float) -> int:
"""
Rounds a given floating-point number to the nearest integer.
Args:
number (float): The floating-point number to round.
Returns:
int: The rounded i... |
code_exercise_27k | 4,160 | Write a Python function to calculate the sum of two given floating-point numbers and handle potential floating-point precision issues. The function should return the sum as a string formatted to two decimal places.
The code should start with:
```python
def calculate_sum_with_precision(num1: float, num2: float) -> str:... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_sum_with_precision(num1: float, num2: float) -> str:
"""
Calculate the sum of two given floating-point numbers and return the result formatted to two decimal places.
Parameters:
num1 (float): The first flo... |
code_exercise_27k | 4,162 | Write a Python function to find the common elements between two lists, ignoring the order of elements. The function should return a list of common elements in ascending order. The code should start with:
```python
from typing import List
def find_common_elements(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 find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Returns a list of common elements between two input lists, sorted in ascending order.
Args:
list1 (List[int]): ... |
code_exercise_27k | 4,165 | Write a Python function to find the largest common prefix of a given list of strings. The function should return an empty string if there is no common prefix. The function should handle an empty input list by raising a `ValueError`.
The code should start with:
```python
from typing import List
def find_largest_common... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_largest_common_prefix(strings: List[str]) -> str:
"""
Find the largest common prefix of a set of strings.
Args:
strings (List[str]): A list of strings.
Returns:
str: Th... |
code_exercise_27k | 4,172 | Write a Python function to convert a nested dictionary into a flat dictionary. The flat dictionary should have keys that are concatenated from the nested keys using a specified separator. The function should handle any level of nested dictionaries.
The code should start with:
```python
from typing import Dict
def fla... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def flatten_nested_dict(nested_dict: Dict, parent_key: str = '', sep: str = '_') -> Dict:
"""
Recursively flattens a nested dictionary into a flat dictionary.
Args:
nested_dict (Dict): The n... |
code_exercise_27k | 4,173 | Write a Python function to find the maximum of three given floating-point numbers. The function should handle the input validation to ensure that all inputs are floating-point numbers. If any input is not a floating-point number, the function should raise a `TypeError`.
The code should start with:
```python
def find_m... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_maximum_of_three(a: float, b: float, c: float) -> float:
"""
Finds the maximum of three given floating-point numbers.
Args:
a (float): The first floating-point number.
b (float): The second floating... |
code_exercise_27k | 4,179 | Write a Python function to find the intersection of two lists, preserving the order of the first list. The function should return a new list containing the elements that are common to both input lists, in the same order as they appear in the first list. 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 intersection_preserve_order(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the intersection of two lists, preserving the order of the first list.
Args:
list1 (List[int]): The... |
code_exercise_27k | 4,184 | Write a Python function to find the median value in a given dictionary. The dictionary contains key-value pairs, and the function should return the median of the values. If the dictionary is empty, the function should raise a `ValueError`.
The code should start with:
```python
from typing import Dict, Union
def find_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Union
def find_median_value(dictionary: Dict[any, Union[int, float]]) -> Union[int, float]:
"""
Find the median value in a dictionary.
Parameters:
dictionary (dict): A dictionary containing key... |
code_exercise_27k | 4,185 | Write a Python function to calculate the percentage of a given number. The function should handle both positive and negative percentages and return the absolute value of the calculated percentage. The function should also raise a `ValueError` if the number is negative. The code should start with:
```python
def calculat... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_percentage(number: float, percentage: float) -> float:
"""
Calculates the absolute value of the percentage of a given number.
Args:
number (float): The number for which the percentage needs to be calcu... |
code_exercise_27k | 4,190 | Write a Python function to find the k smallest elements in a given set efficiently. The function should return a list of the k smallest elements in ascending order. If k is greater than the size of the set, the function should return all elements of the set in ascending order. The function should handle the case where ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set, List
def find_k_smallest_elements(input_set: Set[int], k: int) -> List[int]:
"""
Find the k smallest elements in a given set and return them in ascending order.
Args:
input_set (Set[int]): T... |
code_exercise_27k | 4,192 | Write a Python function to check if all values in a given dictionary are of the same type. The function should return `True` if all values are of the same type, and `False` otherwise. The function should handle dictionaries with any number of key-value pairs, including empty dictionaries.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_values_same_type(dictionary: dict) -> bool:
"""
Check if all values in a dictionary are of the same type.
Args:
dictionary (dict): The dictionary to check.
Returns:
bool: True if all values are ... |
code_exercise_27k | 4,196 | Write a Python function to find the keys with the highest value in a given dictionary. If there are multiple keys with the same highest value, the function should return all such keys. The function should handle an empty dictionary by raising a `ValueError`.
The code should start with:
```python
from typing import Dic... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def get_keys_with_highest_value(dictionary: Dict[str, int]) -> List[str]:
"""
Find the keys with the highest value in a given dictionary.
Args:
dictionary (Dict[str, int]): A dictionar... |
code_exercise_27k | 4,197 | Write a Python function to find the intersection of two lists while excluding elements from the second list based on specific properties. The function should take three parameters: the first list, the second list, and a callable that determines which elements from the second list should be excluded. The function should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Callable
def find_intersection_with_exclusion(list1: List, list2: List, properties: Callable) -> set:
"""
Finds the intersection of two lists while excluding elements from the second list based on speci... |
code_exercise_27k | 4,198 | Write a Python function to filter elements from a set based on a given condition. The function should take a set and a condition function as input and return a new set containing only the elements that do not satisfy the condition. The code should start with:
```python
def filter_elements(input_set: set, condition) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
def filter_elements(input_set: set, condition) -> set:
"""
Filters elements from a set based on a given condition.
Args:
input_set (set): The set from which elements should be filtered.
condition (function):... |
code_exercise_27k | 4,201 | Write a Python function to round a given floating-point number to a specified number of decimal places. The function should handle both positive and negative floating-point numbers and ensure that the number of decimal places is a non-negative integer. The function should also raise appropriate `ValueError` exceptions ... | Here is the Python code to solve the given problem in the markdown block:
```python
def round_number(number: float, decimal_places: int) -> float:
"""
Rounds a given floating-point number to a specified number of decimal places.
Args:
number (float): The number to be rounded.
decimal_places... |
code_exercise_27k | 4,203 | Write a Python function to find the intersection of two lists, considering only the elements at odd indices. The function should return a list containing the intersection of the odd-indexed elements from both input lists. The order of the elements in the result list should be the same as their appearance in the first i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_odd_index_intersection(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the intersection of two lists, considering elements at odd indices.
Args:
list1 (List[int]): The fi... |
code_exercise_27k | 4,206 | Write a Python function to find the intersection of two lists, considering elements greater than a given value. The function should return a new list containing the intersection of the two input lists, considering only the elements greater than the given value. 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, Union
def find_intersection_greater_than(list1: List[Union[int, float]], list2: List[Union[int, float]], value: Union[int, float]) -> List[Union[int, float]]:
"""
Find the intersection of two lists, con... |
code_exercise_27k | 4,211 | Write a Python function to find the sub-dictionary within a given dictionary that has the highest sum of its values. The input dictionary contains sub-dictionaries, each with numeric values. The function should return the sub-dictionary with the highest sum of its values.
The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def find_subdict_with_highest_sum(data: Dict[str, Dict[str, int]]) -> Dict[str, int]:
"""
Find the sub-dictionary with the highest sum of values.
Args:
data (Dict[str, Dict[str, int]]): A di... |
code_exercise_27k | 4,212 | Write a Python function to find the intersection of two lists, excluding elements from the first list that have duplicates in the second list. The function should return a list containing the elements that are present in the first list but not in the second list, ensuring that no duplicates from the second list are inc... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_exclusion(list1: List[int], list2: List[int]) -> List[int]:
"""
Finds the intersection of two lists, excluding elements from the first list that have duplicates in the second l... |
code_exercise_27k | 4,221 | Write a Python function to calculate the average of elements in a given set of numbers. The function should handle the case where the input set is empty by raising a `ValueError`. The code should start with:
```python
def calculate_average(s: set) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_average(s: set) -> float:
"""
Calculate the average of elements in a set.
Args:
s (set): The set of numbers.
Returns:
float: The average of elements in the set.
Raises:
ValueE... |
code_exercise_27k | 4,222 | Write a Python function to find the minimum of three given floating-point numbers without using the built-in `min` function. The function should compare the numbers manually and return the smallest one. The code should start with:
```python
def find_minimum_manual(a: float, b: float, c: float) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_minimum_manual(a: float, b: float, c: float) -> float:
"""
Find the minimum of three floating-point numbers without using the built-in min function.
Args:
a (float): The first floating-point number.
... |
code_exercise_27k | 4,223 | Write a Python function to find the key with the most consecutive occurrences in a given dictionary. The dictionary is assumed to have keys that are consecutive in nature, meaning that the keys are in a sequence without gaps. The function should return the key that has the highest number of consecutive occurrences. If ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def find_key_with_most_consecutive_occurrences(dictionary: Dict[Any, Any]) -> Any:
"""
Finds the key with the most consecutive occurrences in a dictionary.
Args:
dictionary (dict): The ... |
code_exercise_27k | 4,227 | Write a Python function to group the values of a dictionary by their keys. If a key appears multiple times in the dictionary, its values should be grouped into a list. The function should handle cases where the dictionary contains duplicate keys, ensuring that all values associated with a key are collected into a singl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def group_values_by_key(dictionary: Dict[Any, Any]) -> Dict[Any, list]:
"""
Group the values of a dictionary by their keys.
Parameters:
dictionary (dict): The input dictionary to be grouped... |
code_exercise_27k | 4,232 | Write a Python function to find the element that occurs the maximum number of times in a given list. If there are multiple elements with the same maximum frequency, return the first one encountered. The function should handle lists of any data type.
The code should start with:
```python
from typing import List, Any
d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def find_most_frequent_element(lst: List[Any]) -> Any:
"""
Find the element that occurs the maximum number of times in a given list.
Args:
lst (List[Any]): The input list.
Returns:... |
code_exercise_27k | 4,234 | Write a Python function to check if a dictionary is a deep subset of another dictionary. A deep subset means that all keys in the subset dictionary must exist in the superset dictionary, and the corresponding values must either be equal or the value in the subset must be a deep subset of the corresponding value in the ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def is_deep_subset(subset: Dict, superset: Dict) -> bool:
"""
Check if a dictionary is a deep subset of another dictionary.
Args:
subset (Dict): The dictionary to be checked as a subset.
... |
code_exercise_27k | 4,236 | Write a Python function to determine if a given set is a proper superset of another set. A set `set1` is considered a proper superset of `set2` if `set1` contains all elements of `set2` and `set1` is not equal to `set2`.
The code should start with:
```python
def is_proper_superset(set1: set, set2: set) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_proper_superset(set1: set, set2: set) -> bool:
"""
Determine if `set1` is a proper superset of `set2`.
Args:
set1 (set): The first set to be checked for being a proper superset.
set2 (set): The second... |
code_exercise_27k | 4,237 | Write a Python function to calculate the power of a given number raised to an exponent using an efficient iterative approach. The function should handle both positive and negative exponents, but should raise a `ValueError` if the base is zero and the exponent is negative. The code should start with:
```python
def power... | Here is the Python code to solve the given problem in the markdown block:
```python
def power(base: float, exponent: int) -> float:
"""
Calculates the power of a given number raised to an exponent using an efficient iterative approach.
Args:
base (float): The base number.
exponent (int): Th... |
code_exercise_27k | 4,240 | Write a Python function to determine if one dictionary is a deep superset of another dictionary. A dictionary is considered a deep superset if it contains all the key-value pairs of the other dictionary, and if any value in the other dictionary is itself a dictionary, the corresponding value in the first dictionary mus... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_deep_superset(dict1: dict, dict2: dict) -> bool:
"""
Determine if one dictionary is a deep superset of another dictionary.
Args:
dict1 (dict): The first dictionary to be checked.
dict2 (dict): The sec... |
code_exercise_27k | 4,241 | Write a Python function to calculate the modulo of two given numbers, ensuring that the function handles edge cases such as division by zero. The function should return the result of the modulo operation or raise a `ZeroDivisionError` if the divisor is zero.
The code should start with:
```python
def calculate_modulo(a... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_modulo(a: int, b: int) -> int:
"""
Calculate the modulo of two given numbers.
This function takes in two integers, `a` and `b`, and returns the modulo of `a` divided by `b`.
The modulo operation calculates... |
code_exercise_27k | 4,243 | Write a Python function to convert a given decimal number to its binary representation. The function should handle both positive and zero decimal numbers. The code should start with:
```python
def decimal_to_binary(decimal: int) -> str:
```
| 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 convert. Must be a non-negative integer.
Returns:
str:... |
code_exercise_27k | 4,255 | Write a Python function to find the prime factors 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_factors(number: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def prime_factors(number: int) -> List[int]:
"""
Finds the prime factors of a given number.
Args:
number (int): The number to find prime factors for.
Returns:
List[int]: A list ... |
code_exercise_27k | 4,256 | Write a Python function to determine if one dictionary is a proper subset of another dictionary. A dictionary `dict1` is considered a proper subset of `dict2` if all keys in `dict1` are also in `dict2`, all values in `dict1` match the corresponding values in `dict2`, and `dict2` has at least one additional key that is ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_proper_subset(dict1: dict, dict2: dict) -> bool:
"""
Determine if dict1 is a proper subset of dict2.
Args:
dict1 (dict): The first dictionary to be checked.
dict2 (dict): The second dictionary to be c... |
code_exercise_27k | 4,258 | Write a Python function to find the smallest element in a given set that is greater than a specified value. If no such element exists, the function should return `None`. The function should handle sets containing integers or floats and should raise appropriate exceptions for invalid input types.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_smallest_greater_than(given_value: int | float, input_set: set[int | float]) -> int | float | None:
"""
Finds the smallest element greater than a given value in a set.
Args:
given_value (int | float): The v... |
code_exercise_27k | 4,260 | Write a Python function to find the intersection of two lists, considering only elements less than a given value. The function should return a set of common elements from both lists that are less than the specified value.
The code should start with:
```python
from typing import List, Set
def find_intersection_below_v... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Set
def find_intersection_below_value(list1: List[int], list2: List[int], value: int) -> Set[int]:
"""
Find the intersection of two lists, considering only elements less than a given value.
Args:
... |
code_exercise_27k | 4,261 | Write a Python function to determine if a dictionary is a proper superset of another dictionary. A dictionary `dict1` is considered a proper superset of `dict2` if `dict1` contains all the keys and corresponding values of `dict2`, and `dict1` has at least one key-value pair that `dict2` does not have.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_proper_superset(dict1: dict, dict2: dict) -> bool:
"""
Determine if dict1 is a proper superset of dict2.
A dictionary dict1 is a proper superset of dict2 if dict1 contains all the keys and corresponding values of dic... |
code_exercise_27k | 4,263 | Write a Python function to calculate the factorial of a given number using recursion. The function should handle non-negative integers only and raise a `ValueError` for negative inputs. The code should start with:
```python
def factorial(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculates the factorial of a given non-negative integer using recursion.
Args:
n (int): The number for which factorial needs to be calculated.
Returns:
int: The factor... |
code_exercise_27k | 4,267 | Write a Python function to find the intersection of two lists, excluding elements from the second list that have a specific property. The function should take two lists and a function as arguments. The function argument will be used to determine if an element from the second list should be excluded from the intersectio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Callable
def find_intersection_with_exclusion(list1: List, list2: List, exclude_property: Callable[[any], bool]) -> List:
"""
Find the intersection of two lists, excluding elements from the second list ... |
code_exercise_27k | 4,269 | Write a Python function to find the k-th smallest element in a given set of integers. If the set has fewer than k elements, the function should return `None`. The function should be efficient and handle edge cases appropriately.
The code should start with:
```python
from typing import Set, Optional
def find_kth_small... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set, Optional
def find_kth_smallest(numbers: Set[int], k: int) -> Optional[int]:
"""
Finds the k-th smallest element in a given set of integers.
Args:
numbers (Set[int]): A set of integers from w... |
code_exercise_27k | 4,272 | Write a Python function to check if a dictionary is a proper deep superset of another dictionary. A proper deep superset means that the first dictionary contains all the keys and values of the second dictionary, but is not equal to it. The function should handle nested dictionaries (dictionaries within dictionaries) an... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_proper_deep_superset(dict1: dict, dict2: dict) -> bool:
"""
Check if dict1 is a proper deep superset of dict2. A proper deep superset means
that dict1 is a superset of dict2, but not equal to dict2.
Args:
... |
code_exercise_27k | 4,277 | Write a Python function to calculate the sum of squares of elements in a given set. The function should handle sets of integers and return the sum of their squares. The code should start with:
```python
def sum_of_squares(numbers: set) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_squares(numbers: set) -> int:
"""
Calculate the sum of squares of elements in a set.
Args:
numbers (set): A set of integers.
Returns:
int: The sum of the squares of the elements in the set.
... |
code_exercise_27k | 4,278 | Write a Python function to find the intersection of two lists, considering elements ending with a specific substring. The function should return a list of elements that are present in both lists and end with the specified substring. The code should start with:
```python
from typing import List
def find_intersection_wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_substring(list1: List[str], list2: List[str], substring: str) -> List[str]:
"""
Find the intersection of two lists, considering elements ending with a specific substring.
... |
code_exercise_27k | 4,279 | Write a Python function to count the occurrences of each character in a given string, including spaces and punctuation. The function should return a dictionary where each key is a character from the input string and the corresponding value is the count of occurrences. The code should start with:
```python
from typing i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_characters(string: str) -> Dict[str, int]:
"""
Count the occurrences of each character in a given string.
Args:
string (str): The input string.
Returns:
Dict[str, int]... |
code_exercise_27k | 4,283 | Write a Python function to check if one set contains all elements from another set. The function should return `True` if all elements of the second set are present in the first set, and `False` otherwise. The function should handle sets of any size and type of elements.
The code should start with:
```python
def contai... | Here is the Python code to solve the given problem in the markdown block:
```python
def contains_all_elements(set1: set, set2: set) -> bool:
"""
Check if set1 contains all elements from set2.
Args:
set1 (set): The first set to be checked.
set2 (set): The second set containing elements to be... |
code_exercise_27k | 4,285 | Write a Python function to remove all occurrences of a specified key from a given dictionary. The function should return a new dictionary that does not contain the specified key. The original dictionary should remain unchanged.
The code should start with:
```python
def remove_key(dictionary: dict, key) -> dict:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_key(dictionary: dict, key) -> dict:
"""
Remove all occurrences of a specified key from a given dictionary.
Args:
dictionary (dict): The input dictionary from which the key is to be removed.
key: T... |
code_exercise_27k | 4,287 | Write a Python function to find the element that occurs the minimum number of times in a given list. If there are multiple elements that occur the same minimum number of times, the function should return the first one encountered in the list.
The code should start with:
```python
from typing import List
def find_mini... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_minimum_occurrences(data_list: List[int]) -> int:
"""
Finds the element that occurs the minimum number of times in a given list.
Args:
data_list (List[int]): The list in which the m... |
code_exercise_27k | 4,288 | 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 that contains no repeated characters.
The code should start with:
```python
def length_of_longest_su... | Here is the Python code to solve the given problem in the markdown block:
```python
def length_of_longest_substring(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 ... |
code_exercise_27k | 4,291 | Write a Python function to determine the relationship between two sets, specifically whether one set is a proper subset of the other, equal to the other, or neither. The function should return a string indicating the relationship.
The code should start with:
```python
def check_set_relationship(set_a: set, set_b: set)... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_set_relationship(set_a: set, set_b: set) -> str:
"""
Determine the relationship between two sets.
Args:
set_a (set): The first set to be checked.
set_b (set): The second set to be checked.
Ret... |
code_exercise_27k | 4,295 | Write a Python function to remove all leading and trailing whitespace characters from a given string. The function should handle various types of whitespace characters, including spaces, tabs, and newline characters. The code should start with:
```python
def remove_whitespace(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_whitespace(string: str) -> str:
"""
Removes all leading and trailing whitespace characters from the given string.
Args:
string (str): The input string from which leading and trailing whitespaces are to be... |
code_exercise_27k | 4,301 | Write a Python function to find the intersection of two lists, considering elements from the first list that appear more than once. The function should return a new list containing the intersection of the two input lists. The code should start with:
```python
from typing import List
def find_intersection_with_frequenc... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_frequency(list1: List[int], list2: List[int]) -> List[int]:
"""
Finds the intersection of two lists, considering elements from the first list that appear more than once.
A... |
code_exercise_27k | 4,303 | Write a Python function to implement the Run-Length Encoding (RLE) algorithm for compressing a given string. The function should take a string as input and return the compressed string using the RLE algorithm. The compressed string should consist of pairs of characters and their consecutive counts.
The code should sta... | Here is the Python code to solve the given problem in the markdown block:
```python
def run_length_encoding(string: str) -> str:
"""
Compresses a given string using the Run-Length Encoding (RLE) algorithm.
Args:
string (str): The input string to be compressed.
Returns:
str: The compres... |
code_exercise_27k | 4,309 | Write a Python function to check if a given set is a perfect superset of another set. A set `set1` is considered a perfect superset of `set2` if `set1` contains all elements of `set2` and has at least one additional element that is not in `set2`.
The function should start with:
```python
def is_perfect_superset(set1: ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_superset(set1: set, set2: set) -> bool:
"""
Check if one set is a perfect superset of another set.
Args:
set1 (set): The first set to be checked if it is a perfect superset.
set2 (set): The se... |
code_exercise_27k | 4,311 | Write a Python function to find the length of a dictionary using loop iteration. The function should iterate through the dictionary and count the number of key-value pairs. The code should start with:
```python
def find_dict_length(d: dict) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def find_dict_length(d: dict) -> int:
"""
Calculate the length of a dictionary using loop iteration.
Args:
d (dict): The dictionary whose length is to be determined.
Returns:
int: The length of the di... |
code_exercise_27k | 4,314 | Write a Python function to find the frequency of each word in a given string, ignoring symbols and punctuation marks. The function should return a dictionary where each key-value pair represents a word and its frequency in the string. The code should start with:
```python
import re
from typing import Dict
def count_wo... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
from typing import Dict
def count_word_frequency(string: str) -> Dict[str, int]:
"""
Calculate the frequency of each word in a given string, ignoring symbols and punctuation marks.
Args:
string (str): The... |
code_exercise_27k | 4,318 | Write a Python function to find the most frequent character in a given string, considering only alphabetic characters and ignoring case sensitivity. If there are multiple characters with the same highest frequency, return the first one encountered in the string.
The code should start with:
```python
def find_most_freq... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_most_frequent_char(string: str) -> str:
"""
Find the most frequent character in a given string, considering only alphabetic characters and ignoring case sensitivity.
If there are multiple characters with the same hi... |
code_exercise_27k | 4,322 | Write a Python function to determine if two sets contain the same elements, regardless of their order. The function should return `True` if the sets are equivalent, and `False` otherwise. The code should start with:
```python
def are_sets_equivalent(set1: set, set2: set) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def are_sets_equivalent(set1: set, set2: set) -> bool:
"""
Determine if two sets contain the same elements, regardless of their order.
Args:
set1 (set): The first set to compare.
set2 (set): The second set t... |
code_exercise_27k | 4,325 | Write a Python function to replace all occurrences of a substring within a string without using the built-in `replace` function. The function should handle cases where the substring to be replaced is empty or the string itself is empty. The function should also be able to handle overlapping occurrences of the substring... | Here is the Python code to solve the given problem in the markdown block:
```python
def replace_substring(string: str, old_substring: str, new_substring: str) -> str:
"""
Replaces all occurrences of a substring within a string without using the built-in replace function.
Args:
string (str): The ori... |
code_exercise_27k | 4,326 | Write a Python function to calculate the sum of all values in a given dictionary. The dictionary may contain nested dictionaries, lists, and other data structures. The function should recursively traverse the dictionary and sum all the numeric values it encounters.
The code should start with:
```python
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union, Dict, List
def calculate_sum_of_values(dictionary: Union[Dict, List]) -> float:
"""
Calculates the sum of all numeric values in a dictionary, including nested dictionaries and lists.
Args:
... |
code_exercise_27k | 4,329 | Write a Python function to calculate the average of all values in a given dictionary. The function should handle dictionaries with numeric values only and should raise appropriate exceptions for invalid inputs.
The code should start with:
```python
from typing import Dict, Union
def calculate_average(dictionary: Dict... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Union
def calculate_average(dictionary: Dict[str, Union[int, float]]) -> float:
"""
Calculate the average of all values in a given dictionary.
Args:
dictionary (Dict[str, Union[int, float]]... |
code_exercise_27k | 4,330 | Write a Python function to determine if two given strings are anagrams of each other, considering only alphanumeric characters and ignoring case. The function should return `True` if the strings are anagrams, and `False` otherwise. The code should start with:
```python
def are_anagrams(str1: str, str2: str) -> bool:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Determine if two given strings are anagrams of each other, considering only alphanumeric characters and ignoring case.
Args:
str1 (str): The first string to check.... |
code_exercise_27k | 4,331 | Write a Python function to iterate over a dictionary and convert all values to uppercase. The function should return a new dictionary with the same keys but with all values converted to uppercase. The original dictionary should remain unchanged.
The code should start with:
```python
from typing import Dict
def conver... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def convert_dict_values_to_uppercase(dictionary: Dict[str, str]) -> Dict[str, str]:
"""
Convert all values in a dictionary to uppercase.
Args:
dictionary (Dict[str, str]): The dictionary who... |
code_exercise_27k | 4,339 | Write a Python function to find the unique keys in each of two given dictionaries. The function should return a dictionary containing two sets: one set for the unique keys in the first dictionary and another set for the unique keys in the second dictionary. The code should start with:
```python
from typing import Dict,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Set
def find_unique_keys(dict1: Dict, dict2: Dict) -> Dict[str, Set]:
"""
Finds the unique keys in each of the two given dictionaries.
Args:
dict1 (Dict): The first dictionary.
dict... |
code_exercise_27k | 4,340 | Write a Python function to find the common elements among an arbitrary number of sets. The function should accept a variable number of sets as arguments and return a set containing the common elements found in all provided sets. The function should also validate that all arguments are of type `set`.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set, Any
def find_common_elements(*sets: Set[Any]) -> Set[Any]:
"""
Find the common elements among an arbitrary number of sets.
Args:
*sets (Set[Any]): Variable number of sets to find common elem... |
code_exercise_27k | 4,346 | Write a Python function to determine if a set is a perfect subset of another set or identical. A perfect subset means that every element of the first set is present in the second set, but the second set may have additional elements that are not in the first set. The function should return `True` if the first set is a p... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_subset(s1: set, s2: set) -> bool:
"""
Determine if the first set is a perfect subset of the second set or identical.
Args:
s1 (set): The first set to be checked.
s2 (set): The second set to be... |
code_exercise_27k | 4,349 | Write a Python function to create a new set by multiplying each element in the original set by a given factor. The function should handle both integer and floating-point factors and ensure that the resulting set contains unique elements. The code should start with:
```python
def multiply_set_elements(original_set: set,... | Here is the Python code to solve the given problem in the markdown block:
```python
def multiply_set_elements(original_set: set, factor: float) -> set:
"""
Creates a new set by multiplying each element in the original set by the given factor.
Args:
original_set (set): The original set with elements... |
code_exercise_27k | 4,356 | Write a Python function to find the intersection of two lists, considering elements from the first list that contain a specific substring. The function should return a list of elements that have both the specific substring and are present in both input lists. The code should start with:
```python
from typing import Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection_with_substring(list1: List[str], list2: List[str], substring: str) -> List[str]:
"""
Find the intersection of two lists, using elements from the first list that contain a specific s... |
code_exercise_27k | 4,361 | Write a Python function to find the second highest value in a given dictionary. The function should handle dictionaries with at least two values and return the second highest value. If the dictionary is empty or contains fewer than two values, the function should raise a `ValueError`.
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Union
def find_second_highest_value(dictionary: Dict[str, Union[int, float]]) -> Union[int, float]:
"""
Finds the second highest value in a given dictionary.
Args:
dictionary (Dict[str, Uni... |
code_exercise_27k | 4,362 | Write a Python function to check if a given set is a superset of all subsets of another set. The function should generate all possible subsets of the second set and verify if each of these subsets is a subset of the first set. The code should start with:
```python
from typing import Set
def is_superset_of_all_subsets(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set
def is_superset_of_all_subsets(superset: Set[int], subset: Set[int]) -> bool:
"""
Check if a given set is a superset of all subsets of another set.
Args:
superset (Set[int]): The set to check... |
code_exercise_27k | 4,370 | Write a Python function to count the number of elements in a given set that satisfy a specified condition. The condition is defined by a function that takes an element as input and returns a boolean value indicating whether the condition is satisfied. The function should start with:
```python
def count_elements_with_co... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_elements_with_condition(input_set: set, condition: callable) -> int:
"""
Count the number of elements in a set that satisfy a specified condition.
Args:
input_set (set): The set of elements to be evaluated... |
code_exercise_27k | 4,372 | Write a Python function to filter a dictionary by removing all key-value pairs where the value is less than a specified threshold. The function should return the modified dictionary. The code should start with:
```python
from typing import Dict, Union
def filter_dictionary_by_threshold(dictionary: Dict[str, Union[int,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Union
def filter_dictionary_by_threshold(dictionary: Dict[str, Union[int, float]], threshold: Union[int, float]) -> Dict[str, Union[int, float]]:
"""
Filters a dictionary by removing all key-value pairs... |
code_exercise_27k | 4,378 | Write a Python function to find the keys with the top `n` highest values in a given dictionary. The function should return a list of these keys. If `n` is greater than the number of items in the dictionary, the function should return all keys sorted by their values in descending order.
The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def find_top_n_keys(dictionary: Dict[str, int], n: int) -> List[str]:
"""
Finds the keys with the top n highest values in a dictionary.
Args:
dictionary (Dict[str, int]): The dictionar... |
code_exercise_27k | 4,379 | Write a Python function to find the nth smallest element in a given list of integers using a heap data structure. The function should efficiently handle the task by maintaining a min-heap of size n and updating it as necessary. The code should start with:
```python
from typing import List
import heapq
def find_nth_sma... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import heapq
def find_nth_smallest(numbers: List[int], n: int) -> int:
"""
Find the nth smallest element in a list of integers using a heap data structure.
Args:
numbers (List[int]): The lis... |
code_exercise_27k | 4,393 | Write a Python function to remove all elements from a given set that do not satisfy a specified condition. The condition is defined by a function that takes an element as input and returns a boolean value. The function should return the updated set containing only the elements that satisfy the condition.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_elements(set_: set, condition: callable) -> set:
"""
Remove elements from a set that do not satisfy a given condition.
Args:
set_ (set): The set from which elements will be removed.
condition (cal... |
code_exercise_27k | 4,396 | Write a Python function to find the smallest window in a given string that contains all characters of another string. The function should return the smallest substring that includes all characters from the pattern string. If no such window exists, the function should return `None`.
The code should start with:
```pytho... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_smallest_window(string: str, pattern: str) -> str:
"""
Finds the smallest window in the given string that contains all characters of the pattern.
Args:
string (str): The input string.
pattern (str):... |
code_exercise_27k | 4,397 | Write a Python function to find the keys in a dictionary that have the highest sum of their corresponding values. The values can be either integers, floats, or lists of integers/floats. If a value is a list, the sum should be the sum of all elements in the list. The function should return a list of keys that have the h... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Union
def find_keys_with_highest_sum(dictionary: Dict[str, Union[int, float, List[Union[int, float]]]]) -> List[str]:
"""
Find the keys with the highest sum of their corresponding values in a dict... |
code_exercise_27k | 4,401 | Write a Python function to determine if a given string can be segmented into a space-separated sequence of dictionary words. The function should take a string and a set of dictionary words as input and return a boolean indicating whether the string can be segmented according to the dictionary.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
def can_segment_string(s: str, word_dict: set) -> bool:
"""
Determines if a given string can be segmented into a space-separated sequence of dictionary words.
Args:
s (str): The input string to be checked for segmen... |
code_exercise_27k | 4,405 | Write a Python function to reverse the 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_words_in_sentence(sentence: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_in_sentence(sentence: str) -> str:
"""
Reverses the words in a given sentence while preserving the order of characters within each word.
Args:
sentence (str): The input sentence whose words are to ... |
code_exercise_27k | 4,410 | Write a Python function to implement the Knuth-Morris-Pratt (KMP) algorithm for string searching. The function should return a list of all starting indices in the text where the pattern is found. The function should be efficient and should handle edge cases such as empty strings and patterns that do not match.
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def compute_prefix(pattern: str) -> List[int]:
"""
Compute the prefix table used in the Knuth-Morris-Pratt algorithm.
Args:
pattern (str): The pattern to search for.
Returns:
Li... |
code_exercise_27k | 4,411 | Write a Python function to find the intersection of two lists of dictionaries, where each dictionary has a unique key `'id'`. The function should return a set of `'id'` values that are present in both lists. The code should start with:
```python
from typing import List, Dict, Set
def find_intersection_of_dicts(list1: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Set
def find_intersection_of_dicts(list1: List[Dict[str, int]], list2: List[Dict[str, int]]) -> Set[int]:
"""
Find the intersection of two lists of dictionaries based on the 'id' key.
Args:
... |
code_exercise_27k | 4,413 | Write a Python function to safely access a value associated with a specific key in a dictionary. If the key is not found in the dictionary, the function should return a default value instead of raising a `KeyError`.
The code should start with:
```python
from typing import Any, Dict, Optional
def safe_access_dictionar... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Any, Dict, Optional
def safe_access_dictionary_value(dictionary: Dict[Any, Any], key: Any, default: Optional[Any] = None) -> Any:
"""
Safely accesses a value associated with a specific key in a dictionary.
... |
code_exercise_27k | 4,415 | Write a Python function to check if a key exists in a dictionary and return the corresponding value if the key exists, otherwise return a default value. The function should handle cases where the dictionary is empty or the key is not found.
The code should start with:
```python
def get_value_or_default(dictionary: dic... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_value_or_default(dictionary: dict, key, default_value=None) -> any:
"""
Check if a key exists in a dictionary and return the corresponding value if the key exists, otherwise return a default value.
Args:
dic... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.