source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 19,987 | Write a Python function to sort a list of numbers represented as continued fractions in descending order using the selection sort algorithm. A continued fraction is represented as a list of integers, where the first element is the integer part, and the subsequent elements are the coefficients of the continued fraction.... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def compare_continued_fractions(cf1: List[int], cf2: List[int]) -> int:
"""
Compares two continued fractions represented as lists of integers.
Args:
cf1 (list): The first continued fract... |
code_exercise_27k | 19,990 | Write a Python function to create a square identity matrix of size n x n using NumPy. The function should return a NumPy array representing the identity matrix. The code should start with:
```python
import numpy as np
def create_identity_matrix(n: int) -> np.ndarray:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_identity_matrix(n: int) -> np.ndarray:
"""
Create a square identity matrix of size n x n using NumPy.
Args:
n (int): The size of the square identity matrix.
Returns:
numpy... |
code_exercise_27k | 20,012 | Write a Python function to sort a list using the insertion 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 insertion_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 insertion_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Insertion Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
... |
code_exercise_27k | 20,014 | Write a Python function to sort a list containing both positive and negative numbers using the insertion sort algorithm. The function should handle lists with both positive and negative integers, and it should return the sorted list.
The code should start with:
```python
from typing import List
def insertion_sort_mix... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_mixed_numbers(lst: List[int]) -> List[int]:
"""
Sorts a list containing both positive and negative numbers using the insertion sort algorithm.
Args:
lst (List[int]): The l... |
code_exercise_27k | 20,015 | Write a Python function to sort a list containing duplicate elements using the insertion sort algorithm. The function should handle lists with duplicate elements and ensure that the relative order of duplicate elements is preserved (i.e., it should be a stable sort).
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
def insertion_sort_with_duplicates(lst: List[int]) -> List[int]:
"""
Sorts a list containing duplicate elements using the insertion sort algorithm.
Parameters:
lst (List[int]): The list to b... |
code_exercise_27k | 20,020 | Write a Python function to sort a list that is already sorted in descending order using the insertion sort algorithm. The function should return the list sorted in ascending order. The code should start with:
```python
from typing import List
def sort_descending_to_ascending(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 sort_descending_to_ascending(arr: List[int]) -> List[int]:
"""
Sorts a list that is already sorted in descending order using the insertion sort algorithm.
Args:
arr (List[int]): The inpu... |
code_exercise_27k | 20,023 | Write a Python function to sort a list using the Insertion Sort algorithm. The function should handle lists of any length, including empty lists and lists with a single element. The function should return the sorted list.
The code should start with:
```python
from typing import List
def insertion_sort(lst: List[int])... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort(lst: List[int]) -> List[int]:
"""
Sorts a list using the Insertion Sort algorithm.
Args:
lst (List[int]): The list of integers to be sorted.
Returns:
List[int... |
code_exercise_27k | 20,025 | Write a Python function to sort a list with a large number of elements using the insertion sort algorithm. The function should be efficient and handle large datasets. The code should start with:
```python
from typing import List
def efficient_insertion_sort(items: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def efficient_insertion_sort(items: List[int]) -> List[int]:
"""
Sorts a list of elements using the insertion sort algorithm.
Args:
items (List[int]): The list of elements to be sorted.
... |
code_exercise_27k | 20,028 | Write a Python function to sort a list containing both integers and floating-point numbers using the insertion sort algorithm. The function should handle both types of numbers and ensure that the list is sorted in ascending order. The code should start with:
```python
from typing import List, Union
def sort_mixed_list... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def sort_mixed_list(arr: List[Union[int, float]]) -> List[Union[int, float]]:
"""
Sorts a list containing both integers and floating-point numbers using the insertion sort algorithm.
Args:
... |
code_exercise_27k | 20,032 | Write a Python function to sort a list using the insertion sort algorithm. The function should handle lists with elements of any comparable type, including integers, floats, and strings. The insertion sort algorithm should be implemented from scratch without using any built-in sorting functions.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, TypeVar
T = TypeVar('T')
def insertion_sort(lst: List[T]) -> List[T]:
"""
Sorts a list using the insertion sort algorithm.
Args:
lst (List[T]): The list to be sorted.
Returns:
... |
code_exercise_27k | 20,044 | Write a Python function to generate a 2D NumPy array with shape (n, n) filled with values from 1 to n^2 in a checkerboard pattern. The checkerboard pattern alternates between non-zero values and zeros, starting with a non-zero value in the top-left corner. The function should handle any positive integer n.
The code sh... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_checkerboard_array(n: int) -> np.ndarray:
"""
Generates a 2D NumPy array filled with values from 1 to n^2 in a checkerboard pattern.
Parameters:
n (int): The size of the square grid (n x n... |
code_exercise_27k | 20,045 | Write a Python function to sort a list of lists based on the length of each list using the insertion sort algorithm. The function should handle lists of varying lengths and ensure that the sorting is done in-place. The code should start with:
```python
from typing import List
def sort_lists_by_length(lst: List[List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_lists_by_length(lst: List[List[int]]) -> List[List[int]]:
"""
Sorts a list of lists based on the length of each sublist using the insertion sort algorithm.
Args:
lst (List[List[int]... |
code_exercise_27k | 20,052 | Write a Python function to create a copy of a given 2D NumPy array and transpose it. The function should ensure that the original array remains unmodified and return the transposed copy. The code should start with:
```python
import numpy as np
def transpose_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
def transpose_array(arr: np.ndarray) -> np.ndarray:
"""
Create a copy of a given 2D NumPy array and transpose it.
Parameters:
arr (np.ndarray): The 2D NumPy array to be transposed.
Returns:
... |
code_exercise_27k | 20,055 | Write a Python function to create a 2D NumPy array with a specified shape, where each cell contains the sum of its row and column index. The function should take two integer parameters: the number of rows (`n_rows`) and the number of columns (`n_cols`). The function should return the generated 2D NumPy array.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_sum_array(n_rows: int, n_cols: int) -> np.ndarray:
"""
Generate a 2D NumPy array with shape (n_rows, n_cols), where each cell contains the sum of its row and column index.
Args:
n_rows... |
code_exercise_27k | 20,056 | Write a Python function to sort a list containing infinity values using the insertion sort algorithm. The function should handle infinity values correctly by treating them as the largest possible values and placing them at the end of the sorted list. The code should start with:
```python
from typing import List
def in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_with_infinity(lst: List[float]) -> List[float]:
"""
Sorts a list containing infinity values using the insertion sort algorithm.
Infinity values are treated as the largest possible... |
code_exercise_27k | 20,058 | Write a Python function to sort a list of strings using the insertion sort algorithm while ignoring case sensitivity. The function should handle lists of varying lengths and strings with different cases. The code should start with:
```python
from typing import List
def insertion_sort_ignore_case(lst: List[str]) -> Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_ignore_case(lst: List[str]) -> List[str]:
"""
Sorts a list of strings using the insertion sort algorithm while ignoring case sensitivity.
Args:
lst (List[str]): The list o... |
code_exercise_27k | 20,060 | Write a Python function to create a NumPy array from a given list and reverse the order of its elements along the first axis. The function should handle lists of any dimension (1D, 2D, etc.). The code should start with:
```python
import numpy as np
from typing import List
def reverse_array(list_data: List) -> np.ndarr... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def reverse_array(list_data: List) -> np.ndarray:
"""
Create a NumPy array from a given list and reverse the order of its elements along the first axis.
Args:
list_data (L... |
code_exercise_27k | 20,068 | Write a Python function to create a NumPy array from a given list and delete the second column. The function should handle the conversion of the input list to a NumPy array and ensure that the input is a 2-dimensional array-like object. If the input does not meet these criteria, the function should raise a `ValueError`... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def delete_second_column(arr: List[List[int]]) -> np.ndarray:
"""
Create a NumPy array from a given list and delete the second column.
Parameters:
- arr: List[List[int]]
... |
code_exercise_27k | 20,073 | Write a Python function to find the indices of all non-zero elements in a given list and return them as a NumPy array. The function should convert the input list to a NumPy array and then use NumPy's `nonzero` function to find the indices of the non-zero elements. 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
import numpy as np
def find_nonzero_indices(arr: List[int]) -> np.ndarray:
"""
Finds the indices of all non-zero elements in a given list and returns them as a NumPy array.
Args:
arr (List[i... |
code_exercise_27k | 20,077 | Write a Python function to sort a list of strings considering only lowercase characters using the insertion sort algorithm. The function should ignore any non-lowercase characters and sort only the lowercase characters within each string. The code should start with:
```python
from typing import List
def sort_lowercase... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_lowercase_characters(lst: List[str]) -> List[str]:
"""
Sorts the lowercase characters in each string of the list using the Insertion Sort algorithm.
Parameters:
lst (List[str]): Lis... |
code_exercise_27k | 20,084 | Write a Python function to sort a list containing lists with various lengths using the insertion sort algorithm. The sorting should be based on the length of the sublists. The function should start with:
```python
from typing import List
def sort_nested_lists_by_length(arr: List[List[int]]) -> List[List[int]]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_nested_lists_by_length(arr: List[List[int]]) -> List[List[int]]:
"""
Sorts a list containing sublists with various lengths using the insertion sort algorithm.
Args:
arr (List[List[i... |
code_exercise_27k | 20,085 | Write a Python function to create a NumPy array from a given list and extract the even values. The function should return a NumPy array containing only the even values from the input list. The code should start with:
```python
import numpy as np
from typing import List
def extract_even_values(lst: List[int]) -> np.nda... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def extract_even_values(lst: List[int]) -> np.ndarray:
"""
Create a NumPy array from a given list and extract the even values.
Args:
lst (List[int]): The input list of int... |
code_exercise_27k | 20,090 | Write a Python function to create a NumPy array from a given list and swap the first and last columns of the array. The function should handle cases where the input list has fewer than two columns by raising a `ValueError`. The code should start with:
```python
import numpy as np
from typing import List
def swap_colum... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def swap_columns(array: List[List[int]]) -> np.ndarray:
"""
Create a NumPy array from a given list and swap the first and last columns.
Args:
array (List[List[int]]): The ... |
code_exercise_27k | 20,092 | Write a Python function to determine the maximum flow in a directed graph with capacity and demand constraints using the Ford-Fulkerson algorithm. The function should take a graph represented as an adjacency dictionary, a source node, and a sink node as inputs, and return the maximum flow from the source to the sink.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
from collections import defaultdict
def max_flow_ford_fulkerson(graph: Dict[Any, Dict[Any, int]], source: Any, sink: Any) -> int:
"""
Determine the maximum flow in a directed graph with capacity and... |
code_exercise_27k | 20,101 | Write a Python function to create a NumPy array from a given 2D list and replace all negative values with their absolute values. The function should handle any size of 2D list and ensure that the resulting array contains only non-negative values.
The code should start with:
```python
import numpy as np
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def replace_negatives_with_absolute_values(lst: List[List[int]]) -> np.ndarray:
"""
Create a NumPy array from a given 2D list and replace all negative values with their absolute values... |
code_exercise_27k | 20,109 | Write a Python function to sort a list of dictionaries based on the sum of a specific key's value using the insertion sort algorithm. The function should handle cases where the key might not exist in some dictionaries and should sort those dictionaries to the end of the list.
The code should start with:
```python
from... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dict_list_by_key_sum(dict_list: List[Dict[str, int]], key: str) -> List[Dict[str, int]]:
"""
Sorts a list of dictionaries based on the sum of a specific key's value using the insertion sor... |
code_exercise_27k | 20,130 | Write a Python function to sort a list of dictionaries based on the sum of the lengths of all key-value pairs using the insertion sort algorithm. The function should handle dictionaries with various data types as values, including strings, lists, and other dictionaries. The length of a key-value pair should be calculat... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Any
def calculate_length(dictionary: Dict[str, Any]) -> int:
"""
Calculate the sum of the lengths of all key-value pairs in a dictionary.
Args:
dictionary (dict): The dictionary to ca... |
code_exercise_27k | 20,134 | Write a Python function to sort a list of dictionaries based on the average value of a specific key's values using the insertion sort algorithm. The function should handle cases where the key's values are lists of numbers. The code should start with:
```python
from typing import List, Dict
def sort_list_of_dicts(lst: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_list_of_dicts(lst: List[Dict[str, any]], key: str) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries based on the average value of a specific key's values using the insertion sort ... |
code_exercise_27k | 20,140 | Write a Python function to sort a list of dictionaries based on the median of a specific key's values using the insertion sort algorithm. The function should handle lists of dictionaries where each dictionary contains a list of values for the specified key. The median should be calculated for each dictionary's list of ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Any
def sort_dict_list_by_median(lst: List[Dict[str, List[Any]]], key: str) -> List[Dict[str, List[Any]]]:
"""
Sorts a list of dictionaries based on the median of a specific key's values using the... |
code_exercise_27k | 20,146 | Write a Python function to create a NumPy array from a given 2D list and replace all NaN values with a specified replacement value. The function should handle cases where the input list is empty or contains no NaN values.
The code should start with:
```python
from typing import List, Union
import numpy as np
def repl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
import numpy as np
def replace_nan_with_value(input_list: List[List[Union[int, float]]], replacement_value: Union[int, float]) -> np.ndarray:
"""
Create a NumPy array from a given 2D list and repl... |
code_exercise_27k | 20,158 | Write a Python function to perform element-wise division of two 2D NumPy arrays. The function should take two 2D lists as input, convert them to NumPy arrays, and then perform the element-wise division. The result should be returned as a NumPy array. The code should start with:
```python
import numpy as np
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def element_wise_division(lst1: List[List[float]], lst2: List[List[float]]) -> np.ndarray:
"""
Perform element-wise division of two 2D NumPy arrays.
Parameters:
lst1 (List[Lis... |
code_exercise_27k | 20,169 | Write a Python function to calculate the element-wise sine of a given 2D list converted to a NumPy array. The function should return a NumPy array containing the element-wise sine values. The code should start with:
```python
import numpy as np
from typing import List
def elementwise_sine_2d_list(lst_2d: List[List[flo... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def elementwise_sine_2d_list(lst_2d: List[List[float]]) -> np.ndarray:
"""
Calculate the element-wise sine of a 2D list converted to a NumPy array.
Parameters:
lst_2d (List[Li... |
code_exercise_27k | 20,175 | Write a Python function to convert a given 2D list into a NumPy array and calculate the element-wise cumulative sum along the columns. The function should return the resulting NumPy array.
The code should start with:
```python
import numpy as np
from typing import List
def calculate_cumulative_sum_columns(input_list:... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def calculate_cumulative_sum_columns(input_list: List[List[int]]) -> np.ndarray:
"""
Converts a 2D list to a NumPy array and calculates the element-wise cumulative sum along the column... |
code_exercise_27k | 20,176 | Write a Python function to sort a dictionary by its keys using the insertion sort algorithm. The function should take a dictionary as input and return a new dictionary with the same key-value pairs, but sorted by the keys in ascending order. The code should start with:
```python
from typing import Dict
def sort_dict_b... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_dict_by_keys(dict_to_sort: Dict[str, int]) -> Dict[str, int]:
"""
Sorts a dictionary by its keys using the insertion sort algorithm.
Args:
dict_to_sort (Dict[str, int]): The diction... |
code_exercise_27k | 20,177 | Write a Python function to create a NumPy array from a given 2D list and calculate the element-wise cumulative product along the rows. The function should return a NumPy array with the same shape as the input data, where each element represents the cumulative product along the rows.
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def row_cumulative_product(data: List[List[int]]) -> np.ndarray:
"""
Create a NumPy array from a given 2D list and calculate the element-wise cumulative product along the rows.
Ar... |
code_exercise_27k | 20,179 | Write a Python function to calculate the element-wise mean along the columns of a given 2D list using NumPy. The function should convert the 2D list to a NumPy array and then compute the mean for each column. The code should start with:
```python
from typing import List
import numpy as np
def calculate_mean_2D_list_co... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import numpy as np
def calculate_mean_2D_list_columns(arr: List[List[float]]) -> np.ndarray:
"""
Calculate the element-wise mean along the columns of a 2D list using NumPy.
Parameters:
arr (List... |
code_exercise_27k | 20,182 | Write a Python function to calculate the element-wise median along the rows of a given 2D list and return the result as a 1D NumPy array. The function should convert the input 2D list to a NumPy array and then compute the median for each row.
The code should start with:
```python
from typing import List
import numpy a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import numpy as np
def calculate_row_median(arr: List[List[float]]) -> np.ndarray:
"""
Calculate the element-wise median along the rows of a given 2D list and return the result as a 1D NumPy array.
... |
code_exercise_27k | 20,192 | Write a Python function to sort a dictionary by its values in ascending order based on the number of lowercase letters in the values using the insertion sort algorithm. The function should return a new dictionary with the sorted key-value pairs.
The code should start with:
```python
from typing import Dict, Tuple
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Tuple
def sort_dict_by_lowercase_letters(dictionary: Dict[str, str]) -> Dict[str, str]:
"""
Sorts a dictionary by its values in ascending order based on the number of lowercase letters in the values usi... |
code_exercise_27k | 20,206 | Write a Python function to create a NumPy array from a given 2D list and check if all elements in the array are finite. The function should return `True` if all elements are finite and `False` otherwise. The code should start with:
```python
from typing import List
import numpy as np
def are_all_elements_finite(matrix... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import numpy as np
def are_all_elements_finite(matrix: List[List[float]]) -> bool:
"""
Create a NumPy array from a given 2D list and check if all elements are finite.
Args:
matrix (List[List... |
code_exercise_27k | 20,209 | Write a Python function to convert a given 2D list into a NumPy array and calculate the rank of the array. The function should validate the input to ensure it is a 2D list with rows of equal length. If the input is invalid, the function should raise a `ValueError`.
The code should start with:
```python
import numpy as... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def calculate_array_rank(two_d_list: List[List[int]]) -> int:
"""
Convert a 2D list into a NumPy array and calculate the rank of the array.
Args:
two_d_list (List[List[int... |
code_exercise_27k | 20,228 | Write a Python function to sort a list of dictionaries based on the alphabetical order of a specific substring in a specific value using the insertion sort algorithm. The function should handle cases where the substring is not present in the value by placing such dictionaries at the end of the sorted list.
The code sh... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dict_list_by_substring(dict_list: List[Dict[str, str]], key: str, substring: str) -> List[Dict[str, str]]:
"""
Sorts a list of dictionaries based on the alphabetical order of a specific su... |
code_exercise_27k | 20,229 | Write a Python function to convert a given 3D list into a 2D NumPy array with a specified shape. The function should ensure that the reshaped array maintains the original elements in the same order. The code should start with:
```python
from typing import List
import numpy as np
def reshape_3d_list_to_2d(arr: List[Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import numpy as np
def reshape_3d_list_to_2d(arr: List[List[List[int]]], shape: tuple) -> np.ndarray:
"""
Convert a 3D list into a 2D NumPy array with the specified shape.
Parameters:
arr (List[... |
code_exercise_27k | 20,240 | Write a Python function to calculate the element-wise maximum along the third dimension of a given 3D list and return the result as a NumPy array. The function should handle the conversion of the 3D list to a NumPy array and perform the necessary computation.
The code should start with:
```python
import numpy as np
fr... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def elementwise_max_3d(data: List[List[List[int]]]) -> np.ndarray:
"""
Calculate the element-wise maximum along the third dimension of a given 3D list and return the result as a NumPy ... |
code_exercise_27k | 20,243 | Write a Python function to create a NumPy array from a given 3D list and calculate the element-wise maximum along the first dimension. The function should return a NumPy array with the element-wise maximum values.
The code should start with:
```python
import numpy as np
from typing import List
def find_elementwise_ma... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def find_elementwise_maximum(data: List[List[List[int]]]) -> np.ndarray:
"""
Create a NumPy array from a given 3D list and calculate the element-wise maximum along the first dimension.... |
code_exercise_27k | 20,246 | Write a Python function to calculate the element-wise mean along the first dimension of a given 3D list and return the result as a NumPy array. The function should handle the conversion of the input list to a NumPy array and perform the necessary mean calculation.
The code should start with:
```python
import numpy as ... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def calculate_mean_3d_array(arr: List[List[List[float]]]) -> np.ndarray:
"""
Calculate the element-wise mean along the first dimension of a given 3D list and return the result as a Num... |
code_exercise_27k | 20,250 | Write a Python function to sort a list of dictionaries based on the number of words in a specific value using the insertion sort algorithm. The function should take a list of dictionaries and a key as input, and return the sorted list of dictionaries. The sorting should be based on the number of words in the value asso... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dict_list_by_word_count(dict_list: List[Dict[str, str]], key: str) -> List[Dict[str, str]]:
"""
Sorts a list of dictionaries based on the number of words in a specific value using the inse... |
code_exercise_27k | 20,253 | Write a Python function to sort a list of dictionaries based on the sum of the lengths of all words in a specific value using the insertion sort algorithm. The function should take a list of dictionaries and a key name as input, and return the sorted list of dictionaries. The sorting should be based on the sum of the l... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dictionaries_by_word_length_sum(lst: List[Dict[str, List[str]]], key: str) -> List[Dict[str, List[str]]]:
"""
Sorts a list of dictionaries based on the sum of the lengths of all words in a... |
code_exercise_27k | 20,254 | Write a Python function to create a NumPy array from a given 3D list and calculate the element-wise concatenation along the first axis. The function should handle lists of varying sizes and ensure that the concatenation is performed correctly.
The code should start with:
```python
import numpy as np
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def concatenate_3D_list(arr: List[List[List[int]]]) -> np.ndarray:
"""
Creates a NumPy array from a given 3D list and calculates the element-wise concatenation along the first axis.
... |
code_exercise_27k | 20,262 | Write a Python function to create a NumPy array from a given 3D list and calculate the element-wise square root along all dimensions. The function should handle any valid 3D list input and return a NumPy array with the square root of each element.
The code should start with:
```python
import numpy as np
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List
def calculate_element_wise_square_root(data: List[List[List[float]]]) -> np.ndarray:
"""
Create a NumPy array from a given 3D list and calculate the element-wise square root along all ... |
code_exercise_27k | 20,270 | Write a Python function to calculate the element-wise cosine of a given 3D list and return the result as a NumPy array. The function should handle the conversion of the input list to a NumPy array and apply the cosine function element-wise across all dimensions.
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
from typing import List
def element_wise_cosine(arr: List[List[List[float]]]) -> np.ndarray:
"""
Calculate the element-wise cosine of a given 3D list and return the result as a NumPy array.
Parameters:
... |
code_exercise_27k | 20,289 | Write a Python function to calculate the maximum flow in a directed graph with multiple capacities per edge, demand constraints, and vertex capacities using the Ford-Fulkerson algorithm. The function should handle the following:
1. **Graph Representation**: The graph is represented as an adjacency list where each verte... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
from collections import defaultdict
def calculate_maximum_flow(graph: Dict[Any, Dict[Any, int]], source: Any, sink: Any) -> int:
"""
Calculate the maximum flow in a directed graph with multiple capa... |
code_exercise_27k | 20,290 | Write a Python function to create a 2D array with a specified number of rows and columns, filled with a given value. The function should handle the case where the number of rows or columns is zero by returning an empty list. The code should start with:
```python
from typing import List, Any
def create_2d_array_with_va... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def create_2d_array_with_value(rows: int, columns: int, value: Any) -> List[List[Any]]:
"""
Creates a 2D array with the specified number of rows and columns,
filled with a given value.
Args... |
code_exercise_27k | 20,296 | Write a Python function to multiply all the elements of a given array by a specified factor. The function should handle both positive and negative integers, as well as floating-point numbers. The code should start with:
```python
from typing import List
def multiply_array_by_factor(arr: List[float], factor: float) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def multiply_array_by_factor(arr: List[float], factor: float) -> List[float]:
"""
Multiply all the elements of a given array by a specified factor.
Args:
arr (List[float]): The input array c... |
code_exercise_27k | 20,300 | Write a Python function to multiply every element of a given array by a specified multiplier. The function should take two parameters: the array of numbers and the multiplier. The function should return a new array with each element multiplied by the specified multiplier.
The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def multiply_array_elements(array: List[int], multiplier: int) -> List[int]:
"""
Multiplies every element of the given array by the specified multiplier.
Args:
array (List[int]): The array o... |
code_exercise_27k | 20,301 | Write a Python function to subtract a given value `x` from every element of a given list of integers. The function should return a new list with each element of the given list subtracted by `x`. The code should start with:
```python
from typing import List
def subtract_value(arr: List[int], x: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def subtract_value(arr: List[int], x: int) -> List[int]:
"""
Subtract a given value `x` from each element of the given list.
Args:
arr (List[int]): A list of integers.
x (int): The v... |
code_exercise_27k | 20,302 | Write a Python function to divide all the elements of a given array by a specified divisor. The function should handle both integer and floating-point divisors. The result should be a list of floating-point numbers, even if the divisor is an integer.
The code should start with:
```python
from typing import List
def d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def divide_array(arr: List[int], divisor: float) -> List[float]:
"""
Divide each element of a given array by a specified divisor.
Parameters:
arr (List[int]): The input array to be divided by th... |
code_exercise_27k | 20,304 | Write a Python function to compute the square of each element in a given array and return a new array containing the squares. The function should handle both positive and negative integers, as well as zero. The code should start with:
```python
from typing import List
def compute_squares(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 compute_squares(arr: List[int]) -> List[int]:
"""
Computes the square of each element in the given array.
Args:
arr (List[int]): The input array of integers.
Returns:
List... |
code_exercise_27k | 20,306 | Write a Python function to round off the values of a given list of floating-point numbers to the nearest integer. The function should handle lists of any length and return a new list with the rounded values. The code should start with:
```python
from typing import List
def round_to_nearest_integer(arr: List[float]) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def round_to_nearest_integer(arr: List[float]) -> List[int]:
"""
Rounds off the values of a given list of floating-point numbers to the nearest integer.
Args:
arr (List[float]): The input li... |
code_exercise_27k | 20,309 | Write a Python function to compute the mean of all elements in a given list of integers. The function should handle the case where the input list is empty by returning `None` instead of raising an exception. The code should start with:
```python
from typing import List, Optional
def compute_mean(arr: List[int]) -> Opt... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def compute_mean(arr: List[int]) -> Optional[float]:
"""
Compute the mean of all elements in a given list of integers.
Args:
arr (List[int]): The input list of integers.
Retur... |
code_exercise_27k | 20,317 | Write a Python function to multiply two given lists element-wise. The function should take two lists of integers as input and return a new list where each element is the product of the corresponding elements from the input lists. If the input lists have different lengths, the function should raise a `ValueError`.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def elementwise_multiply(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Multiply two given lists element-wise.
Args:
arr1 (List[int]): First input list of integers.
arr2 (List[i... |
code_exercise_27k | 20,323 | Write a Python function to compute the sum of all the elements in a given array. The function should handle both integer and floating-point numbers. The code should start with:
```python
from typing import List, Union
def compute_array_sum(arr: List[Union[int, float]]) -> Union[int, float]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def compute_array_sum(arr: List[Union[int, float]]) -> Union[int, float]:
"""
Compute the sum of all elements in a given array.
Args:
arr (List[Union[int, float]]): The array of numbe... |
code_exercise_27k | 20,324 | Write a Python function to sort a list of dictionaries based on the number of unique words in all values using the insertion sort algorithm. The function should handle dictionaries with string values and sort them in ascending order of the number of unique words in their values.
The code should start with:
```python
f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dicts_by_unique_words(lst: List[Dict[str, str]]) -> List[Dict[str, str]]:
"""
Sorts a list of dictionaries based on the number of unique words in all values using the insertion sort algori... |
code_exercise_27k | 20,325 | Write a Python function to compute the product of all elements in a given array. The function should handle both lists and tuples as input. If the input array is empty, the function should raise a `ValueError` with the message "Input array is empty." The code should start with:
```python
from typing import Union, List,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union, List, Tuple
def array_product(arr: Union[List[Union[int, float]], Tuple[Union[int, float]]]) -> Union[int, float]:
"""
Compute the product of all elements in a given array.
Args:
arr (list... |
code_exercise_27k | 20,326 | Write a Python function to sort the elements of a given array in descending order. The function should use the built-in `sorted` function with a custom key or reverse parameter to achieve the sorting. The code should start with:
```python
from typing import List
def sort_array_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 sort_array_descending(arr: List[int]) -> List[int]:
"""
Sorts the elements of a given array in descending order.
Args:
arr (List[int]): The array to be sorted.
Returns:
List... |
code_exercise_27k | 20,329 | Write a Python function to find the indices of the maximum value in each row of a given 2D array. The function should return a list of indices, where each index corresponds to the position of the maximum value in the respective row. The code should start with:
```python
from typing import List
def find_max_indices_in_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_indices_in_rows(array: List[List[int]]) -> List[int]:
"""
Find the indices of the maximum value in each row of a given 2D array.
Args:
array (List[List[int]]): The input 2D arra... |
code_exercise_27k | 20,337 | Write a Python function to find the common values between two given arrays, ensuring that the function handles cases where the arrays contain duplicate values. The function should return a list of unique common values sorted in ascending order. The code should start with:
```python
from typing import List
def find_com... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_values(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Find the common values between two given arrays, ensuring unique values and sorted order.
Args:
arr1 (List[int]): F... |
code_exercise_27k | 20,339 | Write a Python function to check whether a given NumPy array contains any missing values (NaN). The function should return `True` if the array contains at least one NaN value, and `False` otherwise. The code should start with:
```python
import numpy as np
def contains_nan(arr: np.ndarray) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def contains_nan(arr: np.ndarray) -> bool:
"""
Check whether a given NumPy array contains any missing values (NaN).
Parameters:
arr (numpy.ndarray): The array to check for missing values.
Return... |
code_exercise_27k | 20,341 | Write a Python function to compute the Euclidean distance between two given lists of numbers. The Euclidean distance is defined as the square root of the sum of the squared differences between corresponding elements of the two lists. The function should handle lists of any length, but it should raise a `ValueError` if ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def euclidean_distance(arr1: List[float], arr2: List[float]) -> float:
"""
Compute the Euclidean distance between two lists of numbers.
The Euclidean distance is defined as the square ro... |
code_exercise_27k | 20,346 | Write a Python function to calculate the element-wise differences between two given lists of integers. The function should ensure that the two lists have the same length before performing the calculation. If the lengths do not match, the function should return an empty list. The code should start with:
```python
from t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def element_wise_differences(list1: List[int], list2: List[int]) -> List[int]:
"""
Calculate the element-wise differences between two given lists of integers.
Args:
list1 (List[int]): The fi... |
code_exercise_27k | 20,351 | Write a Python function to implement the Quick Sort algorithm recursively. The function should take a list of integers as input and return the sorted list. The Quick Sort algorithm should be implemented using the divide-and-conquer approach, where the list is partitioned into two sub-arrays based on a pivot element, an... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quick_sort(arr: List[int]) -> List[int]:
"""
Sorts the given list using the Quick Sort algorithm recursively.
Quick Sort is a divide-and-conquer algorithm that works by selecting a pivot element... |
code_exercise_27k | 20,353 | Write a Python function to create a 2D array with specified dimensions and fill it with random integers within a given range. The function should take three parameters: the number of rows, the number of columns, and a tuple representing the range of integers (inclusive). The function should return the 2D array.
The co... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import random
def create_random_2d_array(rows: int, cols: int, range_tuple: Tuple[int, int]) -> List[List[int]]:
"""
Create a 2D array filled with random integers within a specified range.
Ar... |
code_exercise_27k | 20,356 | Write a Python function to extract the diagonal values from a given square matrix. The function should return a new list containing the values from the main diagonal of the matrix. The matrix is represented as a list of lists, where each inner list represents a row of the matrix. 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
def get_diagonal_values(array: List[List[int]]) -> List[int]:
"""
Extracts the diagonal values from a given square matrix.
Args:
array (List[List[int]]): A square matrix represented as a lis... |
code_exercise_27k | 20,357 | Write a Python function to sort a given array using the Quick Sort algorithm. The function should be able to handle arrays with duplicate elements and should return the sorted array. The code should start with:
```python
from typing import List
def quick_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 quick_sort(arr: List[int]) -> List[int]:
"""
Sorts an array using the Quick Sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
List[int]: The sorted arr... |
code_exercise_27k | 20,359 | Write a Python function to generate a checkerboard pattern using a given 2D array. The function should replace elements in the array based on their positions: elements at positions where both row and column indices are either both even or both odd should be replaced with 'X', and elements at positions where one index i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_checkerboard(array: List[List[str]]) -> List[List[str]]:
"""
Generate a checkerboard pattern using a given 2D array.
Args:
- array: A 2D list representing the input array.
Return... |
code_exercise_27k | 20,360 | Write a Python function to implement the Quick Sort algorithm with a customizable pivot selection strategy. The function should allow the user to choose between three pivot selection strategies:
1. **First Element**: Always choose the first element of the array as the pivot.
2. **Last Element**: Always choose the last ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Callable
def quick_sort(arr: List[int], pivot_strategy: Callable[[List[int]], int]) -> List[int]:
"""
Sorts an array using the Quick Sort algorithm with a customizable pivot selection strategy.
Par... |
code_exercise_27k | 20,361 | Write a Python function to flatten a given 2D array into a 1D array. The function should handle any nested lists within the input list, ensuring that all elements are flattened into a single list. The code should start with:
```python
from typing import List
def flatten_nested_array(arr: List[List[int]]) -> List[int]:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def flatten_nested_array(arr: List[Any]) -> List[int]:
"""
Flatten a nested 2D array into a 1D array.
Args:
arr (list): The input nested array to be flattened.
Returns:
lis... |
code_exercise_27k | 20,362 | Write a Python function to sort a list of strings in lexicographic order using the Quick Sort algorithm. The function should handle lists of strings and sort them based on their lexicographic (dictionary) order. The code should start with:
```python
from typing import List
def lexicographic_quicksort(arr: List[str]) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def lexicographic_quicksort(arr: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the Quick Sort algorithm.
Args:
arr (List[str]): List of strings to be sort... |
code_exercise_27k | 20,367 | Write a Python function to reshape a given 2D list into a new shape. The function should ensure that the total number of elements in the input list matches the total number of elements in the desired shape. If the shapes do not match, the function should raise a `ValueError`.
The code should start with:
```python
from... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def reshape_list(array: List[List[int]], new_shape: Tuple[int, int]) -> List[List[int]]:
"""
Reshape a given 2D list into a new shape.
Args:
array (List[List[int]]): The input 2D list... |
code_exercise_27k | 20,369 | Write a Python function to compute the mean of a given list of numbers without using external libraries like NumPy. The function should handle lists of integers and floats, and should return the mean as a float. The code should start with:
```python
from typing import List
def compute_mean(numbers: List[float]) -> flo... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def compute_mean(numbers: List[float]) -> float:
"""
Compute the mean of a list of numbers.
Args:
numbers (List[float]): The list of numbers for which to compute the mean.
Returns:
... |
code_exercise_27k | 20,371 | Write a Python function to calculate the median of a given list of numbers. The function should handle the following cases:
1. If the input list is empty, return `None`.
2. If the input list contains an odd number of elements, return the middle element.
3. If the input list contains an even number of elements, return t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def calculate_median_from_list(numbers: List[float]) -> Optional[float]:
"""
Calculate the median of a given list of numbers.
Args:
numbers (List[float]): The list of numbers from ... |
code_exercise_27k | 20,372 | Write a Python function to implement the Quick Sort algorithm with a random pivot selection to optimize its performance. The function should sort a list of integers in ascending order. The code should start with:
```python
from typing import List
import random
def quicksort_random_pivot(arr: List[int]) -> List[int]:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def quicksort_random_pivot(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Quick Sort algorithm with a random pivot selection.
Args:
arr (List[int]): The l... |
code_exercise_27k | 20,375 | Write a Python function to calculate the mode of a given list of integers. The mode is the value that appears most frequently in the list. If there are multiple modes, return all of them. The function should handle lists of any length, including empty lists. The code should start with:
```python
from typing import List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def calculate_mode(numbers: List[int]) -> Union[List[int], None]:
"""
Calculate the mode of a given list of integers.
The mode is the value that appears most frequently in the list. If there ... |
code_exercise_27k | 20,376 | Write a Python function to compute the range of values in a given list of integers. The range is defined as the difference between the maximum and minimum values in the list. The function should handle empty lists by returning `None`.
The code should start with:
```python
from typing import List, Optional
def compute... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def compute_range(arr: List[int]) -> Optional[int]:
"""
Compute the range of values in a given list of integers.
The range is defined as the difference between the maximum and minimum valu... |
code_exercise_27k | 20,380 | Write a Python function to analyze the time complexity of the Quick Sort algorithm and compare it to other sorting algorithms like Bubble Sort and Insertion Sort. The function should return a string that explains the time complexity of Quick Sort, Bubble Sort, and Insertion Sort, and provide a brief comparison of their... | Here is the Python code to solve the given problem in the markdown block:
```python
def analyze_sorting_algorithms() -> str:
"""
Analyzes the time complexity of Quick Sort, Bubble Sort, and Insertion Sort, and compares their performance characteristics.
Returns:
str: A string explaining the time co... |
code_exercise_27k | 20,386 | Write a Python function to find the minimum value in a given list of integers. The function should handle empty lists by returning `None` instead of raising an exception. The code should start with:
```python
from typing import List, Optional
def find_minimum_in_list(numbers: List[int]) -> Optional[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_minimum_in_list(numbers: List[int]) -> Optional[int]:
"""
Find the minimum value in a list of integers.
Args:
numbers (List[int]): The list of integers from which to find ... |
code_exercise_27k | 20,390 | Write a Python function to calculate the sum of elements in a given list of integers. The function should handle lists of any length, including empty lists. The code should start with:
```python
from typing import List
def calculate_list_sum(lst: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def calculate_list_sum(lst: List[int]) -> int:
"""
Calculate the sum of elements in a given list of integers.
Args:
lst (List[int]): The input list of integers.
Returns:
int: ... |
code_exercise_27k | 20,398 | Write a Python function to find the indices where a specific condition is satisfied in a given NumPy array. The condition is defined by a callable function that takes an element of the array as input and returns a boolean value. The function should return a tuple of arrays, where each array contains the indices where t... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import Callable, Tuple
def find_indices(array: np.ndarray, condition: Callable[[float], bool]) -> Tuple[np.ndarray, ...]:
"""
Find the indices where a specific condition is satisfied in a NumPy ar... |
code_exercise_27k | 20,400 | Write a Python function to sort a given NumPy array in ascending order. The function should handle the input validation to ensure that the input is a NumPy array. If the input is not a NumPy array, the function should raise a `TypeError`. The code should start with:
```python
import numpy as np
def sort_numpy_array(ar... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def sort_numpy_array(array: np.ndarray) -> np.ndarray:
"""
Sorts a given NumPy array in ascending order.
Args:
array (numpy.ndarray): The array to be sorted.
Returns:
numpy.ndarray: ... |
code_exercise_27k | 20,410 | Write a Python function to round the values of a given NumPy array to a specified decimal place. The function should handle both positive and negative decimal places. The code should start with:
```python
import numpy as np
def round_array(arr: np.ndarray, decimal_place: int) -> np.ndarray:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def round_array(arr: np.ndarray, decimal_place: int) -> np.ndarray:
"""
Round the values of a given NumPy array to a specified decimal place.
Args:
arr (numpy.ndarray): The input NumPy array.
... |
code_exercise_27k | 20,413 | Write a Python function to check if all elements in a given list are unique. The function should return `True` if all elements are unique and `False` otherwise. The code should start with:
```python
from typing import List
def check_list_unique(lst: List[int]) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def check_list_unique(lst: List[int]) -> bool:
"""
Check if all elements in a given list are unique.
Args:
lst (List[int]): The input list of integers.
Returns:
bool: True if al... |
code_exercise_27k | 20,417 | Write a Python function to calculate the absolute difference between two NumPy arrays. The function should ensure that the input arrays have the same shape and return a new NumPy array containing the absolute difference between corresponding elements of the input arrays. 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 calculate_absolute_difference(array1: np.ndarray, array2: np.ndarray) -> np.ndarray:
"""
Calculate the absolute difference between two NumPy arrays.
Args:
array1 (np.ndarray): The first NumPy... |
code_exercise_27k | 20,419 | Write a Python function to calculate the Euclidean distance between two NumPy arrays. The function should handle arrays of any shape and ensure that the arrays have the same shape before performing the calculation. If the arrays have different shapes, the function should raise a `ValueError`.
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 euclidean_distance(arr1: np.ndarray, arr2: np.ndarray) -> float:
"""
Calculate the Euclidean distance between two NumPy arrays.
Parameters:
arr1 (numpy.ndarray): First input array.
arr2 (nump... |
code_exercise_27k | 20,422 | Write a Python function to merge two sorted lists into a single sorted list using the Merge Sort algorithm. The function should efficiently combine the elements from both lists while maintaining the sorted order. The code should start with:
```python
from typing import List
def merge_sorted_lists(list1: List[int], lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merge two sorted lists into a single sorted list using the Merge Sort algorithm.
Args:
list1 (List[int]): The fir... |
code_exercise_27k | 20,436 | Write a Python function to check if a given list is already sorted in non-decreasing order using the Merge Sort algorithm. The function should return `True` if the list is sorted and `False` otherwise. The code should start with:
```python
from typing import List
def is_sorted_using_merge_sort(arr: List[int]) -> bool:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_sorted_using_merge_sort(arr: List[int]) -> bool:
"""
Check if a given list is sorted in non-decreasing order using the Merge Sort algorithm.
Args:
arr (List[int]): The input list to b... |
code_exercise_27k | 20,442 | Write a Python function to find the largest and smallest elements in a given list using the Merge Sort algorithm. The function should return a tuple containing the smallest and largest elements in the list. The code should start with:
```python
from typing import List, Tuple
def find_largest_smallest(lst: List[int]) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def merge_sort(lst: List[int]) -> List[int]:
"""
Sorts the given list using the Merge Sort algorithm.
Parameters:
lst (list): The list to be sorted.
Returns:
list: The sorted lis... |
code_exercise_27k | 20,448 | Write a Python function to determine if a given string contains a cycle, where a cycle is defined as a repeating pattern within the string. The function should return `True` if a cycle is found, and `False` otherwise. The code should start with:
```python
def has_cycle(string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def has_cycle(string: str) -> bool:
"""
Determine if the given string contains a cycle, where a cycle is defined as a repeating pattern within the string.
Args:
string (str): The input string to be checked for cycle... |
code_exercise_27k | 20,451 | Write a Python function to find the length of the shortest repeating cycle in a given string. If no repeating cycle is found, the function should return -1. A cycle is defined as a substring that, when repeated, forms the entire string.
The code should start with:
```python
def find_cycle_length(string: str) -> int:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_cycle_length(string: str) -> int:
"""
Find the length of the shortest repeating cycle in the given string.
Args:
string (str): The input string to analyze.
Returns:
int: The length of the short... |
code_exercise_27k | 20,454 | Write a Python function to find the kth smallest element in a given list using the Merge Sort algorithm. The function should be efficient and handle edge cases such as an empty list or invalid k values.
The code should start with:
```python
from typing import List
def find_kth_smallest(arr: List[int], k: int) -> int:... | 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 list in ascending order using the Merge Sort algorithm.
Parameters:
- arr (list): The list to be sorted.
Returns:
- list... |
code_exercise_27k | 20,457 | Write a Python function to find the kth largest element in a given list using the Quickselect algorithm. The Quickselect algorithm is a selection algorithm to find the kth smallest element in an unordered list. It is similar to the QuickSort algorithm but only recurses into one half of the data. 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 find_kth_largest_element(lst: List[int], k: int) -> int:
"""
Finds the kth largest element in a given list using the Quickselect algorithm.
Args:
lst (List[int]): The list of integers to... |
code_exercise_27k | 20,469 | Write a Python function to sort a list of tuples based on the second element of each tuple using the Merge Sort algorithm. The function should be able to handle lists of varying lengths, including empty lists and lists with duplicate second elements. The code should start with:
```python
from typing import List, Tuple
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def merge_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 the Merge Sort algorithm.
Args:
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.