source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
7,184
Write a Python function to create a nested dictionary from a list of keys and values. Each key in the list is a string where each level of nesting is separated by a dot (`.`). The function should handle any level of nesting and ensure that the dictionary structure is correctly formed. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def create_nested_dictionary(keys: List[str], values: List[Any]) -> dict: """ Create a nested dictionary from a list of keys and values. Args: keys (List[str]): A list of keys where eac...
code_exercise_27k
7,185
Write a Python function to concatenate two lists such that each element from the first list is followed by its corresponding element from the second list, squared. The function should raise a `ValueError` if the two lists have different lengths. The code should start with: ```python from typing import List def concat...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def concat_and_square(list1: List[int], list2: List[int]) -> List[int]: """ Concatenates two lists, with each element from the first list followed by its corresponding element from the second list, squar...
code_exercise_27k
7,189
Write a Python function to concatenate two lists such that each element from the first list is followed by the number of times it appears in the second list. The function should handle cases where the lists are empty or contain duplicate elements. The code should start with: ```python from typing import List def conca...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def concatenate_lists(list1: List[int], list2: List[int]) -> List[int]: """ Concatenates two lists such that each element from the first list is followed by the number of times it appears in the sec...
code_exercise_27k
7,194
Write a Python function to reverse a list of integers using a different approach than the given code snippet. The function should use list slicing to achieve the reversal. The code should start with: ```python from typing import List def reverse_list_with_slicing(lst: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list_with_slicing(lst: List[int]) -> List[int]: """ Reverse a list of integers using list slicing. Args: lst (List[int]): A list of integers to be reversed. Returns: ...
code_exercise_27k
7,199
Write a Python function to reverse the order of items in a given dictionary without using any built-in functions or methods that directly reverse the dictionary or its keys. The function should iterate through the dictionary in reverse order and construct a new dictionary with the items in the reversed order. The code...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_dict(dict_obj: dict) -> dict: """ Reverse the order of items in a given dictionary without using any built-in functions or methods that directly reverse the dictionary or its keys. Args: dict_obj (dict):...
code_exercise_27k
7,201
Write a Python function to reverse a list of tuples. Each tuple in the list should remain unchanged, but the order of the tuples in the list should be reversed. The function should handle lists of varying lengths, including empty lists. The code should start with: ```python from typing import List, Tuple def reverse_l...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def reverse_list_of_tuples(lst: List[Tuple]) -> List[Tuple]: """ Reverses a list of tuples. Args: lst (List[Tuple]): The list of tuples to be reversed. Returns: List[Tupl...
code_exercise_27k
7,205
Write a Python function to flatten a nested dictionary into a single-level dictionary. The function should handle nested dictionaries of arbitrary depth and use a specified separator to concatenate keys from different levels of nesting. The function should also handle cases where the nested dictionary contains non-dict...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any def flatten_nested_dict(nested_dict: Dict[str, Any], separator: str = '.') -> Dict[str, Any]: """ Flatten a nested dictionary into a single-level dictionary. Args: nested_dict (Dict[str...
code_exercise_27k
7,206
Write a Python function to insert an element at the beginning of a list. The function should return a new list with the element inserted at the beginning, without modifying the original list. The code should start with: ```python from typing import List, Any def insert_at_beginning(lst: List[Any], element: Any) -> Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_at_beginning(lst: List[Any], element: Any) -> List[Any]: """ Inserts an element at the beginning of a list and returns a new list. Args: lst (List[Any]): The original list in...
code_exercise_27k
7,209
Write a Python function to insert an element at a specified position in a list. The function should take three parameters: the list, the element to be inserted, and the position at which the element should be inserted. If the specified position is out of bounds, the function should insert the element at the beginning o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_element_at_position(lst: List[Any], element: Any, position: int) -> List[Any]: """ Insert an element at a specified position in a list. Args: lst (List[Any]): The list to ins...
code_exercise_27k
7,210
Write a Python function to determine if a given year is a leap year. The function should return `True` if the year is a leap year and `False` otherwise. A leap year is defined as follows: - A year is a leap year if it is divisible by 4. - However, if the year is also divisible by 100, it is not a leap year unless it is...
Here is the Python code to solve the given problem in the markdown block: ```python def is_leap_year(year: int) -> bool: """ Determines if a given year is a leap year. Args: year (int): The year to check. Returns: bool: True if the year is a leap year, False otherwise. Examples: ...
code_exercise_27k
7,217
Write a Python function to insert multiple elements at the beginning of a list. The function should take two arguments: the original list and a list of elements to be inserted. The function should return the updated list with the elements inserted at the beginning. 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 List def insert_elements_at_beginning(lst: List[int], elements: List[int]) -> List[int]: """ Insert multiple elements at the beginning of a list. Args: lst (List[int]): The original list. ...
code_exercise_27k
7,218
Write a Python function to sort a list of strings in alphabetical order, ignoring the case. The function should handle lists of varying lengths and strings of different cases. The code should start with: ```python from typing import List def sort_strings_case_insensitive(string_list: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_case_insensitive(string_list: List[str]) -> List[str]: """ Sorts a list of strings in alphabetical order, ignoring the case. Args: string_list (List[str]): The list of strin...
code_exercise_27k
7,219
Write a Python function to insert multiple elements at a specified position in a list. The function should take three arguments: the list to be modified, the elements to be inserted, and the position at which the elements should be inserted. The function should return the updated 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 def insert_elements_at_position(lst: List[int], elements: List[int], position: int) -> List[int]: """ Inserts multiple elements at a specified position in a list. Args: lst (List[int]): The ...
code_exercise_27k
7,220
Write a Python function to sort a list of integers in descending order, ignoring negative values. The function should return a new list containing only the non-negative integers sorted in descending order. The code should start with: ```python from typing import List def sort_non_negative_descending(lst: List[int]) ->...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_non_negative_descending(lst: List[int]) -> List[int]: """ Sorts a list of integers in descending order, ignoring negative values. Args: lst (List[int]): A list of integers to be sor...
code_exercise_27k
7,221
Write a Python function to insert multiple elements at a specified index in a list. The function should handle the insertion efficiently and ensure that the index is within the bounds of the list. The function should not use any built-in list insertion methods like `insert` or `extend` directly. Instead, it should manu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_elements_at_index(lst: List[int], index: int, elements: List[int]) -> List[int]: """ Insert multiple elements at a specified index in a list. Args: lst (List[int]): The original l...
code_exercise_27k
7,225
Write a Python function to insert a sublist at a specified position within another list. The function should take three arguments: the main list, the sublist to be inserted, and the position at which the sublist should be inserted. The position should be zero-based, meaning that position 0 corresponds to the beginning ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_sublist_at_position(main_list: List, sublist: List, position: int) -> List: """ Inserts a sublist at a specified position within another list. Args: main_list (list): The list to ...
code_exercise_27k
7,228
Write a Python function to insert a sublist at a specified position within another list. The function should take three parameters: the original list, the sublist to be inserted, and the position at which the sublist should be inserted. The position should be zero-based, meaning that position 0 corresponds to the begin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_sublist_at_position(original_list: List[int], sublist: List[int], position: int) -> List[int]: """ Inserts a sublist at a specified position within another list. Args: original_li...
code_exercise_27k
7,232
Write a Python function to insert a sublist at a specified index in another list. The function should handle the insertion efficiently and return the modified list. The function should also raise an `IndexError` if the specified index is out of range. 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 insert_sublist(original_list: List, sublist: List, index: int) -> List: """ Inserts a sublist at a specified index in another list. Args: original_list (List): The original list. ...
code_exercise_27k
7,235
Write a Python function to sort a list of strings based on the number of uppercase letters in each string. The function should return the sorted list in ascending order of the number of uppercase letters. The code should start with: ```python from typing import List def sort_strings_by_uppercase_count(strings: List[st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_uppercase_count(strings: List[str]) -> List[str]: """ Sorts a list of strings based on the number of uppercase letters in each string. Args: strings (List[str]): A list o...
code_exercise_27k
7,236
Write a Python function to remove a specified key from a nested dictionary. The function should recursively traverse the nested dictionary and remove the specified key at any level of nesting. If the key is found and removed, the function should return the updated dictionary. If the key is not found, the original dicti...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_key_from_nested_dict(nested_dict: dict, key_to_remove: str) -> dict: """ Remove a specified key from a nested dictionary. This function recursively traverses the nested dictionary and removes the specified key ...
code_exercise_27k
7,237
Write a Python function to add a specific number of days to a given date. The function should take a date string in the format 'YYYY-MM-DD' and an integer representing the number of days to add. The function should return the resulting date in the same 'YYYY-MM-DD' format. The code should start with: ```python from da...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def add_days(date_str: str, days: int) -> str: """ Add a specific number of days to a given date. Args: date_str (str): Date in the format 'YYYY-MM-DD'. days (int): ...
code_exercise_27k
7,238
Write a Python function to insert the elements of a tuple into a list at a specified index. The function should handle the insertion efficiently and return the modified list. The function should not modify the original list but return a new list with the inserted elements. The function should also handle cases where th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def insert_tuple_elements(lst: List[int], tup: Tuple[int], index: int) -> List[int]: """ Insert the elements of a tuple into a list at a specified index. Args: lst (List[int]): The or...
code_exercise_27k
7,239
Write a Python function to sort a list of dictionaries based on the values of multiple keys. The function should handle cases where the keys might not exist in some dictionaries and should sort those dictionaries last. The code should start with: ```python from typing import List, Dict def sort_list_of_dicts_with_miss...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_list_of_dicts_with_missing_keys(list_of_dicts: List[Dict[str, any]], sort_keys: List[str]) -> List[Dict[str, any]]: """ Sorts a list of dictionaries based on the values of multiple keys, h...
code_exercise_27k
7,240
Write a Python function to find the date of the first Monday of a given year. The function should take an integer representing the year as input and return a `datetime.date` object representing the date of the first Monday of that year. The code should start with: ```python import datetime def find_first_monday(year:...
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def find_first_monday(year: int) -> datetime.date: """ Find the date of the first Monday of a given year. Args: year (int): The year for which the first Monday is to be found. Returns: ...
code_exercise_27k
7,243
Write a Python function to sort a dictionary by its values in ascending order. The function should return a new dictionary with the same keys and values, but sorted by the values. The code should start with: ```python from typing import Dict def sort_dict_by_values(dictionary: Dict[str, int]) -> Dict[str, int]: ```
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 ascending order. Args: dictionary (Dict[str, int]): The dictionary to be sorted...
code_exercise_27k
7,245
Write a Python function to sort a list of strings based on the length of each word in the string. The function should return the sorted list of strings. The code should start with: ```python from typing import List def sort_strings_by_word_length(string_list: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_word_length(string_list: List[str]) -> List[str]: """ Sorts a list of strings based on the length of each word in the string. Args: string_list (List[str]): List of strin...
code_exercise_27k
7,246
Write a Python function to calculate the number of seconds between two given dates and times. The function should accept two strings representing the dates and times in the format "YYYY-MM-DD HH:MM:SS". The function should return the number of seconds between these two dates and times as an integer. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def calculate_seconds_between_datetimes(start_datetime: str, end_datetime: str) -> int: """ Calculate the number of seconds between two given dates and times. Args: start_datetime (str...
code_exercise_27k
7,248
Write a Python function to find the key-value pair with the maximum value in a given dictionary. The function should return a tuple containing the key and the maximum value. If the dictionary is empty, the function should return `None`. The code should start with: ```python def find_max_value_pair(dictionary: dict) ->...
Here is the Python code to solve the given problem in the markdown block: ```python def find_max_value_pair(dictionary: dict) -> tuple: """ Finds the key-value pair with the maximum value in a dictionary. Args: dictionary (dict): The dictionary to search. Returns: tuple: A tuple contai...
code_exercise_27k
7,251
Write a Python function to reverse a list of strings while ignoring duplicate values. The function should return the reversed list with all duplicate values removed, preserving the order of the first occurrence of each unique string. The code should start with: ```python from typing import List def reverse_unique_str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_unique_strings(lst: List[str]) -> List[str]: """ Reverse a list of strings while ignoring duplicate values. Parameters: lst (List[str]): A list of strings. Returns: List[str...
code_exercise_27k
7,256
Write a Python function to insert an element after every occurrence of a specified value in a given list. The function should iterate through the list and insert the specified element immediately after each occurrence of the specified value. The function should handle cases where the specified value does not exist in t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_element_after_occurrences(lst: List[Any], value: Any, element: Any) -> List[Any]: """ Inserts an element after every occurrence of a specified value in a list. Args: lst (Lis...
code_exercise_27k
7,257
Write a Python function to reverse a list of tuples while ignoring duplicate values. The function should return a list of tuples in reverse order, ensuring that each value in the tuples is unique. The code should start with: ```python from typing import List, Tuple def reverse_unique_tuples(lst: List[Tuple]) -> List[T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def reverse_unique_tuples(lst: List[Tuple]) -> List[Tuple]: """ Reverses a list of tuples while ignoring duplicate values. Args: lst (List[Tuple]): The input list of tuples. Retu...
code_exercise_27k
7,259
Write a Python function to insert an element before every occurrence of a specified value in a given list. The function should handle lists of any type and ensure that the insertion is done correctly without modifying the original list. The function should start with: ```python from typing import List, Any def insert_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_before_value(lst: List[Any], value: Any, element: Any) -> List[Any]: """ Insert an element before every occurrence of a specified value in a list. Args: lst (List[Any]): The ...
code_exercise_27k
7,263
Write a Python function to insert an element after every even number in a given list. The function should iterate through the list, check if each number is even, and if so, insert the specified element immediately after it. The function should return the modified list. The code should start with: ```python from typing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_after_even_numbers(lst: List[int], element: Any) -> List[Any]: """ Inserts an element after every even number in the given list. Args: lst (List[int]): The input list of inte...
code_exercise_27k
7,264
Write a Python function to convert a list of tuples into a dictionary. Each tuple in the list should contain exactly two elements, where the first element is the key and the second element is the value. If a key appears more than once in the list, the value associated with the last occurrence of that key should be used...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def convert_tuples_to_dict(lst: List[Tuple[any, any]]) -> Dict[any, any]: """ Convert a list of tuples into a dictionary. Each tuple in the list should contain exactly two elements, whe...
code_exercise_27k
7,267
Write a Python function to insert a specified element before every even number in a given list of integers. The function should return the updated list with the specified element inserted before each even number. The code should start with: ```python from typing import List def insert_element_before_even(numbers: Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_element_before_even(numbers: List[int], element: int) -> List[int]: """ Insert a specified element before every even number in a given list of integers. Args: numbers (List[int]):...
code_exercise_27k
7,268
Write a Python function to sort a list of dictionaries based on the length of their values, then by their keys. The function should handle lists of dictionaries where each dictionary contains only string values. The code should start with: ```python from typing import List, Dict def sort_dicts_by_length_and_key(dicts:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dicts_by_length_and_key(dicts: List[Dict[str, str]]) -> List[Dict[str, str]]: """ Sorts a list of dictionaries based on the length of their values, then by their keys. Args: d...
code_exercise_27k
7,270
Write a Python function to find the symmetric difference between two dictionaries. The symmetric difference should include keys that are present in either of the dictionaries but not in both, along with their corresponding values. If a key is present in both dictionaries but has different values, include both key-value...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def symmetric_difference(dict1: Dict, dict2: Dict) -> Dict: """ Find the symmetric difference between two dictionaries. The symmetric difference includes keys that are present in either of the dicti...
code_exercise_27k
7,271
Write a Python function to insert an element after every odd number in a given list. The function should iterate through the list, check if each number is odd, and if so, insert the specified element immediately after it. The function should return the modified list. The code should start with: ```python from typing i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_after_odd(lst: List[int], element: Any) -> List[Any]: """ Inserts an element after every odd number in a given list. Args: lst (List[int]): The input list of integers. ...
code_exercise_27k
7,272
Write a Python function to reverse the order of characters in each string within a list. The function should handle lists of strings of varying lengths and should return a new list with each string reversed. The code should start with: ```python from typing import List def reverse_strings_in_list(strings: List[str]) -...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_strings_in_list(strings: List[str]) -> List[str]: """ Reverse the order of characters in each string within a list. Args: strings (List[str]): The list of strings to be reversed....
code_exercise_27k
7,278
Write a Python function to insert an element at a random position in a given list. The function should ensure that the element is inserted at a position chosen uniformly at random from all possible positions in the list. The code should start with: ```python from typing import List, Any import random def insert_elemen...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any import random def insert_element_random_position(lst: List[Any], element: Any) -> List[Any]: """ Inserts an element at a random position in a given list. Args: lst (List[Any]): The inpu...
code_exercise_27k
7,282
Write a Python function to find the earliest and latest date from a list of dates in the format "YYYY-MM-DD". The function should return a tuple containing the earliest and latest dates as `datetime` objects. The code should start with: ```python from datetime import datetime from typing import List, Tuple def get_ear...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime from typing import List, Tuple def get_earliest_and_latest_dates(date_list: List[str]) -> Tuple[datetime, datetime]: """ Get the earliest and latest date from a list of dates in the format "YYYY-MM...
code_exercise_27k
7,283
Write a Python function to sort a list of dictionaries based on the length of their values, and then by the sum of their keys. If two dictionaries have the same length of values, the one with the smaller sum of keys should come first. The function should handle dictionaries with integer keys and string values. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dicts(lst: List[Dict[int, str]]) -> List[Dict[int, str]]: """ Sorts a list of dictionaries based on the length of their values, then by the sum of their keys. Args: lst (List[...
code_exercise_27k
7,286
Write a Python function to clone or copy a dictionary. The function should handle nested dictionaries and ensure that the copy is a deep copy, meaning that changes to the original dictionary should not affect the copied dictionary and vice versa. The code should start with: ```python from typing import Any, Dict def d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict def deep_clone_dictionary(dictionary: Dict[Any, Any]) -> Dict[Any, Any]: """ Deep clone or copy a dictionary, including nested dictionaries. Args: dictionary (Dict[Any, Any]): The dicti...
code_exercise_27k
7,287
Write a Python function to reverse a list of strings while ignoring leading and trailing whitespace. The function should return a new list with the reversed strings. The code should start with: ```python from typing import List def reverse_strings(strings: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_strings(strings: List[str]) -> List[str]: """ Reverses a list of strings, ignoring leading and trailing whitespace. Args: strings (List[str]): A list of strings to be reversed. ...
code_exercise_27k
7,289
Write a Python function to sort a list of integers in ascending order, moving all zeros to the end of the list. The function should handle lists with both positive and negative integers, and ensure that the relative order of non-zero elements is preserved after sorting. The code should start with: ```python from typing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_list_with_zeros(lst: List[int]) -> List[int]: """ Sorts a list of integers in ascending order, moving all zeros to the end of the list. Args: lst (List[int]): The list of integers t...
code_exercise_27k
7,291
Write a Python function to map two lists into a dictionary, ensuring that the function handles cases where the lengths of the two lists are not equal by returning a dictionary with only the mappings that could be made. The function should not raise any exceptions. The code should start with: ```python from typing impo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def map_lists_to_dict(keys: List[str], values: List[int]) -> Dict[str, int]: """ Maps two lists into a dictionary, handling cases where the lengths of the lists are not equal. Args: ke...
code_exercise_27k
7,295
Write a Python function to extract a subset of keys from a given dictionary. The function should return a new dictionary containing only the specified keys. If a specified key does not exist in the original dictionary, it should be ignored. The code should start with: ```python from typing import Dict, List def extra...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def extract_subset_of_keys(dictionary: Dict[str, int], keys: List[str]) -> Dict[str, int]: """ Extracts a subset of keys from a given dictionary. Args: dictionary (Dict[str, int]): The...
code_exercise_27k
7,299
Write a Python function to calculate the average value of a given dictionary. The dictionary contains numerical values as its values. If the dictionary is empty, the function should return `None`. The code should start with: ```python from typing import Dict, Union def calculate_average(dic: Dict[str, int]) -> Union[f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Union def calculate_average(dic: Dict[str, int]) -> Union[float, None]: """ Calculates the average value of a dictionary. Args: dic (Dict[str, int]): The input dictionary with string keys a...
code_exercise_27k
7,301
Write a Python function to calculate the difference between two given times in hours, minutes, and seconds. The function should take two tuples as input, each representing a time in the format `(hours, minutes, seconds)`. The function should return a tuple representing the difference between the two times in the same f...
Here is the Python code to solve the given problem in the markdown block: ```python def time_difference(time1: tuple, time2: tuple) -> tuple: """ Calculate the difference between two given times (hours, minutes, seconds). Parameters: time1 (tuple): A tuple representing the first time as (hours, minutes...
code_exercise_27k
7,302
Write a Python function to sort a list of strings in alphabetical order, considering numeric digits within each string. The sorting should treat numeric digits as numbers, not as characters. For example, the string "abc123" should be considered less than "abc45" because 123 is less than 45. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import re def alphanumeric_sort(string_list: List[str]) -> List[str]: """ Sorts a list of strings in alphabetical order, considering numeric digits within each string. Args: string_list (Lis...
code_exercise_27k
7,304
Write a Python function to sort a list of integers in ascending order using the **merge sort** algorithm. The function should not use Python's built-in sort function or any other built-in sorting utilities. The code should start with: ```python from typing import List def merge_sort(nums: 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_sort(nums: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using the merge sort algorithm. Args: nums (List[int]): The list of integers to be sorted. ...
code_exercise_27k
7,306
Write a Python function to find the difference between two dictionaries based on their values. The function should return a new dictionary where each key is a key present in either or both dictionaries, and the value is a tuple containing the values from both dictionaries for that key. If a key is present in only one d...
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 based on their values. Args: dict1 (Dict[Any,...
code_exercise_27k
7,311
Write a Python function to insert an element in a list after the first odd number encountered. If no odd number is found, append the element to the end of the list. The function should handle lists of integers and return the updated list. The code should start with: ```python from typing import List def insert_after_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_after_first_odd(lst: List[int], element: int) -> List[int]: """ Insert an element in a list after the first odd number encountered. If no odd number is found, append the element to the end...
code_exercise_27k
7,313
Write a Python function to find the second largest value in a given dictionary. If the dictionary contains fewer than two unique values, the function should return `None`. The function should handle dictionaries with integer values only. The code should start with: ```python from typing import Dict, Optional def find...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Optional def find_second_largest_value(dictionary: Dict[str, int]) -> Optional[int]: """ Find the second largest value in a given dictionary. Args: dictionary (Dict[str, int]): The input di...
code_exercise_27k
7,318
Write a Python function to reverse the order of tuples within a list of tuples. Each tuple in the list should be reversed, and the order of the tuples in the list should remain unchanged. The function should return a new list with the reversed tuples. The code should start with: ```python from typing import List, Tupl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def reverse_tuple_order(lst: List[Tuple[any, any]]) -> List[Tuple[any, any]]: """ Reverse the order of tuples within a list of tuples. Args: lst (List[Tuple[any, any]]): The input lis...
code_exercise_27k
7,325
Write a Python function to sort a list of dictionaries based on the occurrence of a specific key in each dictionary. If the key is not present in a dictionary, it should be considered as having an occurrence of 0. The sorting should be in descending order of the key's occurrence. The code should start with: ```python ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dicts_by_key_occurrence(dicts: List[Dict[str, int]], key: str) -> List[Dict[str, int]]: """ Sorts a list of dictionaries based on the occurrence of a specific key in each dictionary. ...
code_exercise_27k
7,327
Write a Python function to insert an element after each positive number in a given list of integers. The function should iterate through the list and insert the specified element immediately after each positive number. The function should return the modified list. The code should start with: ```python from typing impo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_after_positive_numbers(nums: List[int], element: Any) -> List[Any]: """ Inserts an element after each positive number in a given list of integers. Args: nums (List[int]): The...
code_exercise_27k
7,329
Write a Python function to calculate the number of minutes between two given time strings in the format 'HH:MM'. The function should handle cases where the second time is on the next day, i.e., it can be smaller than the first time. The function should also validate the input time strings to ensure they are in the corr...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def calculate_minutes_between_times(time1: str, time2: str) -> int: """ Calculate the number of minutes between two given time strings in the format 'HH:MM'. Args: time1 (st...
code_exercise_27k
7,333
Write a Python function to insert an element before each positive number in a given list. The function should handle both positive and negative integers, as well as zero. The code should start with: ```python from typing import List def insert_element_before_positive_numbers(lst: List[int], element: int) -> List[int]:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_element_before_positive_numbers(lst: List[int], element: int) -> List[int]: """ Inserts an element before each positive number in a given list. Args: lst (List[int]): The input li...
code_exercise_27k
7,335
Write a Python function to insert an element after each negative number in a given list. The function should iterate through the list, check for negative numbers, and insert the specified element immediately after each negative number. The function should return the updated list. The code should start with: ```python ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_after_negative(numbers: List[int], element: Any) -> List[int]: """ Insert an element after each negative number in a list. Args: numbers (List[int]): The input list of number...
code_exercise_27k
7,341
Write a Python function to reverse the order of strings within a list, ignoring the case. The function should take a list of strings as input and return a new list where each string is reversed and converted to lowercase. The original list should not be modified. The code should start with: ```python from typing impor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_strings_in_list(lst: List[str]) -> List[str]: """ Reverses the order of strings within a list, ignoring the case. Args: lst (List[str]): The input list of strings. Returns: ...
code_exercise_27k
7,342
Write a Python function to insert an element after each occurrence of a specified value in a list. The function should handle cases where the specified value appears multiple times and at the end of the list. The code should start with: ```python from typing import List, Any def insert_element_after_value(lst: List[An...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_element_after_value(lst: List[Any], value: Any, element: Any) -> List[Any]: """ Inserts an element in a list after each occurrence of a specified value. Args: lst (List[Any])...
code_exercise_27k
7,343
Write a Python function to convert a given date string from a specified input format to the ISO 8601 format (YYYY-MM-DD). The function should handle various input date formats and return the date in the ISO format. The code should start with: ```python from datetime import datetime def convert_to_iso(datetime_str: str...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def convert_to_iso(datetime_str: str, input_format: str) -> str: """ Converts a given date string from a specified input format to the ISO 8601 format (YYYY-MM-DD). Args: datetime_str ...
code_exercise_27k
7,344
Write a Python function to find the key-value pairs that are common between two dictionaries. The function should return a new dictionary containing only the common key-value pairs. The code should start with: ```python def find_common_pairs(dict1: dict, dict2: dict) -> dict: ```
Here is the Python code to solve the given problem in the markdown block: ```python def find_common_pairs(dict1: dict, dict2: dict) -> dict: """ Finds the common key-value pairs between two dictionaries. Args: dict1 (dict): The first dictionary. dict2 (dict): The second dictionary. Ret...
code_exercise_27k
7,345
Write a Python function to sort a list of integers in ascending order, grouping even and odd numbers separately. The function should return a new list with even numbers appearing first, followed by odd numbers, both in ascending order. The code should start with: ```python from typing import List def sort_even_odd(nu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_even_odd(numbers: List[int]) -> List[int]: """ Sorts a list of integers in ascending order, grouping even and odd numbers separately. Args: numbers (List[int]): A list of integers t...
code_exercise_27k
7,346
Write a Python function to insert an element before each zero in a given list. The function should iterate through the list and insert the specified element immediately before each occurrence of zero. The function should return the updated list. The code should start with: ```python from typing import List, Any def i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def insert_before_zeros(lst: List[Any], elem: Any) -> List[Any]: """ Insert an element before each zero in a given list. Args: lst (List[Any]): The input list containing elements of any...
code_exercise_27k
7,347
Write a Python function to count the number of key-value pairs in a dictionary without using the built-in `len()` function. The function should iterate through the dictionary and count the pairs manually. The code should start with: ```python def count_key_value_pairs(dictionary: dict) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_key_value_pairs(dictionary: dict) -> int: """ Counts the number of key-value pairs in a dictionary without using the len() function. Args: dictionary (dict): The dictionary to count the key-value pairs. ...
code_exercise_27k
7,348
Write a Python function to calculate the average number of days in a list of given years, considering leap years. The function should handle an empty list by raising a `ValueError`. The code should start with: ```python from typing import List def calculate_average_days(years: List[int]) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_average_days(years: List[int]) -> float: """ Calculate the average number of days in a list of given years, considering leap years. Args: years (List[int]): A list of years. ...
code_exercise_27k
7,353
Write a Python function to calculate the number of overlapping days between two date ranges. The function should take four string inputs representing the start and end dates of two date ranges, all in "YYYY-MM-DD" format. The function should return the number of days that overlap between the two date ranges. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime def count_overlapping_days(start_date1: str, end_date1: str, start_date2: str, end_date2: str) -> int: """ Calculate the number of overlapping days between two date ranges. Args: start...
code_exercise_27k
7,354
Write a Python function to convert a given list into a zigzag list. A zigzag list is defined as a list where the elements are arranged in such a way that for every pair of adjacent elements, one element is smaller than its neighbors and the other is larger. The function should start with: ```python from typing import L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def zigzag_list(lst: List[int]) -> List[int]: """ Converts a given list into a zigzag list. A zigzag list is defined as a list where for every pair of adjacent elements, one element is smaller t...
code_exercise_27k
7,357
Write a Python function to find the date that is `n` weeks ago from a given date. The function should accept a date string in the format 'YYYY-MM-DD' and an integer `n` representing the number of weeks. The function should return the date that is `n` weeks ago from the given date in the same format 'YYYY-MM-DD'. The c...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def find_date_n_weeks_ago(given_date: str, n: int) -> str: """ Finds the date that is n weeks ago from a given date. Args: given_date (str): The given date in the format 'YY...
code_exercise_27k
7,358
Write a Python function to insert elements into a list such that the list becomes non-decreasing. The function should take a list of integers as input and return a new list with the elements inserted in the correct positions to maintain the non-decreasing order. The function should handle edge cases such as an empty li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def make_non_decreasing(numbers: List[int]) -> List[int]: """ Inserts elements into a list such that the list becomes non-decreasing. Args: numbers (List[int]): The list of integers to be mo...
code_exercise_27k
7,359
Write a Python function to find the median value in a dictionary. The dictionary contains key-value pairs where the values are numerical. The function should return the median value from the dictionary. If the dictionary is empty, the function should return `None`. 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 Dict, Union def find_median_value(dictionary: Dict[str, int]) -> Union[float, int, None]: """ Find the median value in a dictionary. Args: dictionary (Dict[str, int]): A dictionary containing key...
code_exercise_27k
7,361
Write a Python function to find the date that is n days into the future from a given date. The function should handle both string input in the format 'YYYY-MM-DD' and `datetime` objects. The function should return the future date as a string in the format 'YYYY-MM-DD'. The code should start with: ```python from dateti...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timedelta def find_future_date(date, days): """ Find the date that is n days into the future from a given date. Parameters: date (str or datetime): The starting date in the format 'YYYY-M...
code_exercise_27k
7,363
Write a Python function to combine two dictionaries by selecting the key-value pair from the dictionary with the higher value for each key. If a key exists in both dictionaries, the value from the dictionary with the higher value should be used. If a key exists only in one dictionary, it should be included in the resul...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def combine_dictionaries(dict1: Dict[str, int], dict2: Dict[str, int]) -> Dict[str, int]: """ Combine two dictionaries by selecting the key-value pair from the dictionary with the higher value. Args...
code_exercise_27k
7,364
Write a Python function to reverse a list of integers, ignoring negative values and preserving their order. The function should return a new list with the positive integers reversed and the negative integers remaining in their original positions. The code should start with: ```python from typing import List def rever...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_positive_integers(lst: List[int]) -> List[int]: """ Reverse a list of integers, ignoring negative values and preserving their order. Args: lst (List[int]): A list of integers. ...
code_exercise_27k
7,366
Write a Python function to find the date that is `n` months ago from a given date. The function should handle edge cases such as the end of months (e.g., February 29th in a leap year) and ensure that the resulting date is valid. The function should return the date in the format 'YYYY-MM-DD'. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime from dateutil.relativedelta import relativedelta def date_n_months_ago(given_date: str, n: int) -> str: """ Returns the date that is n months ago from the given_date. Args: given_date ...
code_exercise_27k
7,390
Write a Python function to compute the cumulative sum of values for each key in a given dictionary. The function should return a new dictionary where each key maps to the cumulative sum of values encountered so far, including the current value. The code should start with: ```python from typing import Dict def cumulat...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def cumulative_sum_dict(dictionary: Dict[str, int]) -> Dict[str, int]: """ Compute the cumulative sum of values for each key in the given dictionary. Args: dictionary (Dict[str, int]): The i...
code_exercise_27k
7,394
Write a Python function to flatten a nested dictionary into a single-level dictionary. The keys in the resulting dictionary should be a concatenation of the original keys separated by underscores. The function should handle any level of nested dictionaries. 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 def flatten_nested_dict(dictionary: Dict) -> Dict: """ Recursively flattens a nested dictionary into a single-level dictionary. Args: dictionary (dict): The nested dictionary to be flatt...
code_exercise_27k
7,395
Write a Python function to sort a list of integers in ascending order while keeping all zeros at the beginning of the list. The function should handle lists with varying numbers of zeros and non-zero integers. The code should start with: ```python from typing import List def sort_list_with_zeros_at_beginning(lst: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_list_with_zeros_at_beginning(lst: List[int]) -> List[int]: """ Sorts a list of integers in ascending order while keeping all zeros at the beginning. Args: lst (List[int]): The list ...
code_exercise_27k
7,398
Write a Python function to reverse a list of integers, sorting the odd numbers in ascending order and the even numbers in descending order. The function should return the modified list. The code should start with: ```python from typing import List def reverse_sort_list(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_sort_list(arr: List[int]) -> List[int]: """ Reverses a list of integers, sorting the odd numbers in ascending order and the even numbers in descending order. Args: arr (List[int]...
code_exercise_27k
7,405
Write a Python function to insert an element into a list at a position where the median of the previous N elements is minimized. The function should return the updated list with the element inserted at the desired position. The function should handle cases where N is greater than the length of the list by raising a `Va...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def insert_element_with_minimum_median(lst: List[Union[int, float]], N: int, element: Union[int, float]) -> List[Union[int, float]]: """ Inserts an element at a position in a list where the median...
code_exercise_27k
7,410
Write a Python function to convert a list of dictionaries into a dictionary of lists. Each key in the resulting dictionary should correspond to a key in the dictionaries, and the value should be a list of values corresponding to that key in the dictionaries. The function should handle dictionaries with different keys a...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Any def convert_list_of_dicts(lst: List[Dict[str, Any]]) -> Dict[str, List[Any]]: """ Converts a list of dictionaries into a dictionary of lists. Args: lst (List[Dict[str, Any]]): Lis...
code_exercise_27k
7,413
Write a Python function to find the key-value pair with the minimum value in a given dictionary. The function should return a tuple containing the key and the minimum value. If the dictionary is empty, the function should raise a `ValueError`. The code should start with: ```python def find_min_value(dictionary: dict) ...
Here is the Python code to solve the given problem in the markdown block: ```python def find_min_value(dictionary: dict) -> tuple: """ Finds the key-value pair with the minimum value in a dictionary. Args: dictionary (dict): The input dictionary to search. Returns: tuple: A tuple conta...
code_exercise_27k
7,423
Write a Python function to sort a list of dictionaries based on the number of occurrences of a specific substring in their values. The function should return a new list of dictionaries sorted in descending order based on the count of the specified substring in the 'value' field of each dictionary. The code should star...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dicts_by_substring_count(dict_list: List[Dict[str, str]], substring: str) -> List[Dict[str, str]]: """ Sorts a list of dictionaries based on the number of occurrences of a specific substri...
code_exercise_27k
7,436
Write a Python function to swap the case of the values in a given dictionary. The function should return a new dictionary with the same keys but with the values having their case swapped (i.e., uppercase letters become lowercase and vice versa). The function should handle dictionaries with string values only. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def swap_dict_values_case(dictionary: Dict[str, str]) -> Dict[str, str]: """ Swaps the case of the values in a given dictionary. This function takes a dictionary where all values are strings and ret...
code_exercise_27k
7,438
Write a Python function to map the values of a dictionary to their absolute values. The function should return a new dictionary with the same keys as the input dictionary, but with the values replaced by their absolute values. The function should raise a `TypeError` if the input argument is not of type `dict`. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python def map_dict_values(dictionary: dict) -> dict: """ Maps the values of a dictionary to their absolute values. Args: dictionary (dict): The input dictionary. Returns: dict: A new dictionary with the same ...
code_exercise_27k
7,442
Write a Python function to convert a dictionary into a list of dictionaries, where each dictionary in the list contains a single key-value pair from the original dictionary. The function should handle dictionaries with any type of keys and values. The code should start with: ```python from typing import Dict, List de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def convert_dict_to_list_of_dicts(dictionary: Dict) -> List[Dict]: """ Convert a dictionary into a list of dictionaries, where each dictionary in the list contains a single key-value pair from the ...
code_exercise_27k
7,443
Write a Python function to sort a list of strings in alphabetical order, ignoring leading and trailing whitespace and case sensitivity. The function should preserve the original case of the strings after sorting. The code should start with: ```python from typing import List def sort_strings_ignore_case_and_whitespace(...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_ignore_case_and_whitespace(strings: List[str]) -> List[str]: """ Sorts a list of strings in alphabetical order, ignoring leading and trailing whitespace and case sensitivity. The ori...
code_exercise_27k
7,449
Write a Python function to merge two dictionaries by updating only the common key-value pairs. If a key exists in both dictionaries but has different values, the value from the second dictionary should be used. The function should return a new dictionary containing the updated key-value pairs. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def merge_common_pairs(dict1: Dict[str, int], dict2: Dict[str, int]) -> Dict[str, int]: """ Merge two dictionaries by updating only the common key-value pairs. If a key exists in both dictionaries bu...
code_exercise_27k
7,459
Write a Python function to swap the values of two specified keys in a dictionary. The function should handle cases where the keys are not present in the dictionary by returning the original dictionary unchanged. Additionally, the function should handle cases where the input is not a dictionary by returning `None`. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any, Optional def swap_dictionary_values(dictionary: Dict[Any, Any], key1: Any, key2: Any) -> Optional[Dict[Any, Any]]: """ Swaps the values of two specified keys in a dictionary. Args: dic...
code_exercise_27k
7,461
Write a Python function to sort a list of integers in descending order while moving all negative values to the front of the list. The function should maintain the relative order of the negative values and the non-negative values within their respective groups. The code should start with: ```python from typing import L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_list_desc_with_negative(lst: List[int]) -> List[int]: """ Sorts a list of integers in descending order while moving all negative values to the front of the list. Parameters: lst (List[i...
code_exercise_27k
7,463
Write a Python function to reverse a list of dictionaries and sort them based on the sum of the lengths of their keys and values. The function should start with: ```python from typing import List, Dict def reverse_and_sort_dict_list(list_of_dicts: List[Dict[str, any]]) -> List[Dict[str, any]]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def reverse_and_sort_dict_list(list_of_dicts: List[Dict[str, any]]) -> List[Dict[str, any]]: """ Reverse a list of dictionaries and sort them based on the sum of the lengths of their keys and value...
code_exercise_27k
7,464
Write a Python function to remove a specific key-value pair from multiple dictionaries at once. The function should take a list of dictionaries and a key as input, and return the updated list of dictionaries with the specified key-value pair removed from each dictionary. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def remove_key_value_from_dicts(dicts: List[Dict], key: str) -> List[Dict]: """ Remove a specific key-value pair from multiple dictionaries at once. Args: dicts (List[Dict]): List of d...
code_exercise_27k
7,467
Write a Python function to remove empty dictionaries from a given dictionary of dictionaries. The function should recursively traverse the dictionary and remove any empty dictionaries it encounters. The code should start with: ```python from typing import Dict def remove_empty_dicts(dictionary: Dict) -> Dict: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def remove_empty_dicts(dictionary: Dict) -> Dict: """ Recursively removes empty dictionaries from a given dictionary of dictionaries. Args: dictionary (dict): The dictionary of dictionaries ...
code_exercise_27k
7,469
Write a Python function to create a dictionary from a list of keys and a default value. The function should ensure that all keys in the list are unique and handle cases where the list of keys might contain duplicates. If duplicates are found, the function should raise a `ValueError`. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, List, Dict def create_dictionary_unique_keys(keys: List[Any], default_value: Any) -> Dict[Any, Any]: """ Create a dictionary from a list of unique keys and a default value. Args: keys (List[...
code_exercise_27k
7,474
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 empty dictionaries by returning `True`, as an empty dictionary can be considered to have all values of the sa...
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
7,480
Write a Python function to filter a list of dictionaries based on a specific key. The function should return a list of dictionaries that contain the specified key. The code should start with: ```python from typing import List, Dict def filter_dicts(dicts: List[Dict[str, any]], key: str) -> List[Dict[str, any]]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def filter_dicts(dicts: List[Dict[str, any]], key: str) -> List[Dict[str, any]]: """ Filter a list of dictionaries based on a specific key. Args: dicts (List[Dict[str, any]]): A list o...
code_exercise_27k
7,483
Write a Python function to sort a list of dictionaries based on the number of unique values they contain. The function should return a new list of dictionaries sorted in ascending order based on the number of unique values in each dictionary. The code should start with: ```python from typing import List, Dict def sort...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dict_list_by_unique_values(dict_list: List[Dict]) -> List[Dict]: """ Sorts a list of dictionaries based on the number of unique values they contain. Args: dict_list (List[Dict...
code_exercise_27k
7,484
Write a Python function to create a dictionary from a list of tuples, where the first element of each tuple becomes the key and the second element becomes the value. The function should handle cases where the list of tuples is empty or any tuple does not have at least two elements by raising a `ValueError`. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def create_dictionary(tuples: List[Tuple]) -> Dict: """ Creates a dictionary from a list of tuples, where the first element of each tuple becomes the key and the second element becomes the v...