source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 21,032 | Write a Python function to sort a list of tuples using the counting sort algorithm. Each tuple in the list contains exactly two integers, and the sorting should be based on the first element of the tuples. The function should handle lists with tuples containing non-negative integers only.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def counting_sort_tuples(lst: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Sorts a list of tuples using the counting sort algorithm based on the first element of each tuple.
Args:
... |
code_exercise_27k | 21,038 | Write a Python function to sort a list of dictionaries using the Shell Sort algorithm based on a specified key. The function should be able to handle dictionaries with different keys and values, and it should return the sorted list of dictionaries.
The code should start with:
```python
from typing import List, Dict
d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def shell_sort_dictlist(dict_list: List[Dict[str, any]], sort_key: str) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries using the Shell Sort algorithm based on a specified key.
Args:... |
code_exercise_27k | 21,042 | Write a Python function to sort a list of integers using the gnome sort algorithm. The function should take a list of integers as input and return the sorted list. The code should start with:
```python
from typing import List
def gnome_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 gnome_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the gnome sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
List... |
code_exercise_27k | 21,047 | Write a Python function to determine if two given lists of integers are permutations of each other. A permutation of a list is another list that contains the same elements, with the same frequency, but possibly in a different order.
The code should start with:
```python
from typing import List
def are_permutations(li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def are_permutations(list1: List[int], list2: List[int]) -> bool:
"""
Determine if two lists of integers are permutations of each other.
Args:
list1 (List[int]): The first list of integers.
... |
code_exercise_27k | 21,049 | Write a Python function to find the largest sum of two distinct elements in a given list of integers. The function should return the largest sum, and if the list contains fewer than two elements, it should raise a `ValueError`.
The code should start with:
```python
from typing import List
def find_largest_sum_of_two_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_largest_sum_of_two_distinct_elements(nums: List[int]) -> int:
"""
Find the largest sum of two distinct elements in a list of integers.
Args:
nums (List[int]): The input list of inte... |
code_exercise_27k | 21,053 | Write a Python function to find the first missing positive integer in a given list of integers. The function should return the smallest positive integer that is missing from the list. The code should start with:
```python
from typing import List
def find_first_missing_positive(nums: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def find_first_missing_positive(nums: List[int]) -> int:
"""
Find the first missing positive integer in a list of integers.
Args:
nums (List[int]): A list of integers.
Returns:
... |
code_exercise_27k | 21,072 | Write a Python function to sort a list of integers using the bottom-up merge sort algorithm. The bottom-up merge sort algorithm sorts the list by iteratively merging sublists of increasing size, starting from sublists of size 1. The code should start with:
```python
from typing import List
def bottom_up_merge_sort(num... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bottom_up_merge_sort(nums: List[int]) -> List[int]:
"""
Sorts a list of integers using the bottom-up merge sort algorithm.
Args:
nums (List[int]): List of integers to be sorted.
Ret... |
code_exercise_27k | 21,075 | Write a Python function to sort a list of strings using the three-way quicksort algorithm. The function should handle strings with duplicate values efficiently by partitioning the list into three parts: elements smaller than the pivot, elements equal to the pivot, and elements greater than the pivot. The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def three_way_quicksort(arr: List[str]) -> List[str]:
"""
Sorts a list of strings using the three-way quicksort algorithm.
Args:
arr (List[str]): The list of strings to be sorted.
Retur... |
code_exercise_27k | 21,091 | Write a Python function to sort a list of integers using the cycle sort algorithm. The cycle sort algorithm is an in-place comparison sort with a time complexity of O(n^2) in the worst case. It minimizes the number of writes to memory, making it useful for systems with limited memory write capabilities.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cycle_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the cycle sort algorithm.
Cycle sort is an in-place comparison sort that minimizes the number of writes to memory.
... |
code_exercise_27k | 21,098 | Write a Python function to find the missing number in a list of integers from 1 to N, where exactly one number is missing. The function should efficiently determine the missing number using mathematical properties of the sequence.
The code should start with:
```python
from typing import List
def find_missing_number(n... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_missing_number(nums: List[int]) -> int:
"""
Finds the missing number in a list of integers from 1 to N.
Args:
nums: A list containing integers from 1 to N, with one missing numb... |
code_exercise_27k | 21,103 | 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. In this task, you need to adapt it to find the kth largest element.
The code should start with:
```python
fr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def partition(arr: List[int], low: int, high: int) -> int:
"""
Partitions the array around a pivot element.
Args:
arr (list): The input list.
low (int): The lower index of the curren... |
code_exercise_27k | 21,114 | Write a Python function to sort a list of integers using the comb sort algorithm. The comb sort algorithm is an improvement over the bubble sort algorithm, where the gap between compared elements shrinks by a factor of 1.3 in each iteration until the gap is 1. The code should start with:
```python
from typing import Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def comb_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the comb sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
List[i... |
code_exercise_27k | 21,122 | Write a Python function to sort a list of tuples using the Shell Sort algorithm. Each tuple contains two elements: a string (representing a fruit name) and an integer (representing the quantity of that fruit). The sorting should be based on the integer value in ascending order. If two tuples have the same integer value... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def shell_sort_tuples(lst: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Sorts a list of tuples using the Shell Sort algorithm based on the integer value in ascending order.
Args:
... |
code_exercise_27k | 21,127 | Write a Python function to find the largest sum of contiguous subarrays in a given list using Kadane's algorithm. The function should efficiently compute the maximum sum of any contiguous subarray within the given list of integers.
The code should start with:
```python
from typing import List
def max_subarray_sum(arr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
"""
Finds the largest sum of contiguous subarrays in a given list using Kadane's algorithm.
Args:
arr (List[int]): The input list of integers.
... |
code_exercise_27k | 21,135 | Write a Python function to find the kth smallest element in a given list using the quickselect algorithm. The function should be efficient and handle edge cases appropriately. The code should start with:
```python
from typing import List
import random
def find_kth_smallest(nums: List[int], k: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def find_kth_smallest(nums: List[int], k: int) -> int:
"""
Find the kth smallest element in the given list using the quickselect algorithm.
Args:
nums (List[int]): The input li... |
code_exercise_27k | 21,154 | Write a Python function to sort a list of integers using the insertion sort algorithm. The function should be able to handle lists of any length, including empty lists. The code should start with:
```python
from typing import List
def insertion_sort(nums: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort(nums: List[int]) -> List[int]:
"""
Sorts a list of integers using the insertion sort algorithm.
Args:
nums (List[int]): A list of integers to be sorted.
Returns:
... |
code_exercise_27k | 21,172 | Write a Python function to find the length of the longest increasing subsequence (LIS) in a given list using the dynamic programming approach. The function should return the length of the LIS.
The code should start with:
```python
from typing import List
def longest_increasing_subsequence_length(arr: List[int]) -> in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence_length(arr: List[int]) -> int:
"""
Calculate the length of the longest increasing subsequence in a given list using dynamic programming.
Args:
arr (List[in... |
code_exercise_27k | 21,178 | Write a Python function to sort a list of integers using the counting sort algorithm. The function should handle non-negative integers and return the sorted list. The code should start with:
```python
from typing import List
def counting_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 counting_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of non-negative integers using the Counting Sort algorithm.
Args:
arr (List[int]): The list of non-negative integers to be so... |
code_exercise_27k | 21,205 | Write a Python function to sort a list of strings using the gnome sort algorithm. The function should take a list of strings as input and return the sorted list of strings. The gnome sort algorithm should be implemented as described in the given code snippet.
The code should start with:
```python
from typing import Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def gnome_sort_strings(strings: List[str]) -> List[str]:
"""
Sort a list of strings using the gnome sort algorithm.
Gnome sort is a simple sorting algorithm that works by repeatedly swapping adjacen... |
code_exercise_27k | 21,214 | Write a Python function to sort a list of tuples using the cocktail shaker sort algorithm. Each tuple in the list contains two elements: an integer and a string. The sorting should be based on the integer value in ascending order. If two tuples have the same integer value, their order should be determined by the string... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def cocktail_shaker_sort_tuples(lst: List[Tuple[int, str]]) -> List[Tuple[int, str]]:
"""
Sorts a list of tuples using the cocktail shaker sort algorithm.
Each tuple contains an integer and a... |
code_exercise_27k | 21,220 | Write a Python function to sort a list of integers using the three-way quicksort algorithm. The three-way quicksort algorithm partitions the list into three parts: elements less than the pivot, elements equal to the pivot, and elements greater than the pivot. The function should recursively sort the "lesser" and "great... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def three_way_quicksort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the three-way quicksort algorithm.
Parameters:
arr (List[int]): The input list of integers to be sorted.
... |
code_exercise_27k | 21,247 | Write a Python function to count the number of occurrences of each unique element in a given list. The function should return a dictionary where the keys are the unique elements from the list and the values are the counts of those elements.
The code should start with:
```python
from typing import List, Dict
def count... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_element_occurrences(lst: List) -> Dict[int, int]:
"""
Counts the number of occurrences of each unique element in a list.
Args:
lst (List): The list to count occurrences in.
... |
code_exercise_27k | 21,251 | Write a Python function to check if a given element exists in a list and return the index of the first occurrence of the element if it exists, otherwise return -1. The function should handle lists of any type of elements, including nested lists.
The code should start with:
```python
from typing import List, Any
def f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def find_element_in_list(lst: List[Any], element: Any) -> int:
"""
Find the index of the first occurrence of a given element in a list.
This function searches for the element in the list and re... |
code_exercise_27k | 21,252 | Write a Python function to calculate the mean of a given list of numbers. The function should handle the case where the list is empty by returning `None` instead of raising an exception. The code should start with:
```python
from typing import List, Optional
def calculate_mean(numbers: List[int]) -> Optional[float]:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def calculate_mean(numbers: List[int]) -> Optional[float]:
"""
Calculate the mean of a list of numbers.
Args:
numbers (List[int]): A list of integers.
Returns:
Optiona... |
code_exercise_27k | 21,253 | Write a Python function to find the k-th smallest element in a given list of integers. The function should handle the case where the list is empty by raising a `ValueError`. The function should also handle the case where k is greater than the length of the list by raising a `ValueError`. The code should start with:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_kth_smallest_element(lst: List[int], k: int) -> int:
"""
Finds the k-th smallest element in a list of integers.
Parameters:
lst (List[int]): A list of integers.
k (int): The positio... |
code_exercise_27k | 21,254 | Write a Python function to implement the Particle Swarm Optimization (PSO) algorithm. The function should optimize a given objective function within specified bounds using a population of particles. The algorithm should update the positions and velocities of the particles based on their personal best positions and the ... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def pso(func, bounds, num_particles, max_iterations, inertia_weight=0.8, cognitive_weight=2.0, social_weight=2.0):
"""
Particle Swarm Optimization (PSO) algorithm.
Parameters:
func (callable): The ob... |
code_exercise_27k | 21,256 | Write a Python function to find the k largest elements from a given list of integers. The function should return a list of the k largest elements in descending order. If k is greater than the length of the list, the function should return all elements in the list in descending order. If k is less than or equal to 0, th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_k_largest_elements(lst: List[int], k: int) -> List[int]:
"""
Returns the k largest elements from the given list in descending order.
Args:
lst (List[int]): The list of integers from... |
code_exercise_27k | 21,261 | Write a Python function to find the index of the last occurrence of a specified element in a given list. If the element is not found in the list, the function should return -1. The function should be efficient and handle edge cases such as an empty list or an element that does not exist in the list.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_last_occurrence(lst: List[int], element: int) -> int:
"""
Find the index of the last occurrence of a specified element in a list.
Args:
lst (List[int]): The list to search the eleme... |
code_exercise_27k | 21,262 | Write a Python function to calculate the standard deviation of a list of numbers. The function should handle the following:
1. Ensure the input list contains at least two numbers.
2. Calculate the mean (average) of the numbers.
3. Compute the sum of the squared differences between each number and the mean.
4. Calculate... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def calculate_standard_deviation(numbers: List[int]) -> float:
"""
Calculate the standard deviation of a list of numbers.
Parameters:
numbers (List[int]): A list of numbers.
Ret... |
code_exercise_27k | 21,265 | Write a Python function to calculate the sum of all elements in a given list. The function should handle lists containing integers or floating-point numbers. The function should raise a `TypeError` if the input is not a list or if the elements in the list are not numeric.
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, Union
def find_list_sum(lst: List[Union[int, float]]) -> Union[int, float]:
"""
Calculate the sum of all elements in a given list.
Args:
lst (List[Union[int, float]]): The input list of int... |
code_exercise_27k | 21,266 | Write a Python function to split a given list of integers into two separate lists: one containing all the odd numbers and the other containing all the even numbers. The function should return a tuple where the first element is the list of odd numbers and the second element is the list of even numbers.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def split_list_by_odd_even(lst: List[int]) -> Tuple[List[int], List[int]]:
"""
Split a given list of integers into two separate lists containing odd and even elements.
Args:
lst (List... |
code_exercise_27k | 21,269 | Write a Python function to find and return the indices of all occurrences of a specified element in a given list. The function should handle lists of any type of elements, including integers, strings, and other data types. The function should return an empty list if the specified element is not found in the list.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def find_all_indices(lst: List[Any], element: Any) -> List[int]:
"""
Finds and returns the indices of all occurrences of a specified element in a list.
Args:
lst (List[Any]): The list t... |
code_exercise_27k | 21,271 | Write a Python function to count the total number of elements in a list, including nested lists. The function should recursively count all elements, including those within nested lists. The code should start with:
```python
from typing import List, Union
def count_total_elements(lst: List[Union[int, float, str, List]]... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List, Union
def count_total_elements(lst: List[Union[int, float, str, List]]) -> int:
"""
Count the total number of elements in a list, including elements within nested lists.
Args:
lst (list):... |
code_exercise_27k | 21,272 | Write a Python function to remove all occurrences of a specified element from a given list. The function should return a new list without the specified element, preserving the order of the remaining elements. The code should start with:
```python
from typing import List, Any
def remove_element(lst: List[Any], element:... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List, Any
def remove_element(lst: List[Any], element: Any) -> List[Any]:
"""
Removes all occurrences of a specified element from a given list.
Args:
lst (List[Any]): The original list to remove... |
code_exercise_27k | 21,277 | Write a Python function to find the smallest element in a list of integers or floats. The function should handle empty lists and lists containing non-numeric values by raising appropriate exceptions. The code should start with:
```python
from typing import List, Union
def find_smallest_element(lst: List[Union[int, flo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def find_smallest_element(lst: List[Union[int, float]]) -> Union[int, float]:
"""
Finds the smallest element in a list of integers or floats.
Args:
lst (List[Union[int, float]]): A li... |
code_exercise_27k | 21,278 | Write a Python function to check if a list is sorted in descending order. The function should return `True` if the list is sorted in descending order, and `False` otherwise. The code should start with:
```python
from typing import List
def is_sorted_descending(lst: 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_descending(lst: List[int]) -> bool:
"""
Check if a list is sorted in descending order.
Args:
lst (List[int]): The list to be checked.
Returns:
bool: True if the ... |
code_exercise_27k | 21,281 | Write a Python function to count the frequency of each element in a given list. The function should return a dictionary where the keys are the elements in the list and the values are the corresponding frequencies. The code should start with:
```python
from typing import List, Dict
def count_element_frequency(lst: List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_element_frequency(lst: List) -> Dict[any, int]:
"""
Count the frequency of each element in the given list.
Args:
lst (List): A list containing elements to be counted.
Re... |
code_exercise_27k | 21,282 | Write a Python function to count the number of prime numbers in a given list of integers. The function should use a helper function to check if a number is prime. The code should start with:
```python
from typing import List
def count_prime_numbers(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(number: int) -> bool:
"""
Check if a number is prime.
Parameters:
number (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwi... |
code_exercise_27k | 21,283 | Write a Python function to replace all occurrences of a specified element in a list with a new element. The function should modify the list in place and return the modified list. The code should start with:
```python
from typing import List, Any
def replace_elements(lst: List[Any], old_element: Any, new_element: Any) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def replace_elements(lst: List[Any], old_element: Any, new_element: Any) -> List[Any]:
"""
Replace all occurrences of a specified element in a list with a new element.
Args:
lst (List[A... |
code_exercise_27k | 21,284 | Write a Python function to remove all duplicates from a given list while preserving the order of the elements. The function should return a new list with all duplicate elements removed. The code should start with:
```python
from typing import List
def remove_duplicates_preserve_order(lst: List) -> List:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def remove_duplicates_preserve_order(lst: List) -> List:
"""
Remove duplicates from a list while preserving the order of the elements.
Args:
lst (list): A list of elements with potential d... |
code_exercise_27k | 21,286 | Write a Python function to determine 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 are_elements_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 are_elements_unique(lst: List[int]) -> bool:
"""
Determine if all elements in a given list are unique.
Args:
lst (List[int]): The input list to check for unique elements.
Returns:
... |
code_exercise_27k | 21,287 | Write a Python function to sort a list of numbers in ascending order using the merge sort algorithm. The merge sort algorithm is a divide-and-conquer algorithm that recursively divides the list into halves until each sublist contains a single element, then merges these sublists back together in sorted order.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the merge sort algorithm.
Args:
numbers (List[int]): A list of numbers to be sorted.
... |
code_exercise_27k | 21,294 | Write a Python function to find the second largest element in a given list of integers or floats. The function should handle lists with at least two elements and ensure that the list contains only numeric values. If the list has fewer than two elements or contains non-numeric values, the function should raise a `ValueE... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def find_second_largest(lst: List[Union[int, float]]) -> Union[int, float]:
"""
Finds the second largest element in a list of integers or floats.
Args:
lst: A list of integers or floa... |
code_exercise_27k | 21,298 | Write a Python function to remove all even numbers from a given list while preserving the relative order of the remaining odd numbers. The function should return a new list containing only the odd numbers from the input list. The code should start with:
```python
from typing import List
def remove_even_numbers(numbers... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_even_numbers(numbers: List[int]) -> List[int]:
"""
Removes all even numbers from a list while preserving the relative order of the remaining numbers.
Args:
numbers (List[int]): Th... |
code_exercise_27k | 21,299 | Write a Python function to find the sum of all even numbers in a given list of integers. The function should handle an empty list by returning 0. The code should start with:
```python
from typing import List
def sum_of_evens(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_evens(numbers: List[int]) -> int:
"""
Calculates the sum of all even numbers in a given list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
int:... |
code_exercise_27k | 21,302 | Write a Python function to check if a given list is a palindrome. A list is considered a palindrome if it reads the same backward as forward. The function should return `True` if the list is a palindrome and `False` otherwise. The code should start with:
```python
from typing import List
def is_palindrome_list(lst: Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_palindrome_list(lst: List[int]) -> bool:
"""
Check if a given list is a palindrome.
Args:
lst (List[int]): The list to be checked.
Returns:
bool: True if the list is a pa... |
code_exercise_27k | 21,303 | Write a Python function to perform a linear search on a two-dimensional list and return the indices of all occurrences of a target value. The function should handle any rectangular two-dimensional list (i.e., each row can have a different number of columns). 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, Tuple
def linear_search_2d(arr: List[List], target) -> List[Tuple[int, int]]:
"""
Perform a linear search on a two-dimensional list and return
the indices of all occurrences of the target value.
... |
code_exercise_27k | 21,311 | Write a Python function to find the intersection of two lists, ensuring that the result contains only unique elements and is sorted in ascending order. The function should handle lists with duplicate elements and return the intersection as a sorted list of unique elements.
The code should start with:
```python
from ty... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection(list1: List[int], list2: List[int]) -> List[int]:
"""
Finds the intersection of two lists, ensuring the result contains only unique elements and is sorted in ascending order.
A... |
code_exercise_27k | 21,317 | Write a Python function to check if a list is sorted in ascending order without using the built-in `sorted` function. The function should iterate through the list and compare each element with the next one to determine if the list is sorted.
The code should start with:
```python
from typing import List
def is_list_so... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_list_sorted_ascending(lst: List[int]) -> bool:
"""
Checks if a list is sorted in ascending order without using the built-in sorted function.
Args:
lst (List[int]): The list to be ... |
code_exercise_27k | 21,319 | Write a Python function to check if a list is sorted in descending order. The function should return `True` if the list is sorted in descending order, and `False` otherwise. The code should start with:
```python
from typing import List
def is_sorted_descending(lst: 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_descending(lst: List[int]) -> bool:
"""
Check if a list is sorted in descending order.
Args:
lst (List[int]): The list to be checked.
Returns:
bool: True if the li... |
code_exercise_27k | 21,320 | Write a Python function to check if two lists are equal, considering that the lists may contain nested lists or other complex data structures. The function should perform a deep comparison, meaning it should compare the contents of nested lists recursively.
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, Any
def are_lists_equal(list1: List[Any], list2: List[Any]) -> bool:
"""
Recursively checks if two lists are equal, including nested lists.
Args:
list1 (List[Any]): The first list to compar... |
code_exercise_27k | 21,331 | Write a Python function to count the number of words in a given string, considering that words are separated by any whitespace (spaces, tabs, newlines). The function should handle various edge cases, such as empty strings, strings with multiple spaces, and strings with leading or trailing spaces.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_words(string: str) -> int:
"""
Count the number of words in a given string.
Words are defined as sequences of characters separated by any whitespace (spaces, tabs, newlines).
Args:
string (str): The i... |
code_exercise_27k | 21,336 | Write a Python function to reverse a string without using built-in functions or slicing. The function should iterate through the string and construct the reversed string manually.
The code should start with:
```python
def reverse_string_manual(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string_manual(string: str) -> str:
"""
Reverses the given string without using built-in functions or slicing.
Args:
string (str): The string to be reversed.
Returns:
str: The reversed string... |
code_exercise_27k | 21,352 | Write a Python function to capitalize the first letter of each word in a given string. The function should handle multiple spaces between words and should not alter the case of the other letters in the words. The code should start with:
```python
def capitalize_words(sentence: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def capitalize_words(sentence: str) -> str:
"""
Capitalizes the first letter of each word in a given string.
Args:
sentence (str): The input sentence to be processed.
Returns:
str: The sentence with t... |
code_exercise_27k | 21,360 | Write a Python function to convert a list of strings into a single string, with a specified delimiter between each string. The function should handle an empty list by returning an empty string.
The code should start with:
```python
from typing import List
def convert_list_to_string_with_delimiter(lst: List[str], deli... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_list_to_string_with_delimiter(lst: List[str], delimiter: str) -> str:
"""
Convert a list of strings into a single string with a specified delimiter between each string.
Args:
lst... |
code_exercise_27k | 21,366 | Write a Python function to count the frequency of each character in a given string. The function should return a dictionary where the keys are the characters in the string and the values are the corresponding frequencies. The code should start with:
```python
def count_character_frequencies(string: str) -> dict:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_character_frequencies(string: str) -> dict:
"""
Count the frequency of each character in a given string.
Args:
string (str): The input string.
Returns:
dict: A dictionary where the keys are th... |
code_exercise_27k | 21,369 | Write a Python function to remove all whitespace characters from a given string, including leading, trailing, and in-between whitespace. The function should handle various types of whitespace characters such as spaces, tabs, and newlines. The code should start with:
```python
def remove_all_whitespace(string: str) -> s... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def remove_all_whitespace(string: str) -> str:
"""
Removes all whitespace characters from the given string.
Parameters:
string (str): The input string from which all whitespace characters will be removed.
... |
code_exercise_27k | 21,374 | Write a Python function to calculate the factorial of a given non-negative integer. The factorial of a number \( n \) (denoted as \( n! \)) is the product of all positive integers less than or equal to \( n \). The function should handle edge cases such as \( n = 0 \) and \( n = 1 \) correctly. The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculate the factorial of a given non-negative integer.
The factorial of a number n (denoted as n!) is the product of all positive integers
less than or equal to n.
Args:
... |
code_exercise_27k | 21,377 | Write a Python function to check if a given number is prime. The function should return `True` if the number is prime and `False` otherwise. The function should use a simple primality test algorithm known as trial division, which checks divisibility of the number by all integers up to the square root of the number.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(number: int) -> bool:
"""
Check if a given number is prime.
Args:
number (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
Notes:
... |
code_exercise_27k | 21,389 | Write a Python function to generate the Fibonacci sequence up to a given term `n`. The function should return a list of integers representing the Fibonacci sequence. If `n` is less than 0, the function should raise a `ValueError`.
The code should start with:
```python
from typing import List
def generate_fibonacci(n:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the n-th term.
Args:
n (int): The number of terms to generate, starting from 0.
Returns:
L... |
code_exercise_27k | 21,394 | Write a Python function to calculate the sum of digits in a given number. The function should handle only non-negative integers. The code should start with:
```python
def sum_of_digits(number: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_digits(number: int) -> int:
"""
Calculate the sum of digits in a given non-negative integer.
Args:
number (int): The non-negative integer for which the sum of digits needs to be calculated.
Returns:
... |
code_exercise_27k | 21,399 | Write a Python function to determine if a given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. The function should start with:
```python
def is_armstrong_number(num: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_armstrong_number(num: int) -> bool:
"""
Determine if a given number is an Armstrong number.
An Armstrong number is a number that is equal to the sum of its own digits
each raised to the power of the number of dig... |
code_exercise_27k | 21,404 | Write a Python function to convert a decimal number to its binary representation. The function should handle both positive and zero decimal numbers. The binary representation should be returned as a string.
The code should start with:
```python
def decimal_to_binary(n: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(n: int) -> str:
"""
Converts a decimal number to its binary representation.
Args:
n (int): The decimal number to convert.
Returns:
str: The binary representation of the decimal num... |
code_exercise_27k | 21,409 | Write a Python function to convert a decimal number to its hexadecimal representation. The function should handle non-negative integers and return the hexadecimal string in uppercase. The code should start with:
```python
def decimal_to_hexadecimal(decimal: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_hexadecimal(decimal: int) -> str:
"""
Converts a decimal number to its hexadecimal representation.
Args:
decimal (int): The decimal number to convert. Must be a non-negative integer.
Returns:
... |
code_exercise_27k | 21,415 | Write a Python function to find the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. The function should handle only positive integers and should not raise any exceptions. The code should start with:
```python
def gcd(num1: int, num2: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(num1: int, num2: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two positive integers using the Euclidean algorithm.
Args:
num1 (int): The first positive integer.
num2 (int): The sec... |
code_exercise_27k | 21,422 | Write a Python function to calculate the product of all elements in a given list of numbers. The function should handle both integer and floating-point numbers. The code should start with:
```python
from typing import List, Union
def calculate_product(numbers: 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 calculate_product(numbers: List[Union[int, float]]) -> Union[int, float]:
"""
Calculate the product of all elements in a list of numbers.
Args:
numbers (List[Union[int, float]]): ... |
code_exercise_27k | 21,427 | Write a Python function to determine if a list contains only unique numbers. The function should return `True` if all numbers in the list are unique, and `False` otherwise. The code should start with:
```python
from typing import List
def has_unique_numbers(numbers: List[int]) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def has_unique_numbers(numbers: List[int]) -> bool:
"""
Determines if a list contains only unique numbers.
Args:
numbers (List[int]): A list of integers to check for uniqueness.
Returns... |
code_exercise_27k | 21,434 | Write a Python function to merge two sorted lists into a new sorted list. 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], list2: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merge two sorted lists into a new sorted list.
Args:
list1 (List[int]): The first sorted list.
list2 (Lis... |
code_exercise_27k | 21,440 | Write a Python function to flatten a nested list into a single list. The function should handle lists nested at any level and should not use any external libraries. The code should start with:
```python
from typing import List
def flatten_nested_list(nested_list: List) -> List:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def flatten_nested_list(nested_list: List) -> List:
"""
Flattens a nested list into a single list.
Args:
nested_list (list): A nested list containing elements of any type, including other li... |
code_exercise_27k | 21,454 | Write a Python function to find the maximum sum of consecutive subarray (also known as the maximum subarray sum) in a given list of integers. The function should handle both positive and negative integers and return the maximum sum of any contiguous subarray within 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
def max_subarray_sum(lst: List[int]) -> int:
"""
Find the maximum sum of consecutive subarray in a given list of integers.
Args:
lst (List[int]): A list of integers.
Returns:
in... |
code_exercise_27k | 21,463 | Write a Python function to convert a list of tuples into a dictionary. Each tuple in the list should contain exactly two elements, where the first element will be used as the key and the second element as the value in the resulting dictionary. The function should handle cases where the input is not a list or where any ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Dict
def convert_tuples_to_dict(tuples_list: List[Tuple[any, any]]) -> Dict[any, any]:
"""
Convert a list of tuples to a dictionary.
Args:
tuples_list (List[Tuple[any, any]]): A list... |
code_exercise_27k | 21,477 | Write a Python function to generate a random sample from a given dataset without replacement. The function should ensure that the sample size does not exceed the dataset size and should handle cases where the sample size is less than or equal to zero by returning an empty list. The code should start with:
```python
fro... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_random_sample(data: List, sample_size: int) -> List:
"""
Generate a random sample from the given dataset without replacement.
Args:
data (List): The dataset from w... |
code_exercise_27k | 21,554 | Write a Python function to find the minimum element in a given min-heap. The function should handle the case where the heap is empty by raising a `ValueError`. The code should start with:
```python
from typing import List
def get_min_element(heap: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def get_min_element(heap: List[int]) -> int:
"""
Find the minimum element in a min-heap.
Args:
heap (List[int]): The min-heap represented as a list of integers.
Returns:
int: ... |
code_exercise_27k | 21,567 | Write a Python function to determine if a given array can be converted into a max heap. A max heap is a complete binary tree where each node's value is greater than or equal to its children. The function should return `True` if the array can be converted into a max heap, and `False` otherwise.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_max_heap(array: List[int]) -> bool:
"""
Determine if a given array can be converted into a max heap.
A max heap is a complete binary tree where each node's value is greater than or equal to i... |
code_exercise_27k | 21,575 | Write a Python function to apply a moving average filter to a given signal. The function should calculate the average of a specified number of consecutive data points (window size) and return the filtered signal. The code should start with:
```python
from typing import List
def moving_average_filter(signal: List[float... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def moving_average_filter(signal: List[float], window_size: int) -> List[float]:
"""
Apply a moving average filter to a given signal.
Args:
signal (List[float]): The input signal as a list o... |
code_exercise_27k | 21,584 | Write a Python function to sort a given array using the heap sort algorithm. The function should be able to handle any list of comparable elements and return the sorted list in ascending order. The code should start with:
```python
from typing import List
def heap_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 heap_sort(arr: List[int]) -> List[int]:
"""
Sorts a given array using the heap sort algorithm.
Args:
arr: A list of integers to be sorted.
Returns:
The sorted list in ascend... |
code_exercise_27k | 21,588 | Write a Python function to perform signal interpolation by a factor of 2 on a given signal. The function should take a 1D numpy array as input and return a new 1D numpy array with the interpolated values. The interpolation should insert a new point between each pair of original points, where the new point is the averag... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def interpolate_signal(signal: np.ndarray) -> np.ndarray:
"""
Perform signal interpolation by a factor of 2 on a given signal.
Parameters:
signal (numpy.ndarray): The input signal to be interpola... |
code_exercise_27k | 21,601 | Write a Python function to find the kth smallest element in a given heap. The heap is represented as a list of integers. The function should handle edge cases such as an empty heap or an invalid k value. The code should start with:
```python
from typing import List
import heapq
def find_kth_smallest(heap: List[int], k... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import heapq
def find_kth_smallest(heap: List[int], k: int) -> int:
"""
Find the kth smallest element in a given heap.
Args:
heap (List[int]): The heap as a list of integers.
k (int)... |
code_exercise_27k | 21,614 | Write a Python function to check if a given heap is a binary max heap.
A binary max heap is a complete binary tree where the value of each node is greater than or equal to the values of its children.
The code should start with:
```python
from typing import List
def is_binary_max_heap(heap: List[int]) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_binary_max_heap(heap: List[int]) -> bool:
"""
Check if a given heap is a binary max heap.
A binary max heap is a complete binary tree where the value of each node is greater than or equal to ... |
code_exercise_27k | 21,764 | Write a Python function to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another. The function should be efficient and use dynamic programming to c... | Here is the Python code to solve the given problem in the markdown block:
```python
def levenshtein_distance(str1: str, str2: str) -> int:
"""
Calculate the Levenshtein distance between two strings.
The Levenshtein distance is a measure of the minimum number of single-character edits
(insertions, delet... |
code_exercise_27k | 21,896 | Write a Python function to calculate the total outgoing flow from a given vertex in a directed graph. The graph is represented as an adjacency list, where each key is a vertex and the corresponding value is a list of vertices that the key vertex points to. The function should return the total number of outgoing edges f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Any
def calculate_outgoing_flow(graph: Dict[Any, List[Any]], vertex: Any) -> int:
"""
Calculates the total outgoing flow from a given vertex in a directed graph.
Args:
graph (Dict[Any... |
code_exercise_27k | 21,940 | Write a Python function to compute the Mel-filterbank matrix for Mel spectrogram computation. The function should take the number of FFT points, the sampling rate of the audio, the number of Mel bands, and optional minimum and maximum frequencies as inputs. The function should return the Mel-filterbank matrix.
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def mel_filterbank(n_fft: int, sr: int, n_mels: int = 128, fmin: float = 0.0, fmax: float = None) -> np.ndarray:
"""
Compute the Mel-filterbank matrix for Mel spectrogram computation.
Parameters:
- n... |
code_exercise_27k | 21,958 | Write a Python function to calculate the cut capacity of a given cut-set in a network. The network is represented as a dictionary where each key is a node and the value is a list of tuples representing the edges connected to that node. Each tuple contains a target node and the capacity of the edge. The cut-set is a lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Tuple
def calculate_cut_capacity(cut_set: List[Tuple[str, str]], network: Dict[str, List[Tuple[str, float]]]) -> float:
"""
Calculates the cut capacity of a given cut-set in a network.
Args:
... |
code_exercise_27k | 21,992 | Write a Python function to calculate the mean of a given list of numbers without using any external libraries. The function should handle both empty lists and lists with non-numeric values by returning `None` in such cases. The code should start with:
```python
from typing import List, Union
def calculate_mean(numbers... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def calculate_mean(numbers: List[Union[int, float]]) -> Union[float, None]:
"""
Calculate the mean of a given list of numbers without using external libraries.
Args:
numbers (List[Uni... |
code_exercise_27k | 22,002 | Write a Python function to implement MergeSort recursively. The function should sort an array in ascending order using the MergeSort algorithm. The code should start with:
```python
from typing import List
def merge_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 merge_sort(arr: List[int]) -> List[int]:
"""
Implements the MergeSort algorithm recursively to sort an array in ascending order.
Args:
arr (List[int]): The array to be sorted.
Retur... |
code_exercise_27k | 22,006 | Write a Python function to implement MergeSort iteratively. The function should sort a given list of elements in ascending order using the MergeSort algorithm iteratively. The code should start with:
```python
from typing import List
def merge_sort_iterative(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 merge_sort_iterative(arr: List[int]) -> List[int]:
"""
Sorts an array in ascending order using the MergeSort algorithm (iteratively).
Args:
arr: A list of integers to be sorted.
Ret... |
code_exercise_27k | 22,010 | Write a Python function to merge two sorted arrays into a single sorted array. The function should efficiently combine the elements from both arrays while maintaining the sorted order. The code should start with:
```python
from typing import List
def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Merge two sorted arrays into a single sorted array.
Args:
arr1 (List[int]): The first sorted array.
arr2 (... |
code_exercise_27k | 22,014 | Write a Python function to sort an unsorted array using the MergeSort algorithm. The function should be able to handle arrays of integers and return the sorted array. The code should start with:
```python
from typing import List
def merge_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 merge_sort(arr: List[int]) -> List[int]:
"""
Sorts an unsorted array using the MergeSort algorithm.
Args:
arr (List[int]): The unsorted array to be sorted.
Returns:
List[int... |
code_exercise_27k | 22,033 | Write a Python function to sort a string of space-separated integers using the MergeSort algorithm. The function should handle the conversion of the string to a list of integers, perform the sorting, and then return the sorted list as a space-separated string. The function should also handle cases where the input strin... | Here is the Python code to solve the given problem in the markdown block:
```python
def merge_sort_string(string: str) -> str:
"""
Sorts a string of space-separated integers using the MergeSort algorithm.
Args:
string (str): A string of space-separated integers.
Returns:
str: A sorted ... |
code_exercise_27k | 22,042 | Write a Python function to sort a list of strings using the MergeSort algorithm. The function should be able to handle lists of varying lengths, including empty lists and lists with duplicate strings. The code should start with:
```python
from typing import List
def merge_sort_strings(strings: List[str]) -> List[str]:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_strings(strings: List[str]) -> List[str]:
"""
Sorts a list of strings using the MergeSort algorithm.
Args:
strings (List[str]): The list of strings to be sorted.
Returns:... |
code_exercise_27k | 22,045 | Write a Python function to merge k sorted arrays into a single sorted array using a min-heap. The function should efficiently handle the merging process by leveraging a min-heap to always extract the smallest element from the arrays.
The code should start with:
```python
from typing import List
import heapq
def merge... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import heapq
def merge_k_sorted_arrays(arrays: List[List[int]]) -> List[int]:
"""
Merge k sorted arrays into a single sorted array using a min-heap.
Args:
arrays (List[List[int]]): A list of... |
code_exercise_27k | 22,047 | Write a Python function to sort a list of tuples containing student names and their scores in descending order of scores using MergeSort. The function should be able to handle an empty list and should return a new list of tuples sorted in descending order of scores. The code should start with:
```python
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_students_by_score(students: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Sorts a list of tuples containing student names and scores in descending order of scores using MergeSort.
... |
code_exercise_27k | 22,050 | Write a Python function to merge two sorted arrays such that the resulting array contains only unique elements. The function should efficiently merge the arrays while maintaining the sorted order and ensuring that each element appears only once in the resulting array.
The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_arrays_unique(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Merge two sorted arrays and return a new array with unique elements.
Args:
arr1 (List[int]): A sorted array... |
code_exercise_27k | 22,053 | Write a Python function to sort a list of dictionaries containing student details (name, age, grade) by grade in ascending order using MergeSort. The function should be able to handle an empty list and lists with a single dictionary. The code should start with:
```python
from typing import List, Dict
def sort_students... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_students_by_grade(students: List[Dict[str, any]]) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries containing student details by grade in ascending order using merge sort.
Ar... |
code_exercise_27k | 22,060 | Write a Python function to sort a list of strings using the MergeSort algorithm in lexicographic order. The function should be able to handle an empty list and lists with duplicate strings. The code should start with:
```python
from typing import List
def merge_sort_strings(arr: List[str]) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_strings(arr: List[str]) -> List[str]:
"""
Sorts a list of strings using the MergeSort algorithm in lexicographic order.
Args:
arr (List[str]): The input list of strings to be ... |
code_exercise_27k | 22,064 | Write a Python function to sort a 2D matrix using MergeSort based on a specific column. The function should be able to handle matrices of varying sizes and should sort the matrix in ascending order based on the specified column index. The code should start with:
```python
from typing import List
def sort_2d_matrix_by_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_2d_matrix_by_column(matrix: List[List[int]], sort_column: int) -> List[List[int]]:
"""
Sort a 2D matrix using MergeSort based on a specific column.
Args:
matrix (List[List[int]]): T... |
code_exercise_27k | 22,068 | Write a Python function to find the kth smallest element in a given list of integers using MergeSort. 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 find_kth_smallest(arr: List[int], k: int) -> int:
"""
Finds the kth smallest element in a given list of integers using MergeSort.
Args:
arr (List[int]): List of integers.
k (int)... |
code_exercise_27k | 22,072 | Write a Python function to sort a stack using MergeSort. The stack is represented as a list. The function should be able to handle lists of comparable elements, such as integers, floats, or strings. The function should not use any built-in sorting functions or methods. The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_stack_with_merge_sort(stack: List) -> List:
"""
Sorts a stack using the MergeSort algorithm.
Args:
stack (List): A list representing the stack to be sorted.
Returns:
Li... |
code_exercise_27k | 22,102 | Write a Python function to sort a list of sentences using MergeSort based on the number of words in each sentence. The function should be able to handle a list of strings where each string is a sentence. The sorting should be done in ascending order of the number of words in each sentence.
The code should start with:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_sentences_by_word_count(sentences: List[str]) -> List[str]:
"""
Sorts a list of sentences using MergeSort based on the number of words in each sentence.
Args:
sentences: A list of s... |
code_exercise_27k | 22,112 | Write a Python function to sort a list of IP addresses using MergeSort based on their numerical representation. The function should convert each IP address to its numerical equivalent, sort the list based on these numerical values, and return the sorted list of IP addresses.
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
def sort_ip_addresses(ip_list: List[str]) -> List[str]:
"""
Sorts a list of IP addresses using MergeSort based on their numerical representation.
Args:
ip_list (List[str]): List of strings c... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.