source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
magic_evol_110k
108,719
Write a Python function to retrieve the name of a user based on their user ID from a given dictionary. If the user ID does not exist in the dictionary, the function should return a default message indicating that the user was not found. The function should be efficient and handle edge cases appropriately. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Union def get_user_name(user_id: int, user_info: Dict[int, str]) -> Union[str, None]: """ Retrieve the name of a user based on their user ID from a given dictionary. If the user ID does not exist in...
magic_evol_110k
108,736
Write a Python function to generate the first `n` numbers of the Fibonacci series. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The function should handle cases where `n` is less than or equal to 0 by returning an empty list. The c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_fibonacci(n: int) -> List[int]: """ Generate the first n numbers of the Fibonacci series. Args: n (int): The number of Fibonacci numbers to generate. Returns: List[...
magic_evol_110k
108,738
Write a Python function to multiply all the elements of a given nested array, including any nested arrays present. The array may contain both single digits and other arrays. The function should return the product of all elements. The code should start with: ```python from typing import List, Union def multiply_nested...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def multiply_nested_array(arr: List[Union[int, List]]) -> int: """ Multiply all the elements of a given nested array, including any nested arrays present. Args: arr (List[Union[int, L...
magic_evol_110k
108,763
Write two Python functions to remove duplicate strings from a list while preserving the order of their first occurrence. The first function should be case-sensitive, and the second function should be case-insensitive, but it should retain the original case of the strings in the output list. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates(input_list: List[str]) -> List[str]: """ Remove duplicate strings from the input list while preserving the order of their first occurrence. Args: input_list (List[str])...
magic_evol_110k
108,766
Write a Python function to reverse each string in a given list of strings using list comprehension. The function should take a list of strings as input and return a new list where each string is 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]: """ Reverses each string in the given list of strings using list comprehension. Args: strings (List[str]): A list of strings to ...
magic_evol_110k
108,771
Write a Python function to determine if a given array of numbers, which can include both integers and floating-point numbers, forms a geometric progression. Handle edge cases such as zero and negative numbers. The function should return `True` if the array is a geometric progression and `False` otherwise. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_geometric_progression(arr: List[float]) -> bool: """ Determine if the given array of numbers forms a geometric progression. Args: arr (List[float]): The array of numbers to check. ...
magic_evol_110k
108,773
Write a Python function to filter and return a list of numbers from a given dataset that are divisible by a specified divisor. The function should use list comprehension for the filtering process. The code should start with: ```python from typing import List def filter_divisible_numbers(dataset: List[int], divisor: in...
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def filter_divisible_numbers(dataset: List[int], divisor: int) -> List[int]: """ Filters and returns a list of numbers from the given dataset that are divisible by the specified divisor. Args: ...
magic_evol_110k
108,793
Write a Python function to calculate the product of all elements in a given numerical array. The function should handle arrays with both positive and negative integers, as well as zero. The code should start with: ```python from typing import List def product_of_array(lst: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def product_of_array(lst: List[int]) -> int: """ Calculate the product of all elements in a given numerical array. Args: lst (List[int]): The numerical array containing integers. Return...
magic_evol_110k
108,820
Write a Python function to count the number of `1`'s in the binary representation of every number in the range `[0, num]` and return an array where each element at index `i` is the number of `1`'s in the binary representation of `i`. Additionally, return the sum of all the `1`'s in the binary representation of numbers ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def count_bits_and_sum(num: int) -> Tuple[List[int], int]: """ Count the number of `1`'s in the binary representation of every number in the range `[0, num]` and return an array where each ele...
magic_evol_110k
108,822
Write a Python function to count the number of unique employees for a given company in a list of companies and their employees. The list may contain duplicate employee names across different companies, and employees could work in multiple companies. Company and employee names are case-sensitive. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_unique_employees(company_list: List[List[str]]) -> Dict[str, int]: """ Count the number of unique employees for each company in the given list. Args: company_list (List[List[...
magic_evol_110k
108,861
Write a Python function to find the closest and lowest number in a given list of voltages that is less than or equal to a specified target voltage. The function should use a binary search approach to efficiently find the closest match. The code should start with: ```python from typing import List def find_closest_vol...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_closest_voltage(voltage_map: List[float], target_voltage: float) -> float: """ Find the closest and lowest voltage in a sorted list of voltages that is less than or equal to a specified target v...
magic_evol_110k
108,875
Write a Python function to remove all special characters from a given string, leaving only alphanumeric characters and spaces. The function should handle strings of varying lengths and content, including strings with no special characters, strings with only special characters, and strings with a mix of alphanumeric cha...
Here is the Python code to solve the given problem in the markdown block: ```python import re def remove_special_characters(s: str) -> str: """ Removes all special characters from the given string, leaving only alphanumeric characters and spaces. Args: s (str): The input string from which special ...
magic_evol_110k
108,879
Write a Python function to generate all prime numbers between a given range `[start, end]` efficiently. The function should return a list of prime numbers within the specified range. The code should start with: ```python from typing import List def generate_primes_in_range(start: int, end: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def generate_primes_in_range(start: int, end: int) -> List[int]: """ Generate a list of prime numbers within the specified range [start, end]. Args: start (int): The starting num...
magic_evol_110k
108,884
Write a Python function to sort a composite list of alphanumeric elements into two distinct sorted sequences. The first sequence should contain all numeric data sorted in ascending order, and the second sequence should contain all alphanumeric strings sorted lexicographically. If the input is not a list or contains ele...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def list_sorter(lst: List[Union[int, float, str]]) -> List[List[Union[int, float, str]]]: """ Sorts a composite list of alphanumeric elements into two distinct sorted sequences. Args: ...
magic_evol_110k
108,901
Write a Python function to reverse the order of keys in a given dictionary and compute the product of all dictionary values whose keys are less than or equal to each key when the keys are sorted in ascending order. The function should return a new dictionary with the reversed keys and their corresponding computed produ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict from functools import reduce import operator def reverse_dict_and_compute_product(input_dict: Dict[str, int]) -> Dict[str, int]: """ Reverses the order of keys in the input dictionary and computes the pr...
magic_evol_110k
108,908
Write a Python function to calculate the sum of two input numbers and return the result. The function should handle both positive and negative integers, as well as zero. The code should start with: ```python def sum_two_numbers(num1: int, num2: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_two_numbers(num1: int, num2: int) -> int: """ Calculate the sum of two input numbers. Args: num1 (int): The first number to be summed. num2 (int): The second number to be summed. Returns: ...
magic_evol_110k
108,920
Write a Python function to extract all integer values from a given string and return their sum. The string contains arbitrary characters and integers, and it is guaranteed that the string will contain at least one integer. The integers in the string can be more than one digit long. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python import re def sum_of_integers_in_string(input_str: str) -> int: """ Extracts all integer values from a given string and returns their sum. Args: input_str (str): The input string containing arbitrary characters and...
magic_evol_110k
108,949
Write a Python function to transform a given JSON string into a Python dictionary and add a new friend to the `friends` list within the dictionary. The function should accept a dictionary representing the new friend and return the updated dictionary. The code should start with: ```python import json from typing import...
Here is the Python code to solve the given problem in the markdown block: ```python import json from typing import Dict, List def add_friend_to_json(json_str: str, new_friend: Dict[str, any]) -> Dict[str, any]: """ Transforms a JSON string into a Python dictionary and adds a new friend to the 'friends' list. ...
magic_evol_110k
108,958
Write a Python function to identify the non-overlapping elements in two unorganized lists of strings and provide their frequency. The function should return two dictionaries, each containing the non-overlapping elements and their respective frequencies for each 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, Dict from collections import Counter def find_non_overlapping_elements_with_frequency(list1: List[str], list2: List[str]) -> (Dict[str, int], Dict[str, int]): """ Identifies the non-overlapping elements...
magic_evol_110k
108,959
Write a Python function to divide a given text string using a specified delimiter symbol while maintaining the order of the substrings. The function should handle cases where the delimiter is not present in the string and return the original string as a single-element list. The code should start with: ```python from ty...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def split_string_by_delimiter(text: str, delimiter: str) -> List[str]: """ Splits the given text string using the specified delimiter and returns the substrings in a list. Args: text (str): ...
magic_evol_110k
108,961
Write a Python function to determine if a given integer is a prime number. The function should be optimized for computational efficiency, especially for larger numbers. The function should handle edge cases such as negative numbers, 0, 1, and small prime numbers efficiently. The code should start with: ```python import...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_prime(n: int) -> bool: """ Determines if a given integer is a prime number. Args: n (int): The integer to check for primality. Returns: bool: True if the integer is prime, False othe...
magic_evol_110k
108,964
Write a Python function to convert a boolean value to its alphabetic equivalence, where `True` is represented as 'Yes' and `False` is represented as 'No'. The function should take a boolean value as input and return the corresponding string. The code should start with: ```python def boolean_to_string(value: bool) -> s...
Here is the Python code to solve the given problem in the markdown block: ```python def boolean_to_string(value: bool) -> str: """ Converts a boolean value to its alphabetic equivalence. Args: value (bool): The boolean value to convert. Returns: str: The string 'Yes' if the value is Tr...
magic_evol_110k
108,975
Write a Python function to calculate the maximum volume of rainwater that can be captured between the given structures. Each structure is represented by a non-negative integer in a list, where each integer signifies the height of the structure at a specific coordinate. The goal is to determine the maximum volume of rai...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_rainwater_captured(elevation: List[int]) -> int: """ Calculate the maximum volume of rainwater that can be captured between the given structures. Args: elevation (List[int]): A list ...
magic_evol_110k
108,976
Write a Python function to sort a list of strings that contain both fruit names and the scientific name of a mammal in lexicographical order. The function should handle any list of strings, not just the predefined list. The code should start with: ```python from typing import List def sort_lexicographically(strings: L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_lexicographically(strings: List[str]) -> List[str]: """ Sorts a list of strings lexicographically. Args: strings (List[str]): The list of strings to be sorted. Returns: ...
magic_evol_110k
108,977
Write a Python function to sort a given list of dictionaries by multiple keys in a specified order. Additionally, include a function to handle missing values in the dictionaries - the function should either replace these with a specified value or exclude them from the sorted result. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Union, Optional def sort_dicts_by_keys(data: List[Dict[str, Union[int, float, str]]], sort_keys: List[str], sort_orders: List[str], ...
magic_evol_110k
108,993
Write a Python function to sort a given list of integers in ascending order. The function should return the sorted list. The code should start with: ```python from typing import List def sort_list_ascending(data: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def sort_list_ascending(data: List[int]) -> List[int]: """ Sorts a given list of integers in ascending order. Args: data (List[int]): The list of integers to be sorted. Returns: ...
magic_evol_110k
108,995
Write a Python function to convert a given string of lowercase letters into title case, where the first letter of each word in the string is capitalized. The function should handle multiple spaces between words and leading/trailing spaces correctly. The code should start with: ```python def to_title_case(s: str) -> str...
Here is the Python code to solve the given problem in the markdown block: ```python def to_title_case(s: str) -> str: """ Converts a given string of lowercase letters into title case, where the first letter of each word is capitalized. Args: s (str): The input string to be converted to title case. ...
magic_evol_110k
109,015
Write a Python function to verify whether a list of numerical values is arranged in a strictly ascending sequence. The function should also handle exceptions for non-numeric inputs, empty lists, and lists with only one element. The function should return `True` if the list is strictly ascending, otherwise `False`. If t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def check_strictly_ascending_order(lst: List[Union[int, float]]) -> bool: """ Verifies whether a list of numerical values is arranged in a strictly ascending sequence. Args: lst (List...
magic_evol_110k
109,019
Write a Python function named `unique_sum` that takes a list of integers as input and returns the sum of the unique elements in the list. The function should efficiently extract unique elements from the input list and compute their sum. The code should start with: ```python from typing import List def unique_sum(list_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def unique_sum(list_of_nums: List[int]) -> int: """ Computes the sum of the unique elements in the given list of integers. Args: list_of_nums (List[int]): A list of integers from which to ex...
magic_evol_110k
109,037
Write a Python function to find the longest common subsequence (LCS) among five given strings and return the LCS along with the starting position of the LCS in each string. If no common subsequence is found, return an empty string and an empty dictionary. The function should start with: ```python from typing import Tu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, Dict def LCS(s1: str, s2: str) -> str: """ Helper function to find the longest common subsequence (LCS) between two strings. Args: s1 (str): The first string. s2 (str): The second ...
magic_evol_110k
109,050
Write a Python function to count the frequency of each unique alphabetical character in a given input string, ignoring case and special characters. The function should return a dictionary where the keys are the lowercase alphabetical characters and the values are their respective counts. The code should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def character_count(text: str) -> Dict[str, int]: """ Counts the frequency of each unique alphabetical character in the given input string, ignoring case and special characters. Args: text (...
magic_evol_110k
109,068
Write a Python function to calculate the cumulative sum of every individual element within a provided list. The cumulative sum for a list `[a, b, c, ...]` is defined as `[a, a+b, a+b+c, ...]`. The function should be efficient and handle both positive and negative integers. The code should start with: ```python from ty...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def cumulative_sum(lst: List[int]) -> List[int]: """ Calculate the cumulative sum of every individual element within a provided list. Args: lst (List[int]): The list of integers for which to...
magic_evol_110k
109,071
Write a Python function to create a deep copy of a 2D array, ensuring that changes to the copied array do not affect the original array. The function should handle nested arrays and account for potential changes in the copy affecting the original 2D array. Additionally, consider edge cases such as empty arrays or array...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def deep_copy_2d_array(arr: List[List[int]]) -> List[List[int]]: """ Creates a deep copy of a 2D array, ensuring that changes to the copied array do not affect the original array. Args: arr ...
magic_evol_110k
109,080
Create a Python function that generates two lambda functions: one to double the numerical value of a parameter and another to triple it. The function should handle all cases, including when non-numeric values are passed as arguments. The code should start with: ```python def create_double_and_triple_functions(): ```
Here is the Python code to solve the given problem in the markdown block: ```python def create_double_and_triple_functions(): """ Creates and returns two lambda functions: one to double and another to triple the numerical value of a parameter. If the parameter is not a number (int or float), the functions r...
magic_evol_110k
109,104
Write a Python function to calculate the Least Common Multiple (LCM) of two integers `x` and `y` using an improved algorithm. The function should handle the constraints where `1 <= x, y <= 10^9`. The code should start with: ```python def lcm(x: int, y: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def gcd(x: int, y: int) -> int: """ Compute the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Args: x (int): The first integer. y (int): The second integer. Returns: ...
magic_evol_110k
109,132
Write a Python function to generate and print the Fibonacci sequence up to a given maximum value `max_val`, ensuring that all numbers in the sequence are within the range from 0 to `max_val`. The function should use a recursive approach to calculate the Fibonacci numbers and should handle edge cases where the sequence ...
Here is the Python code to solve the given problem in the markdown block: ```python def print_fibonacci_sequence(max_val: int): """ Generates and prints the Fibonacci sequence up to a given maximum value. Args: max_val (int): The maximum value up to which the Fibonacci sequence should be generated....
magic_evol_110k
109,142
Write a Python function to count the number of vowels in a given string. The function should be case-insensitive and should consider the vowels to be 'a', 'e', 'i', 'o', 'u'. The function should return the total count of vowels found in the input string. The code should start with: ```python def count_vowels(text: str...
Here is the Python code to solve the given problem in the markdown block: ```python def count_vowels(text: str) -> int: """ Count the number of vowels in the given string. Args: text (str): The input string in which to count the vowels. Returns: int: The total number of vowels found in...
magic_evol_110k
109,155
Write a Python function to merge two sets and count the occurrences of each unique value in both sets. The function should return a dictionary where each key is a unique value from the merged sets, and the value is the number of occurrences of that value in both sets combined. If a value occurs in both sets, it should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Set, Dict def merge_sets_and_count(set1: Set[int], set2: Set[int]) -> Dict[int, int]: """ Merges two sets and counts the occurrences of each unique value in both sets. Args: set1 (Set[int]): The ...
magic_evol_110k
109,171
Write a Python function to validate if a given string matches a specific pattern. The string must start with a letter (either uppercase or lowercase) and can be followed by any sequence of letters and numbers. The function should handle cases where the input is an empty string or `None`. The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python import re def validate_string(input_str: str) -> bool: """ Validates if the given string matches the pattern: starts with a letter (either uppercase or lowercase) and can be followed by any sequence of letters and numbers. ...
magic_evol_110k
109,178
Write a Python function to sort a given list of integers in descending order. The function should return the sorted list. The code should start with: ```python from typing import List def sort_descending(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_descending(numbers: List[int]) -> List[int]: """ Sorts a given list of integers in descending order. Args: numbers (List[int]): The list of integers to be sorted. Returns: ...
magic_evol_110k
109,190
Write a Python function to generate a dictionary that contains specified words as keys and their respective frequencies in a given text as values. The function should handle punctuation and ignore case for word frequencies. For example, 'Hello' and 'hello' should be considered the same word. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def word_frequency(text: str, words: List[str]) -> Dict[str, int]: """ Generates a dictionary containing specified words as keys and their respective frequencies in the given text as values. Ha...
magic_evol_110k
109,198
Write a Python function to flatten a nested list, specifically a 2D matrix, into a single linear list. The function should handle any depth of nested lists and ensure that all elements are included in the resulting linear list. The code should start with: ```python from typing import List, Any def flatten_nested_list(...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def flatten_nested_list(nested_list: List[List[Any]]) -> List[Any]: """ Flattens a nested list, specifically a 2D matrix, into a single linear list. Args: nested_list (List[List[Any]]):...
magic_evol_110k
109,212
Write a Python function to implement the Shell Sort algorithm, which is an optimization of the Insertion Sort algorithm. The function should sort a given list of integers in ascending order. The Shell Sort algorithm works by sorting pairs of elements far apart from each other, then progressively reducing the gap betwee...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def shell_sort(lst: List[int]) -> List[int]: """ Sorts a list of integers using the Shell Sort algorithm. Args: lst (List[int]): The list of integers to be sorted. Returns: List...
magic_evol_110k
109,218
Write a Python function to find the median of a given set of numbers. The function should sort the numbers in descending order manually (without using built-in sorting functions) and then determine the median. The median is the middle number in an ordered set if the set has an odd number of elements, or the average of ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_median_manually(nums: List[int]) -> float: """ Find the median of a given set of numbers after sorting them in descending order manually. Args: nums (List[int]): The list of numbers...
magic_evol_110k
109,225
Write a Python function to extract and return a list of values corresponding to specified keys from a given dictionary. The function should handle cases where the keys might not exist in the dictionary by returning `None` for such keys. The code should start with: ```python from typing import Dict, List, Optional def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Optional def extract_values(dictionary: Dict[str, any], keys: List[str]) -> List[Optional[any]]: """ Extracts values from a dictionary based on a list of keys. Args: dictionary (Dict[...
magic_evol_110k
109,226
Write a Python function to calculate the number of unique paths from the top-left corner to the bottom-right corner of an m x n grid, where you can only move right or down. The function should start with: ```python from typing import List def unique_paths(m: int, n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def unique_paths(m: int, n: int) -> int: """ Calculate the number of unique paths from the top-left corner to the bottom-right corner of an m x n grid. Args: m (int): The number of row...
magic_evol_110k
109,228
Write a Python function to find the indices of any symmetric pairs within the rows of a given matrix. A symmetric pair is defined as two rows where one row is the reverse of the other. The function should return a tuple of the indices of the symmetric pair if found, otherwise return `None`. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Optional def find_symmetric_pairs(matrix: List[List[int]]) -> Optional[Tuple[int, int]]: """ Find the indices of any symmetric pairs within the rows of a given matrix. A symmetric pair is de...
magic_evol_110k
109,238
Write a Python function to reverse all the strings in a given list of strings. The function should take a list of strings as input and return a new list where each string is reversed. The original list should remain unchanged. The code should start with: ```python from typing import List def reverse_strings(strings: ...
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 all the strings in a given list of strings. Args: strings (List[str]): A list of strings to be reversed. Returns: ...
magic_evol_110k
109,273
Write a Python function to calculate the area and circumference of a circle given its radius. The function should handle the case where the radius is negative by raising a `ValueError`. The code should start with: ```python import math def circle_calculations(radius: float) -> tuple: ```
Here is the Python code to solve the given problem in the markdown block: ```python import math def circle_calculations(radius: float) -> tuple: """ Calculate the area and circumference of a circle given its radius. Args: radius (float): The radius of the circle. Returns: tuple: A tup...
magic_evol_110k
109,279
Write a Python function to reorder a given dictionary based on the numerical values in descending order. If there are ties in the numerical values, the keys should be sorted in ascending alphabetical order. The function should return the reordered dictionary. The code should start with: ```python from typing import Di...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def reorder_dictionary(my_dict: Dict[str, int]) -> Dict[str, int]: """ Reorders the given dictionary based on the numerical values in descending order. If there are ties in the numerical values, the ...
magic_evol_110k
109,312
Write a Python function to duplicate a given list and subtract a specified value from each element in the duplicated list. The function should return the modified duplicated list. The code should start with: ```python from typing import List def duplicate_and_subtract(original_list: List[int], subtract_value: int) ->...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def duplicate_and_subtract(original_list: List[int], subtract_value: int) -> List[int]: """ Duplicates the given list and subtracts a specified value from each element in the duplicated list. Args: ...
magic_evol_110k
109,314
Write a Python function that uses list comprehension syntax to generate and output all even integers within a given array. The array may contain a mixture of numerical and non-numerical values and may range from 1 through an undefined number that will exceed the limit of 10. The function should handle exceptions for no...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def list_even_numbers(input_list: List[int]) -> List[int]: """ Generates a list of all even integers from a given array, skipping non-numerical elements. Args: input_list (List[int]): The in...
magic_evol_110k
109,334
Write a Python function to manipulate and analyze a given JSON object. The JSON object contains the following fields: "name" with the value 'John Smith', "age" with the value 35, and "location" with the value 'Online'. The function should update the values of these fields to 'John Doe', 40, and 'Offline' respectively, ...
Here is the Python code to solve the given problem in the markdown block: ```python import json def update_json_data(json_data: dict) -> dict: """ Updates the values of the "name", "age", and "location" fields in the given JSON object. Args: json_data (dict): The JSON object to be updated. It must...
magic_evol_110k
109,341
Write a Python function to transform a dataset based on a set of predefined rules. The dataset is represented as a list of dictionaries, where each dictionary represents an individual record. The transformation rules are provided as a list of tuples, where each tuple specifies an action to be performed on a specific co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Tuple def transform_dataset(data: List[Dict[str, any]], rules: List[Tuple[str, str, any]]) -> List[Dict[str, any]]: """ Transforms a dataset based on a set of predefined rules. Args: ...
magic_evol_110k
109,349
Write a Python function to merge multiple dictionaries, ensuring that any duplicate keys are handled properly. The dictionary that appears later in the list should overwrite the value from the dictionaries that appear earlier. The function should accept a list of dictionaries and return a single merged dictionary. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def merge_dictionaries(dicts: List[Dict]) -> Dict: """ Merges a list of dictionaries, ensuring that any duplicate keys are handled properly. The dictionary that appears later in the list should...
magic_evol_110k
109,352
Write a Python function to convert a binary string into its corresponding decimal number. The function should iterate through each character of the binary string, calculate the decimal value based on the position of each bit, and sum these values to get the final decimal number. The code should start with: ```python de...
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_decimal(binary_string: str) -> int: """ Converts a binary string to its corresponding decimal number. Args: binary_string (str): A string composed only of '0's and '1's representing a binary number. ...
magic_evol_110k
109,354
Write a Python function to filter out prime numbers from a given list of numbers. The function should handle edge cases such as non-integer data types, negative numbers, and the number zero. The function should start with: ```python from typing import List def filter_prime_numbers(numbers: List[int]) -> List[int]: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(n: int) -> bool: """ Check if a number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. ...
magic_evol_110k
109,367
Write a Python function to calculate the cumulative sum of all integers present within a list and its nested sublists up to three levels deep. The function should handle lists with integers and nested sublists, and it should return the total sum of all integers. The code should start with: ```python from typing import...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def cumulative_sum_nested_lists(lst: Union[List[Union[int, List[Union[int, List[Union[int, List[int]]]]]]], int]) -> int: """ Calculate the cumulative sum of all integers present within a list and...
magic_evol_110k
109,369
Write a Python function to insert a key into an AVL tree and maintain its balance using the provided rotational operations. The function should handle the insertion of a key into the tree and perform the necessary rotations to ensure the tree remains balanced. The code should start with: ```python class AVLNode: de...
Here is the Python code to solve the given problem in the markdown block: ```python class AVLNode: def __init__(self, key): self.val = key self.left = None self.right = None self.height = 1 class AVLTree: def getHeight(self, node): if not node: return 0 ...
magic_evol_110k
109,378
Write a Python function to calculate the result of raising a given number to the power of another number. The function should take two parameters: the base number and the exponent. The function should return the result of the base number raised to the power of the exponent. The code should start with: ```python def po...
Here is the Python code to solve the given problem in the markdown block: ```python def power_calculation(base: float, exponent: float) -> float: """ Calculate the result of raising a base number to the power of an exponent. Args: base (float): The base number. exponent (float): The exponen...
magic_evol_110k
109,381
Write a Python function to filter and return a list of even numbers from a given list of integers. The function should iterate through the list, check if each number is divisible by 2, and collect all such numbers into a new list. The function should start with: ```python from typing import List def filter_even_number...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_even_numbers(numbers: List[int]) -> List[int]: """ Filters and returns a list of even numbers from the given list of integers. Args: numbers (List[int]): The list of integers to f...
magic_evol_110k
109,382
Write a Python function to calculate the volume of distinct elements in a given list of integer values. The volume is defined as the sum of the absolute values of the unique elements in the list. The function should handle negative integers and zero. The code should start with: ```python from typing import List def c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_volume_of_distinct_elements(arr: List[int]) -> int: """ Calculate the volume of distinct elements in a given list of integer values. The volume is defined as the sum of the absolute val...
magic_evol_110k
109,396
Write a Python function to generate a random string of a specified length that includes both alphanumeric characters and special characters. The string must contain the characters '@', '&', '#', and '*' at least once. The function should ensure that the generated string meets these criteria. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python import random import string def generate_random_string(length: int) -> str: """ Generates a random string of the specified length that includes both alphanumeric characters and special characters. The string must contain th...
magic_evol_110k
109,400
Write a Python function to determine if a given character sequence is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once. The function should be case-insensitive and should ignore non-alphabetic characters. The code should start with: ```python def is_pangram(sentence: str) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_pangram(sentence: str) -> bool: """ Determine if the given sentence is a pangram. A pangram is a sentence that contains every letter of the alphabet at least once. This function is case-insensitive and ignores no...
magic_evol_110k
109,408
Write a Python function that merges two strings by alternating their characters. If one string is longer than the other, the remaining characters from the longer string should be appended to the end of the merged string. The function should start with: ```python def merge_strings(s1: str, s2: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python from itertools import zip_longest def merge_strings(s1: str, s2: str) -> str: """ Merges two strings by alternating their characters. If one string is longer than the other, the remaining characters from the longer string a...
magic_evol_110k
109,415
Write a Python function to compute the sum of each unique element in a given 2D array (matrix). The function should handle any size of the matrix and ensure that only unique elements are summed. The code should start with: ```python from typing import List def sum_of_unique_elements(matrix: List[List[int]]) -> int: ``...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_unique_elements(matrix: List[List[int]]) -> int: """ Computes the sum of each unique element in a given 2D array (matrix). Args: matrix (List[List[int]]): A 2D array of integers. ...
magic_evol_110k
109,416
Write a Python function to calculate the total sum of all constituent elements present in a specified list while maintaining optimality. The function should handle lists containing integers, floats, and nested lists of integers and floats. The code should start with: ```python from typing import List, Union def total_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def total_sum(elements: List[Union[int, float, List]]) -> Union[int, float]: """ Calculate the total sum of all constituent elements in a list, including nested lists. Args: elements ...
magic_evol_110k
109,421
Write a Python function to create a list of `N` empty dictionaries, each with a unique identifier as a key and the value being another empty dictionary. Additionally, each nested dictionary should contain a unique key-value pair, where the key is a string and the value is an integer. The function should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def create_nested_dicts(n: int) -> List[dict]: """ Creates a list of N dictionaries, each with a unique identifier as a key and the value being another empty dictionary. Additionally, each nested dic...
magic_evol_110k
109,431
Write a Python function to find the length of the Longest Common Subsequence (LCS) between two given strings. The function should use dynamic programming to efficiently compute the result. The code should start with: ```python def longest_common_subsequence_length(str1: str, str2: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def longest_common_subsequence_length(str1: str, str2: str) -> int: """ Calculate the length of the Longest Common Subsequence (LCS) between two strings. Args: str1 (str): The first string. str2 (str): The s...
magic_evol_110k
109,446
Write a Python function to determine if two binary sequences can be transformed into each other by exactly one bit inversion operation. The function should return `True` if the transformation is possible, and `False` otherwise. The function should handle binary sequences of the same length and return `False` if the len...
Here is the Python code to solve the given problem in the markdown block: ```python def can_transform_by_single_bit_inversion(bin_seq1: str, bin_seq2: str) -> bool: """ Determines if two binary sequences can be transformed into each other by exactly one bit inversion. Args: bin_seq1 (str): The firs...
magic_evol_110k
109,460
Write a Python function to construct a dictionary where the keys are concatenated strings of the elements from pairs of tuples, and the values are the products of the elements in the corresponding tuples. The function should take a list of tuples as input and return the constructed dictionary. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def construct_dictionary(tuples_list: List[Tuple[int, int]]) -> Dict[str, int]: """ Constructs a dictionary where the keys are concatenated strings of the elements from pairs of tuples, ...
magic_evol_110k
109,462
Write a Python function to generate a SQL query for an API endpoint that extracts data from a relational database table and optionally manipulates the data by applying a condition, performing a join operation with another table, or sorting the data. The function should be flexible enough to handle different combination...
Here is the Python code to solve the given problem in the markdown block: ```python def generate_sql_query(table: str, secondary_table: str = None, condition: str = None, sorting: str = None) -> str: """ Generates a SQL query for an API endpoint to extract and manipulate data from a relational database table. ...
magic_evol_110k
109,471
Write a Python function to compute the value of Euler's number \( e \) using the infinite series definition. The series is given by: \[ e = \sum_{i=0}^{\infty} \frac{1}{i!} \] The function should take an integer `n` as input, which represents the number of terms to use in the series approximation. The function should...
Here is the Python code to solve the given problem in the markdown block: ```python import math def compute_eulers_number(n: int) -> float: """ Compute the value of Euler's number e using the first n terms of the infinite series definition. The series is defined as: e = sum(1/i! for i in range(n)) ...
magic_evol_110k
109,508
Write a Python function to receive a string and a non-negative integer as inputs and print the given string as many times as the input integer. Additionally, the function should record the frequency of each character in the string across all prints and display the sorted character frequencies at the end. The sorting sh...
Here is the Python code to solve the given problem in the markdown block: ```python def print_string_and_character_frequency(s: str, n: int) -> None: """ Prints the given string `n` times and displays the sorted character frequencies. Args: s (str): The string to be printed. n (int): The nu...
magic_evol_110k
109,527
Write a Python function to sort a given list of numerical figures in ascending order. The function should return the sorted list without modifying the original list. The code should start with: ```python from typing import List def sort_numbers(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_numbers(numbers: List[int]) -> List[int]: """ Sorts a given list of numerical figures in ascending order and returns the sorted list. Args: numbers (List[int]): The list of numerica...
magic_evol_110k
109,544
Write a Python function to sort a list of elements using the Bubble Sort algorithm. The function should take a list of integers as input and return the sorted list. The code should start with: ```python from typing import List def bubble_sort(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 bubble_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers using the Bubble Sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: Li...
magic_evol_110k
109,563
Write a Python function to find and return all even numbers within a given range `[start, end]` (both inclusive). The function should handle cases where `start` is greater than `end` by swapping their values. The function should also handle cases where `start` or `end` are negative. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python def find_even_numbers(start: int, end: int) -> list: """ Find and return all even numbers within the given range [start, end] (both inclusive). Args: start (int): The starting number of the range. end (int):...
magic_evol_110k
109,567
Write a Python function to reverse a given string without using any built-in functions or libraries. The reversed string should retain the original capitalization and punctuation. The code should start with: ```python def reverse_string(original_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(original_string: str) -> str: """ Reverses a given string without using any built-in functions or libraries. The reversed string retains the original capitalization and punctuation. Args: orig...
magic_evol_110k
109,570
Write a Python function to insert a given string value at the beginning of a provided list of strings. The function should modify the list in place and return the modified list. The code should start with: ```python from typing import List def insert_at_beginning(string_list: List[str], value: str) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_at_beginning(string_list: List[str], value: str) -> List[str]: """ Inserts a given string value at the beginning of a provided list of strings. Args: string_list (List[str]): The ...
magic_evol_110k
109,573
Write a Python function to calculate the least common multiple (LCM) of two distinct positive integers. The function should use the greatest common divisor (GCD) to compute the LCM efficiently. The code should start with: ```python import math def least_common_multiple(x: int, y: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import math def least_common_multiple(x: int, y: int) -> int: """ Calculate the least common multiple (LCM) of two distinct positive integers. Args: x (int): The first positive integer. y (int): The second ...
magic_evol_110k
109,580
Write a Python function to sort a list of complex numbers based on their magnitudes (absolute values) in ascending order. The function should take a list of complex numbers as input and return the sorted list. The code should start with: ```python from typing import List def sort_complex_numbers_by_magnitude(complex_n...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_complex_numbers_by_magnitude(complex_numbers: List[complex]) -> List[complex]: """ Sorts a list of complex numbers based on their magnitudes (absolute values) in ascending order. Args: ...
magic_evol_110k
109,585
Write a Python function to calculate the Levenshtein Distance between two strings. The Levenshtein Distance is defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other. The function should be efficient and handle strings of varying le...
Here is the Python code to solve the given problem in the markdown block: ```python def levenshtein_distance(s1: str, s2: str) -> int: """ Calculate the Levenshtein Distance between two strings. The Levenshtein Distance is the minimum number of single-character edits (insertions, deletions, or substitu...
magic_evol_110k
109,591
Write a Python function to identify the unique element(s) in a given array of integers and return their positions. A unique element is defined as an element that appears exactly once in the array. The function should efficiently handle both time and space complexity. 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, Dict def find_unique_elements_positions(arr: List[int]) -> Dict[int, List[int]]: """ Identifies the unique elements in the given array and returns their positions. Args: arr (List[int]): Th...
magic_evol_110k
109,593
Write a Python function to sort a given list of numerical values using a multi-step approach. The function should first categorize the numbers into two groups based on a threshold value (e.g., 5), then sort each group individually, and finally combine the sorted groups into a single sorted list. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def multi_step_sort(lst: List[int], threshold: int = 5) -> List[int]: """ Sorts a given list of numerical values using a multi-step approach. The function first categorizes the numbers into two grou...
magic_evol_110k
109,594
Write a Python function to create a mathematical array using the specified integers in the given chronological order: 7, 11, 19, 22. The function should return the array as a NumPy array. The code should start with: ```python import numpy as np def create_mathematical_array() -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_mathematical_array() -> np.ndarray: """ Creates a mathematical array using the specified integers in the given chronological order: 7, 11, 19, 22. Returns: np.ndarray: A NumPy array co...
magic_evol_110k
109,597
Write a Python function to count the number of times the element 2 appears in a given 2D array with dimensions MxN (1 <= M, N <= 100). The function should have a space complexity of O(1) and a time complexity of O(M*N). The code should start with: ```python from typing import List def count_element_2(arr: List[List[i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_element_2(arr: List[List[int]]) -> int: """ Counts the number of times the element 2 appears in a given 2D array. Args: arr (List[List[int]]): A 2D array of integers. Returns:...
magic_evol_110k
109,600
Write a Python function to parse and display every command-line argument value passed to the script. The function should handle varying numbers and types of arguments. The code should start with: ```python import sys def display_arguments(): ```
Here is the Python code to solve the given problem in the markdown block: ```python import sys def display_arguments(): """ Parses and displays every command-line argument value passed to the script. This function iterates over the command-line arguments provided to the script, excluding the script na...
magic_evol_110k
109,609
Write a Python function to determine if two given strings are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. The function should be case-insensitive and should ignore spaces. The code should ...
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. Args: str1 (str): The first string to compare. str2 (str): The second string to compare. Re...
magic_evol_110k
109,628
Write a Python function to find and return the indexes of all occurrences of a given element in a given list. The function should handle lists of any size and return an empty list if the element is not found. The code should start with: ```python from typing import List def find_all_indexes(element: int, lst: List[int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_all_indexes(element: int, lst: List[int]) -> List[int]: """ Find and return the indexes of all occurrences of a given element in a list. Args: element (int): The element to search f...
magic_evol_110k
109,629
Write a Python function to calculate the range of elements in a given list of numbers. The range is defined as the difference between the maximum and minimum values in the list. The function should handle lists of integers and floating-point numbers. The code should start with: ```python from typing import List def ca...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_range(numbers: List[float]) -> float: """ Calculate the range of elements in a given list of numbers. The range is defined as the difference between the maximum and minimum values in t...
magic_evol_110k
109,634
Write a Python function that performs an insertion sort on a list of integers using a binary search method to find the correct insertion position for each element. This approach should maintain optimal performance and efficiency by reducing the number of comparisons needed during the insertion process. The code should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from bisect import bisect_left def binary_insertion_sort(arr: List[int]) -> List[int]: """ Perform an insertion sort on a list of integers using binary search to find the correct insertion position. ...
magic_evol_110k
109,638
Write a Python function to identify the final odd numeric element in a given array that is not a multiple of five. The function should iterate through the array and keep track of the last odd number that is not a multiple of five. If no such number exists, the function should return `None`. 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_final_odd_not_multiple_of_five(array: List[int]) -> Optional[int]: """ Identify the final odd numeric element in the given array that is not a multiple of five. Args: arra...
magic_evol_110k
109,641
Write a Python function to reverse the order of a given list of integers without using built-in functions like `reverse()` or `reversed()`. The function should iterate through the list and construct a new list with elements in reverse order. The code should start with: ```python from typing import List def reverse_li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(lst: List[int]) -> List[int]: """ Reverses the order of a given list of integers without using built-in functions. Args: lst (List[int]): The list of integers to be reversed...
magic_evol_110k
109,646
Write a Python function to trace the occurrence of all unique numbers and non-integer types in a complex multi-dimensional array. The function should handle nested arrays and return a dictionary where the keys are the unique elements and the values are their respective counts. The code should start with: ```python fro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict, List def trace_unique_elements(arr: List[Any]) -> Dict[Any, int]: """ Trace the occurrence of all unique numbers and non-integer types in a complex multi-dimensional array. Args: arr (...
magic_evol_110k
109,677
Write a Python function to determine if a given array of integers is a palindrome. The function should return a boolean value (`True` if the array is a palindrome, `False` otherwise). Additionally, implement error handling to tackle any potential exceptions or unexpected inputs. The function should not use the Python b...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_array_palindrome(arr: List[int]) -> bool: """ Determine if a given array of integers is a palindrome. Args: arr (List[int]): The array of integers to check. Returns: bool...
magic_evol_110k
109,682
Write a Python function to find the minimum and maximum values in a given list of integers without using the built-in Python functions `min()` and `max()`. The function should also handle the situation when the given list is empty by returning `None` for both the minimum and maximum values. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_min_and_max(numbers: List[int]) -> Tuple[int, int]: """ Find the minimum and maximum values in a list of integers without using the built-in min() and max() functions. Args: ...
magic_evol_110k
109,684
Write a Python function to sort a given numerical array in ascending order using the merge sort algorithm. The function should be efficient and handle various edge cases, such as an empty array or an array with duplicate elements. The code should start with: ```python from typing import List def merge_sort(arr: List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts a given numerical array in ascending order using the merge sort algorithm. Args: arr (List[int]): The numerical array to be sorted. ...
magic_evol_110k
109,693
Write a Python function that checks if a sequence of words in a tuple is sorted in ascending order based on their second last character. If the sequence is not sorted, the function should sort it accordingly. Additionally, the function should handle error checking to ensure that every word in the tuple has more than on...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple, List def sort_and_check_tuple(tup: Tuple[str]) -> Tuple[str]: """ Checks if a sequence of words in a tuple is sorted in ascending order based on their second last character. If not sorted, sorts th...
magic_evol_110k
109,698
Write a Python function to sort a list of alphanumeric strings based on the numeric digit embedded in each string. Each string contains exactly one digit. The function should return the sorted list. The code should start with: ```python from typing import List def sort_by_embedded_digit(strings: List[str]) -> List[st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_by_embedded_digit(strings: List[str]) -> List[str]: """ Sorts a list of alphanumeric strings based on the numeric digit embedded in each string. Args: strings (List[str]): A list of...
magic_evol_110k
109,702
Write a Python function to sort a list of positive integers based on the length of their binary representation and their quotient when divided by a given divisor. If multiple integers have the same binary length, they should be sorted according to their quotient when divided by the divisor. The function should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_by_binary_len_and_divisor(arr: List[int], divisor: int) -> List[int]: """ Sorts a list of positive integers based on the length of their binary representation and their quotient when divided by ...