source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
26,671
Write a Python function to sort a list of intervals based on the start time. Each interval is represented as a tuple where the first element is the start time and the second element is the end time. The function should handle any list of intervals, including empty lists and lists with overlapping intervals. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def sort_intervals(intervals: List[Tuple[int, int]]) -> List[Tuple[int, int]]: """ Sorts a list of intervals based on the start time. Args: intervals (List[Tuple[int, int]]): A list o...
code_exercise_27k
26,675
Write a Python function to compute the union of two intervals `[a, b]` and `[c, d]`. The function should return a tuple representing the union of the two intervals. If the intervals do not overlap, the function should return `(None, None)`. The function should also handle cases where the intervals are adjacent (i.e., o...
Here is the Python code to solve the given problem in the markdown block: ```python def compute_interval_union(a: int, b: int, c: int, d: int) -> tuple: """ Compute the union of two intervals [a, b] and [c, d]. Parameters: a (int): The lower bound of the first interval. b (int): The upper bound of ...
code_exercise_27k
26,676
Write a Python function to find all possible permutations of a given list of integers. The function should return a list of lists, where each inner list represents a unique permutation of the input list. The code should start with: ```python from typing import List def find_all_permutations(nums: List[int]) -> List[Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_all_permutations(nums: List[int]) -> List[List[int]]: """ Find all possible permutations of a given list of integers. Args: nums (List[int]): A list of integers. Returns: ...
code_exercise_27k
26,677
Write a Python function to remove overlapping intervals from a given list of intervals. Each interval is represented as a tuple of two integers, where the first integer is the start of the interval and the second integer is the end of the interval. The function should return a new list of intervals with overlapping int...
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]]: """ Merges overlapping intervals from a given list of intervals. Args: intervals: A list of intervals, ...
code_exercise_27k
26,679
Write a Python function to merge overlapping intervals in a given list of intervals. Each interval is represented as a tuple `(start, end)`, where `start` is the starting time and `end` is the ending time of the interval. The function should return a list of merged intervals. The code should start with: ```python from...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def merge_overlapping_intervals(intervals: List[Tuple[int, int]]) -> List[Tuple[int, int]]: """ Merges overlapping intervals in a given list of intervals. Args: intervals: A list of i...
code_exercise_27k
26,680
Write a Python function to find all possible combinations of a given list of integers, including both the combinations of each individual element and the combinations of all elements together. The function should return a list of lists, where each inner list represents a combination. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from itertools import combinations def find_all_combinations(nums: List[int]) -> List[List[int]]: """ Find all possible combinations of a given list of integers. Args: nums (List[int]): The ...
code_exercise_27k
26,688
Write a Python function to split an interval `[a, b]` into sub-intervals based on a given step size. The function should return a list of tuples representing the sub-intervals. Each sub-interval should be of the form `(start, end)`, where `start` is the beginning of the sub-interval and `end` is the end of the sub-inte...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def split_interval(a: float, b: float, step: float) -> List[Tuple[float, float]]: """ Splits an interval [a, b] into sub-intervals based on a given step size. Args: a (float or int): ...
code_exercise_27k
26,704
Write a Python function to split a list of intervals into sub-intervals based on a given maximum length. Each interval is represented as a tuple `(start, end)`, and the function should return a list of sub-intervals that adhere to the maximum length constraint. If an interval's length is less than or equal to the maxim...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def split_intervals(intervals: List[Tuple[int, int]], max_length: int) -> List[Tuple[int, int]]: """ Splits a list of intervals into sub-intervals based on a given maximum length. Args: ...
code_exercise_27k
26,713
Write a Python function to count the number of points that lie within each given interval range. The function should take two lists as input: one list of intervals (each represented as a tuple of two integers) and another list of points (each represented as an integer). The function should return a list of counts, wher...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def count_points_in_intervals(intervals: List[Tuple[int, int]], points: List[int]) -> List[int]: """ Counts the number of points that lie within each given interval range. Args: inter...
code_exercise_27k
26,719
Write a Python function to find the length of the longest proper prefix of a given string that is also a suffix. A proper prefix of a string is a prefix that is not equal to the string itself, and a proper suffix is a suffix that is not equal to the string itself. The function should return the length of the longest su...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_proper_prefix_suffix(string: str) -> int: """ Find the length of the longest proper prefix of a given string that is also a suffix. A proper prefix of a string is a prefix that is not equal to the string its...
code_exercise_27k
26,731
Write a Python function to find all unique combinations of a given sorted list of integers with duplicates. The function should return a list of lists, where each sublist represents a unique combination of the input list. The combinations should be generated in a way that avoids duplicates. 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_unique_combinations(nums: List[int]) -> List[List[int]]: """ Finds all unique combinations in a sorted list of integers. Args: nums (List[int]): A sorted list of integers with possi...
code_exercise_27k
26,739
Write a Python function to find the largest square of the same number in an NxM grid. The grid is represented as a 2D list of integers. The function should return the length of the side of the largest square of the same number found in the grid. The code should start with: ```python from typing import List def larges...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def largest_square_of_same_number(grid: List[List[int]]) -> int: """ Find the largest square of the same number in an NxM grid. Args: grid (List[List[int]]): A 2D list representing the g...
code_exercise_27k
26,746
Write a Python function to solve the 0-1 Knapsack problem using dynamic programming. The function should return the maximum value that can be achieved with the given weight capacity and the list of selected items that contribute to this maximum value. The function should have a time complexity of O(nW), where n is the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def knapsack_01(items: List[int], weights: List[int], values: List[int], max_weight: int) -> Tuple[List[int], int]: """ Solves the 0-1 Knapsack problem using dynamic programming. Args: ...
code_exercise_27k
26,759
Write a Python function to find the minimum subset sum difference for a given set of numbers using the Partition problem. The function should determine the smallest possible difference between the sums of two subsets of the given set. The code should start with: ```python from typing import List def min_subset_sum_dif...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def min_subset_sum_diff(nums: List[int]) -> int: """ Finds the minimum subset sum difference for a given set of numbers using the Partition problem. Args: nums (List[int]): List of positive ...
code_exercise_27k
26,775
Write a Python function to find the minimum number of coins required to make a given sum using the Coin Change problem. The function should return the minimum number of coins needed to reach the target sum, or `-1` if it is not possible to make the sum with the given coin denominations. 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 min_coins_for_sum(coins: List[int], amount: int) -> int: """ Finds the minimum number of coins required to make a given sum using the Coin Change problem. Args: coins (List[int]): A list...
code_exercise_27k
26,790
Write a Python function to generate all possible combinations of a given list of strings. Each combination should be represented as a tuple, and the function should return a list of all such tuples. The function should start with: ```python from typing import List, Tuple def generate_all_combinations(strings: List[str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple from itertools import combinations def generate_all_combinations(strings: List[str]) -> List[Tuple[str]]: """ Generate all possible combinations of a given list of strings. Args: stri...
code_exercise_27k
26,793
Write a Python function to find the minimum number of operations required to convert one string to another using dynamic programming. The operations allowed are: 1. **Insertion**: Insert a character into the source string. 2. **Deletion**: Delete a character from the source string. 3. **Substitution**: Replace a charac...
Here is the Python code to solve the given problem in the markdown block: ```python def min_edit_distance(source: str, target: str) -> int: """ Calculate the minimum number of operations required to convert the source string to the target string. Args: source (str): The source string to be converte...
code_exercise_27k
26,801
Write a Python function to find the length of the longest increasing subarray in a given array. The function should return the length of the longest contiguous subarray where each element is greater than the previous element. The code should start with: ```python from typing import List def longest_increasing_subarra...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_increasing_subarray_length(arr: List[int]) -> int: """ Finds the length of the longest increasing subarray in a given array. Args: arr (List[int]): The input array of integers. ...
code_exercise_27k
26,804
Write a Python function to solve a given Sudoku puzzle using backtracking. The Sudoku puzzle is represented as a 9x9 list where empty cells are denoted by 0. The function should return the solved puzzle if it is solvable, otherwise return `None`. The code should start with: ```python from typing import List, Optional ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def solve_sudoku(board: List[List[int]]) -> Optional[List[List[int]]]: """ Solve a Sudoku puzzle using backtracking. Args: board: A 9x9 list representing the Sudoku puzzle. Empty c...
code_exercise_27k
26,810
Write a Python function to generate all possible permutations of a given list of strings. Each permutation should be represented as a tuple of strings. The function should handle lists of varying lengths and ensure that all permutations are unique. The code should start with: ```python from typing import List, Tuple ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple from itertools import permutations def generate_permutations(strings: List[str]) -> List[Tuple[str]]: """ Generate all possible permutations of a list of strings. Args: strings (list)...
code_exercise_27k
26,811
Write a Python function to find the minimum number of insertions required to make a given string a palindrome using dynamic programming. The function should return the minimum number of insertions needed. The code should start with: ```python def min_insertions_to_palindrome(s: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def min_insertions_to_palindrome(s: str) -> int: """ Calculate the minimum number of insertions required to make the given string a palindrome. Args: s (str): The input string. Returns: int: The minimum...
code_exercise_27k
26,813
Write a Python function to find the length of the longest subarray with an equal number of 0s and 1s in a given binary array. The function should handle only non-empty lists containing only 0s and 1s. The code should start with: ```python from typing import List def longest_subarray_with_equal_zeros_and_ones(arr: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_subarray_with_equal_zeros_and_ones(arr: List[int]) -> int: """ Find the length of the longest subarray with an equal number of 0s and 1s. Args: arr: A list of integers containing...
code_exercise_27k
26,820
Write a Python function to find the length of the longest subarray with a given sum. The function should efficiently handle the input array and target sum, ensuring optimal performance. The code should start with: ```python from typing import List def longest_subarray_length(arr: List[int], target_sum: int) -> int: ``...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_subarray_length(arr: List[int], target_sum: int) -> int: """ Find the length of the longest subarray with a given sum. Args: arr (List[int]): A list of integers representing the ...
code_exercise_27k
26,830
Write a Python function to count the number of ways to arrange parentheses to form a valid expression for a given number of pairs of parentheses. A valid expression is defined as one where every opening parenthesis '(' has a corresponding closing parenthesis ')' and the parentheses are properly nested. The code should...
Here is the Python code to solve the given problem in the markdown block: ```python def count_valid_parentheses(n: int) -> int: """ Counts the number of ways to arrange parentheses to form a valid expression. Args: n (int): The number of pairs of parentheses. Returns: int: The number o...
code_exercise_27k
26,836
Write a Python function to find the length of the longest valid parenthesis substring in a given string. A valid parenthesis substring is defined as a substring that contains properly matched parentheses, i.e., every opening parenthesis '(' has a corresponding closing parenthesis ')'. The function should efficiently co...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_valid_parenthesis(s: str) -> int: """ Find the length of the longest valid parenthesis substring. Args: s (str): Input string consisting of parentheses. Returns: int: Length of the longest v...
code_exercise_27k
26,837
Write a Python function to calculate the minimum number of edits (insertions, deletions, or substitutions) required to convert one string into another using dynamic programming. The function should take two strings as input and return the minimum number of edits required. The code should start with: ```python def min_...
Here is the Python code to solve the given problem in the markdown block: ```python def min_edit_distance(str1: str, str2: str) -> int: """ Calculate the minimum number of edits required to convert str1 to str2 using dynamic programming. Args: str1 (str): The source string. str2 (str): The ...
code_exercise_27k
26,851
Write a Python function to generate all possible combinations of a list of strings with a given maximum length. Each combination should be a tuple of strings, and the function should return a list of all such combinations. The code should start with: ```python from typing import List, Tuple from itertools import combin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple from itertools import combinations def generate_combinations(strings: List[str], max_length: int) -> List[Tuple[str, ...]]: """ Generates all possible combinations of a list of strings with a give...
code_exercise_27k
26,883
Write a Python function to find all unique triplets in a given array of integers that add up to a specified target sum. The function should return a list of tuples, where each tuple represents a triplet of numbers that sum up to the target. The input array may contain duplicate numbers, but the output should not contai...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_triplets_with_sum(nums: List[int], target: int) -> List[Tuple[int, int, int]]: """ Finds all unique triplets in an array that add up to a specified target sum. Args: nums (Li...
code_exercise_27k
26,888
Write a Python function to determine if there exist two distinct numbers in a given unsorted array such that their sum equals a specified target sum. The function should return `True` if such a pair exists, and `False` otherwise. The code should start with: ```python from typing import List def find_two_numbers_with_s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_two_numbers_with_sum(arr: List[int], target_sum: int) -> bool: """ Determine if there exist two distinct numbers in the given array that sum up to the target sum. Args: arr (List[in...
code_exercise_27k
26,894
Write a Python function to find the length of the longest palindromic substring in a given string. The function should efficiently handle the problem using a dynamic programming approach. The code should start with: ```python def longest_palindromic_substring_length(s: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def longest_palindromic_substring_length(s: str) -> int: """ Find the length of the longest palindromic substring in a given string using dynamic programming. Args: s (str): The input string. Returns: i...
code_exercise_27k
26,899
Write a Python function to find the length of the longest substring with at most K distinct characters in a given string. The function should efficiently handle the input string and the integer K, ensuring that the substring length is calculated correctly. The code should start with: ```python def longest_substring_wi...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_substring_with_k_distinct_chars(s: str, k: int) -> int: """ Finds the length of the longest substring with at most K distinct characters. Args: s (str): The input string. k (int): The maximum num...
code_exercise_27k
26,903
Write a Python function to find three numbers in a given array of integers that have the largest sum. The function should return these three numbers as a tuple. The input array must contain at least three elements. If the array has fewer than three elements, the function should raise a `ValueError`. The code should st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_three_largest_sum_numbers(arr: List[int]) -> Tuple[int, int, int]: """ Find three numbers in the given array that have the largest sum. Args: arr (List[int]): A list of integ...
code_exercise_27k
26,909
Write a Python function to find three numbers in a given array of integers that have the smallest sum. The function should return a list of these three numbers. If the input array has fewer than three elements, the function should raise a `ValueError`. The code should start with: ```python from typing import List def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_three_smallest_sum(numbers: List[int]) -> List[int]: """ Find three numbers in an array with the smallest sum. Args: numbers (List[int]): The array of integers. Returns: ...
code_exercise_27k
26,913
Write a Python function to find the length of the longest substring with at most K distinct characters and at least M repeating characters. The function should efficiently handle the input string and return the length of the longest substring that meets the criteria. The code should start with: ```python def find_long...
Here is the Python code to solve the given problem in the markdown block: ```python def find_longest_substring(string: str, K: int, M: int) -> int: """ Finds the length of the longest substring with at most K distinct characters and at least M repeating characters. Args: - string (str): The input s...
code_exercise_27k
26,921
Write a Python function to find all quintuplets in a given array of integers that add up to a specified target sum. Each quintuplet should be a list of five integers from the array that sum to the target. The function should return a list of all such quintuplets. The code should start with: ```python from typing import...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_quintuplets(arr: List[int], target: int) -> List[List[int]]: """ Finds all quintuplets in the given array that add up to the target sum. Args: arr (List[int]): The input array of in...
code_exercise_27k
26,927
Write a Python function to find the length of the longest substring without repeating characters in a given string. The function should efficiently handle the input string and return the length of the longest substring without repeating characters. The code should start with: ```python def longest_substring_without_rep...
Here is the Python code to solve the given problem in the markdown block: ```python def longest_substring_without_repeating(s: str) -> int: """ Find the length of the longest substring without repeating characters in a given string. Parameters: s (str): The input string. Returns: int: The leng...
code_exercise_27k
26,934
Write a Python function to find the length of the longest subarray with the maximum sum among all subarrays that contain at most `k` negative elements. The function should take a list of integers `nums` and an integer `k` as input and return the length of the longest subarray that meets the criteria. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_subarray_with_max_sum(nums: List[int], k: int) -> int: """ Finds the length of the longest subarray with the maximum sum among all subarrays that contain at most k negative elements. ...
code_exercise_27k
26,936
Write a Python function to retrieve the value associated with a given key from a dictionary. If the key is not found in the dictionary, the function should return a default value provided by the user. If no default value is provided, the function should return `None`. The code should start with: ```python def get_valu...
Here is the Python code to solve the given problem in the markdown block: ```python def get_value_with_default(dictionary: dict, key, default=None): """ Retrieve the value associated with a given key from a dictionary. If the key is not found, return a default value provided by the user. If no default v...
code_exercise_27k
26,937
Write a Python function to check if a key exists in a given hashmap (dictionary) and return the corresponding value if the key exists. If the key does not exist, return a default value provided by the user. The function should handle cases where the hashmap or the key is `None`. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Optional def get_value_or_default(hashmap: Optional[dict], key: Any, default: Any) -> Any: """ Check if a key exists in the hashmap and return the corresponding value. If the key does not exist, retu...
code_exercise_27k
26,941
Write a Python function to iterate over the values in a hashmap (dictionary) and return a list containing all the values. The function should handle any dictionary with valid keys and values. The code should start with: ```python from typing import List, Dict def iterate_hashmap_values(hashmap: Dict) -> List: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def iterate_hashmap_values(hashmap: Dict) -> List: """ Iterate over the values in a hashmap (dictionary) and return them as a list. Args: hashmap (dict): The hashmap to iterate over. ...
code_exercise_27k
26,954
Write a Python function to copy the contents of one dictionary (hashmap) to another. The function should create a new dictionary and copy all key-value pairs from the source dictionary to the new dictionary. The function should handle cases where the source dictionary is empty or contains nested dictionaries. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def copy_hashmap(source_map: Dict) -> Dict: """ Copy the contents of one dictionary (hashmap) to another. Args: source_map (Dict): The dictionary to be copied from. Returns: Dic...
code_exercise_27k
26,958
Write a Python function to calculate the sum of all values in a given hashmap (dictionary). The function should handle dictionaries with various types of values, including integers, floats, and nested dictionaries. If a value is a nested dictionary, the function should recursively calculate the sum of its values. The ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union, Dict def calculate_sum_of_values(hashmap: Union[Dict, int, float]) -> Union[int, float]: """ Calculate the sum of all values in a hashmap, including nested dictionaries. Args: hashmap (Uni...
code_exercise_27k
26,959
Write a Python function to perform Principal Component Analysis (PCA) for dimensionality reduction on a given dataset. The function should standardize the input data, compute the covariance matrix, and then determine the eigenvalues and eigenvectors of the covariance matrix. The function should return the reduced featu...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def pca_dimensionality_reduction(X: np.ndarray, k: int) -> (np.ndarray, np.ndarray, np.ndarray): """ Perform Principal Component Analysis (PCA) for dimensionality reduction. Parameters: ----------- ...
code_exercise_27k
26,960
Write a Python function to check if all keys in a given list exist in a specified hashmap (dictionary). The function should return `True` if all keys are present in the hashmap, and `False` otherwise. The function should handle cases where the list or hashmap might be empty. 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 Dict, List def check_all_keys_exist(hashmap: Dict, key_list: List[str]) -> bool: """ Check if all keys in a given list exist in the specified hashmap. Args: hashmap (dict): The hashmap to check. ...
code_exercise_27k
26,961
Write a Python function to merge two dictionaries into one. If both dictionaries have the same key, the value from the second dictionary should overwrite the value from the first dictionary. The function should handle cases where the input is not a dictionary by raising a `TypeError`. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict: """ Merges two dictionaries into one. If both dictionaries have the same key, the value from the second dictionary will overwrite the value f...
code_exercise_27k
26,966
Write a Python function to perform a deep copy of a hashmap containing nested lists or dictionaries. The function should handle arbitrary levels of nesting and ensure that the copied hashmap is completely independent of the original. The code should start with: ```python from typing import Any, Dict def deep_copy_hash...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict import copy def deep_copy_hashmap(hashmap: Dict[Any, Any]) -> Dict[Any, Any]: """ Perform a deep copy of a hashmap containing nested lists or dictionaries. Args: hashmap (dict): The has...
code_exercise_27k
26,967
Write a Python function to check if two dictionaries are equal in terms of keys and values. The function should return `True` if both dictionaries have the same keys and corresponding values, and `False` otherwise. The code should start with: ```python def are_dictionaries_equal(dict1: dict, dict2: dict) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def are_dictionaries_equal(dict1: dict, dict2: dict) -> bool: """ Checks if two dictionaries are equal in terms of keys and values. Args: dict1 (dict): The first dictionary to compare. dict2 (dict): The seco...
code_exercise_27k
26,969
Write a Python function to find the keys with the top n highest values in a given dictionary. The function should return a list of these keys in descending order of their values. If n is greater than the number of keys in the dictionary, the function should return all keys sorted by their values in descending order. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def get_top_n_keys(dictionary: Dict[str, int], n: int) -> List[str]: """ Returns a list of the keys with the top n highest values in the dictionary. Args: dictionary (Dict[str, int]): ...
code_exercise_27k
26,970
Write a Python function to find the keys with the top n lowest values in a given hashmap (dictionary). The function should return a list of keys corresponding to the n lowest values in ascending order. If n is greater than the number of unique values in the hashmap, the function should return all keys sorted by their v...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def top_n_lowest_values(hashmap: Dict[str, int], n: int) -> List[str]: """ Find the keys with the top n lowest values in the hashmap. Args: hashmap (Dict[str, int]): A dictionary repre...
code_exercise_27k
26,972
Write a Python function to check if one dictionary (hashmap) is a subset of another dictionary. A dictionary `hashmap1` is considered a subset of `hashmap2` if every key-value pair in `hashmap1` exists in `hashmap2` with the same values. The function should return `True` if `hashmap1` is a subset of `hashmap2`, and `Fa...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def is_subset(hashmap1: Dict, hashmap2: Dict) -> bool: """ Checks if hashmap1 is a subset of hashmap2. Args: hashmap1 (Dict): The first dictionary to check. hashmap2 (Dict): The seco...
code_exercise_27k
26,980
Write a Python function to create a hashmap of unique words from a given string by splitting it. The function should count the occurrences of each word in the string and return a dictionary where the keys are the unique words and the values are their respective counts. 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 Dict def create_word_map(sentence: str) -> Dict[str, int]: """ Creates a hashmap of unique words from a given sentence. Args: sentence (str): The sentence from which to create the word map. ...
code_exercise_27k
26,981
Write a Python function to count the frequency of each word in a given string using a hashmap (dictionary). The function should handle multiple spaces and punctuation marks (excluding apostrophes) by treating them as delimiters for words. The function should also be case-insensitive, meaning "Hello" and "hello" should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict import re def count_word_frequencies(input_string: str) -> Dict[str, int]: """ Count the frequency of each word in a given string using a hashmap. This function handles multiple spaces, punctuation ...
code_exercise_27k
26,982
Write a Python function to remove duplicate values from a given list using a hashmap (dictionary). The function should return a new list that contains only the unique elements from the original list, maintaining the order of their first appearance. The code should start with: ```python from typing import List def rem...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_with_hashmap(lst: List[int]) -> List[int]: """ Removes duplicate values from a list using a hashmap (dictionary). Args: lst (List[int]): A list containing integer eleme...
code_exercise_27k
26,989
Write a Python function to determine if a given hashmap (represented as a dictionary) contains a cycle by following the key-value pairs. A cycle exists if following the key-value pairs leads to a previously visited key. The code should start with: ```python from typing import Dict def has_cycle_in_hashmap(hashmap: Di...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def has_cycle_in_hashmap(hashmap: Dict[int, int]) -> bool: """ Determine if a given hashmap contains a cycle by following the key-value pairs. Args: hashmap (Dict[int, int]): A dictionary re...
code_exercise_27k
26,998
Write a Python function to sort the keys in a hashmap by their associated values in descending order. If two keys have the same value, their original order should be preserved. The function should return a new dictionary with the keys sorted by their associated values. 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 Dict def sort_hashmap_by_value_desc(hashmap: Dict[str, int]) -> Dict[str, int]: """ Sorts the keys in a hashmap by their associated values in descending order. Parameters: hashmap (dict): The has...
code_exercise_27k
27,008
Write a Python function to find the reverse complement of a given DNA sequence using a hashmap. The DNA sequence consists only of the nucleotides 'A', 'T', 'C', and 'G'. The function should return the reverse complement of the input sequence. The code should start with: ```python def reverse_complement(sequence: str) -...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_complement(sequence: str) -> str: """ Find the reverse complement of a DNA sequence. Parameters: sequence (str): The input DNA sequence, containing only nucleotides 'A', 'T', 'C', and 'G'. Returns: ...
code_exercise_27k
27,014
Write a Python function to convert a hashmap (dictionary) to a list of tuples sorted by the keys in descending order. The function should handle the case where the input is not a dictionary by raising a `TypeError`. The code should start with: ```python from typing import List, Tuple, Dict def hashmap_to_sorted_list_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def hashmap_to_sorted_list_desc(hashmap: Dict) -> List[Tuple]: """ Converts a hashmap to a list of tuples sorted by the keys in descending order. Args: hashmap (dict): A hashmap...
code_exercise_27k
27,016
Write a Python function to convert a hashmap (a dictionary where keys are strings in dot notation) into a nested dictionary. The keys in the dot notation represent the hierarchy of the nested dictionary. The function should start with: ```python from typing import Dict def convert_hashmap_to_nested_dict(hashmap: Dict[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def convert_hashmap_to_nested_dict(hashmap: Dict[str, any]) -> Dict[str, any]: """ Convert a hashmap (a dictionary with keys in dot notation) into a nested dictionary. Args: hashmap (dict): ...
code_exercise_27k
27,018
Write a Python function to perform a batch update of values in a hashmap using an update dictionary. The function should update the values of items in the hashmap with values from the update dictionary. If a key in the update dictionary does not exist in the hashmap, it should be ignored. The function should return the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def batch_update_values(hashmap: Dict[str, str], update_dict: Dict[str, str]) -> Dict[str, str]: """ Update the values of items in a hashmap with values from an update dictionary. Args: hash...
code_exercise_27k
27,020
Write a Python function to calculate the average value of a given hashmap (dictionary). The function should handle the case where the hashmap is empty by raising a `ValueError`. The code should start with: ```python def calculate_average_value(hashmap: dict) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_average_value(hashmap: dict) -> float: """ Calculate the average value in a hashmap. Args: hashmap (dict): A dictionary containing key-value pairs. Returns: float: The average value in the...
code_exercise_27k
27,023
Write a Python function to find the longest common prefix among the keys in a given hashmap. The hashmap contains keys as strings. The function should return the longest common prefix as a string. If there is no common prefix, the function should return an empty string. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def longest_common_prefix_in_hashmap(hashmap: Dict[str, any]) -> str: """ Find the longest common prefix among the keys in a hashmap. Args: hashmap (Dict[str, any]): The hashmap containing k...
code_exercise_27k
27,026
Write a Python function to find the k largest values in a given hashmap (dictionary) and return them as a list of tuples in descending order of their values. Each tuple should contain the key and the corresponding value from the hashmap. The code should start with: ```python from typing import Dict, List, Tuple def f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def find_k_largest_values(hashmap: Dict[str, int], k: int) -> List[Tuple[str, int]]: """ Find the k largest values in a given hashmap and return them as a list of tuples in descending order ...
code_exercise_27k
27,037
Write a Python function to find all keys in a given dictionary that have the same values as any other key in the dictionary. The function should return a list of such keys. The code should start with: ```python from typing import Dict, List def find_keys_with_same_values(hashmap: Dict[str, int]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_keys_with_same_values(hashmap: Dict[str, int]) -> List[str]: """ Find all keys in the given dictionary that have the same values as any other key. Args: hashmap (Dict[str, int...
code_exercise_27k
27,042
Write a Python function to determine if a given hashmap is acyclic, meaning no key-value pairs form a cycle. The hashmap is represented as a dictionary where each key maps to a list of values. The function should return `True` if the hashmap is acyclic and `False` otherwise. 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 Dict, List, Any def is_hashmap_acyclic(hashmap: Dict[Any, List[Any]]) -> bool: """ Determine if a given hashmap is acyclic, meaning no key-value pairs form a cycle. Args: hashmap (Dict[Any, List[...
code_exercise_27k
27,055
Write a Python function to check if all values in a given hashmap (dictionary) are greater than a specified threshold. The function should return `True` if all values are greater than the threshold, and `False` otherwise. The code should start with: ```python def all_values_greater_than(hashmap: dict, threshold: int) -...
Here is the Python code to solve the given problem in the markdown block: ```python def all_values_greater_than(hashmap: dict, threshold: int) -> bool: """ Check if all values in a hashmap are greater than a given threshold. Args: hashmap (dict): The hashmap to check. threshold (int): The t...
code_exercise_27k
27,072
Write a Python function to check if all values in a given hashmap are powers of two. A power of two is a number that can be expressed as \(2^k\) where \(k\) is a non-negative integer. The function should return `True` if all values are powers of two, and `False` otherwise. The code should start with: ```python def is_...
Here is the Python code to solve the given problem in the markdown block: ```python def is_power_of_two_hashmap(hashmap: dict) -> bool: """ Check if all values in a hashmap are powers of two. Parameters: hashmap (dict): A hashmap where the keys are unique and the values are positive integers. Retu...
code_exercise_27k
27,077
Write a Python function to find the keys in a dictionary that have values in strictly ascending or strictly descending order. The function should return a dictionary containing only those keys and their corresponding values. The code should start with: ```python from typing import Dict def find_keys_with_asc_desc_valu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def find_keys_with_asc_desc_values(dictionary: Dict[str, int]) -> Dict[str, int]: """ Finds the keys in a dictionary that have values in strictly ascending or strictly descending order. Args: ...
code_exercise_27k
27,079
Write a Python function to determine if the keys of a given hashmap form a valid arithmetic progression. An arithmetic progression is a sequence of numbers such that the difference between the consecutive terms is constant. The function should return `True` if the keys form a valid arithmetic progression, and `False` o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def is_valid_arithmetic_progression(hashmap: Dict[int, any]) -> bool: """ Determines if the keys of the given hashmap form a valid arithmetic progression. Args: hashmap (Dict[int, any]): The...
code_exercise_27k
27,082
Write a Python function to find all keys in a given hashmap that are anagrams of any other key in the hashmap. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. The function should return a list of such keys. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_anagram_keys(hashmap: Dict[str, any]) -> List[str]: """ Finds the keys in the given hashmap that are anagrams of any other key. Args: hashmap (dict): The input hashmap where k...
code_exercise_27k
27,086
Write a Python function to check if a given hashmap (dictionary) contains any values that are prime numbers. The function should return `True` if there is at least one prime number among the values, and `False` otherwise. The function should use a helper function to determine if a number is prime. The code should star...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def is_prime(n: int) -> bool: """ Check if a number is prime. Parameters: n (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ if n <= ...
code_exercise_27k
27,090
Write a Python function to find the keys in a given hashmap (dictionary) that have palindromic values. A palindromic string reads the same forwards and backwards. The function should return a list of keys whose corresponding values are palindromes. The code should start with: ```python from typing import Dict, List d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def find_palindromic_keys(hashmap: Dict[str, str]) -> List[str]: """ Finds the keys in a hashmap that have palindromic values. Args: hashmap (Dict[str, str]): A dictionary where the va...
code_exercise_27k
27,096
Write a Python function to count the number of occurrences of a target value in a sorted array using a binary search approach. The function should efficiently find the first and last occurrence of the target value and then compute the count based on these indices. The code should start with: ```python from typing impo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_occurrences(sorted_arr: List[int], target: int) -> int: """ Count the number of occurrences of a target value in a sorted array using binary search. Args: sorted_arr (List[int]): A...
code_exercise_27k
27,098
Write a Python function to find a peak element in a given array. A peak element is defined as an element that is greater than its neighbors. If the array has multiple peaks, the function should return any one of them. The function should use a binary search approach to efficiently find the peak element. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_peak_element(nums: List[int]) -> Optional[int]: """ Finds a peak element in an array using a binary search approach. A peak element is an element that is greater than its neighbor...
code_exercise_27k
27,101
Write a Python function to check if an array is sorted in ascending order using a divide-and-conquer approach. The function should recursively divide the array into halves, check if each half is sorted, and then merge and check if the merged array is sorted. The code 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 is_array_sorted(arr: List[int]) -> bool: """ Check if an array is sorted in ascending order using divide and conquer. Args: arr (List[int]): The input array to be checked. Returns: ...
code_exercise_27k
27,109
Write a Python function to sort an 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 array using the MergeSort algorithm. Args: arr (List[int]): The array to be sorted. Returns: List[int]: The sorted arra...
code_exercise_27k
27,111
Write a Python function to find the majority element in a given array using a divide-and-conquer approach. The majority element is defined as an element that appears more than `n/2` times in an array of length `n`. If no such element exists, the function should return `None`. 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, Optional def find_majority_element(arr: List[int]) -> Optional[int]: """ Find the majority element in the given array using a divide-and-conquer approach. The majority element is defined as an elem...
code_exercise_27k
27,122
Write a Python function to reverse a string without using any built-in string reversal methods or any other built-in functions that directly reverse strings. The function should use a loop to manually reverse the string. The code should start with: ```python def reverse_string(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses a given string without using any built-in string reversal methods. Args: string (str): The input string to be reversed. Returns: str: The reversed st...
code_exercise_27k
27,123
Write a Python function to find the second largest element in a given list. The function should handle edge cases such as an empty list or a list with fewer than two elements by returning `None`. The function should also efficiently handle lists with duplicate elements. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_second_largest(lst: List[int]) -> Optional[int]: """ Find the second largest element in a given list of integers. Args: lst (List[int]): The list of integers to search for...
code_exercise_27k
27,125
Write a Python function to calculate the factorial of a given non-negative integer using recursion. The function should handle the base case where the input is 0 or 1, and it should raise a `ValueError` if the input is negative. The code should start with: ```python def factorial(n: int) -> int: ```
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 using recursion. Args: n (int): The non-negative integer for which factorial needs to be calculated. Returns: in...
code_exercise_27k
27,127
Write a Python function to implement a binary search algorithm to find the index of a target element in a sorted list. The function should return the index of the target element if it exists in the list, otherwise it should return -1. The code should start with: ```python from typing import List, Any def binary_search...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def binary_search(arr: List[Any], target: Any) -> int: """ Perform binary search on a sorted list 'arr' to find the index of the 'target' element. Args: arr (List[Any]): A sorted list o...
code_exercise_27k
27,130
Write a Python function to calculate the sum of two numbers, ensuring that the function handles both integer and floating-point inputs. The function should be able to handle edge cases such as negative numbers, zero, and very large numbers. The code should start with: ```python def calculate_sum(num1: int | float, num2...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_sum(num1: int | float, num2: int | float) -> int | float: """ Calculates the sum of two numbers. Args: num1 (int | float): The first number. num2 (int | float): The second number. Returns:...
code_exercise_27k
27,133
Write a Python function to calculate the factorial of a given non-negative integer using an iterative approach. The function should handle the case where the input number is negative by raising a `ValueError`. The code should start with: ```python def factorial(n: int) -> int: ```
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 non-negative integer. Args: n (int): The non-negative integer to calculate the factorial of. Returns: int: The factorial of the input numbe...
code_exercise_27k
27,137
Write a Python function to find the maximum and minimum elements in a list of numbers. The function should return a tuple containing the minimum and maximum values. If the input list is empty, the function should raise a `ValueError`. The code should start with: ```python from typing import List, Tuple def find_max_m...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_min(numbers: List[int]) -> Tuple[int, int]: """ Find the maximum and minimum elements in a list of numbers. Args: numbers (List[int]): A list of integers. Returns: ...
code_exercise_27k
27,138
Write a Python function to sort a list of numbers in ascending order using the bubble sort algorithm. The function should not modify the original list but return a new sorted list. The code should start with: ```python from typing import List def bubble_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 bubble_sort(numbers: List[int]) -> List[int]: """ Sorts a list of numbers in ascending order using the bubble sort algorithm. Args: numbers (List[int]): The list of numbers to be sorted....
code_exercise_27k
27,139
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]: """ Count the number of occurrences of each unique element in a given list. Args: lst (List): The list in which to count occ...
code_exercise_27k
27,140
Write a Python function to check whether a given number is prime or not. The function should return `True` if the number is prime and `False` otherwise. The function should handle edge cases such as numbers less than or equal to 1, which are not prime. The code should start with: ```python def is_prime(n: int) -> bool:...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Check whether a given number is prime or not. Args: n (int): The number to be checked. Returns: bool: True if the number is prime, False otherwise. """ # Number...
code_exercise_27k
27,142
Write a Python function to merge two sorted lists into a single 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(lst1: List[int], lst2: 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(lst1: List[int], lst2: List[int]) -> List[int]: """ Merge two sorted lists into a single sorted list. Args: lst1 (List[int]): The first sorted list. lst2 (List...
code_exercise_27k
27,143
Write a Python function to find the Greatest Common Divisor (GCD) of two given numbers using the Euclidean algorithm. The function should handle both positive and negative integers. The code should start with: ```python def gcd(a: int, b: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def gcd(a: int, b: int) -> int: """ Find the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm. Parameters: a (int): First number. b (int): Second number. Returns: int: GCD of the t...
code_exercise_27k
27,144
Write a Python function to generate all possible combinations of a given list. Each combination should be represented as a tuple, and the function should return a list of all such tuples. The function should handle lists of varying lengths and ensure that all possible combinations are generated, including the empty com...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import itertools def generate_all_combinations(lst: List[int]) -> List[Tuple[int]]: """ Generate all possible combinations of a given list. Args: lst (List[int]): The input list from ...
code_exercise_27k
27,146
Write a Python function to swap the values of two variables without using a temporary variable. The function should take two arguments and return a tuple containing the swapped values. The code should start with: ```python def swap_variables(a: int, b: int) -> tuple: ```
Here is the Python code to solve the given problem in the markdown block: ```python def swap_variables(a: int, b: int) -> tuple: """ Swaps the values of two variables without using a temporary variable. Args: a (int): The first variable. b (int): The second variable. Returns: t...
code_exercise_27k
27,147
Write a Python function to determine whether a given string is an anagram of another string. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. The function should handle strings with spaces and should be case-insensitiv...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(word1: str, word2: str) -> bool: """ Determine whether two given strings are anagrams of each other. Parameters: word1 (str): The first word or phrase. word2 (str): The second word or phrase. Ret...
code_exercise_27k
27,148
Write a Python function to perform a binary search on a sorted list and return the index of the target element if it exists, otherwise return -1. The function should handle edge cases such as an empty list, a list with a single element, and a list where the target is not present. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def binary_search(arr: List[int], target: int) -> int: """ Performs binary search to find the position of a target element in a sorted list. Args: arr (List[int]): A sorted list of integers....
code_exercise_27k
27,150
Implement a Python function to sort a list of numbers using the Quick Sort 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 quick_sort(nums: List[float]) -> List[float]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def quick_sort(nums: List[float]) -> List[float]: """ Sorts a list of numbers in ascending order using the Quick Sort algorithm. Args: nums (List[float]): List of numbers to be sorted. ...
code_exercise_27k
27,151
Write a Python function to check whether a given string is a valid palindrome or not, considering only alphanumeric characters and ignoring cases. The function should return `True` if the string is a palindrome, and `False` otherwise. The code should start with: ```python def is_valid_palindrome(s: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_palindrome(s: str) -> bool: """ Check whether a given string is a valid palindrome or not, considering only alphanumeric characters and ignoring cases. Args: s (str): The input string to be checked. ...
code_exercise_27k
27,154
Write a Python function to perform matrix addition. The function should take two matrices as input and return the result of their addition. The matrices are represented as lists of lists, where each inner list represents a row of the matrix. The function should ensure that the matrices have the same dimensions before p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def add_matrices(matrix1: List[List[int]], matrix2: List[List[int]]) -> List[List[int]]: """ Add two matrices together. Args: matrix1 (List[List[int]]): The first matrix. matrix2 (Li...
code_exercise_27k
27,155
Write a Python function to find the shortest path from a given start node to a given end node in a weighted graph using Dijkstra's algorithm. The function should return the shortest path as a list of nodes and the total distance of the path. The code should start with: ```python from typing import Dict, List, Tuple fr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple from heapq import heappop, heappush def find_shortest_path(graph: Dict[str, Dict[str, int]], start: str, end: str) -> Tuple[List[str], int]: """ Find the shortest path from the start node to...
code_exercise_27k
27,158
Write a Python function to calculate the sum of all elements in a given list, including handling lists with mixed data types (integers, floats, and strings representing numbers). The function should convert string representations of numbers to their numeric equivalents before summing them up. The code should start with...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def sum_of_mixed_list(lst: List[Union[int, float, str]]) -> Union[int, float]: """ Calculates the sum of all elements in a given list, including elements that are strings representing numbers. ...
code_exercise_27k
27,159
Write a Python function to find the maximum and minimum elements in a given list without using Python's built-in `max()` and `min()` functions. The function should iterate through the list to determine the maximum and minimum values. The code should start with: ```python from typing import List, Tuple def find_max_mi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_min_elements(lst: List[int]) -> Tuple[int, int]: """ Find the maximum and minimum elements in a given list without using built-in max() and min() functions. Args: lst (Li...
code_exercise_27k
27,160
Write a Python function to calculate the sum of all even numbers in a given list. The function should handle lists of integers and return the sum of all even integers in the list. The code should start with: ```python from typing import List def sum_of_even_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 sum_of_even_numbers(numbers: List[int]) -> int: """ Calculates the sum of all even numbers in a given list. Args: numbers (List[int]): A list of integers. Returns: int: The ...
code_exercise_27k
27,161
Write a Python function to sort a list in ascending order using the Bubble Sort algorithm. The function should handle lists of integers and return the sorted list. The code should start with: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: """ Sorts the given list in ascending order using the Bubble Sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Retu...