source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 6,348 | Write a Python function to merge two sorted lists into a single sorted list without using extra space. The function should modify the first list in place to include all elements from both lists in sorted order. The second list should remain unchanged.
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 merge_sorted_lists_in_place(list1: List[int], list2: List[int]) -> None:
"""
Merges two sorted lists into a single sorted list without using extra space.
The first list is modified in place to in... |
code_exercise_27k | 6,353 | Write a Python function to calculate the difference between the maximum and minimum elements in a given list. The function should handle both integer and floating-point numbers. If the input list is empty, the function should raise a `ValueError` with a specific message.
The code should start with:
```python
from typi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def get_difference(lst: List[Union[int, float]]) -> Union[int, float]:
"""
Calculates the difference between the maximum and minimum elements in a given list.
Args:
lst (List[Union[in... |
code_exercise_27k | 6,354 | Write a Python function to implement a binary search algorithm that searches for a target value in a sorted array. The function should return the index of the target value if it is found, otherwise it should return -1. The function should handle edge cases such as an empty array or a target value that is not present in... | 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:
"""
Search for a target value in a sorted array using the binary search algorithm.
Args:
arr (List[int]): A sorted list of integers.
... |
code_exercise_27k | 6,355 | Write a Python function to merge two dictionaries, and if the keys are the same, find the maximum value between the two. The function should return the merged dictionary with the maximum values for common keys. The code should start with:
```python
from typing import Dict
def merge_dicts_with_max_values(dict1: Dict[st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def merge_dicts_with_max_values(dict1: Dict[str, int], dict2: Dict[str, int]) -> Dict[str, int]:
"""
Merge two dictionaries and find the maximum value for any common keys.
Args:
dict1 (Dict[... |
code_exercise_27k | 6,357 | Write a Python function to merge two dictionaries, and if the keys are the same, multiply the corresponding values and store them in a new dictionary. The function should handle cases where the dictionaries have different keys and should not modify the original dictionaries.
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
def merge_and_multiply_dicts(dict1: Dict[str, int], dict2: Dict[str, int]) -> Dict[str, int]:
"""
Merges two dictionaries and multiplies the corresponding values if the keys are the same.
Args:
... |
code_exercise_27k | 6,359 | Write a Python function to implement the bubble sort algorithm. The function should take a list of integers as input and return the sorted list. 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
d... | 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 | 6,360 | Write a Python function to check if all elements in a given list are prime numbers. The function should return `True` if all elements are prime, and `False` otherwise. The code should start with:
```python
from typing import List
def are_all_elements_prime(numbers: List[int]) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if the given number is prime.
Parameters:
num (int): The number to be checked.
Returns:
bool: True if the number is prime, False otherwise.
... |
code_exercise_27k | 6,361 | Write a Python function to implement the Selection Sort algorithm. The function should sort a list of integers in ascending order. 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 using the Selection Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
... |
code_exercise_27k | 6,364 | Write a Python function to merge two dictionaries into a new dictionary. If both dictionaries have the same key, the value from the second dictionary should overwrite the value from the first dictionary. The function should return the merged dictionary.
The code should start with:
```python
from typing import Dict
de... | 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 a new dictionary. If both dictionaries have the same key,
the value from the second dictionary will overwrit... |
code_exercise_27k | 6,366 | Write a Python function to check if two lists have the same set of elements, considering the frequency of each element. The function should return `True` if the two lists have the same elements with the same frequencies, and `False` otherwise. The code should start with:
```python
from typing import List
def check_sam... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
from collections import Counter
def check_same_elements_with_frequency(list1: List[int], list2: List[int]) -> bool:
"""
Check if two lists have the same set of elements with the same frequencies.
Ar... |
code_exercise_27k | 6,368 | Write a Python function to implement the insertion sort algorithm. The function should take a list of integers as input and return the sorted list. The insertion sort algorithm works by iterating through the list and inserting each element into its correct position in the sorted part of the list.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the insertion sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
... |
code_exercise_27k | 6,369 | Write a Python function to replace all negative elements in a given list with their absolute values. The function should return a new list with the modifications. The code should start with:
```python
from typing import List
def replace_negatives_with_absolute_values(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 replace_negatives_with_absolute_values(lst: List[int]) -> List[int]:
"""
Replaces all negative elements in a given list with their absolute values.
Args:
lst (List[int]): The input list ... |
code_exercise_27k | 6,371 | Write a Python function to implement the merge sort algorithm. The function should take a list of integers as input and return the sorted list. The merge sort algorithm should be implemented using recursion and should divide the list into two halves, sort each half, and then merge the sorted halves back together.
The ... | 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 merge sort algorithm.
Args:
arr (List[int]): The unsorted array.
Returns:
List[int]: The sorted array.
... |
code_exercise_27k | 6,373 | Write a Python function to replace all even elements in a given list with the cube of their values. The function should return the modified list. The code should start with:
```python
from typing import List
def replace_even_values_with_cube(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 replace_even_values_with_cube(lst: List[int]) -> List[int]:
"""
Replaces all even elements in a list with the cube of their values.
Args:
lst (List[int]): The input list of integers.
... |
code_exercise_27k | 6,374 | Write a Python function to add a new key-value pair to a given dictionary if the key does not already exist in the dictionary. If the key exists, the function should return the original dictionary without any modifications. The function should handle dictionaries with any type of keys and values.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def add_key_value_pair(dictionary: Dict[Any, Any], key: Any, value: Any) -> Dict[Any, Any]:
"""
Adds a new key-value pair to the given dictionary if the key does not already exist.
If the key ex... |
code_exercise_27k | 6,375 | Write a Python function to split a given list into two separate lists, one containing the elements at even indices and the other containing the elements at odd indices. The function should return a tuple of these two lists. The code should start with:
```python
from typing import List, Tuple
def split_list_by_indices(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def split_list_by_indices(lst: List[int]) -> Tuple[List[int], List[int]]:
"""
Split a given list into two lists, with the even-indexed elements in one list
and the odd-indexed elements in ano... |
code_exercise_27k | 6,377 | Write a Python function to remove a specified key-value pair from a given dictionary. If the key does not exist in the dictionary, the function should return the original dictionary without any modifications. The function should not raise any exceptions.
The code should start with:
```python
from typing import Dict, A... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def remove_key_if_exists(dictionary: Dict[Any, Any], key: Any) -> Dict[Any, Any]:
"""
Remove a key-value pair from a dictionary if the key exists.
Args:
dictionary (dict): The dictionar... |
code_exercise_27k | 6,381 | Write a Python function to combine two given lists by alternating their elements. If the lengths of the input lists are not equal, the function should raise a `ValueError`. The function should start with:
```python
from typing import List
def combine_lists(list1: List, list2: List) -> List:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def combine_lists(list1: List, list2: List) -> List:
"""
Combine two lists by alternating their elements.
Args:
list1 (List): The first list.
list2 (List): The second list.
Retu... |
code_exercise_27k | 6,382 | Write a Python function to implement a linear search algorithm that finds the index of an element in a given list. If the element is not found, the function should return -1. The function should be able to handle lists of any data type and should be efficient for small to moderately sized lists.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def linear_search(arr: List[Any], element: Any) -> int:
"""
Perform a linear search to find the index of an element in the given list.
Args:
arr (List[Any]): The list in which to perfor... |
code_exercise_27k | 6,386 | Write a Python function to update the value of a specified key in a dictionary to a given new value. If the key does not exist in the dictionary, the function should add the key with the new value. The function should return the updated dictionary.
The code should start with:
```python
from typing import Dict, Any
de... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def update_or_add_key(dictionary: Dict[str, Any], key: str, new_value: Any) -> Dict[str, Any]:
"""
Update the value of a specified key in the dictionary to a given new value.
If the key does not... |
code_exercise_27k | 6,387 | Write a Python function to find the largest k elements from a given list efficiently. The function should handle cases where k is greater than the length of the list and where k is less than or equal to zero. The function should return the k largest elements in descending order.
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
import heapq
def find_largest_elements(lst: List[int], k: int) -> List[int]:
"""
Finds the largest k elements from a given list efficiently.
Args:
lst (List[int]): The input list from which ... |
code_exercise_27k | 6,388 | Write a Python function to get all the values in a dictionary and store them in a list called 'values'. The function should take a dictionary as input and return a list of all the values in the dictionary. The code should start with:
```python
from typing import Dict, List
def get_values(dictionary: Dict) -> List:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def get_values(dictionary: Dict) -> List:
"""
Get all the values from a dictionary and store them in a list.
Args:
dictionary (dict): The dictionary from which to retrieve the values.
... |
code_exercise_27k | 6,389 | Write a Python function to compute the cumulative sum of elements in a given list. The cumulative sum at each position in the list is the sum of all elements up to and including that position. The function should return a new list containing these cumulative sums.
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 cumulative_sum(lst: List[int]) -> List[int]:
"""
Computes the cumulative sum of elements in the given list.
Args:
lst (List[int]): The input list of integers.
Returns:
List[... |
code_exercise_27k | 6,390 | Write a Python function to merge multiple dictionaries into a single dictionary. The function should take a variable number of dictionaries as input and return a new dictionary that contains all the key-value pairs from the input dictionaries. If there are duplicate keys, the value from the last dictionary in the input... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def merge_dictionaries(*dicts: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Merges multiple dictionaries into a single dictionary.
Args:
*dicts: A variable number of dictionaries to be merged... |
code_exercise_27k | 6,391 | Write a Python function to find the smallest k elements from a given list of integers using a max-heap data structure. The function should efficiently handle cases where `k` is greater than the length of the list and should return an empty list if `k` is less than or equal to zero. The function should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import heapq
def find_smallest_k_elements(lst: List[int], k: int) -> List[int]:
"""
Returns the smallest 'k' elements from the given list using a max-heap.
Args:
lst (List[int]): The list of... |
code_exercise_27k | 6,392 | Write a Python function to clear all items from a given dictionary and return the cleared dictionary. The function should handle dictionaries with various types of keys and values. The code should start with:
```python
def clear_dictionary(dictionary: dict) -> dict:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def clear_dictionary(dictionary: dict) -> dict:
"""
Clears all items from the given dictionary and returns the cleared dictionary.
Args:
dictionary (dict): The dictionary to be cleared.
Returns:
dict: T... |
code_exercise_27k | 6,395 | Write a Python function to find the number of distinct elements in a sorted list using constant extra space. The function should not use any additional data structures like sets or dictionaries to store elements. The function should only iterate through the list once and use a single variable to keep track of the count... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_distinct_elements(lst: List) -> int:
"""
Count the number of distinct elements in a sorted list using constant extra space.
Args:
lst (List): A sorted list of elements.
Return... |
code_exercise_27k | 6,396 | Write a Python function to create a deep copy of a given dictionary. A deep copy means that any nested objects within the dictionary are also copied, so that changes to the original dictionary do not affect the copied dictionary. The function should start with:
```python
from typing import Any, Dict
import copy
def de... | 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_dict(original_dict: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Create a deep copy of the given dictionary.
Args:
original_dict (Dict[Any, Any]): The dictionary to ... |
code_exercise_27k | 6,398 | Write a Python function to create a new dictionary with the same keys as the input dictionary, but with all values set to a specified default value. The function should handle any type of dictionary, including nested dictionaries. The default value should be applied to all leaf nodes in the dictionary.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Any, Dict
def create_new_dict_with_default(my_dict: Dict[Any, Any], default_value: Any) -> Dict[Any, Any]:
"""
Create a new dictionary with the same keys as 'my_dict', but all values set to 'default_value'.
... |
code_exercise_27k | 6,399 | Write a Python function to find the longest palindrome substring in a given string. A palindrome is a string that reads the same backward as forward. The function should return the longest palindrome substring found in the input string. If there are multiple palindromes of the same maximum length, return any one of the... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_palindrome_substring(s: str) -> str:
"""
Returns the longest palindrome substring in a given string.
Args:
s (str): The string to find the longest palindrome substring from.
Returns:
str: Th... |
code_exercise_27k | 6,402 | Write a Python function to find the number of distinct elements in a list using linear time complexity. The function should efficiently handle lists with duplicate elements and return the count of unique elements. The code should start with:
```python
from typing import List
def count_distinct_elements(arr: List[int])... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_distinct_elements(arr: List[int]) -> int:
"""
Returns the number of distinct elements in a list.
Args:
arr (List[int]): A list of integers.
Returns:
int: The number of... |
code_exercise_27k | 6,403 | Write a Python function to find the longest substring in a given string that contains no repeating characters. The function should return the longest such substring. The code should start with:
```python
def longest_substring_no_repeats(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_substring_no_repeats(s: str) -> str:
"""
Returns the longest substring in a given string with no repeating characters.
Args:
s (str): The input string.
Returns:
str: The longest substring wi... |
code_exercise_27k | 6,406 | Write a Python function to count the frequency of each character in a given string and return the result as a dictionary. The function should handle any string input, including empty strings and strings with special characters. The code should start with:
```python
from typing import Dict
def count_character_frequency... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_character_frequency(string: str) -> Dict[str, int]:
"""
Count the frequency of each character in a given string.
Args:
string (str): The input string to analyze.
Returns:
... |
code_exercise_27k | 6,410 | Write a Python function to check if a given string is a valid palindrome, considering only alphanumeric characters and ignoring case. The function should use a two-pointers approach to compare characters from both ends of the string.
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 if a given string is a valid palindrome, considering only alphanumeric characters and ignoring case.
Args:
s (str): The string to be checked.
Returns:
... |
code_exercise_27k | 6,411 | Write a Python function to check if all values in a given dictionary are of the same data type. The function should return `True` if all values are of the same type, and `False` otherwise. The code should start with:
```python
from typing import Dict, Any
def check_dict_values_same_type(data: Dict[Any, Any]) -> bool:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def check_dict_values_same_type(data: Dict[Any, Any]) -> bool:
"""
Check if all values in a dictionary are of the same data type.
Args:
data (Dict[Any, Any]): The dictionary to be check... |
code_exercise_27k | 6,420 | Write a Python function to reverse the characters in a given string. The function should handle both non-empty and empty strings. 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:
"""
Reverse the characters in a given string.
Args:
string (str): The input string to be reversed.
Returns:
str: The reversed string.
Example:
>>> reve... |
code_exercise_27k | 6,424 | Write a Python function to remove all vowels from a given string. The function should handle both uppercase and lowercase vowels. The code should start with:
```python
def remove_vowels(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_vowels(string: str) -> str:
"""
Removes all vowels from a given string.
Args:
string (str): The input string from which vowels will be removed.
Returns:
str: The input string with all vowels ... |
code_exercise_27k | 6,425 | Write a Python function to check if a given dictionary is empty. The function should return `True` if the dictionary is empty and `False` otherwise. The code should start with:
```python
def is_dict_empty(dictionary: dict) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_dict_empty(dictionary: dict) -> bool:
"""
Check if a given dictionary is empty.
Args:
dictionary (dict): The dictionary to check.
Returns:
bool: True if the dictionary is empty, False otherwise.
... |
code_exercise_27k | 6,426 | Write a Python function to remove all consonants from a given string. The function should return the modified string containing only vowels and any non-alphabetic characters. The code should start with:
```python
def remove_consonants(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_consonants(string: str) -> str:
"""
Removes all consonants from the given string.
Parameters:
string (str): The input string from which consonants will be removed.
Returns:
str: The given string with... |
code_exercise_27k | 6,436 | Write a Python function to find the length of the longest continuous substring with the same character in a given string. The function should efficiently handle strings of varying lengths and return the correct length of the longest continuous substring.
The code should start with:
```python
def longest_continuous_sub... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_continuous_substring(string: str) -> int:
"""
Returns the length of the longest continuous substring with the same character in a given string.
Args:
string (str): The input string to analyze.
Retur... |
code_exercise_27k | 6,440 | Write a Python function to determine if a given string is a pangram. A pangram is a string that contains every letter of the alphabet at least once. The function should be case-insensitive and should ignore non-alphabetic characters.
The code should start with:
```python
def is_pangram(input_string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_pangram(input_string: str) -> bool:
"""
Determines if a given string is a pangram.
A pangram is a string that contains every letter of the alphabet at least once.
This function is case-insensitive and ignores non... |
code_exercise_27k | 6,442 | Write a Python function to calculate the sum of all values in a given dictionary. The dictionary contains string keys and integer values. The function should handle dictionaries with varying numbers of key-value pairs and ensure that only integer values are summed.
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 Dict
def sum_of_values(dictionary: Dict[str, int]) -> int:
"""
Calculate the sum of all integer values in a given dictionary.
Args:
dictionary (Dict[str, int]): A dictionary with string keys and ... |
code_exercise_27k | 6,445 | Write a Python function to check if any key in a given dictionary starts with a specified prefix. The function should be case-insensitive.
The code should start with:
```python
from typing import Dict
def check_keys_start_with_prefix(dictionary: Dict[str, any], prefix: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def check_keys_start_with_prefix(dictionary: Dict[str, any], prefix: str) -> bool:
"""
Check if any key in the given dictionary starts with the specified prefix.
Args:
dictionary (Dict[str, ... |
code_exercise_27k | 6,447 | Write a Python function to return the first k smallest elements from a given list of integers, where k is a user-entered value. The function should handle cases where k is greater than the length of the list or less than or equal to zero by returning an empty list. The function should also ensure that the returned list... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_k_smallest_elements(lst: List[int], k: int) -> List[int]:
"""
Returns the first k smallest elements from a given list of integers.
Args:
lst (List[int]): The list of integers from wh... |
code_exercise_27k | 6,451 | Write a Python function to find the element(s) with the maximum frequency count in a given list of integers. If there are multiple elements with the same maximum frequency, return all of them. The function should handle an empty list by returning an empty list.
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_max_frequency_elements(numbers: List[int]) -> List[int]:
"""
Find the element(s) with the maximum frequency count in a given list of integers.
Args:
numbers (List[int]): A list of i... |
code_exercise_27k | 6,455 | Write a Python function to remove duplicate characters from a given string while preserving the order of their first occurrence. The function should handle any string input, including empty strings and strings with all unique characters. The code should start with:
```python
def remove_duplicates_preserve_order(string:... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_duplicates_preserve_order(string: str) -> str:
"""
Removes duplicate characters from the given string while preserving the order of their first occurrence.
Args:
string (str): The input string from which ... |
code_exercise_27k | 6,457 | Write a Python function to find the symmetric difference between two dictionaries. The symmetric difference should include keys that are present in one dictionary but not the other, and keys that are present in both dictionaries but have different values. The function should return a new dictionary containing these dif... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def symmetric_difference(dict1: Dict, dict2: Dict) -> Dict:
"""
Find the symmetric difference between two dictionaries.
The symmetric difference includes keys that are present in one dictionary but ... |
code_exercise_27k | 6,459 | Write a Python function to remove punctuation from a given string. The function should handle various types of punctuation marks and return the cleaned string. The code should start with:
```python
import string
def remove_punctuation(input_string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import string
def remove_punctuation(input_string: str) -> str:
"""
Removes all punctuation marks from the given input string.
Args:
input_string (str): The input string from which punctuation marks are to be remov... |
code_exercise_27k | 6,461 | Write a Python function to find the largest prime number smaller than a given integer `n`. The function should return `None` if no such prime number exists. The code should start with:
```python
def largest_prime_smaller_than(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def largest_prime_smaller_than(n: int) -> int:
"""
Returns the largest prime number smaller than the given integer 'n'.
Args:
n (int): The maximum limit for finding the largest prime number smaller than 'n'.
... |
code_exercise_27k | 6,464 | Write a Python function to find all keys in a given dictionary that have values equal to a specified target value. The function should return a list of these keys. The code should start with:
```python
from typing import Dict, List, Any
def find_keys_with_target_value(dictionary: Dict[Any, Any], target_value: Any) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Any
def find_keys_with_target_value(dictionary: Dict[Any, Any], target_value: Any) -> List[Any]:
"""
Find all keys in the dictionary that have values equal to the specified target value.
Args... |
code_exercise_27k | 6,467 | Write a Python function to calculate the sum of all elements in a given matrix. The matrix is represented as a list of lists, where each inner list represents a row of the matrix. The function should handle matrices of any size, including empty matrices. The code should start with:
```python
from typing import List
de... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_matrix(matrix: List[List[int]]) -> int:
"""
Calculate the sum of all elements in the given matrix.
Args:
matrix (List[List[int]]): A 2-dimensional matrix represented as a list of ... |
code_exercise_27k | 6,471 | Write a Python function to find the transpose of a given matrix. The matrix is represented as a 2D list. The transpose of a matrix is obtained by swapping its rows with its columns. The function should handle matrices of any size, including non-square matrices.
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 transpose_matrix(matrix: List[List[int]]) -> List[List[int]]:
"""
Calculates the transpose of a given matrix.
Parameters:
matrix (List[List[int]]): A 2D list representing the matrix.
Re... |
code_exercise_27k | 6,474 | Write a Python function to merge two dictionaries, giving priority to the values from the first dictionary if keys clash. The function should return a new dictionary that combines the key-value pairs from both dictionaries, with the values from the first dictionary taking precedence over the second dictionary in case o... | 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, giving priority to the values from the first dictionary if keys clash.
Args:
dict1 (Dict): The first di... |
code_exercise_27k | 6,478 | Write a Python function to count the number of occurrences of each unique value in a given dictionary. The function should return a new dictionary where the keys are the unique values from the input dictionary, and the values are the counts of occurrences of each unique value.
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 Dict
def count_unique_values(dictionary: Dict[str, int]) -> Dict[int, int]:
"""
Count the number of occurrences of each unique value in the given dictionary.
Args:
dictionary (Dict[str, int]): Th... |
code_exercise_27k | 6,480 | Write a Python function to check if a given matrix is symmetric. A matrix is symmetric if it is equal to its transpose. In other words, the element at row `i` and column `j` should be equal to the element at row `j` and column `i` for all `i`, `j`. The function should return `True` if the matrix is symmetric and `False... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_symmetric(matrix: List[List[int]]) -> bool:
"""
Check if a given matrix is symmetric.
A matrix is symmetric if it is equal to its transpose.
In other words, the element at row i and c... |
code_exercise_27k | 6,482 | Write a Python function to determine if a given range of integers contains both positive and negative numbers. The function should take two integers, `start` and `end`, representing the start (inclusive) and end (exclusive) of the range, respectively. The function should return `True` if the range contains both positiv... | Here is the Python code to solve the given problem in the markdown block:
```python
def has_positive_and_negative_numbers(start: int, end: int) -> bool:
"""
Determine if a given range of integers contains both positive and negative numbers.
Args:
start (int): The starting value of the range (inclus... |
code_exercise_27k | 6,485 | Write a Python function to find the largest element in each row of a given matrix. The matrix is represented as a list of lists, where each inner list represents a row. The function should return a list containing the largest element from each row. If the matrix or any row is empty, the function should raise a `ValueEr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_largest_elements(matrix: List[List[int]]) -> List[int]:
"""
Find the largest element in each row of a given matrix.
Args:
matrix (List[List[int]]): A 2D matrix represented as a list... |
code_exercise_27k | 6,492 | Write a Python function to calculate the average value of all the numerical values in a given dictionary. The function should handle dictionaries with mixed data types and only consider numerical values (integers and floats) for the average calculation. If the dictionary does not contain any numerical values, the funct... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_average(dictionary: dict) -> float:
"""
Calculate the average value of all the numerical values in the given dictionary.
Args:
dictionary (dict): A dictionary containing numerical values.
Returns:... |
code_exercise_27k | 6,498 | Write a Python function to reverse the keys and values of a given dictionary. The function should handle dictionaries with any hashable types as keys and values. The code should start with:
```python
from typing import Dict, Any
def reverse_dictionary(dictionary: Dict[Any, Any]) -> Dict[Any, Any]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def reverse_dictionary(dictionary: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Reverse the keys and values of a given dictionary.
Args:
dictionary (Dict[Any, Any]): The dictionary to be reve... |
code_exercise_27k | 6,501 | Write a Python function to create a new dictionary by excluding the specified keys from the original dictionary. The function should handle cases where the keys to exclude are not present in the original dictionary. The function should also handle cases where the original dictionary is empty or the keys to exclude list... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Set, Union
def exclude_keys(original_dict: Dict, keys_to_exclude: Union[List, Set]) -> Dict:
"""
Create a new dictionary by excluding specified keys from the original dictionary.
Args:
... |
code_exercise_27k | 6,509 | Write a Python function to calculate the sum of the digits of a given positive integer. The function should handle only positive integers and should raise a `ValueError` if the input is not a positive integer. The code should start with:
```python
def sum_of_digits(num: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_digits(num: int) -> int:
"""
Calculate the sum of the digits of a given positive integer.
Args:
num (int): The positive integer for which the sum of digits is to be calculated.
Returns:
int: ... |
code_exercise_27k | 6,511 | Write a Python function to filter a dictionary to include only key-value pairs where the value is a list. The function should return a new dictionary containing only the key-value pairs where the value is a list. The code should start with:
```python
from typing import Dict, Any
def filter_dict_by_list_values(d: Dict[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def filter_dict_by_list_values(d: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Filter a dictionary to include only key-value pairs where the value is a list.
Args:
d (Dict[Any, Any]): The dic... |
code_exercise_27k | 6,512 | Write a Python function to convert a decimal number to a Roman numeral. The function should handle decimal numbers between 1 and 3999. The code should start with:
```python
def decimal_to_roman(num: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_roman(num: int) -> str:
"""
Converts a decimal number to a Roman numeral.
Args:
num (int): The decimal number to be converted. Must be between 1 and 3999.
Returns:
str: The Roman numeral ... |
code_exercise_27k | 6,513 | Write a Python function to reverse the digits of a given positive integer. The function should handle the input validation to ensure that the input is a positive integer. The code should start with:
```python
def reverse_positive_integer(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_positive_integer(n: int) -> int:
"""
Reverses the digits of a given positive integer.
Args:
n (int): The positive integer to be reversed.
Returns:
int: The reversed integer. If the input is ... |
code_exercise_27k | 6,518 | Write a Python function to filter out elements from a given list that are divisible by a specified divisor using list comprehension. The function should return a new list containing only the elements that are not divisible by the divisor.
The code should start with:
```python
from typing import List
def filter_divisi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_divisible_elements(lst: List[int], divisor: int) -> List[int]:
"""
Filters out elements from the given list that are divisible by the specified divisor.
Args:
lst (List[int]): The... |
code_exercise_27k | 6,523 | Write a Python function to filter out the odd numbers from a given list using a list comprehension. The function should return a new list that only contains the even numbers from the input list. The code should start with:
```python
from typing import List
def filter_odd_numbers(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 filter_odd_numbers(numbers: List[int]) -> List[int]:
"""
Filter out the odd numbers from a given list and return a new list containing only the even numbers.
Args:
numbers (List[int]): A... |
code_exercise_27k | 6,525 | Write a Python function to create a dictionary where each key is a name from a given list of names, and the corresponding value is a list of their favorite colors from a given list of lists of favorite colors. The function should ensure that the lengths of the names list and the favorite colors list are the same. If th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def create_favorite_colors_dict(names: List[str], favorite_colors: List[List[str]]) -> Dict[str, List[str]]:
"""
Generates a dictionary with names as keys and a list of their favorite colors as val... |
code_exercise_27k | 6,526 | Write a Python function to generate a list comprehension that extracts the first letter of each word in a given sentence. The function should handle sentences with multiple spaces between words and should not include empty strings or spaces in the resulting list. 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 extract_first_letters(sentence: str) -> List[str]:
"""
Returns a list containing the first letter of each word in a given sentence.
Args:
- sentence (str): The sentence from which to ext... |
code_exercise_27k | 6,528 | Write a Python function to remove all key-value pairs from a given dictionary where the values are empty lists. The function should return a new dictionary with the specified key-value pairs removed. The code should start with:
```python
from typing import Dict, List, Any
def remove_empty_lists(dictionary: Dict[Any, L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Any
def remove_empty_lists(dictionary: Dict[Any, List[Any]]) -> Dict[Any, List[Any]]:
"""
Removes all key-value pairs from a dictionary where the values are empty lists.
Args:
diction... |
code_exercise_27k | 6,529 | Write a Python function to find the common elements between two lists using a list comprehension. The function should return a new list containing only the elements that are present in both input lists. The code should start with:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the common elements between two lists using list comprehension.
Args:
list1 (List[int]): The first list of... |
code_exercise_27k | 6,530 | Write a Python function to check if a given dictionary is symmetric. A dictionary is considered symmetric if for every key-value pair `(k, v)`, there exists a corresponding key-value pair `(v, k)`. The function should return `True` if the dictionary is symmetric and `False` otherwise.
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_symmetric(dictionary: dict) -> bool:
"""
Check if a dictionary is symmetric.
A symmetric dictionary has keys and values that are mirrored.
For example, {'a': 'b', 'b': 'a'} is symmetric, but {'a': 'b', 'b': 'c'}... |
code_exercise_27k | 6,531 | Write a Python function to capitalize the first letter of each word in a given sentence using a list comprehension. The function should return a list of words with each word's first letter capitalized. The code should start with:
```python
from typing import List
def capitalize_words(sentence: str) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def capitalize_words(sentence: str) -> List[str]:
"""
Capitalizes the first letter of each word in a given sentence.
Args:
sentence (str): The sentence in which to capitalize the first lette... |
code_exercise_27k | 6,533 | Write a Python function to filter out the vowels from a given word using a list comprehension. The function should return a list of characters from the input word that are not vowels. The code should start with:
```python
def filter_vowels(word: str) -> list:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def filter_vowels(word: str) -> list:
"""
Filter out the vowels from a given word using a list comprehension.
Args:
word (str): The input word to filter.
Returns:
list: A list of characters from the inp... |
code_exercise_27k | 6,544 | Write a Python function to filter out the students who scored above a specified threshold from a given list of scores. The function should return a new list containing only the scores that are above the specified threshold. The code should start with:
```python
from typing import List
def filter_scores_above_threshold... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_scores_above_threshold(scores: List[int], threshold: int) -> List[int]:
"""
Filters and returns a new list containing scores above the specified threshold.
Args:
scores (List[int]... |
code_exercise_27k | 6,548 | Write a Python function to create a new list where each element is the product of the corresponding element in the given list and its index. The function should use list comprehension for this purpose.
The code should start with:
```python
from typing import List
def multiply_by_index(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 multiply_by_index(lst: List[int]) -> List[int]:
"""
Return a new list with each element in the given list multiplied by its index.
Args:
lst (List[int]): The list of integers.
Retur... |
code_exercise_27k | 6,549 | Write a Python function to find the two keys with the highest values in a given dictionary. If the dictionary has fewer than two key-value pairs, raise a `ValueError`. The function should return a tuple containing the two keys with the highest values.
The code should start with:
```python
from typing import Dict, Tupl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Tuple
def find_keys_with_highest_values(dictionary: Dict[str, int]) -> Tuple[str, str]:
"""
Finds the two keys with the highest values in a dictionary.
Args:
dictionary (Dict[str, int]): Th... |
code_exercise_27k | 6,554 | Write a Python function to generate a list comprehension that reverses the order of words in a given sentence. The function should take a string as input and return a list of words in the reversed order. The code should start with:
```python
from typing import List
def reverse_sentence(sentence: str) -> List[str]:
```... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def reverse_sentence(sentence: str) -> List[str]:
"""
Reverses the order of words in a given sentence.
Args:
sentence (str): The input sentence to be reversed.
Returns:
List[s... |
code_exercise_27k | 6,556 | Write a Python function to remove all key-value pairs from a given dictionary where the keys contain repeating characters. The function should return the updated dictionary. The code should start with:
```python
from typing import Dict
def remove_keys_with_repeating_chars(dictionary: Dict[str, any]) -> Dict[str, any]:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def remove_keys_with_repeating_chars(dictionary: Dict[str, any]) -> Dict[str, any]:
"""
Remove key-value pairs from the dictionary where the keys contain repeating characters.
Args:
dictiona... |
code_exercise_27k | 6,558 | Write a Python function to find all prime numbers within a given range using a list comprehension. The function should take two integers, `start` and `end`, as input and return a list of all prime numbers within this range (inclusive). The function should be efficient and avoid unnecessary computations.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_prime_numbers(start: int, end: int) -> List[int]:
"""
Find all prime numbers within a given range using a list comprehension.
Args:
start (int): The starting number of the range (in... |
code_exercise_27k | 6,560 | Write a Python function to find the difference between two dictionaries, considering only keys present in both dictionaries. The function should return a dictionary where the keys are the common keys and the values are tuples containing the corresponding values from both dictionaries. If the values are the same, they s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def find_difference(dict1: Dict[Any, Any], dict2: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Finds the difference between two dictionaries, considering only keys present in both dictionaries.
Args:... |
code_exercise_27k | 6,567 | Write a Python function to calculate the cumulative sum of a given list of numbers using a list comprehension. The function should take a list of integers as input and return a list of integers where each element is the cumulative sum of the elements up to that index in the input list.
The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cumulative_sum(numbers: List[int]) -> List[int]:
"""
Calculate the cumulative sum of a given list of numbers using a list comprehension.
Args:
numbers (List[int]): A list of integers for... |
code_exercise_27k | 6,570 | Write a Python function to find the longest common prefix in a list of strings. The function should return an empty string if there is no common prefix. The code should start with:
```python
from typing import List
def longest_common_prefix(strs: List[str]) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_common_prefix(strs: List[str]) -> str:
"""
Find the longest common prefix in a list of strings.
Args:
strs (List[str]): List of strings to find the longest common prefix.
Re... |
code_exercise_27k | 6,572 | Write a Python function to calculate the median value of the values in a given dictionary. The dictionary contains numeric values as values. The function should handle the following cases:
1. If the dictionary is empty, raise a `ValueError`.
2. If the dictionary contains non-numeric values, raise a `ValueError`.
3. Cal... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def calculate_median(dictionary: Dict[str, int]) -> float:
"""
Calculates the median value of the dictionary values.
Args:
dictionary (Dict[str, int]): A dictionary containing numeric values... |
code_exercise_27k | 6,573 | Write a Python function to filter out and return a new list containing only the prime numbers from a given list of integers. The function should be efficient and handle lists of varying sizes. The code should start with:
```python
from typing import List
def extract_prime_numbers(original_list: List[int]) -> List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def is_prime(num: int) -> bool:
"""
Helper function to check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the num... |
code_exercise_27k | 6,575 | Write a Python function to determine if a given number is a happy number. A number is considered happy if, by repeatedly replacing it with the sum of the squares of its digits, it eventually reaches 1. If it enters a cycle that does not include 1, it is not a happy number.
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_happy_number(num: int) -> bool:
"""
Determines if a given number is a happy number.
A number is considered happy if, by repeatedly replacing it with the sum of the squares of its digits,
it eventually reaches 1. ... |
code_exercise_27k | 6,576 | Write a Python function to filter out and return a new list containing only the even numbers from a given list of integers. The function should handle the following:
- Ensure the input is a list.
- Ensure the list is not empty.
- Return a new list containing only the even numbers from the input list.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_even_numbers(input_list: List[int]) -> List[int]:
"""
Filters out and returns a new list containing only the even numbers from the given list of integers.
Args:
input_list (List[int]... |
code_exercise_27k | 6,578 | Write a Python function to find the maximum subarray sum in a given list of integers. The function should return the maximum sum of any contiguous subarray within the list. The code should start with:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
"""
Find the maximum sum of any contiguous subarray within the given list of integers.
Args:
arr (List[int]): A list of integers.
Returns:
... |
code_exercise_27k | 6,579 | Write a Python function to find the unique elements in a given list using a list comprehension. The function should return a new list containing only the unique elements from the input list, maintaining their original order of first appearance.
The code should start with:
```python
from typing import List
def find_un... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_unique_elements(lst: List[int]) -> List[int]:
"""
Find unique elements in a given list using list comprehension.
Args:
lst (List[int]): The input list from which to find unique elem... |
code_exercise_27k | 6,580 | Write a Python function to convert the keys of a given dictionary to lowercase. The function should return a new dictionary with the same values but with all keys converted to lowercase. The code should start with:
```python
from typing import Dict
def convert_keys_to_lowercase(dictionary: Dict[str, any]) -> Dict[str,... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import Dict
def convert_keys_to_lowercase(dictionary: Dict[str, any]) -> Dict[str, any]:
"""
Convert the keys of a dictionary to lowercase.
This function takes a dictionary as input and returns a new dict... |
code_exercise_27k | 6,581 | Write a Python function to determine if one dictionary is a subset of another dictionary. A dictionary `check_dict` is considered a subset of `main_dict` if all key-value pairs in `check_dict` are present in `main_dict`. The function should return `True` if `check_dict` is a subset of `main_dict`, and `False` otherwise... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_subset_of_dict(check_dict: dict, main_dict: dict) -> bool:
"""
Determine if check_dict is a subset of main_dict.
Args:
check_dict (dict): The dictionary to check if it is a subset.
main_dict (dict): T... |
code_exercise_27k | 6,582 | Write a Python function to generate all possible combinations of two given lists using a list comprehension. Each combination should be represented as a tuple containing one element from each list. The function should handle lists of different lengths and types.
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, Tuple
def generate_combinations(list1: List, list2: List) -> List[Tuple]:
"""
Generate all possible combinations of two given lists.
Args:
list1 (List): The first input list.
list2 ... |
code_exercise_27k | 6,583 | Write a Python function to calculate the number of days in a given month and year. The function should handle leap years correctly and raise a `ValueError` if the month is not a valid month number (1-12). The code should start with:
```python
def get_num_days_in_month(year: int, month: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def get_num_days_in_month(year: int, month: int) -> int:
"""
Calculate the number of days in a given month and year.
Args:
year (int): The year of the month.
month (int): The month for which to calculate the... |
code_exercise_27k | 6,584 | Write a Python function to concatenate two lists using slice notation. The function should take two lists as input and return a new list that is the result of concatenating the second list to the end of the first list. The function should use slice notation to achieve this concatenation.
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 concatenate_lists_with_slices(list1: List[int], list2: List[int]) -> List[int]:
"""
Concatenates two lists using slice notation.
Args:
list1 (List[int]): The first list to concatenate.
... |
code_exercise_27k | 6,586 | Write a Python function to find the largest prime number within a given range using list comprehension. The function should return the largest prime number or `None` if no prime numbers are found within the specified range.
The code should start with:
```python
def find_largest_prime(start: int, end: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_largest_prime(start: int, end: int) -> int:
"""
Find the largest prime number within a given range using list comprehension.
Args:
start (int): The start of the range (inclusive).
end (int): The end... |
code_exercise_27k | 6,587 | Write a Python function to find the common key-value pairs between two dictionaries. The function should return a new dictionary containing only the key-value pairs that exist in both input dictionaries with the same values. The code should start with:
```python
from typing import Dict
def find_common_pairs(dict1: Dic... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def find_common_pairs(dict1: Dict, dict2: Dict) -> Dict:
"""
Find the common key-value pairs between two dictionaries.
Args:
dict1 (Dict): The first dictionary.
dict2 (Dict): The sec... |
code_exercise_27k | 6,591 | Write a Python function to find the longest subsequence of equal elements in a given list. The function should return the longest subsequence as a list. If there are multiple subsequences of the same maximum length, return the first one encountered.
The code should start with:
```python
from typing import List
def lo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_subsequence_of_equal_elements(arr: List[int]) -> List[int]:
"""
Find the longest subsequence of equal elements in a given list.
Args:
arr (List[int]): The input list of integers.... |
code_exercise_27k | 6,594 | Write a Python function to calculate the average of each sublist in a nested list using a list comprehension. The function should handle nested lists of varying lengths and ensure that the averages are calculated correctly. The code should start with:
```python
from typing import List
def calculate_averages(nested_lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_averages(nested_list: List[List[float]]) -> List[float]:
"""
Calculates the average of each sublist in a nested list.
Args:
nested_list (List[List[float]]): A nested list of nu... |
code_exercise_27k | 6,595 | Write a Python function to find the maximum and minimum values in a list without using built-in functions like `max()` and `min()`. The function should handle an empty list by raising a `ValueError`. The code should start with:
```python
from typing import List, Tuple
def find_max_min(lst: List[int]) -> Tuple[int, int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_min(lst: List[int]) -> Tuple[int, int]:
"""
Find the maximum and minimum values in a list without using built-in functions.
Args:
lst (List[int]): A list of integers.
... |
code_exercise_27k | 6,597 | Write a Python function to sort a dictionary based on its values in descending order. The function should return a new dictionary with the sorted items. The original dictionary should remain unchanged.
The code should start with:
```python
from typing import Dict
def sort_dict_by_values_descending(my_dict: Dict[str, ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_dict_by_values_descending(my_dict: Dict[str, int]) -> Dict[str, int]:
"""
Sorts a dictionary based on the values in descending order.
Args:
my_dict (Dict[str, int]): The dictionary ... |
code_exercise_27k | 6,603 | Write a Python function to generate all possible substrings of a given string using list comprehension. The function should return a list containing all substrings, including single characters and the entire string itself. The code should start with:
```python
from typing import List
def get_all_substrings(string: str... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_all_substrings(string: str) -> List[str]:
"""
Generate a list of all possible substrings of a given string.
Args:
string (str): The input string from which to generate substrings.
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.