source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 15,042 | Write a Python function to calculate the factorial of a given number using a recursive approach. The function should handle non-negative integers only. If a negative number is provided, the function should raise a `ValueError`.
The code should start with:
```python
def recursive_factorial(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def recursive_factorial(n: int) -> int:
"""
Calculate the factorial of a given number using a recursive approach.
Parameters:
n (int): The number for which the factorial will be calculated.
Returns:
int: The fa... |
code_exercise_27k | 15,047 | Write a Python function to generate a list of random numbers between two specified values using the `random` module. The function should accept three parameters: the lower bound (`low`), the upper bound (`high`), and the number of random numbers to generate (`count`). The function should return a list of `count` random... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_random_numbers(low: int, high: int, count: int) -> List[int]:
"""
Generate a list of random numbers between `low` and `high` (inclusive).
Args:
low (int): The lowe... |
code_exercise_27k | 15,049 | Create a Python function that implements a simple calculator with additional mathematical operations using the `math` module. The calculator should support basic arithmetic operations (addition, subtraction, multiplication, division) and additional operations such as square root, factorial, and logarithm. The function ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculator(operation: str, *args) -> float:
"""
A simple calculator that supports basic arithmetic operations and additional mathematical operations.
Parameters:
operation (str): The operation to perfor... |
code_exercise_27k | 15,051 | Write a Python function to safely divide two numbers, handling `TypeError` and `ZeroDivisionError` exceptions. The function should accept two parameters, `a` and `b`, and return the result of the division. If either `a` or `b` is not a numeric type, a `TypeError` should be raised with a specific message. If `b` is zero... | Here is the Python code to solve the given problem in the markdown block:
```python
def safe_divide(a, b):
"""
Safely divide two numbers, handling TypeError and ZeroDivisionError.
Parameters:
a (int/float): The dividend.
b (int/float): The divisor.
Returns:
float: The result of... |
code_exercise_27k | 15,053 | Write a Python function to determine if a given number is prime using the `math` module. The function should efficiently check for primality by iterating only up to the square root of the number. The code should start with:
```python
import math
def is_prime(number: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime(number: int) -> bool:
"""
Determine if a given number is prime.
Args:
number (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwi... |
code_exercise_27k | 15,054 | Write a Python function to reverse a given string using a custom implementation without relying on built-in functions like `reversed()` or string slicing (`[::-1]`). The function should iterate through the string and construct the reversed string manually.
The code should start with:
```python
def reverse_string_manua... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string_manual(string: str) -> str:
"""
Reverses a given string using a custom implementation without built-in functions.
Args:
string (str): The original string to be reversed.
Returns:
str:... |
code_exercise_27k | 15,059 | Write a Python function to sort a list of strings in alphabetical order using the built-in `sorted()` function. The function should handle an empty list and return it as is. The function should also handle lists with duplicate strings and maintain their frequency in the sorted output. The code should start with:
```pyt... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_strings(strings: List[str]) -> List[str]:
"""
Sorts a list of strings in alphabetical order.
Args:
strings (List[str]): The list of strings to be sorted.
Returns:
List[... |
code_exercise_27k | 15,064 | Write a Python function to find the maximum and minimum values in a given list of numbers without using built-in functions `max()` and `min()`. 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_without_builtins(lst: List[int]) -> Tuple[int, int]:
"""
Finds the maximum and minimum values in a list without using built-in functions max() and min().
Args:
lst (L... |
code_exercise_27k | 15,073 | Write a Python function to safely divide two integers, handling the case where the divisor is zero by raising a `ValueError`. The function should return the quotient as a float if the division is valid.
The code should start with:
```python
def safe_divide(dividend: int, divisor: int) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def safe_divide(dividend: int, divisor: int) -> float:
"""
Safely divide a dividend by a divisor and return the quotient.
Args:
dividend (int): The number to be divided (dividend).
divisor (int): The number ... |
code_exercise_27k | 15,075 | Write a Python function to convert a decimal number to its binary representation without using the built-in `bin()` function. The function should handle non-negative integers only. The code should start with:
```python
def decimal_to_binary_manual(decimal: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary_manual(decimal: int) -> str:
"""
Converts a decimal number to its binary representation without using the built-in bin() function.
Args:
decimal (int): The non-negative decimal number to be con... |
code_exercise_27k | 15,081 | Write a Python function to convert a list of strings to lowercase using the built-in functions. The function should take a list of strings as input and return a new list where each string is converted to lowercase. The code should start with:
```python
from typing import List
def convert_list_to_lowercase(strings: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_list_to_lowercase(strings: List[str]) -> List[str]:
"""
Converts a list of strings to lowercase using built-in functions.
Args:
strings (List[str]): The list of strings to be con... |
code_exercise_27k | 15,084 | Write a Python function to calculate the sum of all elements in a list using a custom implementation without using the built-in `sum()` function. The function should handle lists containing integers and floating-point numbers. The code should start with:
```python
from typing import List
def custom_sum_of_list_element... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_sum_of_list_elements(lst: List[float]) -> float:
"""
Calculate the sum of all elements in a list using a custom implementation.
Args:
lst (List[float]): The list of numbers (integ... |
code_exercise_27k | 15,087 | Write a Python function to calculate the average of a list of numbers using the built-in functions. The function should handle the following cases:
1. If the input is not a list, raise a `ValueError` with the message "The input must be a list."
2. If the list is empty, raise a `ValueError` with the message "The list ca... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_average(numbers: List[float]) -> float:
"""
Calculate the average of a list of numbers.
Parameters:
numbers (List[float]): A list of numbers.
Returns:
float: The average o... |
code_exercise_27k | 15,093 | Write a Python function to check if a given word is a palindrome using built-in functions. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). The function should handle case insensitivity and ignore non-al... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(word: str) -> bool:
"""
Check if a given word is a palindrome, ignoring case and non-alphanumeric characters.
Parameters:
word (str): The word to be checked.
Returns:
bool: True if the word is... |
code_exercise_27k | 15,101 | Write a Python function to determine if two given strings are anagrams of each other. 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 ignore non-alphabetic characters and case differences.
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Determine if two strings are anagrams of each other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using al... |
code_exercise_27k | 15,105 | Write a Python function to remove duplicate elements from a given list while preserving the order of the elements. The function should use a set to track seen elements and a list to maintain the order of elements. The code should start with:
```python
from typing import List
def remove_duplicates_preserving_order(lst:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_preserving_order(lst: List[int]) -> List[int]:
"""
Removes duplicate elements from a list while preserving the order of the elements.
Args:
lst (List[int]): The list fr... |
code_exercise_27k | 15,109 | Write a Python function to convert a string to a list of characters using the built-in functions. The function should handle various edge cases, such as an empty string, a string with special characters, and a string with mixed case characters. The code should start with:
```python
from typing import List
def string_t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def string_to_list_of_chars(string: str) -> List[str]:
"""
Converts a string to a list of individual characters.
Args:
string (str): The input string to be converted.
Returns:
L... |
code_exercise_27k | 15,115 | Write a Python function to find the largest prime factor of a given number using the `math` module. The function should handle edge cases such as non-positive integers and ensure that the largest prime factor is correctly identified. The code should start with:
```python
import math
def largest_prime_factor(n: int) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def largest_prime_factor(n: int) -> int:
"""
Finds the largest prime factor of a given number.
Args:
n (int): The number for which we want to find the largest prime factor.
Returns:
int: Th... |
code_exercise_27k | 15,124 | Write a Python function to convert a list of integers to a string, where each integer is separated by a specified delimiter. The function should handle lists of varying lengths, including empty lists. The code should start with:
```python
from typing import List
def convert_list_to_string_with_delimiter(input_list: Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_list_to_string_with_delimiter(input_list: List[int], delimiter: str) -> str:
"""
Convert a list of integers to a string with each integer separated by a specified delimiter.
Args:
... |
code_exercise_27k | 15,126 | Write a Python function to safely divide two numbers, handling the case where the divisor is zero by returning a default value instead of raising an `AssertionError`. The function should also handle cases where the inputs are not numeric by returning a default value.
The code should start with:
```python
def safe_divi... | Here is the Python code to solve the given problem in the markdown block:
```python
def safe_divide(a, b, default_value=None):
"""
Safely divide two numbers, returning a default value if the divisor is zero or if the inputs are not numeric.
Args:
a (float or int): The dividend.
b (float or ... |
code_exercise_27k | 15,128 | Write a Python function to check if a list contains only unique elements using a custom implementation without relying on built-in functions like `set` or `len`. The function should iterate through the list and use a data structure to keep track of elements that have been seen. The code should start with:
```python
fr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def contains_only_unique_elements(lst: List[int]) -> bool:
"""
Check if a list contains only unique elements using a custom implementation.
Args:
lst (List[int]): The list to be checked for ... |
code_exercise_27k | 15,131 | Write a Python function to reverse a given list without using any built-in functions or methods that directly reverse the list. The function should use a loop to manually reverse the elements of the list.
The code should start with:
```python
from typing import List
def manual_reverse_list(lst: List) -> List:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def manual_reverse_list(lst: List) -> List:
"""
Reverses a list manually without using built-in functions or methods.
Args:
lst (list): The list to be reversed.
Returns:
list: T... |
code_exercise_27k | 15,138 | Write a Python function to count the frequency of each element in a given list. The function should return a dictionary where the keys are the elements from the list and the values are their respective frequencies. The code should start with:
```python
from typing import List, Dict
def count_element_frequencies(lst: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_element_frequencies(lst: List) -> Dict[int, int]:
"""
Count the frequency of each element in a given list.
Args:
lst (List): The input list to be counted.
Returns:
... |
code_exercise_27k | 15,141 | Write a Python function to calculate the power of a given number using the `math` module. The function should handle both positive and negative exponents, as well as special cases like raising to the power of zero or raising zero to any power. The function should also handle edge cases such as very large exponents and ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_power(base: float, exponent: float) -> float:
"""
Calculates the power of a given number using the math module.
Args:
base (float): The base number.
exponent (float): The exponent.... |
code_exercise_27k | 15,143 | Write a Python function to sort a list of tuples based on a specific element using the built-in `sorted()` function. The function should take a list of tuples and an integer representing the index of the element to be used for sorting. The function should return the sorted list of tuples.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_element(tuples_list: List[Tuple], element_index: int) -> List[Tuple]:
"""
Sorts a list of tuples based on a specific element.
Args:
tuples_list (List[Tuple]): The l... |
code_exercise_27k | 15,149 | Write a Python function to calculate the factorial of a given non-negative integer using dynamic programming. The function should handle the case where the input is a negative number by raising a `ValueError`. The code should start with:
```python
def factorial_dp(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_dp(n: int) -> int:
"""
Calculate the factorial of a given non-negative integer using dynamic programming.
Args:
n (int): The number for which the factorial is to be calculated.
Returns:
in... |
code_exercise_27k | 15,153 | Write a Python function to generate the first `n` terms of the Fibonacci series using dynamic programming. The function should return a list containing the Fibonacci series up to the `n`-th term. The code should start with:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the first n terms of the Fibonacci series using dynamic programming.
Args:
n (int): The number of terms in the series to generate.
... |
code_exercise_27k | 15,154 | Write a Python function to calculate the absolute value of a number without using the built-in `abs()` function. The function should handle both integers and floating-point numbers. The code should start with:
```python
def calculate_absolute_value(number: int | float) -> int | float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_absolute_value(number: int | float) -> int | float:
"""
Calculates the absolute value of a number without using the built-in abs() function.
Args:
number (int or float): The input number.
Returns:... |
code_exercise_27k | 15,157 | Write a Python function to convert a string representation of a dictionary into an actual dictionary object. The string should be formatted as a valid Python dictionary literal, where keys and values are separated by colons, and each key-value pair is separated by commas. The function should handle various types of val... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
import ast
def string_to_dict(string: str) -> Dict:
"""
Converts a string representation of a dictionary into an actual dictionary object.
This function takes a string formatted as a valid Python di... |
code_exercise_27k | 15,165 | Write a Python function to determine if a given number is a perfect square without using the `math` module. The function should handle both positive and negative integers. The code should start with:
```python
def is_perfect_square(num: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_square(num: int) -> bool:
"""
Determine if a given number is a perfect square without using the math module.
Args:
num (int): The number to check.
Returns:
bool: True if the number is a p... |
code_exercise_27k | 15,169 | Write a Python function to shuffle the elements of a given list using the Fisher-Yates algorithm. The function should return a new list with the elements shuffled, without modifying the original list. The code should start with:
```python
from typing import List
import random
def shuffle_list(lst: List[int]) -> List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def shuffle_list(lst: List[int]) -> List[int]:
"""
Shuffles the elements of a list using the Fisher-Yates algorithm.
Args:
lst (List[int]): The list to be shuffled.
Return... |
code_exercise_27k | 15,175 | Write a Python function to merge two dictionaries into a single 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 use the built-in dictionary methods to achieve this.
The code should start with:
```python
d... | Here is the Python code to solve the given problem in the markdown block:
```python
def merge_dicts(dict1: dict, dict2: dict) -> dict:
"""
Merge two dictionaries into a single dictionary. If both dictionaries have the same key,
the value from the second dictionary will overwrite the value from the first dic... |
code_exercise_27k | 15,179 | Write a Python function to solve the coin change problem using dynamic programming. The function should determine the minimum number of coins needed to make a given amount using the available coin denominations. If it is impossible to make the amount with the given coins, the function should return -1.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def min_coins_needed(coins: List[int], amount: int) -> int:
"""
Solves the coin change problem using dynamic programming to find the minimum number of coins needed to make a given amount.
Args:
... |
code_exercise_27k | 15,185 | 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 take a string as input and return the minimum number of insertions needed.
The code should start with:
```python
def min_insertions_to_palindrome(string: str) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
def min_insertions_to_palindrome(string: str) -> int:
"""
Calculate the minimum number of insertions required to make a given string a palindrome.
Args:
string (str): The input string.
Returns:
int: The... |
code_exercise_27k | 15,186 | Write a Python function to determine if two lists have any common elements using the built-in functions. The function should return `True` if there is at least one common element between the two lists, and `False` otherwise. The code should start with:
```python
from typing import List
def has_common_elements(list1: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def has_common_elements(list1: List[int], list2: List[int]) -> bool:
"""
Determines if two lists have any common elements.
Args:
list1 (List[int]): The first list of integers.
list2 ... |
code_exercise_27k | 15,188 | Write a Python function to find the length of the longest increasing subsequence (LIS) in a given array using dynamic programming. The function should return the length of the LIS, not the subsequence itself.
The code should start with:
```python
from typing import List
def longest_increasing_subsequence_length(nums:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence_length(nums: List[int]) -> int:
"""
Calculate the length of the longest increasing subsequence in a given array using dynamic programming.
Args:
nums: A li... |
code_exercise_27k | 15,199 | Write a Python function to generate a list of prime numbers within a specified range using the `math` module. The function should be efficient and handle edge cases such as negative numbers, zero, and ranges where no primes exist.
The code should start with:
```python
import math
from typing import List
def generate_... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
from typing import List
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if n is prime, False otherwise.
"""
if ... |
code_exercise_27k | 15,200 | Write a Python function to count the number of vowels in a given string using built-in functions. The function should be case-insensitive and should consider both uppercase and lowercase vowels. The code should start with:
```python
def count_vowels(string: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_vowels(string: str) -> int:
"""
Counts the number of vowels in a given string.
Parameters:
string (str): The input string.
Returns:
int: The number of vowels in the string.
Example:
>>> count... |
code_exercise_27k | 15,201 | Write a Python function to calculate the square root of a number, handling `ValueError` for negative numbers and `TypeError` for incorrect data types. The function should return the square root if the input is valid, otherwise raise the appropriate exception.
The code should start with:
```python
def calculate_square_... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_square_root(number):
"""
Calculate the square root of a number.
Parameters:
number (float or int): The number to calculate the square root of.
Returns:
float: The square root of the number.
R... |
code_exercise_27k | 15,204 | Write a Python function to calculate the sine of a given angle using the `math` module. The function should accept the angle in radians and return the sine value. Additionally, the function should raise a `TypeError` if the input is not a float or integer.
The code should start with:
```python
import math
def calcula... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_sine(angle: float) -> float:
"""
Calculate the sine of a given angle in radians.
Args:
angle (float): The angle in radians.
Returns:
float: The sine of the angle.
Raises:... |
code_exercise_27k | 15,208 | Write a Python function to calculate the sum of all even numbers in a given list using built-in functions. The function should handle input validation to ensure the input is a list of integers. If the input is not a list or contains non-integer elements, appropriate exceptions should be raised.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_even_numbers(numbers: List[int]) -> int:
"""
Calculate the sum of all even numbers in a given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The sum ... |
code_exercise_27k | 15,209 | Write a Python function to find the minimum number of intervals to remove from a given set of intervals to make the rest non-overlapping. Each interval is represented as a tuple `(start, end)`, where `start` and `end` are integers. The function should use dynamic programming to solve the problem efficiently.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def min_intervals_to_remove(intervals: List[Tuple[int, int]]) -> int:
"""
Finds the minimum number of intervals to remove to make the rest non-overlapping using dynamic programming.
Args:
... |
code_exercise_27k | 15,216 | Write a Python function to calculate the square of each number in a given list using the built-in `map` function and a lambda function. The function should return a new list containing the square of each number from the input list. The code should start with:
```python
from typing import List
def square_numbers(number... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def square_numbers(numbers: List[int]) -> List[int]:
"""
Calculate the square of each number in a list using the built-in `map` function and a lambda function.
Args:
numbers (List[int]): A l... |
code_exercise_27k | 15,220 | Write a Python function to calculate the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm. The Euclidean algorithm is a more manual approach compared to using the `math.gcd` function from the `math` module. The function should take two integers as input and return their GCD.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd_euclidean(a: int, b: int) -> int:
"""
Calculates the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm.
Args:
a (int): The first number.
b (int): The second number.
Retu... |
code_exercise_27k | 15,221 | Write a Python function to count the number of unique paths in an m x n grid, starting from the top-left corner (0, 0) and moving only right or down to reach the bottom-right corner (m-1, n-1). The function should use dynamic programming to efficiently compute the number of unique paths.
The code should start with:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_unique_paths(m: int, n: int) -> int:
"""
Count the number of unique paths in an m x n grid.
Args:
m (int): The number of rows in the grid.
n (int): The number of columns in the grid.
Returns:
... |
code_exercise_27k | 15,224 | Write a Python function to safely divide two numbers, handling a `ValueError` when the denominator is zero. The function should return the result of the division if the denominator is non-zero, and raise a `ValueError` with a specific error message if the denominator is zero.
The code should start with:
```python
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
def safe_divide(numerator: float, denominator: float) -> float:
"""
Safely divides two numbers, raising a ValueError if the denominator is zero.
Args:
numerator (float): The numerator in the division.
denomi... |
code_exercise_27k | 15,233 | Write a Python function to find the maximum sum of a subarray using dynamic programming. The function should take a list of integers as input and 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_dp(nums: List[int]) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum_dp(nums: List[int]) -> int:
"""
Find the maximum sum of a contiguous subarray using dynamic programming.
Args:
nums (List[int]): The input array of integers.
Return... |
code_exercise_27k | 15,237 | Write a Python function to calculate the product of all numbers in a given list using the built-in `reduce` function from the `functools` module. The function should handle the case where the input list is empty by raising a `ValueError`. The code should start with:
```python
from typing import List
from functools impo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
from functools import reduce
def product_of_list(numbers: List[int]) -> int:
"""
Calculate the product of all numbers in a list.
Args:
numbers (List[int]): A list of integers.
Returns:
... |
code_exercise_27k | 15,243 | Write a Python function to find the length of the longest increasing subsequence (LIS) in a given array using dynamic programming. The function should return the length of the LIS, not the subsequence itself.
The code should start with:
```python
from typing import List
def longest_increasing_subsequence_length(nums:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence_length(nums: List[int]) -> int:
"""
Calculate the length of the longest increasing subsequence in a given array using dynamic programming.
Args:
nums (List... |
code_exercise_27k | 15,247 | Write a Python function to determine if a given string can be segmented into valid words using memoization. The function should take a string and a set of valid words as input and return a boolean indicating whether the string can be segmented into valid words. The code should start with:
```python
from typing import S... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set
def can_segment_string(s: str, word_dict: Set[str]) -> bool:
"""
Determine if a given string can be segmented into valid words using memoization.
Args:
s (str): The input string to be segment... |
code_exercise_27k | 15,249 | Write a Python function to calculate the nth Catalan number using memoization. The function should efficiently compute the Catalan number by storing previously computed values to avoid redundant calculations. The code should start with:
```python
def catalan_number(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def catalan_number(n: int) -> int:
"""
Calculate the nth Catalan number using memoization.
Args:
n (int): The index of the Catalan number to calculate.
Returns:
int: The nth Catalan number.
"""
... |
code_exercise_27k | 15,252 | Write a Python function to calculate the binomial coefficient using memoization. The binomial coefficient, denoted as C(n, k), represents the number of ways to choose k items from a set of n distinct items without considering their order. The function should use a dictionary to store previously calculated values to avo... | Here is the Python code to solve the given problem in the markdown block:
```python
def binomial_coefficient_memoized(n: int, k: int) -> int:
"""
Calculate the binomial coefficient C(n, k) using memoization.
The binomial coefficient, denoted as C(n, k), represents the number of ways
to choose k items f... |
code_exercise_27k | 15,253 | Write a Python function to convert a given string to title case using the built-in `title()` method. The function should handle various edge cases, such as strings with special characters, numbers, and mixed-case input. The code should start with:
```python
def convert_to_title_case(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_title_case(string: str) -> str:
"""
Convert a given string to title case using the built-in `title()` method.
Parameters:
string (str): The input string to be converted to title case.
Returns:
st... |
code_exercise_27k | 15,254 | Write a Python function to find the length of the longest common subsequence (LCS) of two given strings using dynamic programming. The function should use a bottom-up approach to fill a 2D table and return the length of the LCS.
The code should start with:
```python
def longest_common_subsequence_length(s1: str, s2: s... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_subsequence_length(s1: str, s2: str) -> int:
"""
Find the length of the longest common subsequence of two strings using dynamic programming.
Args:
s1 (str): The first string.
s2 (str): The... |
code_exercise_27k | 15,256 | Write a Python function to calculate the logarithm base 10 of a given number using the `math` module. The function should handle cases where the input number is zero or negative by returning `None` and should also handle non-numeric inputs gracefully by returning `None`. The code should start with:
```python
import mat... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_log_base_10(number):
"""
Calculates the logarithm base 10 of a given number using the math module.
Parameters:
number (float or int): The number for which the logarithm is calculated.
... |
code_exercise_27k | 15,258 | Write a Python function to determine if a given graph is a clique. A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the subset are adjacent. The graph is represented as a dictionary where the keys are vertices and the values are lists of adjacent vertices.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def is_clique(graph: Dict[int, List[int]]) -> bool:
"""
Determine if the given graph is a clique.
A clique is a subset of vertices of an undirected graph such that every two distinct vertices ... |
code_exercise_27k | 15,259 | Write a Python function to calculate the minimum number of insertions or deletions required to convert one string into another using memoization. The function should be able to handle strings of varying lengths and should efficiently compute the result using a memoization technique to avoid redundant calculations.
The... | Here is the Python code to solve the given problem in the markdown block:
```python
def min_edit_distance(s1: str, s2: str) -> int:
"""
Calculate the minimum number of insertions or deletions required to convert one string to another using memoization.
Args:
s1 (str): The first string.
s2 (... |
code_exercise_27k | 15,261 | Write a Python function to find the maximum clique size in a graph given an adjacency matrix and the number of vertices. A clique is a subset of vertices in which every pair of vertices is connected by an edge. The function should start with:
```python
from typing import List
def find_max_clique_size(adjacency_matrix:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_clique_size(adjacency_matrix: List[List[int]], num_vertices: int) -> int:
"""
Finds the maximum clique size in a graph given an adjacency matrix and the number of vertices.
Args:
... |
code_exercise_27k | 15,262 | Write a Python function to calculate the edit distance (also known as Levenshtein distance) between two strings using memoization. The edit distance is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other. The function should use a memoizati... | Here is the Python code to solve the given problem in the markdown block:
```python
def edit_distance_memoized(str1: str, str2: str) -> int:
"""
Calculate the edit distance between two strings using memoization.
Parameters:
str1 (str): The first string.
str2 (str): The second string.
Returns:
... |
code_exercise_27k | 15,266 | Write a Python function to calculate the maximum sum of non-adjacent elements in a given array using dynamic programming. The function should efficiently compute the result by iterating through the array and storing intermediate results to avoid redundant calculations.
The code should start with:
```python
from typing... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_sum_non_adjacent_dp(arr: List[int]) -> int:
"""
Calculate the maximum sum of non-adjacent elements in an array using dynamic programming.
Args:
arr (List[int]): The input array.
... |
code_exercise_27k | 15,267 | Write a Python function to convert a list of integers to their binary representations using the built-in `bin` function. The function should handle input validation to ensure that the input is a list of integers. If the input is not a list, the function should raise a `TypeError`. The function should return a list of s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_to_binary(numbers: List[int]) -> List[str]:
"""
Convert a given list of integers to their binary representations.
Args:
numbers (List[int]): List of integers to be converted.
... |
code_exercise_27k | 15,272 | Write a Python function to find the maximum clique size in a given graph represented by an adjacency list. A clique is a subset of vertices in which every two distinct vertices are adjacent. The function should take the adjacency list and the number of vertices as inputs and return the size of the maximum clique.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_clique_size(adj_list: List[List[int]], num_vertices: int) -> int:
"""
Find the maximum clique size in a graph given the adjacency list and the number of vertices.
Args:
adj_list... |
code_exercise_27k | 15,274 | Write a Python function to determine if a given number is a power of two using bitwise operations. The function should return `True` if the number is a power of two, and `False` otherwise. The code should start with:
```python
def is_power_of_two_bitwise(n: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_power_of_two_bitwise(n: int) -> bool:
"""
Determine if a given number is a power of two using bitwise operations.
Args:
n (int): The number to check.
Returns:
bool: True if the number is a power ... |
code_exercise_27k | 15,277 | Write a Python function to convert a list of strings to uppercase using the built-in functions. The function should take a list of strings as input and return a new list where each string is converted to uppercase. The code should start with:
```python
from typing import List
def convert_to_uppercase(strings: List[str... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_to_uppercase(strings: List[str]) -> List[str]:
"""
Convert a list of strings to uppercase.
Args:
strings (List[str]): The list of strings to be converted to uppercase.
Retur... |
code_exercise_27k | 15,279 | Write a Python function to calculate the sum of all odd numbers in a given list using built-in functions. The function should filter out the odd numbers and then sum them up. The code should start with:
```python
from typing import List
def sum_of_odds(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_odds(numbers: List[int]) -> int:
"""
Calculate the sum of all odd numbers in a given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The sum of all... |
code_exercise_27k | 15,280 | Write a Python function to find the longest palindrome subsequence in a given string using dynamic programming. The function should return the longest palindrome subsequence as a string. The code should start with:
```python
def longest_palindrome_subsequence(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_palindrome_subsequence(s: str) -> str:
"""
Find the longest palindrome subsequence in a given string using dynamic programming.
Args:
s (str): The input string.
Returns:
str: The longest pal... |
code_exercise_27k | 15,283 | Write a Python function to find the third largest number in a given list of integers using built-in functions. The function should handle edge cases such as lists with fewer than three elements and non-list inputs. The code should start with:
```python
from typing import List
def find_third_largest(numbers: List[int])... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_third_largest(numbers: List[int]) -> int:
"""
Find the third largest number in a list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The thir... |
code_exercise_27k | 15,288 | Write a Python function to find the maximum clique size in a given undirected graph. A clique is a subset of vertices in which every two distinct vertices are adjacent. The function should take an edge list and the number of vertices as inputs and return the size of the largest clique in the graph.
The code should sta... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_clique_size(edge_list: List[Tuple[int, int]], num_vertices: int) -> int:
"""
Finds the maximum clique size in a graph given the edge list and the number of vertices.
Args:
... |
code_exercise_27k | 15,302 | Write a Python function to remove leading and trailing whitespace from a given string using the built-in `strip()` method. Additionally, ensure that the function handles various edge cases, such as empty strings, strings with only whitespace, and strings with multiple leading and trailing whitespaces.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_whitespace(string: str) -> str:
"""
Remove leading and trailing whitespace from a given string using the built-in `strip()` method.
Args:
string (str): The input string from which to remove leading and tr... |
code_exercise_27k | 15,305 | Write a Python function to determine the longest repeating subsequence in a given string using memoization. The function should return the longest subsequence that appears at least twice in the string without overlapping. The code should start with:
```python
def longest_repeating_subsequence(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_repeating_subsequence(s: str) -> str:
"""
Returns the longest repeating subsequence in a string using memoization.
Parameters:
s (str): The input string.
Returns:
str: The longest repeating subseque... |
code_exercise_27k | 15,306 | Write a Python function to convert a given string into a tuple where each character of the string becomes a separate element in the tuple. The function should handle input validation to ensure that the input is a string. If the input is not a string, the function should raise a `TypeError`.
The code should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
def string_to_tuple(input_string: str) -> tuple:
"""
Converts a string to a tuple where each character of the string becomes a separate element in the tuple.
Args:
input_string (str): The string to be converted to a... |
code_exercise_27k | 15,317 | Write a Python function to determine if two given strings are rotations of each other. A rotation of a string is defined as a new string that is formed by shifting characters from the beginning of the string to the end. For example, "abc" and "cab" are rotations of each other.
The code should start with:
```python
def... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_rotations(str1: str, str2: str) -> bool:
"""
Determine if two strings are rotations of each other.
Args:
str1 (str): The first string.
str2 (str): The second string.
Returns:
bool: True ... |
code_exercise_27k | 15,318 | Write a Python function to calculate the maximal product of cutting a rope into pieces using dynamic programming. The function should take the length of the rope as input and return the maximal product of the lengths of the pieces. The rope must be cut at least once.
The code should start with:
```python
def max_produ... | Here is the Python code to solve the given problem in the markdown block:
```python
def max_product_cutting(length: int) -> int:
"""
Calculate the maximal product of cutting a rope into pieces using dynamic programming.
Args:
length (int): The length of the rope.
Returns:
int: The maxi... |
code_exercise_27k | 15,324 | Write a Python function to calculate the square of each even number in a given list using built-in functions. The function should filter out the odd numbers and then apply the square operation to the remaining even numbers. The code should start with:
```python
from typing import List
def square_even_numbers(numbers: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def square_even_numbers(numbers: List[int]) -> List[int]:
"""
Calculates the square of each even number in a list.
Args:
numbers (List[int]): A list of integers.
Returns:
List[i... |
code_exercise_27k | 15,337 | Write a Python function to remove all occurrences of a specific element from a given list using built-in functions. The function should return a new list with all occurrences of the specified element removed, without modifying the original list. The code should start with:
```python
from typing import List, Any
def re... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def remove_element(lst: List[Any], element: Any) -> List[Any]:
"""
Remove all occurrences of a specific element from a list.
Args:
lst (List[Any]): The list from which the elements shou... |
code_exercise_27k | 15,342 | Write a Python function to convert a list of strings into a single string where each string is separated by a specified delimiter. The function should handle an empty list by returning an empty string. The code should start with:
```python
from typing import List
def convert_list_to_delimited_string(strings: List[str]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_list_to_delimited_string(strings: List[str], delimiter: str) -> str:
"""
Convert a list of strings to a single string where each string is separated by a specified delimiter.
Args:
... |
code_exercise_27k | 15,345 | Write a Python function to calculate the kth permutation of numbers from 1 to n using memoization. The function should return the kth permutation as a list of integers. The permutation index k is 1-based.
The code should start with:
```python
from typing import List
def get_kth_permutation(n: int, k: int) -> List[int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_kth_permutation(n: int, k: int) -> List[int]:
"""
Calculate the kth permutation of numbers from 1 to n using memoization.
Args:
n (int): The maximum number in the sequence.
k... |
code_exercise_27k | 15,350 | Write a Python function to count the number of vowels (both lowercase and uppercase) in a given string. The function should be case-insensitive and should return the total count of vowels in the string.
The code should start with:
```python
def count_vowels(string: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_vowels(string: str) -> int:
"""
Counts the number of vowels (a, e, i, o, u) in a given string.
Parameters:
string (str): The input string.
Returns:
int: The number of vowels in the string.
Exampl... |
code_exercise_27k | 15,352 | Write a Python function to remove duplicates from a given list of numbers and return a new list with only the unique elements. The function should maintain the order of the first occurrence of each element. The code should start with:
```python
from typing import List
def remove_duplicates_keep_order(numbers: List[int... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def remove_duplicates_keep_order(numbers: List[int]) -> List[int]:
"""
Removes duplicates from a list of numbers and returns a new list with only the unique elements,
maintaining the order of the f... |
code_exercise_27k | 15,358 | Write a Python function to check if a given string is a palindrome, considering only alphanumeric characters and ignoring case. The function should return `True` if the string is a palindrome and `False` otherwise. The code should start with:
```python
def is_palindrome(string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def is_palindrome(string: str) -> bool:
"""
Checks if a string is a palindrome, considering only alphanumeric characters and ignoring case.
Args:
string (str): The string to check.
Returns:
bool: True... |
code_exercise_27k | 15,359 | Write a Python function to calculate the nth Fibonacci number using an iterative approach. The function should handle the base cases directly and use a loop to compute the Fibonacci number for any positive integer `n`. The function should also include input validation to ensure that `n` is a positive integer.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number using an iterative approach.
This function takes in a positive integer `n` and returns the nth Fibonacci number.
Parameters:
n (int): The pos... |
code_exercise_27k | 15,362 | Write a Python function to find the largest difference between any two elements in a given list of integers. The function should handle edge cases such as an empty list or a list with fewer than two elements by raising a `ValueError`. The code should start with:
```python
from typing import List
def find_largest_diffe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_largest_difference(numbers: List[int]) -> int:
"""
Finds the largest difference between any two elements in the given list of integers.
Args:
numbers (List[int]): A list of integers... |
code_exercise_27k | 15,365 | Write a Python function to calculate the factorial of a given non-negative integer using an iterative approach. The function should handle edge cases such as zero and should raise a `ValueError` for negative inputs. 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.
Args:
n (int): The non-negative integer for which the factorial is to be calculated.
Returns:
int: The factor... |
code_exercise_27k | 15,366 | Write a Python function to find the length of the longest increasing subsequence (LIS) in a given array. The function should return the length of the LIS. The code should start with:
```python
from typing import List
def longest_increasing_subsequence_length(arr: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence_length(arr: List[int]) -> int:
"""
Finds the length of the longest increasing subsequence in an array.
Args:
arr (List[int]): The input array.
Returns... |
code_exercise_27k | 15,376 | Write a Python function to determine if a given word is an anagram of another word. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The function should handle case insensitivity and ignore any whitespace in the input words. The function should return `True` if the two words a... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_anagram(word1: str, word2: str) -> bool:
"""
Determines if two words are anagrams of each other.
An anagram is a word or phrase formed by rearranging the letters of another word or phrase.
This function is case-i... |
code_exercise_27k | 15,382 | Write a Python function to calculate the sum of the digits of a given number. The function should handle both positive and negative integers. The code should start with:
```python
def sum_of_digits(number: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_digits(number: int) -> int:
"""
Calculate the sum of the digits of a given number.
Args:
number (int): The number for which to calculate the sum of digits.
Returns:
int: The sum of the digits... |
code_exercise_27k | 15,383 | Write a Python function to calculate the number of ways to reach the bottom-right corner of an `n x n` maze from the top-left corner, given that you can only move right or down. The maze is represented as a 2D grid where each cell can be reached from the cell above it or the cell to its left.
The code should start wit... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_ways_to_maze(n: int) -> int:
"""
Calculate the number of ways to reach the bottom-right corner of an n x n maze
from the top-left corner, given that you can only move right or down.
Args:
n (int): The ... |
code_exercise_27k | 15,385 | Write a Python function to count the number of words in a given sentence, considering various edge cases such as multiple spaces, leading and trailing spaces, and punctuation. The function should handle these cases gracefully and return the correct word count.
The code should start with:
```python
def count_words(sent... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_words(sentence: str) -> int:
"""
Counts the number of words in a given sentence, handling multiple spaces, leading/trailing spaces, and punctuation.
Args:
sentence (str): The sentence to count the words fr... |
code_exercise_27k | 15,387 | Write a Python function to find the longest common prefix among a list of strings. The function should return an empty string if no common prefix is found. The code should start with:
```python
from typing import List
def longest_common_prefix(strings: 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(strings: List[str]) -> str:
"""
Finds the longest common prefix among a list of strings.
Args:
strings (List[str]): A list of strings to check.
Returns:
... |
code_exercise_27k | 15,388 | Write a Python function to find the length of the longest common subsequence (LCS) between two given strings. The function should use dynamic programming to efficiently compute the result. The code should start with:
```python
def longest_common_subsequence_length(string1: str, string2: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_subsequence_length(string1: str, string2: str) -> int:
"""
Calculates the length of the longest common subsequence between two strings.
Args:
string1 (str): The first input string.
string2... |
code_exercise_27k | 15,393 | Write a Python function to determine if a given string can be formed by concatenating words from a provided list of words. The function should use dynamic programming to efficiently check for possible concatenations.
The code should start with:
```python
from typing import List
def can_form_string(string: str, words:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def can_form_string(string: str, words: List[str]) -> bool:
"""
Determines if a given string can be formed by concatenating words from a provided list of words.
Args:
string (str): The input... |
code_exercise_27k | 15,397 | Write a Python function to filter out prime numbers from a given list of integers. The function should return a new list containing only the prime numbers found in the input list. The code should start with:
```python
from typing import List
def filter_primes(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_primes(numbers: List[int]) -> List[int]:
"""
Filters out prime numbers from a given list of integers.
Args:
numbers (List[int]): The list of integers to filter.
Returns:
... |
code_exercise_27k | 15,398 | Write a Python function to create a checkerboard pattern NumPy array of size `n x m`, where `n` is the number of rows and `m` is the number of columns. The checkerboard pattern should alternate between 1 and 0, starting with 1 in the top-left corner. The function should handle cases where `n` or `m` is 0, returning an ... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_checkerboard(n: int, m: int) -> np.ndarray:
"""
Create a checkerboard pattern NumPy array of size n x m.
Parameters:
- n (int): Number of rows in the array.
- m (int): Number of column... |
code_exercise_27k | 15,399 | Write a Python function to calculate the square root of a given number without using any built-in functions or libraries. The function should use an iterative method to approximate the square root with a specified level of accuracy.
The code should start with:
```python
def calculate_sqrt(x: float, epsilon: float = 0.... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_sqrt(x: float, epsilon: float = 0.00001) -> float:
"""
Calculate the square root of a given number using an iterative method without built-in functions or libraries.
Args:
x (float): The number for whi... |
code_exercise_27k | 15,401 | Write a Python function to count the number of occurrences of each unique element in a given list. The function should return a dictionary where the keys are the unique elements from the list and the values are the counts of those elements.
The code should start with:
```python
from typing import List, Dict
def count... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_element_occurrences(lst: List) -> Dict[int, int]:
"""
Counts the number of occurrences of each unique element in a list.
Args:
lst (List): The list in which to count occurren... |
code_exercise_27k | 15,402 | Write a Python function to compute the median of each row in a given NumPy array. The function should handle arrays of any shape and return a NumPy array containing the median of each row. The code should start with:
```python
import numpy as np
def compute_median_of_rows(arr: np.ndarray) -> np.ndarray:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def compute_median_of_rows(arr: np.ndarray) -> np.ndarray:
"""
Compute the median of each row in a given NumPy array.
Parameters:
- arr (numpy.ndarray): Input array.
Returns:
- numpy.ndarray... |
code_exercise_27k | 15,404 | Write a Python function to check if a given string is a palindrome. The function should ignore non-alphanumeric characters and be case-insensitive. The code should start with:
```python
def is_palindrome(string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(string: str) -> bool:
"""
Check if a given string is a palindrome, ignoring non-alphanumeric characters and case.
A palindrome is a word, phrase, number, or other sequence of characters that reads the same... |
code_exercise_27k | 15,405 | Write a Python function to compute the mean of each row in a given NumPy array. The function should take a 2-dimensional NumPy array as input and return a 1-dimensional NumPy array containing the mean of each row. The code should start with:
```python
import numpy as np
def compute_row_means(arr: np.ndarray) -> np.nda... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def compute_row_means(arr: np.ndarray) -> np.ndarray:
"""
Compute the mean of each row in a given NumPy array.
Parameters:
- arr (np.ndarray): The 2-dimensional NumPy array for which row means need t... |
code_exercise_27k | 15,408 | Write a Python function to find the maximum sum of a subarray with no adjacent elements in a given array. The function should take a list of integers as input and return the maximum sum. The subarray should not contain any adjacent elements.
The code should start with:
```python
from typing import List
def max_subarr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum_no_adjacent(arr: List[int]) -> int:
"""
Find the maximum sum of a subarray with no adjacent elements.
Args:
arr (List[int]): The input array of integers.
Returns:
... |
code_exercise_27k | 15,409 | Write a Python function to find the maximum element in a given NumPy array. The function should handle empty arrays by raising a `ValueError`. The code should start with:
```python
import numpy as np
def find_max_element(arr: np.ndarray) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def find_max_element(arr: np.ndarray) -> float:
"""
Find the maximum element in a given NumPy array.
Parameters:
arr (numpy.ndarray): The input array.
Returns:
float: The maximum ele... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.