source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 20,472 | Write a Python function to sort a list of integers in non-increasing order using the Merge Sort algorithm. The function should be able to handle lists of any size, including empty lists and lists with duplicate elements. The code should start with:
```python
from typing import List
def merge_sort_non_increasing(arr: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_non_increasing(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in non-increasing order using the Merge Sort algorithm.
Args:
arr (List[int]): The list of integers t... |
code_exercise_27k | 20,477 | Write a Python function to determine if a given directed graph contains a cycle. The graph is represented as a dictionary where keys are vertices and values are lists of vertices that the key vertex has directed edges to. The function should return `True` if the graph contains a cycle, and `False` otherwise.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def has_cycle_in_graph(graph: Dict[int, List[int]]) -> bool:
"""
Determine if a given directed graph contains a cycle.
Args:
graph (Dict[int, List[int]]): A dictionary representing the... |
code_exercise_27k | 20,479 | Write a Python function to calculate the mean of each row in a given 2D NumPy array. The function should take a 2D NumPy array as input and return a 1D NumPy array containing the mean values of each row. The code should start with:
```python
import numpy as np
def calculate_row_means(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 calculate_row_means(arr: np.ndarray) -> np.ndarray:
"""
Calculates the mean of each row in a 2D NumPy array.
Args:
arr (np.ndarray): The input 2D NumPy array.
Returns:
np.ndarray... |
code_exercise_27k | 20,480 | Write a Python function to sort a list of dates (in YYYY-MM-DD format) in chronological order using the Merge Sort algorithm. The function should handle dates in string format and return a new list containing the sorted dates.
The code should start with:
```python
from typing import List
def sort_dates(dates: List[st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_dates(dates: List[str]) -> List[str]:
"""
Sorts a list of dates in chronological order using the Merge Sort algorithm.
Args:
dates (List[str]): A list of dates in the YYYY-MM-DD for... |
code_exercise_27k | 20,481 | Write a Python function to find the length of the longest cycle in a directed graph. The function should use depth-first search (DFS) to traverse the graph and identify cycles. If multiple cycles are found, the function should return the length of the longest one. The graph is represented as a dictionary where keys are... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def find_longest_cycle_length(graph: Dict[str, List[str]]) -> int:
"""
Finds the length of the longest cycle in a directed graph using depth-first search (DFS).
Args:
graph (dict): A d... |
code_exercise_27k | 20,490 | Write a Python function to calculate the sum of each column in a 2D NumPy array. The function should take a 2D NumPy array as input and return a 1D NumPy array containing the sum of each column. The code should start with:
```python
import numpy as np
def calculate_column_sums(array: np.ndarray) -> np.ndarray:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def calculate_column_sums(array: np.ndarray) -> np.ndarray:
"""
Calculates the sum of each column in a 2D NumPy array.
Args:
array (numpy.ndarray): The input 2D array.
Returns:
numpy... |
code_exercise_27k | 20,493 | Write a Python function to sort a list of binary numbers (strings) in ascending order using the Merge Sort algorithm. The function should handle lists of any length, including empty lists and lists with duplicate binary numbers. The function should be efficient and should correctly handle binary numbers represented as ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_binary(numbers: List[str]) -> List[str]:
"""
Sort a list of binary numbers (strings) in ascending order using Merge Sort algorithm.
Args:
numbers (List[str]): List of binary n... |
code_exercise_27k | 20,495 | Write a Python function to create a new NumPy array where each element of the input NumPy array is squared. The function should handle both integer and floating-point arrays. The code should start with:
```python
import numpy as np
def square_elements(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 square_elements(arr: np.ndarray) -> np.ndarray:
"""
Create a new NumPy array where each element of the input array is squared.
Parameters:
arr (numpy.ndarray): An input NumPy array containing ele... |
code_exercise_27k | 20,497 | Write a Python function to sort a list of Roman numerals in ascending order using the Merge Sort algorithm. The function should convert Roman numerals to their corresponding decimal values for comparison during the sorting process. The code should start with:
```python
from typing import List
def merge_sort_roman_nume... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_roman_numerals(roman_numerals: List[str]) -> List[str]:
"""
Sorts a list of Roman numerals in ascending order using the Merge Sort algorithm.
Args:
roman_numerals (List[str]):... |
code_exercise_27k | 20,511 | Write a Python function to calculate the cumulative product of elements in a 1D NumPy array. The function should take a 1D NumPy array as input and return another 1D NumPy array containing the cumulative product of the input array's elements. The function should raise a `ValueError` if the input array is not 1-dimensio... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def cumulative_product(array: np.ndarray) -> np.ndarray:
"""
Calculate the cumulative product of elements in a 1D NumPy array.
Parameters:
array (np.ndarray): 1D NumPy array to calculate the cumulati... |
code_exercise_27k | 20,513 | Write a Python function to flatten a 2D NumPy array to a 1D array. The function should ensure that the input is a 2D array and raise a `ValueError` if it is not. The function should return the flattened 1D array.
The code should start with:
```python
import numpy as np
def flatten_2d_array(array: np.ndarray) -> np.nd... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def flatten_2d_array(array: np.ndarray) -> np.ndarray:
"""
Flattens a 2D NumPy array to a 1D array.
Parameters
----------
array : numpy.ndarray
The 2D array to be flattened.
Returns
... |
code_exercise_27k | 20,520 | Write a Python function to calculate the square of each element in a given NumPy array that is divisible by a specified divisor. The function should return a new NumPy array containing the squared values of the elements that meet the divisibility condition.
The code should start with:
```python
import numpy as np
def... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def calculate_squared_divisible_by(arr: np.ndarray, divisor: int) -> np.ndarray:
"""
Calculates the square of each element in a NumPy array that is divisible by a specified divisor.
Args:
arr (nu... |
code_exercise_27k | 20,523 | Write a Python function to replace every negative value in a given NumPy array with 0. The function should take a NumPy array as input and return the modified NumPy array. The code should start with:
```python
import numpy as np
def replace_negative_with_zero(array: np.ndarray) -> np.ndarray:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def replace_negative_with_zero(array: np.ndarray) -> np.ndarray:
"""
Replace every negative value in the input NumPy array with 0.
Args:
array (np.ndarray): Input NumPy array.
Returns:
... |
code_exercise_27k | 20,524 | Write a Python function to find the maximum number of nodes to be visited before entering a cycle in a linked list. The function should return the length of the cycle if a cycle exists, otherwise it should return 0. The linked list is represented by a head node, and each node has a value and a next pointer.
The code s... | Here is the Python code to solve the given problem in the markdown block:
```python
class ListNode:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def find_cycle_length(head: ListNode) -> int:
"""
Find the maximum number of nodes to be visited before entering a ... |
code_exercise_27k | 20,530 | Write a Python function to sort a list of strings based on the number of vowels in each string using the Merge Sort algorithm. The function should be efficient and handle both uppercase and lowercase vowels.
The code should start with:
```python
from typing import List
def sort_strings_by_vowel_count(strings: List[st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_strings_by_vowel_count(strings: List[str]) -> List[str]:
"""
Sorts a list of strings based on the number of vowels in each string using the Merge Sort algorithm.
Args:
strings (List... |
code_exercise_27k | 20,537 | Write a Python function to calculate the outer product of two given NumPy arrays. The function should take two NumPy arrays as input and return their outer product. The outer product of two vectors \( \mathbf{a} \) and \( \mathbf{b} \) is a matrix where each element at position (i, j) is the product of \( a_i \) and \(... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def outer_product(arr1: np.ndarray, arr2: np.ndarray) -> np.ndarray:
"""
Calculate the outer product of two NumPy arrays.
Parameters:
arr1 (np.ndarray): The first input NumPy array.
arr2 (np.ndar... |
code_exercise_27k | 20,541 | Write a Python function to sort a list of strings in lexicographic order using a custom sorting key. The custom sorting key should prioritize strings that start with uppercase letters before those that start with lowercase letters, and within each group, strings should be sorted lexicographically.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def sort_strings_with_custom_key(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order with a custom key that prioritizes
strings starting with uppercase letters befo... |
code_exercise_27k | 20,553 | Write a Python function to find the row with the highest sum of elements in a given 2D NumPy array. The function should return the index of the row with the highest sum. If there are multiple rows with the same highest sum, return the index of the first occurrence.
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
def find_row_with_highest_sum(arr: np.ndarray) -> int:
"""
Find the row with the highest sum of elements in a 2D NumPy array.
Args:
arr (numpy.ndarray): 2D array of shape (n, m)
Returns:
... |
code_exercise_27k | 20,555 | Write a Python function to sort a list of tuples based on multiple keys in ascending order. Each tuple in the list contains multiple elements, and the sorting should be performed first by the first element, then by the second element, and so on. The function should handle tuples of any length and should return the sort... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_multiple_keys(lst: List[Tuple]) -> List[Tuple]:
"""
Sorts a list of tuples based on multiple keys in ascending order.
Args:
lst (List[Tuple]): A list of tuples to b... |
code_exercise_27k | 20,560 | Write a Python function to sort a list of tuples based on multiple keys, where the first key is sorted in ascending order and the second key is sorted in descending order. The function should handle any list of tuples where each tuple contains exactly two elements.
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_tuples_by_keys(data: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Sort a list of tuples based on multiple keys. The first key is sorted in ascending order,
and the second key ... |
code_exercise_27k | 20,569 | Write a Python function to replace all occurrences of a specific value with another value in a 2D NumPy array. The function should return the modified array without altering the original array. The code should start with:
```python
import numpy as np
def replace_value_in_array(arr: np.ndarray, old_val, new_val) -> np.... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def replace_value_in_array(arr: np.ndarray, old_val, new_val) -> np.ndarray:
"""
Replace all occurrences of a specific value in a 2D NumPy array with another value.
Parameters:
arr (numpy.ndarray): T... |
code_exercise_27k | 20,584 | Write a Python function to determine if two given strings can form a cycle by concatenating them in a specific way. Specifically, the function should check if one string can be obtained by rotating the other string. For example, rotating "abc" by one position to the left results in "bca". The function should return `Tr... | Here is the Python code to solve the given problem in the markdown block:
```python
def can_form_cycle(str1: str, str2: str) -> bool:
"""
Determine if two strings can form a cycle by rotating one of them.
Parameters:
str1 (str): The first string.
str2 (str): The second string.
Returns:
boo... |
code_exercise_27k | 20,585 | Write a Python function to implement the merge sort algorithm and demonstrate its stability. The function should sort a list of tuples based on the first element of each tuple in ascending order. The code should start with:
```python
from typing import List, Tuple
def merge_sort_stable(arr: List[Tuple[int, str]]) -> L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def merge_sort_stable(arr: List[Tuple[int, str]]) -> List[Tuple[int, str]]:
"""
Sorts a list of tuples based on the first element of each tuple in ascending order using the merge sort algorithm.
... |
code_exercise_27k | 20,588 | Write a Python function to determine if there is a cycle formed by combining two integer arrays. A cycle is defined as a sequence where the elements of the first array can be rearranged such that they match the elements of the second array in a cyclic order. The function should return `True` if such a cycle exists, and... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def has_cycle(array1: List[int], array2: List[int]) -> bool:
"""
Determines if there is a cycle formed by combining two integer arrays.
Args:
array1 (List[int]): The first integer array.
... |
code_exercise_27k | 20,589 | Write a Python function to implement a stable version of the Quick Sort algorithm that sorts a list of tuples based on their first element. The function should maintain the relative order of tuples with equal first elements. The code should start with:
```python
from typing import List, Tuple
def stable_quick_sort(arr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def stable_quick_sort(arr: List[Tuple[int, str]]) -> List[Tuple[int, str]]:
"""
Implements a stable version of the Quick Sort algorithm to sort a list of tuples based on their first element.
... |
code_exercise_27k | 20,605 | Write a Python function to check if there is a cycle formed by combining two directed graphs represented as adjacency lists. The function should return `True` if there is a cycle in the combined graph, and `False` otherwise. The code should start with:
```python
from typing import Dict, List
def has_cycle_combining_gr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def has_cycle_combining_graphs(graph1: Dict[int, List[int]], graph2: Dict[int, List[int]]) -> bool:
"""
Check if there is a cycle formed by combining two directed graphs represented as adjacency li... |
code_exercise_27k | 20,611 | Implement the cocktail sort algorithm and demonstrate its stability. The cocktail sort, also known as bidirectional bubble sort, is a variation of the bubble sort that sorts the list in both directions alternately. The task is to write a Python function that sorts a list of tuples using the cocktail sort algorithm, ens... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def cocktail_sort(arr: List[Tuple[int, str]]) -> List[Tuple[int, str]]:
"""
Sorts the given list of tuples in ascending order using the cocktail sort algorithm.
The sorting is stable, meaning ... |
code_exercise_27k | 20,623 | Write a Python function to rearrange an array of integers such that the absolute difference between consecutive elements is maximized. The function should take a list of integers as input and return the rearranged list.
The code should start with:
```python
from typing import List
def maximize_consecutive_difference(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def maximize_consecutive_difference(arr: List[int]) -> List[int]:
"""
Rearranges the given array of integers to maximize the absolute difference between consecutive elements.
Args:
arr (List... |
code_exercise_27k | 20,624 | Write a Python function to find the index of the first occurrence of a specific value in a 1D Numpy array. If the value is not found, the function should return -1. The function should be efficient and handle edge cases such as an empty array or a value that does not exist in the array.
The code should start with:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def find_first_occurrence(arr: np.ndarray, value) -> int:
"""
Find the index of the first occurrence of a specific value in a 1D Numpy array.
Parameters:
arr (numpy.ndarray): The 1D Numpy array.
... |
code_exercise_27k | 20,630 | Write a Python function to sort a list of floating-point numbers by considering only their integral part while maintaining stability. Stability means that if two numbers have the same integral part, their relative order in the input list should be preserved in the output 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
def sort_list_by_integral_part(numbers: List[float]) -> List[float]:
"""
Sorts a list of floating-point numbers by considering only their integral part and maintains stability.
Args:
numbers... |
code_exercise_27k | 20,634 | Write a Python function to implement a stable sorting algorithm that can handle large datasets efficiently. The function should sort the given list in ascending order using the merge sort 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]:
"""
Sorts the given array in ascending order using the merge sort algorithm.
Args:
arr (List[int]): The input list to be sorted.
Returns:
... |
code_exercise_27k | 20,639 | Write a Python function to merge overlapping intervals from a given list of intervals while ensuring stability. Each interval is represented as a tuple of two integers (start, end). The function should return a new list of merged intervals, maintaining the order of intervals as they appear in the input list.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def merge_intervals(intervals: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Merge overlapping intervals from a given list of intervals while ensuring stability.
Args:
interval... |
code_exercise_27k | 20,642 | Write a Python function to replace all elements in a given NumPy array that are greater than a specified threshold with that threshold value. The function should handle both integer and floating-point arrays. The code should start with:
```python
import numpy as np
def replace_elements_above_threshold(arr: np.ndarray,... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def replace_elements_above_threshold(arr: np.ndarray, threshold: float) -> np.ndarray:
"""
Replace all elements in a NumPy array that are greater than a specified threshold with that threshold value.
Arg... |
code_exercise_27k | 20,643 | Write a Python function to find the length of the longest chain formed by combining multiple cycles in an undirected graph. The graph is represented as a dictionary where keys are vertices and values are lists of adjacent vertices. The function should start with:
```python
from typing import Dict, List
def find_longes... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def find_longest_chain(graph: Dict[int, List[int]]) -> int:
"""
Find the length of the longest chain formed by combining multiple cycles in an undirected graph.
Args:
- graph: a dictionary... |
code_exercise_27k | 20,645 | Write a Python function to check if any element in a given NumPy array satisfies a specified condition. The condition is provided as a callable function that takes an array element as input and returns a boolean value. The function should return `True` if at least one element in the array satisfies the condition, and `... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def check_condition(arr: np.ndarray, condition: callable) -> bool:
"""
Check if any element in a NumPy array satisfies a given condition.
Parameters:
arr (numpy.ndarray): The input array.
... |
code_exercise_27k | 20,652 | Write a Python function to sort a list of tuples based on complex conditions involving multiple attributes, while ensuring stability. The function should use a custom comparison function to determine the order of the tuples. The code should start with:
```python
from typing import List, Tuple
from functools import cmp_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
from functools import cmp_to_key
def sort_tuples_by_attributes(tuples: List[Tuple]) -> List[Tuple]:
"""
Sorts a list of tuples based on complex conditions involving multiple attributes, while ensu... |
code_exercise_27k | 20,654 | Write a Python function to sort a list of integers in ascending order using the Bubble Sort algorithm. The function should handle lists of any length, including empty lists. The code should start with:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
code_exercise_27k | 20,656 | Write a Python function to perform binary search on a sorted NumPy array to find a specific value. The function should return the index of the target value if found, otherwise return `None`. The code should start with:
```python
import numpy as np
def binary_search_numpy(arr: np.ndarray, target: float) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def binary_search_numpy(arr: np.ndarray, target: float) -> int:
"""
Performs binary search on a sorted NumPy array to find a specific value.
Args:
arr (numpy.ndarray): The sorted NumPy array to s... |
code_exercise_27k | 20,657 | Write a Python function to sort a list of strings in alphabetical order using the Insertion Sort algorithm. The function should handle lists of strings and ensure that the input is a list and all elements are strings. If the input is not a list or contains non-string elements, the function should raise a `TypeError`.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_strings(lst: List[str]) -> List[str]:
"""
Sorts a list of strings in alphabetical order using the Insertion Sort algorithm.
Args:
lst (List[str]): A list of strings to be ... |
code_exercise_27k | 20,660 | Write a Python function to sort a list of integers in descending order using the Selection Sort algorithm. The function should return a new list with the integers sorted in descending order. The code should start with:
```python
from typing import List
def selection_sort_descending(lst: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort_descending(lst: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using the Selection Sort algorithm.
Args:
lst (List[int]): A list of integers to ... |
code_exercise_27k | 20,664 | Write a Python function to implement the Counting Sort algorithm for sorting a list of non-negative integers. The function should handle lists with elements ranging from 0 to the maximum value in the 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]:
"""
Perform counting sort on a list of non-negative integers.
Args:
arr (List[int]): List of non-negative integers to be sorted.
Returns:... |
code_exercise_27k | 20,668 | Write a Python function to sort a list of floating-point numbers in non-decreasing order using the Shell Sort algorithm. The function should handle lists of any length and should return the sorted list. The code should start with:
```python
from typing import List
def shell_sort(numbers: List[float]) -> List[float]:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def shell_sort(numbers: List[float]) -> List[float]:
"""
Sorts a list of floating-point numbers in non-decreasing order using the Shell Sort algorithm.
Args:
numbers (List[float]): The list ... |
code_exercise_27k | 20,671 | Implement the Merge Sort algorithm to sort a list of integers in ascending order. The function should be able to handle lists of any length, including empty lists and lists with duplicate elements. 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 a list of integers in ascending order using the Merge Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
code_exercise_27k | 20,697 | Write a Python function to calculate the partial sums along a specific axis of a given NumPy array. The function should handle the input validation to ensure the axis is within the valid range of the array's dimensions. The code should start with:
```python
import numpy as np
def calculate_partial_sums(arr: np.ndarray... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def calculate_partial_sums(arr: np.ndarray, axis: int = 0) -> np.ndarray:
"""
Calculate the partial sums along a specific axis of a NumPy array.
Parameters:
arr (np.ndarray): The input NumPy array.
... |
code_exercise_27k | 20,704 | Write a Python function to sort a list of floating-point numbers in non-increasing order using the Gnome Sort algorithm. The function should handle lists of any length, including empty lists. The code should start with:
```python
from typing import List
def gnome_sort_non_increasing(numbers: List[float]) -> List[float... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def gnome_sort_non_increasing(numbers: List[float]) -> List[float]:
"""
Sorts a list of floating-point numbers in non-increasing order using the Gnome Sort algorithm.
Args:
numbers (List[flo... |
code_exercise_27k | 20,709 | Write a Python function to sort a list of numbers in ascending order using the bubble sort algorithm. The function should be efficient and handle various edge cases, such as an empty list or a list with duplicate elements. The code should start with:
```python
from typing import List
def bubble_sort(lst: List[int]) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(lst: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the bubble sort algorithm.
Args:
lst (List[int]): The list of numbers to be sorted.
Re... |
code_exercise_27k | 20,712 | Write a Python function to sort a list of strings in lexicographic order using the bubble sort algorithm. The function should handle lists of varying lengths and strings of different lengths. The code should start with:
```python
from typing import List
def lexicographic_bubble_sort(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 lexicographic_bubble_sort(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the bubble sort algorithm.
Args:
strings (List[str]): The list of str... |
code_exercise_27k | 20,714 | Write a Python function to calculate the sum of squares of the elements in a given NumPy array. The function should handle arrays of any shape and return the sum of squares as a float. The code should start with:
```python
import numpy as np
def sum_of_squares(arr: np.ndarray) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def sum_of_squares(arr: np.ndarray) -> float:
"""
Calculate the sum of squares of the elements in a NumPy array.
Args:
arr (np.ndarray): The input array.
Returns:
float: The sum of s... |
code_exercise_27k | 20,725 | Write a Python function to sort a list of numbers in ascending order using the quicksort algorithm. The function should be able to handle lists of integers and floating-point numbers. The code should start with:
```python
from typing import List
def quicksort(arr: List[float]) -> List[float]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort(arr: List[float]) -> List[float]:
"""
Sorts a list of numbers in ascending order using the quicksort algorithm.
Args:
arr (List[float]): A list of numbers to be sorted.
Re... |
code_exercise_27k | 20,728 | Write a Python function to sort a list of strings in lexicographic order using the quicksort 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 quicksort_lexicographic(arr: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort_lexicographic(arr: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the quicksort algorithm.
Args:
arr (List[str]): The list of strings to be s... |
code_exercise_27k | 20,739 | Write a Python function to sort a list of numbers in descending order using the merge sort algorithm. The function should be able to handle lists of any length, including empty lists. The merge sort algorithm should be implemented in a way that it recursively divides the list into halves until each sublist contains onl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts a list of numbers in descending order using the merge sort algorithm.
Args:
arr (List[int]): The list of numbers to be sorte... |
code_exercise_27k | 20,742 | Write a Python function to create a NumPy array with a specified shape and round the values in the array to a given number of decimal places. The function should return the rounded array. The code should start with:
```python
import numpy as np
def create_rounded_array(shape: tuple, decimal_places: int) -> np.ndarray:... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_rounded_array(shape: tuple, decimal_places: int) -> np.ndarray:
"""
Create a NumPy array with the given shape and round the values to the specified number of decimal places.
Args:
shap... |
code_exercise_27k | 20,744 | Write a Python function to sort a list of strings in reverse lexicographic order using the merge sort algorithm. 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 reverse_lexicographic_sort(strings: List[str]) -> List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_lexicographic_sort(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in reverse lexicographic order using the merge sort algorithm.
Args:
strings: A list of strings t... |
code_exercise_27k | 20,757 | Write a Python function to sort a list of strings in lexicographic order using the radix sort algorithm. The function should handle strings of varying lengths and ensure that the sorting is stable. The code should start with:
```python
from typing import List
def lexicographic_radix_sort(strings: List[str]) -> List[st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def lexicographic_radix_sort(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the radix sort algorithm.
Args:
strings (List[str]): A list of strings... |
code_exercise_27k | 20,761 | Write a Python function to sort a list of numbers in ascending order using the selection sort algorithm. The function should take a list of numbers as input and return the sorted list. The code should start with:
```python
from typing import List
def selection_sort(numbers: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the selection sort algorithm.
Args:
numbers (List[int]): A list of numbers to be sor... |
code_exercise_27k | 20,765 | Write a Python function to sort a list of strings in lexicographic order using the selection sort algorithm. The function should handle lists of strings and return the sorted list. The code should start with:
```python
from typing import List
def sort_strings_lexicographically(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 sort_strings_lexicographically(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the selection sort algorithm.
Args:
strings (List[str]): A list ... |
code_exercise_27k | 20,776 | Write a Python function to sort a list of numbers in descending order using the selection sort algorithm. The function should not modify the original list but return a new list with the sorted numbers. The code should start with:
```python
from typing import List
def selection_sort_descending(nums: List[int]) -> List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort_descending(nums: List[int]) -> List[int]:
"""
Sorts a list of numbers in descending order using the selection sort algorithm.
Args:
nums (List[int]): A list of numbers to ... |
code_exercise_27k | 20,777 | Write a Python function to calculate the element-wise product of a given Numpy array with a scalar value. The function should take a Numpy array and a scalar as input and return a new Numpy array where each element is the product of the corresponding element in the input array and the scalar.
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 element_wise_product(arr: np.ndarray, scalar: float) -> np.ndarray:
"""
Calculate the element-wise product of a numpy array with a scalar.
Args:
arr (numpy.ndarray): The numpy array to be mul... |
code_exercise_27k | 20,785 | Write a Python function to sort a list of numbers in ascending order using the count sort algorithm. The function should handle lists with both positive and negative integers. The code should start with:
```python
from typing import List
def count_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 count_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the count sort algorithm.
Args:
arr (List[int]): The list of numbers to be sorted.
Retu... |
code_exercise_27k | 20,786 | Write a Python function to calculate the cumulative minimum of elements in a given NumPy array. The function should take a NumPy array as input and return a new NumPy array where each element at index `i` is the minimum value from the start of the array up to index `i`.
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 cumulative_minimum(arr: np.ndarray) -> np.ndarray:
"""
Calculate the cumulative minimum of elements in a given NumPy array.
Parameters:
arr (numpy.ndarray): The input NumPy array.
Returns:
... |
code_exercise_27k | 20,789 | Write a Python function to calculate the squared Euclidean distance between the elements of a given NumPy array. The function should return a square matrix where each element represents the squared Euclidean distance between two elements of the input array. The input array is expected to be a 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 calculate_squared_euclidean_distance(array: np.ndarray) -> np.ndarray:
"""
Calculates the squared Euclidean distance between the elements of a 2D NumPy array.
Args:
array (np.ndarray): A 2D N... |
code_exercise_27k | 20,790 | Write a Python function to sort a list of strings in lexicographic order using the count sort algorithm. The function should handle strings of varying lengths and ensure that the sorting is stable. The code should start with:
```python
from typing import List
def count_sort_lexicographic(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 count_sort_lexicographic(arr: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographic order using the count sort algorithm.
Args:
arr (List[str]): List of strings to be sor... |
code_exercise_27k | 20,791 | Write a Python function to find the number of distinct cycles in a given string. A cycle is defined as any rotation of the string where the characters are shifted to the right. For example, the string "abc" has the following cycles: "abc", "bca", and "cab". The function should return the number of unique cycles.
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_distinct_cycles(string: str) -> int:
"""
Counts the number of distinct cycles in a given string.
A cycle is defined as any rotation of the string where the characters are shifted to the right.
For example, the... |
code_exercise_27k | 20,793 | Write a Python function to sort a list of numbers in descending order using the count sort algorithm. The function should handle non-negative integers only. The code should start with:
```python
from typing import List
def count_sort_descending(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 count_sort_descending(nums: List[int]) -> List[int]:
"""
Sorts a list of non-negative integers in descending order using the count sort algorithm.
Args:
nums (List[int]): List of non-neg... |
code_exercise_27k | 20,794 | Write a Python function to subtract two NumPy arrays element-wise. The function should ensure that the input arrays have compatible shapes before performing the subtraction. If the shapes are incompatible, the function should raise a `ValueError`.
The code should start with:
```python
import numpy as np
def subtract_... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def subtract_arrays(a: np.ndarray, b: np.ndarray) -> np.ndarray:
"""
Subtract two NumPy arrays element-wise.
Args:
a (numpy.ndarray): The first input array.
b (numpy.ndarray): The second ... |
code_exercise_27k | 20,804 | Write a Python function to compute the natural logarithm of each element in a given NumPy array. The function should handle both positive and non-positive elements in the array. For non-positive elements, the function should return a default value (e.g., `None`). 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, Union
def compute_logarithm_with_default(arr: np.ndarray, default_value: Union[int, float, None] = None) -> List[Union[float, None]]:
"""
Compute the natural logarithm of each element... |
code_exercise_27k | 20,806 | Write a Python function to sort a list of strings in lexicographic order using the Shell sort algorithm. The function should be able to handle lists of varying lengths and should return the sorted list. The code should start with:
```python
from typing import List
def shell_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 shell_sort_strings(strings: List[str]) -> List[str]:
"""
Sort a list of strings in lexicographic order using the Shell sort algorithm.
Args:
strings (List[str]): The list of strings to b... |
code_exercise_27k | 20,807 | Write a Python function to sort a list of numbers in descending order using the Shell Sort algorithm. The function should take a list of numbers as input and return the sorted list in descending order. The code should start with:
```python
from typing import List
def shell_sort_descending(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 shell_sort_descending(nums: List[int]) -> List[int]:
"""
Sorts a list of numbers in descending order using the Shell Sort algorithm.
Args:
nums (List[int]): A list of numbers to be sorte... |
code_exercise_27k | 20,812 | Write a Python function to generate a NumPy array filled with a specified value, given the dimensions of the array. The function should be able to handle both integer and floating-point values. The code should start with:
```python
import numpy as np
from typing import Union
def generate_filled_array(rows: int, column... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import Union
def generate_filled_array(rows: int, columns: int, fill_value: Union[int, float]) -> np.ndarray:
"""
Generate a NumPy array filled with a specified value and given dimensions.
Ar... |
code_exercise_27k | 20,824 | Write a Python function to find the number of common elements in the cycles formed by two integer arrays. The cycles are formed by repeatedly iterating through the elements of each array. The function should start with:
```python
from typing import List
def count_common_elements_in_cycles(arr1: List[int], arr2: List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_common_elements_in_cycles(arr1: List[int], arr2: List[int]) -> int:
"""
Counts the number of common elements in the cycles formed by two integer arrays.
Args:
arr1 (List[int]): The... |
code_exercise_27k | 20,826 | Write a Python function to flatten a given NumPy array to a 1D array. The function should handle arrays of any dimensionality and return a 1D array containing all the elements of the input array in a flattened form.
The code should start with:
```python
import numpy as np
def flatten_numpy_array(arr: np.ndarray) -> n... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def flatten_numpy_array(arr: np.ndarray) -> np.ndarray:
"""
Flattens a given NumPy array to a 1D array.
Parameters:
arr (numpy.ndarray): The input array of any dimensionality.
Returns:
numpy... |
code_exercise_27k | 20,827 | Write a Python function to sort a list of integers in ascending order using the Bubble Sort algorithm. The function should be efficient and handle edge cases such as an empty list or a list with a single element. The code should start with:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
code_exercise_27k | 20,831 | Write a Python function to sort a list of strings in lexicographical order using the Insertion Sort algorithm. The function should be able to handle lists of varying lengths and should not modify the original list. The code should start with:
```python
from typing import List
def insertion_sort_lexicographical(words: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_lexicographical(words: List[str]) -> List[str]:
"""
Sorts a list of strings in lexicographical order using the Insertion Sort algorithm.
Args:
words (List[str]): A list of... |
code_exercise_27k | 20,833 | Write a Python function to perform the Selection Sort algorithm on a given list of floats in descending order. The function should iterate through the list, find the maximum element in the unsorted portion, and place it at the beginning of the unsorted portion. This process should be repeated until the entire list is s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort_descending(numbers: List[float]) -> List[float]:
"""
Sorts a list of floats in descending order using the Selection Sort algorithm.
Args:
numbers (List[float]): A list of ... |
code_exercise_27k | 20,841 | Write a Python function to sort a list of tuples in ascending order based on the second element of each tuple using the Merge Sort algorithm. The function should be named `sort_tuples_by_second_element` and should take a single parameter, a list of tuples, and return the sorted list of tuples.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_second_element(lst: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Sorts a list of tuples in ascending order based on the second element of each tuple using the Merge Sort... |
code_exercise_27k | 20,856 | Implement the Shell Sort algorithm to sort a list of floats in ascending order. The function should be able to handle lists of varying lengths and should return the sorted list. The code should start with:
```python
from typing import List
def shell_sort(arr: List[float]) -> List[float]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def shell_sort(arr: List[float]) -> List[float]:
"""
Perform Shell Sort to sort a list of floats in ascending order.
Shell Sort is a variation of the Insertion Sort algorithm. It starts
by sorti... |
code_exercise_27k | 20,880 | Write a Python function to sort a list of tuples based on the second element in descending order. The function should take a list of tuples as input and return the sorted list. The code should start with:
```python
from typing import List, Tuple
def sort_tuples_by_second_element(lst: List[Tuple[int, int]]) -> List[Tup... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_second_element(lst: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Sorts a list of tuples based on the second element in descending order.
Args:
lst (List[Tup... |
code_exercise_27k | 20,892 | Write a Python function to sort a list of dictionaries based on multiple keys. The function should handle cases where the keys might not exist in some dictionaries and should sort those dictionaries last. The code should start with:
```python
from typing import List, Dict
def sort_dicts_with_missing_keys(data: List[Di... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dicts_with_missing_keys(data: List[Dict[str, any]], keys: List[str]) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries based on multiple keys, handling cases where keys might be mi... |
code_exercise_27k | 20,895 | Write a Python function to sort a list of integers in non-descending order. The function should handle edge cases such as an empty list and ensure that all elements in the list are integers. If the input is not a list or contains non-integer elements, the function should raise appropriate exceptions.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_integers(nums: List[int]) -> List[int]:
"""
Sorts a list of integers in non-descending order.
Args:
nums (List[int]): List of integers to be sorted.
Returns:
List[int]:... |
code_exercise_27k | 20,901 | Write a Python function to sort a list of tuples based on the first element in descending order. The function should handle lists of tuples where each tuple contains at least one element. The code should start with:
```python
from typing import List, Tuple
def sort_tuples_by_first_element_descending(tuples_list: List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_first_element_descending(tuples_list: List[Tuple]) -> List[Tuple]:
"""
Sorts a list of tuples based on the first element in descending order.
Args:
tuples_list (Lis... |
code_exercise_27k | 20,905 | Write a Python function to sort a list of integers in non-ascending order without modifying the original list. The function should return a new list with the integers sorted in non-ascending order. The code should start with:
```python
from typing import List
def sort_list_non_ascending(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 sort_list_non_ascending(nums: List[int]) -> List[int]:
"""
Sorts a list of integers in non-ascending order without modifying the original list.
Args:
nums (List[int]): The list of intege... |
code_exercise_27k | 20,916 | Write a Python function to sort a list of integers in odd-even order. The function should return a new list where all odd numbers are sorted in ascending order and placed before all even numbers, which are also sorted in ascending order. The code should start with:
```python
from typing import List
def sort_odd_even_o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_odd_even_order(lst: List[int]) -> List[int]:
"""
Sorts a list of integers in odd-even order.
Args:
lst (List[int]): A list of integers to be sorted.
Returns:
List[int]:... |
code_exercise_27k | 20,926 | Write a Python function to sort a list of integers using the bubble sort algorithm. The function should be able to handle lists of any length and should return the sorted list. The code should start with:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the bubble sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
Li... |
code_exercise_27k | 20,929 | Write a Python function to sort a list of strings using the insertion sort algorithm. The function should handle lists of strings with varying lengths and characters. The code should start with:
```python
from typing import List
def insertion_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 insertion_sort_strings(strings: List[str]) -> List[str]:
"""
Sorts a list of strings using the insertion sort algorithm.
Args:
strings (List[str]): The list of strings to be sorted.
... |
code_exercise_27k | 20,935 | Write a Python function to sort a list of dictionaries using the merge sort algorithm based on a specified key. The function should be able to handle lists of dictionaries with varying lengths and different keys. The code should start with:
```python
from typing import List, Dict
def merge_sort_dicts(arr: List[Dict[st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def merge_sort_dicts(arr: List[Dict[str, any]], key: str) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries using the merge sort algorithm based on a specified key.
Args:
arr (... |
code_exercise_27k | 20,941 | Write a Python function to sort a list of strings using the heapsort algorithm. The function should be able to handle a list of strings and return the sorted list. The heapsort algorithm should be implemented from scratch without using any built-in sorting functions.
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 heapsort_strings(strings: List[str]) -> List[str]:
"""
Sorts a list of strings using the heapsort algorithm.
Args:
strings (List[str]): The list of strings to be sorted.
Returns:
... |
code_exercise_27k | 20,947 | Write a Python function to sort a list of dictionaries using the counting sort algorithm based on a specified key. The function should handle dictionaries with integer values for the specified key and return the sorted list of dictionaries.
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 counting_sort_dicts(lst: List[Dict[str, int]], key: str) -> List[Dict[str, int]]:
"""
Sorts a list of dictionaries using the counting sort algorithm based on a specified key.
Args:
... |
code_exercise_27k | 20,949 | Write a Python function to sort a list of integers using the Shell 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 shell_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 shell_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Shell sort algorithm.
Args:
arr (List[int]): A list of integers to be sorted.
Returns:
List[i... |
code_exercise_27k | 20,960 | Write a Python function to sort a list of tuples using the comb sort algorithm. The function should take a list of tuples as input and return the sorted list. Each tuple in the list contains two integers, and the sorting should be based on the first element of the tuples. If two tuples have the same first element, the ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def comb_sort_tuples(tuples_list: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Sorts a list of tuples using the comb sort algorithm.
The sorting is based on the first element of the t... |
code_exercise_27k | 20,969 | Write a Python function to sort a list of integers using the bucket sort algorithm. The function should handle lists with both positive and negative integers. The code should start with:
```python
from typing import List
def bucket_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 bucket_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the bucket sort algorithm.
Args:
arr (List[int]): The input list to be sorted.
Returns:
List[int... |
code_exercise_27k | 20,971 | Write a Python function to calculate the travel cost given the distance covered, average speed, and fuel efficiency. The function should also allow the user to specify the cost per liter of fuel. The code should start with:
```python
def calculate_travel_cost(distance: float, average_speed: float, fuel_efficiency: floa... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_travel_cost(distance: float, average_speed: float, fuel_efficiency: float, cost_per_liter: float) -> float:
"""
Calculates the travel cost based on distance covered, average speed, fuel efficiency, and cost per lit... |
code_exercise_27k | 20,973 | Write a Python function to check if a list of integers is in non-descending order. The function should return `True` if the list is in non-descending order, and `False` otherwise. The code should start with:
```python
from typing import List
def is_non_descending_order(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_non_descending_order(lst: List[int]) -> bool:
"""
Check if a list of integers is in non-descending order.
Args:
lst (List[int]): A list of integers to be checked.
Returns:
... |
code_exercise_27k | 20,979 | Write a Python function to find the second largest number in a list of integers. The function should handle lists with at least two elements and return the second largest number. If the list contains fewer than two elements, the function should raise a `ValueError`. If all elements in the list are the same, the functio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_second_largest(numbers: List[int]) -> int:
"""
Returns the second largest number in a list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The... |
code_exercise_27k | 20,983 | Write a Python function to find the median of a list of integers. The function should handle both odd and even lengths of the list and return the median value. If the input list is empty, the function should raise a `ValueError`.
The code should start with:
```python
from typing import List, Union
def find_median(lst... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def find_median(lst: List[int]) -> Union[int, float]:
"""
Calculate the median of a list of integers.
Parameters:
lst (List[int]): A list of integers.
Returns:
Union[int, float]:... |
code_exercise_27k | 20,990 | Write a Python function to calculate the average temperature of a given list of temperatures. 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_average_temperature(temper... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def calculate_average_temperature(temperatures: List[float]) -> Optional[float]:
"""
Calculates the average temperature of a given list of temperatures.
Args:
temperatures (List[fl... |
code_exercise_27k | 21,005 | Write a Python function to calculate the total travel distance given a list of distances. The function should handle both positive and negative distances, treating negative distances as travel in the opposite direction. The function should also handle an empty list by returning a total distance of 0.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_total_distance(distances: List[int]) -> float:
"""
Calculate the total travel distance given a list of distances.
Args:
distances (List[int]): A list of distances in kilometers... |
code_exercise_27k | 21,007 | Write a Python function to sort a list of integers using the cocktail shaker sort algorithm. The cocktail shaker sort, also known as bidirectional bubble sort, is a variation of the bubble sort algorithm that sorts the list in both directions alternately. The function should start with:
```python
from typing import Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cocktail_shaker_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the cocktail shaker sort algorithm.
Args:
arr (List[int]): List of integers to be... |
code_exercise_27k | 21,011 | Write a Python function to sort a list of strings using the merge sort 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(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 merge sort algorithm.
Args:
arr (List[str]): The list of strings to be sorted.
Returns:
... |
code_exercise_27k | 21,014 | Write a Python function to sort a list of tuples using the quicksort algorithm. Each tuple 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 value in lexicographic... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def quicksort_tuples(arr: List[Tuple[int, str]]) -> List[Tuple[int, str]]:
"""
Sorts a list of tuples using the quicksort algorithm.
Each tuple contains two elements: an integer and a str... |
code_exercise_27k | 21,023 | Write a Python function to sort a list of integers using the selection sort algorithm. The function should be able to handle lists with duplicate elements and negative numbers. The code should start with:
```python
from typing import List
def selection_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the selection sort algorithm.
Args:
arr (List[int]): An unsorted list of integers.
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.