source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
19,508
Write a Python function to find the unique elements and their counts in a given array. The function should return a dictionary where the keys are the unique elements of the input array and the values are their counts. The code should start with: ```python from typing import List, Dict def count_unique_elements(arr: Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_unique_elements(arr: List[int]) -> Dict[int, int]: """ Count the unique elements and their counts in an array. Args: arr (List[int]): The input array of integers. Return...
code_exercise_27k
19,511
Write a Python function to perform element-wise multiplication of two 2D lists (matrices) without using the `numpy` library. The function should take two 2D lists as input and return a new 2D list containing the element-wise product of the input matrices. The code should start with: ```python from typing import List d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def elementwise_multiply(matrix1: List[List[int]], matrix2: List[List[int]]) -> List[List[int]]: """ Perform element-wise multiplication of two 2D lists (matrices). Args: matrix1 (List[List[...
code_exercise_27k
19,520
Write a Python function to find the maximum flow in a flow network with multiple sources and sinks using the Ford-Fulkerson algorithm. The flow network is represented as a dictionary where the keys are tuples representing edges and the values are the capacities. Each tuple `(u, v)` represents a directed edge from node ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Tuple, List from collections import defaultdict def find_max_flow_with_multiple_sources_and_sinks(network: Dict[Tuple[str, str], int], sources: List[str], sinks: List[str]) -> int: """ Finds the maximum...
code_exercise_27k
19,522
Write a Python function to compute the mean of each column in a 2-dimensional array. The function should take a 2-dimensional NumPy array as input and return a NumPy array containing the mean of each column. The code should start with: ```python import numpy as np def compute_mean_of_each_column(arr: np.ndarray) -> np...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def compute_mean_of_each_column(arr: np.ndarray) -> np.ndarray: """ Compute the mean of each column in a 2-dimensional array. Parameters: arr (np.ndarray): The input 2-dimensional array. Returns...
code_exercise_27k
19,532
Write a Python function to sort a list of strings with multiple fields based on a specific field using the Bubble Sort algorithm. Each string in the list represents a record with multiple fields separated by commas. The function should take the list of strings and the index of the field to sort on as input and return t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def bubble_sort_by_field(lst: List[str], field: int) -> List[str]: """ Sorts a list of strings with multiple fields based on a specific field using the Bubble Sort algorithm. Args: lst (List...
code_exercise_27k
19,541
Write a Python function to find the maximum number of vertex-disjoint paths between two vertices in a directed graph using a modified Depth-First Search (DFS) approach. The function should return the count of such paths. The code should start with: ```python from typing import Dict, List, Any def find_max_vertex_disj...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Any from collections import defaultdict def find_max_vertex_disjoint_paths(graph: Dict[Any, List[Any]], source: Any, target: Any) -> int: """ Find the maximum number of vertex-disjoint paths betwe...
code_exercise_27k
19,549
Write a Python function to perform Bubble Sort on a list of objects, sorting based on a specific attribute using a lambda function. The function should be able to handle lists of dictionaries, where each dictionary represents an object with various attributes. The sorting should be done in ascending order based on the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def bubble_sort_by_attribute(objects: List[Dict], key: callable) -> List[Dict]: """ Sorts a list of dictionaries based on a specific attribute using the Bubble Sort algorithm. Args: ob...
code_exercise_27k
19,550
Write a Python function to count the number of strings in a given list that have no repeated characters. The function should be synchronous and should return the count directly. The code should start with: ```python from typing import List def count_strings_with_no_repeats(strings: List[str]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_strings_with_no_repeats(strings: List[str]) -> int: """ Counts the number of strings in a given list that have no repeated characters. Args: strings (List[str]): A list of strings ...
code_exercise_27k
19,551
Write a Python function to generate a 2-dimensional array filled with Gaussian random noise. The function should allow specifying the shape of the array, the mean of the Gaussian distribution, and the standard deviation. The code should start with: ```python import numpy as np def generate_gaussian_noise_array(shape: ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def generate_gaussian_noise_array(shape: tuple, mean: float = 0.0, std: float = 1.0) -> np.ndarray: """ Generate a 2-dimensional array filled with Gaussian random noise. Args: shape (tuple): A tu...
code_exercise_27k
19,554
Write a Python function to compute the rank of a given 2-dimensional array. The rank of a matrix is defined as the maximum number of linearly independent rows or columns in the matrix. The function should handle any size of the 2-dimensional array and return the rank as an integer. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def compute_matrix_rank(matrix: np.ndarray) -> int: """ Compute the rank of a given 2-dimensional numpy array. The rank of a matrix is defined as the maximum number of linearly independent rows or column...
code_exercise_27k
19,561
Write a Python function to create a NumPy array with all elements initialized to a specified value. The function should take two parameters: the shape of the array (a tuple) and the value to initialize all elements with (a float or integer). The function should return the created NumPy array. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_array_with_value(shape: tuple, value: float) -> np.ndarray: """ Create a NumPy array with all elements initialized to a specified value. Parameters: ----------- shape : tuple T...
code_exercise_27k
19,564
Write a Python coroutine that receives a string and returns the number of words with repeated consecutive letters. The coroutine should continuously process incoming strings and yield the cumulative count of such words. The code should start with: ```python async def count_words_with_repeated_letters(): ```
Here is the Python code to solve the given problem in the markdown block: ```python async def count_words_with_repeated_letters(): """ This coroutine receives strings and returns the number of words with repeated consecutive letters. Yields: int: The cumulative count of words with repeated cons...
code_exercise_27k
19,565
Write a Python function to perform element-wise addition of two NumPy arrays. The function should ensure that the input arrays have the same shape and are of type `numpy.ndarray`. If the shapes or types do not match, appropriate exceptions should be raised. The code should start with: ```python import numpy as np def...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_addition(a: np.ndarray, b: np.ndarray) -> np.ndarray: """ Perform element-wise addition of two NumPy arrays. Parameters: a (numpy.ndarray): The first input array. b (numpy.ndarray...
code_exercise_27k
19,566
Write a Python function to perform Bubble Sort on a list of objects, sorting based on a specific attribute using the Schwartzian transform. The Schwartzian transform is a technique to improve the efficiency of sorting by reducing the number of times the key function is called. The function should take a list of objects...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def bubble_sort_schwartzian(lst: List[Any], key: callable) -> List[Any]: """ Perform bubble sort on a list of objects, sorting based on a specific attribute using the Schwartzian transform. Arg...
code_exercise_27k
19,568
Write a Python function to perform element-wise subtraction of two NumPy arrays. The function should ensure that the input arrays have the same shape and return the result of the element-wise subtraction. The code should start with: ```python import numpy as np def elementwise_subtraction(arr1: np.ndarray, arr2: np.nd...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_subtraction(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Perform element-wise subtraction of two NumPy arrays. Parameters: arr1 (numpy.ndarray): The first input array. a...
code_exercise_27k
19,570
Write a Python function to perform element-wise multiplication of two NumPy arrays. The function should ensure that the input arrays have the same shape before performing the multiplication. If the shapes do not match, the function should raise a `ValueError`. The code should start with: ```python import numpy as np ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_multiply(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Perform element-wise multiplication of two NumPy arrays. Parameters: arr1 (numpy.ndarray): The first input array. ...
code_exercise_27k
19,579
Write a Python function to calculate the square root of each element in a given NumPy array. The function should handle both positive and negative elements in the array by returning the absolute value of the square root for negative elements. The code should start with: ```python import numpy as np def square_root_arr...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def square_root_array_with_abs(arr: np.array) -> np.array: """ Calculate the square root of each element in a NumPy array. For negative elements, return the square root of their absolute value. Args:...
code_exercise_27k
19,583
Write a Python function to compute the absolute value of each element in a given NumPy array. The function should handle both real and complex numbers. The code should start with: ```python import numpy as np def compute_absolute_values(array: np.ndarray) -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def compute_absolute_values(array: np.ndarray) -> np.ndarray: """ Compute the absolute value of each element in a given NumPy array. Parameters: ---------- array : numpy.ndarray NumPy arr...
code_exercise_27k
19,586
Write a Python function to find the smallest rectangular region in a 2D grid that covers all occurrences of a specific target value. The function should return the coordinates of the top-left and bottom-right corners of this region. If the target value is not found in the grid, the function should raise a `ValueError`....
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_smallest_region(grid: List[List[int]], target: int) -> Tuple[int, int, int, int]: """ Find the smallest rectangular region in a 2D grid that covers all occurrences of a specific target va...
code_exercise_27k
19,588
Write a Python function to compute the exponential of each element in a given NumPy array. The function should handle both 1D and 2D arrays and return a NumPy array of the same shape with each element exponentiated. The code should start with: ```python import numpy as np def compute_exponential(arr: np.ndarray) -> n...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def compute_exponential(arr: np.ndarray) -> np.ndarray: """ Compute the exponential of each element in a given NumPy array. Parameters: - arr: NumPy array of shape (n,) or (n, m), where n and m repre...
code_exercise_27k
19,595
Write a Python function to calculate the mean of all elements in a given NumPy array. The function should handle both 1D and 2D arrays and return the mean value as a float. The code should start with: ```python import numpy as np def calculate_mean(arr: np.ndarray) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def calculate_mean(arr: np.ndarray) -> float: """ Calculate the mean of all elements in a given NumPy array. Args: arr (numpy.ndarray): The input NumPy array. Returns: float: The mea...
code_exercise_27k
19,612
Write a Python function to calculate the sum of all elements in a given NumPy array. The function should handle arrays of any shape and dimension. The code should start with: ```python import numpy as np def sum_elements(arr: np.ndarray) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def sum_elements(arr: np.ndarray) -> float: """ Calculate the sum of all elements in a given NumPy array. Args: arr (np.ndarray): The NumPy array whose elements are to be summed. Returns: ...
code_exercise_27k
19,617
Write a Python function to implement an adaptive variant of Bubble Sort that adjusts the length of the inner loop dynamically. The function should sort a given list of elements and return the sorted list. The code should start with: ```python from typing import List def adaptive_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 adaptive_bubble_sort(arr: List[int]) -> List[int]: """ Sorts a given list using an adaptive variant of Bubble Sort. This algorithm adjusts the length of the inner loop dynamically by tracking th...
code_exercise_27k
19,624
Write a Python function to compute the cumulative sum of elements in a given list of integers. The cumulative sum at each position should be the sum of all elements up to and including that position in the list. The function should return a new list containing these cumulative sums. 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 def cumulative_sum(arr: List[int]) -> List[int]: """ Compute the cumulative sum of elements in a given list of integers. Args: arr (List[int]): The input list of integers. Returns: ...
code_exercise_27k
19,626
Write a Python function to compute the cumulative product of elements in a given list of integers. The cumulative product at position `i` is the product of all elements from the start of the list up to and including the element at position `i`. The function should not use any external libraries like NumPy, and should b...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def cumulative_product(arr: List[int]) -> List[int]: """ Compute the cumulative product of elements in a given list of integers. Args: arr (List[int]): The input list of integers. Retur...
code_exercise_27k
19,632
Write a Python function to count the number of non-zero elements in a given NumPy array. The function should handle both 1D and 2D arrays and return the count of non-zero elements. The code should start with: ```python import numpy as np def count_nonzero_elements(arr: np.ndarray) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def count_nonzero_elements(arr: np.ndarray) -> int: """ Count the number of non-zero elements in a given NumPy array. Parameters: arr (numpy.ndarray): The input array. Returns: int: The coun...
code_exercise_27k
19,651
Write a Python function to multiply each element in a given NumPy array by a specified scalar value. The function should handle both 1D and 2D arrays and return the resulting array. The code should start with: ```python import numpy as np def multiply_array_by_scalar(arr: np.ndarray, scalar: float) -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def multiply_array_by_scalar(arr: np.ndarray, scalar: float) -> np.ndarray: """ Multiply each element in a NumPy array by a specified scalar value. Parameters: arr (np.ndarray): The input NumPy array...
code_exercise_27k
19,653
Write a Python function to compute the element-wise addition of a scalar to a NumPy array. The function should take a NumPy array and a scalar as inputs and return a new NumPy array where each element is the sum of the corresponding element in the input array and the scalar. The code should start with: ```python impor...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def add_scalar_to_array(arr: np.ndarray, scalar: float) -> np.ndarray: """ Compute the element-wise addition of a scalar to a NumPy array. Parameters: arr (np.ndarray): The input NumPy array. sca...
code_exercise_27k
19,655
Write a Python function to compute the element-wise subtraction of a scalar from a NumPy array. The function should take a NumPy array and a scalar as inputs and return a new NumPy array where each element is the result of subtracting the scalar from the corresponding element in the input array. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def subtract_scalar_from_array(arr: np.ndarray, scalar: float) -> np.ndarray: """ Subtract a scalar from each element of a NumPy array. Parameters: arr (ndarray): The input NumPy array. scalar (f...
code_exercise_27k
19,663
Write a Python function to find the index of the minimum element in a given NumPy array. The function should handle empty arrays by raising a `ValueError`. The code should start with: ```python import numpy as np def find_min_index(arr: np.ndarray) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def find_min_index(arr: np.ndarray) -> int: """ Finds the index of the minimum element in a NumPy array. Args: arr (np.ndarray): The input NumPy array. Returns: int: The index of the...
code_exercise_27k
19,666
Write a Python function to compute the element-wise absolute difference between two NumPy arrays. The function should ensure that the input arrays have the same shape and return the element-wise absolute difference as a new NumPy array. The code should start with: ```python import numpy as np def elementwise_absolute_...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_absolute_difference(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Compute the element-wise absolute difference between two NumPy arrays. Parameters: arr1 (np.ndarray): First ...
code_exercise_27k
19,671
Write a Python function to compute the element-wise absolute difference between a NumPy array and a scalar. The function should handle one-dimensional arrays and return a new array with the same shape as the input array, containing the absolute differences. The code should start with: ```python import numpy as np def ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_abs_diff(array: np.ndarray, scalar: float) -> np.ndarray: """ Compute the element-wise absolute difference between a NumPy array and a scalar. Parameters: array (np.ndarray): A one-di...
code_exercise_27k
19,672
Write a Python function to replace all negative elements in a given NumPy array with zero. The function should handle any shape of the input array and return the modified array. The code should start with: ```python import numpy as np def replace_negatives_with_zero(arr: np.ndarray) -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def replace_negatives_with_zero(arr: np.ndarray) -> np.ndarray: """ Replaces all negative elements in a NumPy array with zero. Args: arr (numpy.ndarray): The input NumPy array. Returns: ...
code_exercise_27k
19,680
Write a Python function to sort a given list in ascending order using the selection sort algorithm. The function should be able to handle lists of integers and return the sorted list. The code should start with: ```python from typing import List def selection_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 selection_sort(arr: List[int]) -> List[int]: """ Sorts a list in ascending order using the selection sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Return...
code_exercise_27k
19,682
Write a Python function to compute the element-wise sign of a given NumPy array. The function should return a new NumPy array with the same shape as the input array, where each element is: - `-1` if the corresponding element in the input array is negative, - `0` if the corresponding element in the input array is zero, ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def element_wise_sign(arr: np.ndarray) -> np.ndarray: """ Compute the element-wise sign of a given NumPy array. Parameters: - arr (np.ndarray): Input NumPy array. Returns: - np.ndarray: NumP...
code_exercise_27k
19,683
Write a Python function to sort a given list in descending order using the Selection Sort algorithm. The function should start with: ```python from typing import List def selection_sort_descending(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 selection_sort_descending(arr: List[int]) -> List[int]: """ Sorts a given list in descending order using the Selection Sort algorithm. Args: arr (List[int]): The list to be sorted. ...
code_exercise_27k
19,686
Write a Python function to sort a list of integers using the selection sort algorithm and print the steps involved in each iteration. The function should return the sorted list. The code should start with: ```python from typing import List def selection_sort_with_steps(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 selection_sort_with_steps(arr: List[int]) -> List[int]: """ Sorts a list of integers using the selection sort algorithm and prints the steps involved in each iteration. Args: arr (List[i...
code_exercise_27k
19,688
Write a Python function to sort a list of strings alphabetically using the selection sort algorithm. The function should handle lists of varying lengths and should be able to sort strings in both ascending and descending order based on a provided boolean flag. 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 selection_sort_strings(strings: List[str], ascending: bool = True) -> List[str]: """ Sorts the given list of strings alphabetically using the selection sort algorithm. Args: strings (Lis...
code_exercise_27k
19,690
Write a Python function to compute the element-wise floor division of two NumPy arrays. The function should handle the following: 1. Ensure the input arrays have the same shape. 2. Perform element-wise floor division. 3. Raise a `ValueError` if division by zero is attempted. The code should start with: ```python impor...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def element_wise_floor_division(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Compute the element-wise floor division of two NumPy arrays. Parameters: arr1 (numpy.ndarray): The first NumPy a...
code_exercise_27k
19,692
Write a Python function to compute the element-wise modulo (remainder) of two NumPy arrays. The function should ensure that the input arrays have the same shape and return an array containing the element-wise modulo results. The code should start with: ```python import numpy as np def elementwise_modulo(array1: np.nda...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_modulo(array1: np.ndarray, array2: np.ndarray) -> np.ndarray: """ Compute the element-wise modulo (remainder) of two NumPy arrays. Args: array1 (np.ndarray): The first input array...
code_exercise_27k
19,693
Write a Python function to calculate the total weight of a minimum spanning tree (MST) using Kruskal's algorithm. The function should take a graph represented as a dictionary where the keys are nodes and the values are lists of tuples representing edges connected to that node. Each edge tuple contains a neighbor node a...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def calculate_mst_weight(graph: Dict[str, List[Tuple[str, int]]]) -> int: """ Calculates the total weight of a minimum spanning tree using Kruskal's algorithm. Args: graph: ...
code_exercise_27k
19,695
Write a Python function to implement the selection sort algorithm to sort a list of floating-point numbers in descending order. The function should return a new list with the elements sorted in descending order without modifying the original list. The code should start with: ```python from typing import List def sele...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def selection_sort_descending(arr: List[float]) -> List[float]: """ Sorts a list of floating-point numbers in descending order using the selection sort algorithm. Args: arr (List[float]): Li...
code_exercise_27k
19,697
Write a Python function to sort a list of tuples based on the second element of each tuple using the insertion sort algorithm. The function should start with: ```python from typing import List, Tuple def insertion_sort_tuples(arr: List[Tuple[any, int]]) -> List[Tuple[any, int]]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def insertion_sort_tuples(arr: List[Tuple[any, int]]) -> List[Tuple[any, int]]: """ Sorts a list of tuples based on the second element of each tuple using insertion sort. Args: arr (L...
code_exercise_27k
19,700
Write a Python function to sort a list of dictionaries based on a specific key's value in ascending order using the selection sort algorithm. The function should not modify the original list but return a new sorted list. The code should start with: ```python from typing import List, Dict def sort_dicts_by_key(dicts_li...
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(dicts_list: List[Dict[str, any]], key: str) -> List[Dict[str, any]]: """ Sorts a list of dictionaries based on a specific key's value in ascending order using the selection so...
code_exercise_27k
19,702
Write a Python function to sort a list of dates in ascending order using the selection sort algorithm. The dates are provided as strings in the format 'YYYY-MM-DD'. The function should return a new list with the dates sorted in ascending order. The code should start with: ```python from typing import List def sort_da...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_dates_with_selection_sort(dates: List[str]) -> List[str]: """ Sorts a list of dates in ascending order using the selection sort algorithm. Args: dates (List[str]): A list of date st...
code_exercise_27k
19,707
Write a Python function to check if a given weighted graph is connected. A graph is considered connected if there is a path between every pair of vertices. The graph is represented as a dictionary where the keys are the vertices and the values are dictionaries representing the edges from that vertex, with each key-valu...
Here is the Python code to solve the given problem in the markdown block: ```python def is_graph_connected(graph: dict) -> bool: """ Check if a given weighted graph is connected. Args: graph (dict): A dictionary representing a weighted graph. The keys of the dictionary should be the vertices of the gra...
code_exercise_27k
19,713
Write a Python function to count the number of `False` elements in a given NumPy array. The function should efficiently handle arrays of various shapes and sizes. The code should start with: ```python import numpy as np def count_false_elements(arr: np.ndarray) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def count_false_elements(arr: np.ndarray) -> int: """ Count the number of False elements in a given NumPy array. Args: arr (numpy.ndarray): The input NumPy array. Returns: int: The n...
code_exercise_27k
19,715
Write a Python function to check if any element in a given NumPy array is `True`. The function should handle arrays of any shape and type, and return a boolean value indicating whether any element in the array is `True`. The code should start with: ```python import numpy as np def check_any_true(arr: np.ndarray) -> bo...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def check_any_true(arr: np.ndarray) -> bool: """ Check if any element in a given NumPy array is True. Args: arr (np.ndarray): The NumPy array to check. Returns: bool: True if any ele...
code_exercise_27k
19,718
Write a Python function to find the vertices with the maximum and minimum degrees in a given graph. The graph is represented as a dictionary where the keys are the vertices and the values are lists of adjacent vertices. The function should return a tuple containing two elements: the vertex with the maximum degree and t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def find_degree_bounds(graph: Dict[str, List[str]]) -> Tuple[str, str]: """ Finds the vertices with the maximum and minimum degrees in a graph. Args: graph (Dict[str, List[str]]...
code_exercise_27k
19,720
Write a Python function to check if all elements in a given NumPy array are `True`. The function should return `True` if all elements in the array are `True`, and `False` otherwise. The code should start with: ```python import numpy as np def all_elements_true(array: np.ndarray) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def all_elements_true(array: np.ndarray) -> bool: """ Check if all elements in a given NumPy array are True. Args: array (np.ndarray): The NumPy array to check. Returns: bool: True i...
code_exercise_27k
19,721
Write a Python function to sort a given list of integers in ascending order using the selection sort algorithm. The function should not use any built-in sorting functions. The code should start with: ```python from typing import List def selection_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 selection_sort(arr: List[int]) -> List[int]: """ Sorts a given list of integers in ascending order using the selection sort algorithm. Args: arr (List[int]): The list of integers to be s...
code_exercise_27k
19,723
Write a Python function to find the unique elements in a given list of integers. The function should return a list containing only the unique elements, sorted in ascending order. The code should start with: ```python from typing import List def find_unique_elements(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 find_unique_elements(arr: List[int]) -> List[int]: """ Find the unique elements in a given list of integers and return them sorted in ascending order. Args: arr (List[int]): The input ...
code_exercise_27k
19,728
Write a Python function to sort a list of floating-point numbers in descending order using scientific notation. The function should implement the selection sort algorithm. The code should start with: ```python from typing import List def selection_sort_descending(numbers: List[float]) -> List[float]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def selection_sort_descending(numbers: List[float]) -> List[float]: """ Sorts a list of floating-point numbers in descending order using scientific notation. Args: numbers (List[float]): A l...
code_exercise_27k
19,737
Write a Python function to determine if a given directed graph contains a cycle. The graph is represented as an adjacency list (dictionary of lists). The function should use Depth First Search (DFS) to detect cycles. The code should start with: ```python from typing import Dict, List def has_cycle(graph: Dict[int, Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def has_cycle(graph: Dict[int, List[int]]) -> bool: """ Determine if the given directed graph contains a cycle using Depth First Search (DFS). Args: graph (Dict[int, List[int]]): The g...
code_exercise_27k
19,745
Write a Python function to find the edge with the maximum weight in a given graph. The graph is represented as a dictionary where the keys are tuples representing edges (u, v) and the values are the weights of the corresponding edges. The function should return a tuple containing the edge with the maximum weight and it...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Tuple def find_max_weight_edge(graph: Dict[Tuple[int, int], int]) -> Tuple[Tuple[int, int], int]: """ Finds the edge with the maximum weight in a given graph. Args: graph (dict): A dictiona...
code_exercise_27k
19,758
Write a Python function to compute the element-wise sum of two NumPy arrays. The function should ensure that the input arrays have the same shape before performing the element-wise addition. If the shapes do not match, the function should raise a `ValueError`. The code should start with: ```python import numpy as np d...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_sum(a: np.ndarray, b: np.ndarray) -> np.ndarray: """ Compute the element-wise sum of two NumPy arrays. Parameters: a (np.ndarray): The first input NumPy array. b (np.ndarray): The...
code_exercise_27k
19,764
Write a Python function to compute the element-wise difference of two NumPy arrays. The function should ensure that the input arrays have the same shape and return an array containing the element-wise difference. The code should start with: ```python import numpy as np def elementwise_difference(arr1: np.ndarray, arr2...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_difference(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Compute the element-wise difference of two NumPy arrays. Parameters: arr1 (numpy.ndarray): The first input array. ...
code_exercise_27k
19,767
Write a Python function to compute the element-wise product of two NumPy arrays. The function should ensure that the input arrays have the same shape before performing the element-wise multiplication. If the shapes do not match, the function should raise a `ValueError`. The code should start with: ```python import num...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def element_wise_product(array1: np.ndarray, array2: np.ndarray) -> np.ndarray: """ Compute the element-wise product of two NumPy arrays. Parameters: - array1 (np.ndarray): The first input array. ...
code_exercise_27k
19,775
Write a Python function to perform element-wise comparison of two NumPy arrays and return a boolean array indicating where the elements are equal. The function should handle arrays of any shape and ensure that the comparison is done correctly for each element. The code should start with: ```python import numpy as np ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_comparison(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Perform element-wise comparison of two NumPy arrays. Parameters: arr1 (numpy.ndarray): First input array. arr2 (n...
code_exercise_27k
19,779
Write a Python function to compute the element-wise logical OR operation of two NumPy arrays. The function should ensure that the input arrays have the same shape and return the resulting array after applying the element-wise logical OR operation. The code should start with: ```python import numpy as np def elementwis...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_logical_or(array1: np.ndarray, array2: np.ndarray) -> np.ndarray: """ Compute the element-wise logical OR operation of two NumPy arrays. Parameters: array1 (numpy.ndarray): The fi...
code_exercise_27k
19,783
Write a Python function to check if any element in a given NumPy array is finite. The function should return `True` if at least one element in the array is finite, and `False` otherwise. The code should start with: ```python import numpy as np def is_any_element_finite(arr: np.ndarray) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def is_any_element_finite(arr: np.ndarray) -> bool: """ Check if any element in a given NumPy array is finite. Args: arr (numpy.ndarray): The input NumPy array. Returns: bool: True i...
code_exercise_27k
19,788
Write a Python function to sort a list of numbers in descending order, considering both odd and even numbers separately using the selection sort algorithm. The function should first sort all odd numbers in descending order, then sort all even numbers in descending order, and finally concatenate the two sorted lists. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def selection_sort_descending(numbers: List[int]) -> List[int]: """ Sorts a list of numbers in descending order, considering odds and evens separately using selection sort. Args: numbers (Li...
code_exercise_27k
19,790
Write a Python function to find the edge with the minimum weight in a given graph. The graph is represented as a dictionary where the keys are vertices and the values are lists of tuples representing the edges. Each tuple represents an edge as `(neighbour, weight)`. The function should return a tuple representing the e...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def find_min_weight_edge(graph: Dict[str, List[Tuple[str, int]]]) -> Tuple[str, str, int]: """ Find the edge with the minimum weight in the given graph. Args: graph (dict): A di...
code_exercise_27k
19,795
Write a Python function to find the maximum weight in a connected subgraph of a graph. The graph is represented as an adjacency list where each key is a node and the value is a list of neighboring nodes. Each node has an associated weight, which is stored in a separate dictionary. The function should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_max_weight_in_subgraph(graph: Dict[int, List[int]], weights: Dict[int, int], start_node: int) -> int: """ Find the maximum weight in a connected subgraph of a graph. Args: gra...
code_exercise_27k
19,805
Write a Python function to concatenate two NumPy arrays along the horizontal axis (axis=1). The function should ensure that the arrays have the same number of rows before concatenation. If the arrays do not have the same number of rows, the function should raise a `ValueError`. The code should start with: ```python im...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def concatenate_arrays_horizontally(array1: np.ndarray, array2: np.ndarray) -> np.ndarray: """ Concatenates two NumPy arrays along the horizontal axis (axis=1). Parameters: array1 (numpy.ndarray): Fi...
code_exercise_27k
19,806
Write a Python function to stack two NumPy arrays vertically. The function should take two NumPy arrays as input and return a new array that is the result of vertically stacking the two input arrays. The code should start with: ```python import numpy as np def stack_arrays_vertically(arr1: np.ndarray, arr2: np.ndarray...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def stack_arrays_vertically(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Vertically stack two NumPy arrays. Parameters: - arr1 (numpy.ndarray): The first array to stack. - arr2 (numpy.n...
code_exercise_27k
19,809
Write a Python function to stack two NumPy arrays horizontally. The function should take two NumPy arrays as input and return a new array that is the result of horizontally stacking the two input arrays. The code should start with: ```python import numpy as np def stack_arrays_horizontally(a: np.ndarray, b: np.ndarray...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def stack_arrays_horizontally(a: np.ndarray, b: np.ndarray) -> np.ndarray: """ Horizontally stack two NumPy arrays. Parameters: a (numpy.ndarray): The first array to stack. b (numpy.ndarray): The...
code_exercise_27k
19,814
Write a Python function to sort a list of integers in ascending order using the selection sort algorithm, and separate prime numbers from non-prime numbers. The prime numbers should appear first in the sorted list, followed by the non-prime numbers. The function 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 is_prime(n: int) -> bool: """Check if a number is prime. Args: n (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ if n < 2: ...
code_exercise_27k
19,816
Write a Python function to find the indices where a given condition is satisfied in a NumPy array. The function should return a list of tuples, where each tuple represents the indices of the array where the condition is true. The code should start with: ```python import numpy as np from typing import List, Tuple def f...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np from typing import List, Tuple def find_indices_where_condition_satisfied(condition: np.ndarray, array: np.ndarray) -> List[Tuple[int, ...]]: """ Find the indices where a given condition is satisfied in a Num...
code_exercise_27k
19,822
Write a Python function to trim the leading zeros from a given 1-dimensional NumPy array. The function should return a new NumPy array with all leading zeros removed. If the input array consists entirely of zeros, the function should return an empty array. The function should raise a `ValueError` if the input array is ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def trim_leading_zeros(arr: np.ndarray) -> np.ndarray: """ Trim leading zeros from a 1-dimensional NumPy array. Parameters: arr (numpy.ndarray): The input 1-dimensional NumPy array. Returns: ...
code_exercise_27k
19,832
Write a Python function to compute the element-wise natural logarithm (base e) of a given NumPy array. The function should handle the input validation to ensure that the input is a NumPy array. The code should start with: ```python import numpy as np def elementwise_ln(arr: np.ndarray) -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_ln(arr: np.ndarray) -> np.ndarray: """ Compute the element-wise natural logarithm (base e) of a NumPy array. Parameters: arr (np.ndarray): The input NumPy array. Returns: np....
code_exercise_27k
19,839
Write a Python function to sort a list of integers in ascending order using the selection sort algorithm, ensuring that even numbers are placed before odd numbers in the sorted list. The function should start with: ```python from typing import List def selection_sort_even_first(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 selection_sort_even_first(numbers: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using selection sort, ensuring even numbers come before odd numbers. Args: num...
code_exercise_27k
19,843
Write a Python function to clip (limit) the values in a list within a specified range. The function should replace any value in the list that is below the lower limit with the lower limit, and any value that is above the upper limit with the upper limit. The function should return a new list with the clipped values. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def clip_list(lst: List[float], lower: float, upper: float) -> List[float]: """ Clip the values in a list within a specified range. Parameters: lst (List[float]): The input list of values. l...
code_exercise_27k
19,849
Write a Python function to determine if a given graph is regular. A graph is considered regular if all its vertices have the same degree (number of edges connected to the vertex). The graph is represented as a dictionary where the keys are vertices and the values are sets of neighboring vertices. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Set def is_regular_graph(graph: Dict[int, Set[int]]) -> bool: """ Determine if a given graph is regular. A graph is regular if all its vertices have the same degree. Args: graph (Dict[...
code_exercise_27k
19,859
Write a Python function to find the minimum spanning tree (MST) of a given connected graph using Prim's algorithm with a binary heap. The graph is represented as a dictionary where each key is a node and each value is a list of tuples representing the adjacent nodes and their edge weights. The function should return th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple import heapq def prim_mst(graph: Dict[str, List[Tuple[str, int]]]) -> List[Tuple[str, str]]: """ Finds the minimum spanning tree of a connected graph using Prim's algorithm with a binary hea...
code_exercise_27k
19,861
Write a Python function to sort a list of integers in ascending order using the selection sort algorithm, while separating square numbers from non-square numbers. The function should return a list where all square numbers appear before non-square numbers, both subsets being sorted in ascending order. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def selection_sort_with_squares(lst: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using selection sort, separating square numbers from non-square numbers. ...
code_exercise_27k
19,864
Write a Python function to compute the element-wise square root of the sum of squares of two NumPy arrays. The function should ensure that the input arrays have the same shape and return an array of the same shape containing the computed values. The code should start with: ```python import numpy as np def elementwise_...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def elementwise_sqrt_sum_of_squares(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray: """ Computes the element-wise square root of the sum of squares of two NumPy arrays. Args: arr1 (np.ndarray)...
code_exercise_27k
19,869
Write a Python function to generate a random sample from a given NumPy array. The function should allow for both sampling a specified number of elements and sampling a specific shape of the output array. The function should handle both 1D and multi-dimensional arrays. The code should start with: ```python import numpy...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def generate_random_sample(arr: np.ndarray, size: int or tuple) -> np.ndarray: """ Generates a random sample from a given NumPy array. Parameters: arr (numpy.ndarray): The input NumPy array. size...
code_exercise_27k
19,871
Write a Python function to sort a list of numbers in descending order based on their binary representation. The input list contains numbers represented as binary strings, and the function should return the sorted list in descending order. The code should start with: ```python from typing import List def sort_binary_n...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_binary_numbers(arr: List[str]) -> List[str]: """ Sorts a list of numbers in descending order based on their binary representation. Args: arr (List[str]): The input list of numbers r...
code_exercise_27k
19,874
Write a Python function to sort a list of strings in ascending order based on the number of consecutive repeating characters using the selection sort algorithm. The function should count the number of consecutive repeating characters in each string and use this count to determine the order of the strings in the sorted ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_consecutive_repeating_chars(word: str) -> int: """ Counts the number of consecutive repeating characters in a word. Args: word (str): The word to count consecutive repeating ch...
code_exercise_27k
19,876
Write a Python function to count the number of occurrences of a specific element in a given NumPy array. The function should efficiently handle large arrays and return the count of the specified element. The code should start with: ```python import numpy as np def count_occurrences(arr: np.ndarray, element) -> int: `...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def count_occurrences(arr: np.ndarray, element) -> int: """ Count the number of occurrences of a specific element in a NumPy array. Args: arr (np.ndarray): The input NumPy array in which to count...
code_exercise_27k
19,877
Write a Python function to sort a list of strings based on the sum of the ASCII values of the digits in each string using a modified selection sort algorithm. The function should handle lists containing strings with varying lengths and characters, and it should only consider the ASCII values of digits ('0' to '9') for ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_digit_ascii_sum(array: List[str]) -> List[str]: """ Sorts a list of strings based on the sum of the ASCII values of the digits in each string. Args: array (List[str]): Th...
code_exercise_27k
19,880
Write a Python function to sort a list of integers in ascending order using the selection sort algorithm, and separate palindrome numbers from non-palindrome numbers. The function should return a list where non-palindrome numbers are placed before palindrome numbers, both sorted in ascending order. The code should sta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_palindrome(num: int) -> bool: """ Check if a given number is a palindrome. Parameters: num (int): The number to check. Returns: bool: True if the number is a palindrome, False ot...
code_exercise_27k
19,881
Write a Python function to reshape a 1-D NumPy array into a row matrix. A row matrix is a 2-D array where each element of the original array is placed in a separate row. The function should take a 1-D NumPy array as input and return a 2-D NumPy array with each element of the original array in a separate row. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def reshape_to_row_matrix(arr: np.ndarray) -> np.ndarray: """ Reshape a 1-D NumPy array into a row matrix. Parameters: arr (numpy.ndarray): The 1-D NumPy array to be reshaped. Returns: numpy...
code_exercise_27k
19,884
Write a Python function to check if a 2-D NumPy array is row-wise sorted in descending order. The function should return `True` if each row of the array is sorted in descending order, and `False` otherwise. The code should start with: ```python import numpy as np def is_rowwise_sorted_descending(arr: np.ndarray) -> bo...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def is_rowwise_sorted_descending(arr: np.ndarray) -> bool: """ Check if a 2-D NumPy array is row-wise sorted in descending order. Args: arr (np.ndarray): A 2-D NumPy array to be checked. Ret...
code_exercise_27k
19,891
Write a Python function to sort a list of strings based on their similarity to a target string using the Jaccard similarity coefficient. The function should use the selection sort algorithm to achieve this. The code should start with: ```python from typing import List def sort_strings_by_similarity(strings: List[str],...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def jaccard_similarity(string1: str, string2: str) -> float: """ Calculate the Jaccard similarity coefficient between two strings. The Jaccard similarity coefficient measures the similarity between ...
code_exercise_27k
19,894
Write a Python function to find the maximum element in a 1-dimensional NumPy array. The function should handle arrays of integers and floating-point numbers. The code should start with: ```python import numpy as np def find_max_element(arr: np.ndarray) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def find_max_element(arr: np.ndarray) -> float: """ Find the maximum element in a 1-dimensional NumPy array. Args: arr (np.ndarray): A 1-dimensional NumPy array containing integers or floating-po...
code_exercise_27k
19,896
Write a Python function to find the minimum element in a 2-dimensional Numpy array. The function should handle empty arrays by raising a `ValueError`. The code should start with: ```python import numpy as np def find_minimum(arr: np.ndarray) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def find_minimum(arr: np.ndarray) -> float: """ Find the minimum element in a 2-dimensional Numpy array. Args: arr (numpy.ndarray): A 2-dimensional Numpy array. Returns: float: The m...
code_exercise_27k
19,899
Write a Python function to calculate the sum of all elements in a 1-dimensional Numpy array. The function should ensure that the input is a 1-dimensional array and raise a `ValueError` if it is not. The code should start with: ```python import numpy as np def calculate_array_sum(arr: np.ndarray) -> float or int: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def calculate_array_sum(arr: np.ndarray) -> float or int: """ Calculate the sum of all elements in a 1-dimensional Numpy array. Parameters: arr (numpy.ndarray): The input 1-dimensional Numpy array. ...
code_exercise_27k
19,903
Write a Python function to calculate the product of all elements in a 2-dimensional NumPy array. The function should handle only 2-dimensional arrays and return the product as a float. If the input array is not 2-dimensional, the function should raise a `ValueError`. The code should start with: ```python import numpy ...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def calculate_array_product(array: np.ndarray) -> float: """ Calculate the product of all elements in a 2-dimensional NumPy array. Parameters: array (np.ndarray): The input 2-dimensional NumPy ar...
code_exercise_27k
19,907
Write a Python function to calculate the mean of a 1-dimensional NumPy array. The function should handle the case where the input array is not 1-dimensional by raising a `TypeError`. The code should start with: ```python import numpy as np def calculate_mean(arr: np.ndarray) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def calculate_mean(arr: np.ndarray) -> float: """ Calculates the mean of a 1-dimensional NumPy array. Args: arr (np.ndarray): The input 1-dimensional NumPy array. Returns: float: The...
code_exercise_27k
19,917
Write a Python function to calculate the cumulative sum of elements in a 1-dimensional list. The function should take a list of integers as input and return a new list where each element is the cumulative sum of the elements up to that position in the input list. 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 cumulative_sum(arr: List[int]) -> List[int]: """ Calculate the cumulative sum of elements in a 1-dimensional list. Args: arr (List[int]): The 1-dimensional list of integers. Returns...
code_exercise_27k
19,918
Write a Python function to sort a list of numbers in descending order based on their octal representation using the selection sort algorithm. The function should handle a list of octal strings and return a new list of octal strings sorted in descending order. The function should raise a `ValueError` if any of the eleme...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def selection_sort_octal_desc(list_of_numbers: List[str]) -> List[str]: """ Sorts a list of octal strings in descending order using the selection sort algorithm. Args: list_of_numbers (List[...
code_exercise_27k
19,919
Write a Python function to calculate the cumulative product of elements in a 1-dimensional list of integers. The function should return a new list containing the cumulative product of the elements. The code should start with: ```python from typing import List def cumulative_product(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 cumulative_product(arr: List[int]) -> List[int]: """ Calculate the cumulative product of elements in a 1-dimensional list of integers. Args: arr (List[int]): A 1-dimensional list of inte...
code_exercise_27k
19,924
Write a Python function to sort a list of strings based on the average ASCII value of the characters in each string using a modified selection sort algorithm. The average ASCII value of a string is calculated by summing the ASCII values of all characters in the string and dividing by the length of the string. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_ascii_average(lst: List[str]) -> List[str]: """ Sorts a list of strings based on the average ASCII value of the characters in each string. Args: lst (List[str]): The list...
code_exercise_27k
19,937
Write a Python function to sort a list of complex numbers in descending order using the selection sort algorithm. The sorting should be based on the real part of the complex numbers. If two complex numbers have the same real part, the sorting should then be based on the imaginary part. 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 selection_sort_complex(numbers: List[complex]) -> List[complex]: """ Sorts a list of complex numbers in descending order using the selection sort algorithm. The sorting is based on the real part...
code_exercise_27k
19,939
Write a Python function to sort a list of strings in ascending order based on the number of words that contain only digits within each string. Use the selection sort algorithm for this purpose. The function should start with: ```python from typing import List def sort_strings_by_digit_count(strings: List[str]) -> List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_digit_only_words(string: str) -> int: """ Counts the number of words in a string that contain only digits. Args: string (str): The string to count digit-only words in. Returns...
code_exercise_27k
19,958
Write a Python function to find the index of the maximum element along a specific axis in a 3-dimensional NumPy array. The function should handle the case where the axis is out of range by raising a `ValueError`. The code should start with: ```python import numpy as np def find_max_index(arr: np.ndarray, axis: int) ->...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def find_max_index(arr: np.ndarray, axis: int) -> np.ndarray: """ Find the index of the maximum element along a specific axis in a 3-dimensional NumPy array. Args: arr (np.ndarray): A 3-dimension...
code_exercise_27k
19,959
Write a Python function to sort a list of fractions in descending order using the selection sort algorithm. The function should handle only fractions and raise a `TypeError` if any element in the input list is not a `Fraction`. The code should start with: ```python from typing import List from fractions import Fraction...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from fractions import Fraction def selection_sort_fractions_descending(numbers: List[Fraction]) -> List[Fraction]: """ Sorts a list of fractions in descending order using the selection sort algorithm. ...
code_exercise_27k
19,965
Write a Python function to calculate the factorial of each element in a 1-dimensional NumPy array. The function should handle both positive and zero values efficiently. The code should start with: ```python import numpy as np from math import factorial def factorial_of_array(arr: np.ndarray) -> np.ndarray: ```
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np from math import factorial def factorial_of_array(arr: np.ndarray) -> np.ndarray: """ Calculate the factorial of each element in a 1-dimensional NumPy array. Args: arr (np.ndarray): A 1-dimension...