source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 11,915 | Write a Python function to find the longest increasing subsequence (LIS) of a given list of integers. The LIS is a subsequence where each element is strictly greater than the previous element. The function should return the length of the LIS.
The code should start with:
```python
from typing import List
def longest_i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence(nums: List[int]) -> int:
"""
Calculate the length of the longest increasing subsequence (LIS) in a given list of integers.
Args:
nums (List[int]): The list... |
code_exercise_27k | 11,917 | Write a Python function to check if a number is a perfect palindrome. A perfect palindrome is a number that reads the same backward as forward. The function should take an integer as input and return a boolean indicating whether the number is a perfect palindrome.
The code should start with:
```python
def is_perfect_p... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_palindrome(num: int) -> bool:
"""
Check if a given number is a perfect palindrome.
A perfect palindrome is a number that reads the same backward as forward.
Args:
num (int): The number to check.
... |
code_exercise_27k | 11,925 | Write a Python function to calculate the number of ways to reach the n-th step in a staircase using only single or double steps. The function should use dynamic programming to efficiently compute the result.
The code should start with:
```python
def count_ways_to_climb_stairs(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_ways_to_climb_stairs(n: int) -> int:
"""
Calculate the number of ways to reach the n-th step in a staircase using only single or double steps.
Args:
n (int): The step number to reach.
Returns:
... |
code_exercise_27k | 11,929 | Write a Python function to find the most frequent element in a given list of integers. The function should return the element that appears most frequently in the list. If there is a tie for the most frequent element, the function should return the element that appears first in the list.
The code should start with:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
from collections import defaultdict
def most_frequent_element(lst: List[int]) -> Optional[int]:
"""
Find the most frequent element in a given list of integers.
Args:
lst (List[int]... |
code_exercise_27k | 11,932 | Write a Python function to find the sum of all divisors of a given number. The function should be efficient and handle both positive and negative integers. The code should start with:
```python
def sum_of_divisors(num: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_divisors(num: int) -> int:
"""
Calculate the sum of all divisors of a given number.
Args:
num (int): The number for which the sum of divisors is to be calculated.
Returns:
int: The sum of all... |
code_exercise_27k | 11,937 | Write a Python function to calculate the Euler's totient function of a given number. The Euler's totient function, denoted as φ(n), counts the number of integers up to n that are coprime with n. Two numbers are coprime if their greatest common divisor (gcd) is 1.
The code should start with:
```python
import math
from ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
from typing import Set
def euler_totient(n: int) -> int:
"""
Calculate the Euler's totient function of a given number n.
The Euler's totient function, φ(n), counts the number of integers up to n
that are co... |
code_exercise_27k | 11,938 | Create a Python decorator that logs the execution time of a function and prints it along with the function name. The decorator should be reusable and should not modify the original function's behavior except for logging the execution time. The code should start with:
```python
import time
from functools import wraps
... | Here is the Python code to solve the given problem in the markdown block:
```python
import time
from functools import wraps
def log_execution_time(func):
"""
Decorator that logs the execution time of a function.
This decorator measures the time taken to execute the wrapped function
and prints the func... |
code_exercise_27k | 11,939 | Write a Python function to find the longest palindromic substring in a given string. A palindromic substring is a substring that reads the same forwards and backwards. The function should return the longest such substring found in the input string. If there are multiple palindromic substrings of the same maximum length... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_palindromic_substring(s: str) -> str:
"""
Find the longest palindromic substring in the given string.
Args:
s (str): The input string to search for the longest palindromic substring.
Returns:
... |
code_exercise_27k | 11,943 | Write a Python function to check if a given expression has balanced brackets, braces, and parentheses. The function should ensure that each opening bracket, brace, or parenthesis has a corresponding closing one and that they are properly nested.
The code should start with:
```python
def is_balanced(expression: str) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_balanced(expression: str) -> bool:
"""
Check if a given expression has balanced brackets, braces, and parentheses.
Args:
expression (str): The expression to be checked.
Returns:
bool: True if the... |
code_exercise_27k | 12,021 | Create a Python decorator that validates the return value of a function after execution. The decorator should take the expected type as an argument and raise a `TypeError` if the return value is not of the expected type. The decorator should be flexible enough to work with any function and any expected return type.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def validate_return_value(expected_type):
"""
A decorator that validates the return value of a function after execution.
Args:
expected_type (type): The expected type of the return value... |
code_exercise_27k | 12,026 | Write a Python decorator that enables or disables caching for a specific function based on a boolean flag. The decorator should cache the function's result when enabled and bypass caching when disabled. The decorator should be flexible enough to be used both with and without arguments.
The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def enable_cache(func=None, *, enabled=True):
"""
Decorator to enable or disable caching for a specific function.
The `enable_cache` decorator can be used to enable or disable caching for a spec... |
code_exercise_27k | 12,031 | Write a Python function to calculate the number of ways to place N queens on an NxN chessboard so that no two queens threaten each other. The function should use a backtracking approach to explore all possible arrangements of queens on the board. The code should start with:
```python
from typing import List
def count_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_valid(board: List[List[bool]], row: int, col: int) -> bool:
"""
Helper function to check if the current position (row, col) on the board is valid.
It checks if the queen at position (row, col)... |
code_exercise_27k | 12,036 | Write a Python function to filter out the even numbers from a given list of integers. The function should return a new list containing only the odd numbers from the input list. The code should start with:
```python
from typing import List
def filter_even_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_even_numbers(numbers: List[int]) -> List[int]:
"""
Filters out the even numbers from a given list of integers.
Args:
numbers (List[int]): A list of integers to be filtered.
R... |
code_exercise_27k | 12,039 | Write a Python decorator that adds a specified prefix and suffix to the return value of a function. The decorator should be flexible enough to handle functions that return different types of values, such as strings, numbers, or lists. The code should start with:
```python
def add_prefix_suffix(prefix: str, suffix: str)... | Here is the Python code to solve the given problem in the markdown block:
```python
def add_prefix_suffix(prefix: str, suffix: str) -> callable:
"""
A decorator function that adds a specified prefix and suffix to the return value of a function.
Args:
prefix (str): The prefix to be added.
su... |
code_exercise_27k | 12,044 | Write a Python function to filter out people who are above a specified age from a given list of tuples containing a name and age. The function should return a list of tuples containing the name and age of people who are above the specified age.
The code should start with:
```python
from typing import List, Tuple
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def filter_people_above_age(people: List[Tuple[str, int]], age_threshold: int) -> List[Tuple[str, int]]:
"""
Filter out people who are above a specified age threshold.
Args:
people (L... |
code_exercise_27k | 12,048 | 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 from the original list. The code should start with:
```python
from typing import List
def filter_prime_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 is_prime(n: int) -> bool:
"""
Check if a number is prime.
Parameters:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
# Number... |
code_exercise_27k | 12,052 | Write a Python function to filter out words from a given list that start with a vowel. The function should return a new list containing only the words that start with a vowel. The function should be case-insensitive, meaning it should consider both uppercase and lowercase vowels.
The code should start with:
```python
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_vowel_words(words: List[str]) -> List[str]:
"""
Filter out words from a list that start with a vowel.
Args:
words (List[str]): A list of words to filter.
Returns:
Lis... |
code_exercise_27k | 12,059 | Write a Python function to filter out strings with more than 5 characters from a given list of strings. The function should return a new list containing only the strings with 5 or fewer characters. The code should start with:
```python
from typing import List
def filter_short_strings(strings: List[str]) -> List[str]:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_short_strings(strings: List[str]) -> List[str]:
"""
Filters out strings with more than 5 characters from the given list.
Args:
strings (List[str]): A list of strings to be filtere... |
code_exercise_27k | 12,060 | Write a Python function to filter out people from a list of tuples where each tuple contains a name and an age. The function should return a list of tuples containing only those people whose names start with the letter 'A' and whose age is below 30.
The code should start with:
```python
from typing import List, Tuple
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def filter_people_by_criteria(data: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Filters out people from a list of tuples based on specific criteria.
Args:
data (List[Tuple[st... |
code_exercise_27k | 12,069 | Write a Python function to calculate the average of a list of numbers using a for loop. The function should handle an empty list by returning `None` instead of attempting to divide by zero. The code should start with:
```python
from typing import List, Optional
def calculate_average(numbers: List[int]) -> Optional[flo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def calculate_average(numbers: List[int]) -> Optional[float]:
"""
Calculate the average of a list of numbers.
Args:
numbers (List[int]): A list of integers.
Returns:
O... |
code_exercise_27k | 12,076 | Create a Python decorator that enforces specific constraints on the return value of a function. The decorator should take a constraint function as an argument, which will be used to validate the return value of the decorated function. If the return value does not satisfy the constraint, the decorator should raise a `Va... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def enforce_constraint(constraint):
"""
A decorator that enforces specific constraints on the return value of a function.
Args:
constraint (function): A function that takes the return va... |
code_exercise_27k | 12,079 | Write a Python function to filter out people with ages that are prime numbers from a given list of tuples. Each tuple contains a name and an age. The function should return a new list of tuples containing only people with non-prime ages.
The code should start with:
```python
from typing import List, Tuple
def filter_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
code_exercise_27k | 12,086 | Write a Python function to find the largest prime number up to a given limit. The function should efficiently determine if a number is prime and then find the largest prime number within the specified range. The code should start with:
```python
def find_largest_prime(limit: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(n: int) -> bool:
"""Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
if n < 2:
return False
f... |
code_exercise_27k | 12,090 | Write a Python function to convert a decimal number to its binary representation using a for loop and range. The function should handle both positive and negative decimal numbers, and the binary representation should be returned as a string. For negative numbers, the function should return the two's complement binary r... | Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(decimal: int) -> str:
"""
Convert a decimal number to its binary representation.
For positive numbers, the binary representation is returned directly.
For negative numbers, the two's complement bin... |
code_exercise_27k | 12,119 | Write a Python function to determine if a given number is a prime number or not. The function should return `True` if the number is prime and `False` otherwise. The function should be efficient and handle edge cases such as negative numbers, 0, and 1.
The code should start with:
```python
def is_prime(number: int) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(number: int) -> bool:
"""
Determine if a given number is a prime number.
Args:
number (int): The number to be checked.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
code_exercise_27k | 12,121 | Create a Python decorator that measures the number of cache hits and misses for a function and prints the result. The decorator should maintain a cache to store the results of function calls and track how often the cache is used. The decorator should also print the total number of cache hits and misses after the functi... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def cache_hits_and_misses(func):
"""
This decorator measures the number of cache hits and misses for a function and prints the result.
Args:
func: The function to be decorated.
Retu... |
code_exercise_27k | 12,126 | 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:
"""
Calculates the factorial of a given non-negative integer.
The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
The factorial of 0 i... |
code_exercise_27k | 12,133 | Write a Python function to reverse the digits of a given number. The function should handle both positive and negative numbers. If the input number is negative, the function should reverse the digits but keep the negative sign at the beginning of the number. The function should return the reversed number as an integer.... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_number(number: int) -> int:
"""
Reverses the digits of a given number, keeping the negative sign if the number is negative.
Args:
number (int): The number to be reversed.
Returns:
int: The r... |
code_exercise_27k | 12,140 | Write a Python function to find the greatest common divisor (GCD) of two integers using the Euclidean algorithm. The function should be able to handle both positive and negative integers. The code should start with:
```python
def gcd(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Finds the greatest common divisor (GCD) of two integers using the Euclidean algorithm.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
... |
code_exercise_27k | 12,153 | 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:
"""
Calculates the sum of the digits of a given number.
Args:
number (int): The number for which the sum of the digits is to be calculated.
Returns:
int: The sum o... |
code_exercise_27k | 12,162 | Write a Python function to filter out strings from a given list that contain both uppercase and lowercase letters. The function should return a new list containing only those strings that meet the criteria. The code should start with:
```python
from typing import List
def filter_mixed_case_strings(strings: List[str]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_mixed_case_strings(strings: List[str]) -> List[str]:
"""
Filters out the strings that contain both uppercase and lowercase letters.
Args:
strings (List[str]): A list of strings to... |
code_exercise_27k | 12,169 | Write a Python function to compute the value of Euler's number (e) to a given number of decimal places. The function should ensure accurate rounding and return the value as a float. The code should start with:
```python
import math
def calculate_euler(decimal_places: int) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_euler(decimal_places: int) -> float:
"""
Calculate the value of Euler's number (e) to a given number of decimal places.
Args:
decimal_places (int): The number of decimal places to which e ... |
code_exercise_27k | 12,178 | Write a Python function to calculate the factorial of a non-negative integer using an iterative approach. The function should handle the case where the input is a negative integer by raising a `ValueError`. The code should start with:
```python
def factorial(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculate the factorial of a non-negative integer using an iterative approach.
Parameters:
n (int): The non-negative integer to calculate the factorial of.
Returns:
int: The fa... |
code_exercise_27k | 12,179 | Write a Python function to find the greatest common divisor (GCD) of two given numbers using the Euclidean algorithm. The function should handle input validation to ensure both numbers are positive integers. If either number is not a positive integer, the function should raise a `ValueError`.
The code should start wit... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two given positive integers using the Euclidean algorithm.
Args:
a (int): The first positive integer.
b (int): The second po... |
code_exercise_27k | 12,184 | Write a Python function to generate the Fibonacci sequence up to a given term `n`. The function should return a list containing the Fibonacci sequence up to the `n`th term. If `n` is less than 1, the function should return an empty list.
The code should start with:
```python
from typing import List
def generate_fibon... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci_sequence(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the 'n'th term.
Args:
n (int): The number of terms in the Fibonacci sequence to generate.
... |
code_exercise_27k | 12,194 | Write a Python function to determine if a given number is a prime number. The function should return `True` if the number is prime and `False` otherwise. The function should handle edge cases such as negative numbers, 0, and 1, which are not prime numbers. The function should also be efficient by only checking divisibi... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(number: int) -> bool:
"""
Determine if a given number is a prime number.
Args:
number (int): The number to be checked.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
code_exercise_27k | 12,198 | Write a Python function to filter out people whose names have a repeated character and ages are perfect squares from a given list of tuples. Each tuple contains a name (string) and an age (integer). The function should return a list of tuples that meet the criteria.
The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import math
def filter_people(people: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Filters out people whose names have a repeated character and ages are perfect squares.
Args:
... |
code_exercise_27k | 12,210 | Write a Python function to calculate the natural logarithm (base e) of a given number to a specified decimal place. The function should handle input validation and ensure that the number is positive and the decimal place is a non-negative integer. The code should start with:
```python
import math
def calculate_ln(numb... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_ln(number: float, decimal_place: int) -> float:
"""
Calculates the natural logarithm (base e) of a given number to a specified decimal place.
Args:
number (float): The number for which the... |
code_exercise_27k | 12,217 | Write a Python function to implement the exponentiation of a base to an exponent without using the built-in exponentiation operator (`**`). The function should handle both positive and negative exponents, as well as the special case where the base is 0 and the exponent is negative. The code should start with:
```python... | Here is the Python code to solve the given problem in the markdown block:
```python
def exponentiation(base: float, exponent: int) -> float:
"""
Calculate the result of raising the base to the exponent without using the built-in exponentiation operator.
Args:
base (float): The base number.
... |
code_exercise_27k | 12,220 | Write a Python function to compute the binomial coefficient given two positive integers `n` and `k`. The binomial coefficient, denoted as `C(n, k)`, represents the number of ways to choose `k` items from `n` items without regard to the order of selection. The function should use dynamic programming to efficiently compu... | Here is the Python code to solve the given problem in the markdown block:
```python
def binomial_coefficient(n: int, k: int) -> int:
"""
Compute the binomial coefficient C(n, k) using dynamic programming.
Parameters:
n (int): A positive integer representing the total number of items.
k (int): A pos... |
code_exercise_27k | 12,223 | Write a Python function to generate the Collatz sequence for a given positive integer. The Collatz sequence is defined as follows: start with any positive integer `n`. If `n` is even, the next number in the sequence is `n // 2`. If `n` is odd, the next number is `3 * n + 1`. Repeat the process until `n` becomes 1. The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_collatz_sequence(n: int) -> List[int]:
"""
Generates the Collatz sequence for a given positive integer.
The Collatz sequence is defined as follows:
- Start with any positive integer... |
code_exercise_27k | 12,225 | Write a Python function to filter out numbers from a given list of integers whose digits are in non-decreasing order. A number has non-decreasing digits if each digit is less than or equal to the digit that follows it. The function should return a new list containing only the numbers that meet this criterion.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_non_decreasing_digits(numbers: List[int]) -> List[int]:
"""
Filters out numbers from a list that have non-decreasing digits.
Args:
numbers (List[int]): A list of integers.
Re... |
code_exercise_27k | 12,227 | Write a Python function to filter out strings from a given list that do not contain any consonants. A consonant is any letter of the alphabet that is not a vowel (i.e., 'a', 'e', 'i', 'o', 'u' and their uppercase counterparts). The function should return a new list containing only the strings that have at least one con... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_with_consonants(strings: List[str]) -> List[str]:
"""
Filters out strings from a given list that do not contain any consonants.
Args:
strings (List[str]): A list of string... |
code_exercise_27k | 12,232 | 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 di... |
code_exercise_27k | 12,241 | Write a Python function to filter out words from a given list that contain both palindromes and anagrams of their reverse. A palindrome is a word that reads the same backward as forward, and an anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_words_with_palindrome_and_anagram_of_reverse(words: List[str]) -> List[str]:
"""
Filter out the words that contain both palindromes and anagrams of their reverse.
Args:
words (Lis... |
code_exercise_27k | 12,246 | Write a Python function to filter out strings from a given list that do not contain any digits. The function should return a new list containing only the strings from the original list that contain at least one digit. The code should start with:
```python
from typing import List
def filter_strings_with_digits(string_l... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_with_digits(string_list: List[str]) -> List[str]:
"""
Filters out strings from a list that do not contain any digits.
Args:
string_list (List[str]): A list of strings to b... |
code_exercise_27k | 12,261 | Write a Python function to implement the bubble sort algorithm to sort a list of numbers in ascending order. The function should repeatedly step through the list, compare adjacent elements, and swap them if they are in the wrong order. The process should be repeated until the list is sorted. 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 bubble_sort(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers using the Bubble Sort algorithm.
Bubble sort compares adjacent pairs of elements and swaps them if they are in the wrong... |
code_exercise_27k | 12,263 | Create a Python decorator function that logs the execution time of a wrapped function and prints it after the function call. The decorator should also print the name of the wrapped function before calling it. The code should start with:
```python
from functools import wraps
import time
def log_execution_time(func):
``... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
import time
def log_execution_time(func):
"""
This decorator function logs the execution time of the wrapped function and prints it after the function call.
It also prints the name of the wrapped... |
code_exercise_27k | 12,267 | Create a Python decorator function that allows a wrapped function to run only if a specific condition is met. The decorator should take a condition function as an argument, which returns a boolean value. If the condition is met, the wrapped function should execute and return its result. If the condition is not met, the... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def run_if(condition):
"""
Decorator function to allow a wrapped function to run only if a specific condition is met.
Args:
condition (function): A function that returns a boolean value.... |
code_exercise_27k | 12,270 | Write a Python function to generate the first `n` triangular numbers. A triangular number is defined as the sum of the natural numbers from 1 to `n`. The function should return a list of these triangular numbers.
The code should start with:
```python
from typing import List
def generate_triangular_numbers(n: int) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_triangular_numbers(n: int) -> List[int]:
"""
Generate the first n triangular numbers.
A triangular number is the sum of the first n natural numbers.
The nth triangular number can be... |
code_exercise_27k | 12,281 | Write a Python function to calculate the power set of a given set. The power set of a set is the set of all possible subsets, including the empty set and the set itself. The function should return a set of frozensets, where each frozenset represents a subset of the input set. 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 Set
def calculate_power_set(s: Set[int]) -> Set[frozenset]:
"""
Calculate the power set of a given set.
The power set of a set is the set of all possible subsets, including the empty set and the set itse... |
code_exercise_27k | 12,285 | Write a Python function to generate a list of prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. The Sieve of Eratosthenes is an efficient algorithm to find all prime numbers up to a specified integer. The function should start with:
```python
from typing import List
def sieve_of_eratosthenes... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sieve_of_eratosthenes(limit: int) -> List[int]:
"""
Generate a list of prime numbers up to a given limit using the Sieve of Eratosthenes algorithm.
Args:
limit (int): The upper limit for... |
code_exercise_27k | 12,289 | Write a Python function to filter out strings from a given list that do not contain any alphabetic characters. The function should return a new list containing only the strings that have at least one alphabetic character.
The code should start with:
```python
from typing import List
def filter_alphabetic_strings(str_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_alphabetic_strings(str_list: List[str]) -> List[str]:
"""
Filters out strings from a given list that do not contain any alphabetic characters.
Args:
str_list (List[str]): A list o... |
code_exercise_27k | 12,290 | Implement the merge sort algorithm to sort a list of numbers. The function should be able to handle lists of integers and floating-point numbers. The code should start with:
```python
from typing import List
def merge_sort(lst: List[float]) -> List[float]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(lst: List[float]) -> List[float]:
"""
Sorts a list of numbers using the merge sort algorithm.
Args:
lst (List[float]): List of numbers to be sorted.
Returns:
List... |
code_exercise_27k | 12,298 | Write a Python function to find all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. The function should return a list of prime numbers. The code should start with:
```python
from typing import List
def find_primes_up_to(limit: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_primes_up_to(limit: int) -> List[int]:
"""
Find all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm.
Args:
limit (int): The upper limit until which to fi... |
code_exercise_27k | 12,300 | Write a Python function to calculate the factorial of a large number using recursion and memoization. The function should be efficient and avoid redundant calculations by storing previously computed factorial values in a dictionary. The code should start with:
```python
def factorial_memoized(n: int, memo: dict = {}) -... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_memoized(n: int, memo: dict = {}) -> int:
"""
Calculate the factorial of a large number using recursion and memoization.
Args:
n (int): The number to calculate the factorial for.
memo (dict, op... |
code_exercise_27k | 12,304 | Create a Python decorator function that runs the wrapped function only if certain conditions on the arguments are met. The decorator should accept a condition function that takes the arguments of the wrapped function and returns a boolean value indicating whether the conditions are met. If the conditions are met, the w... | Here is the Python code to solve the given problem in the markdown block:
```python
from functools import wraps
def run_if(condition_func):
"""
Decorator that runs the wrapped function only if the specified condition on its arguments is met.
Args:
condition_func (callable): A function that takes t... |
code_exercise_27k | 12,307 | Write a Python function to calculate the sum of all even Fibonacci numbers up to a given limit. The function should efficiently compute the sum using an iterative approach. The code should start with:
```python
def sum_even_fibonacci(limit: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_even_fibonacci(limit: int) -> int:
"""
Calculate the sum of all even Fibonacci numbers up to a given limit.
Args:
limit (int): The upper limit for Fibonacci numbers.
Returns:
int: The sum of all... |
code_exercise_27k | 12,308 | Write a Python function to filter out strings from a given list that do not contain any whitespace characters. The function should return a new list containing only the strings that contain at least one whitespace character. The code should start with:
```python
from typing import List
def filter_strings_with_whitespa... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_with_whitespace(strings: List[str]) -> List[str]:
"""
Filters out the strings from a list that do not contain any whitespace characters.
Args:
strings: A list of strings.
... |
code_exercise_27k | 12,317 | Write a Python function to perform matrix addition. The function should take two matrices as input and return their sum. The matrices are represented as lists of lists, where each inner list represents a row of the matrix. The function should raise a `ValueError` if the matrices are not of the same size.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def add_matrices(matrix1: List[List[int]], matrix2: List[List[int]]) -> List[List[int]]:
"""
Perform matrix addition.
This function takes two matrices as input and returns their sum.
Args:
... |
code_exercise_27k | 12,320 | Write a Python function to convert a decimal number to its octal representation. The function should handle both positive and zero decimal numbers. The code should start with:
```python
def decimal_to_octal(decimal: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_octal(decimal: int) -> str:
"""
Converts a decimal number to its octal representation.
Args:
decimal (int): The decimal number to be converted.
Returns:
str: The octal representation of t... |
code_exercise_27k | 12,326 | Write a Python function to filter out strings from a given list that do not contain any non-alphanumeric characters. A non-alphanumeric character is any character that is not a letter (either uppercase or lowercase) or a digit. The function should return a new list containing only the strings that have at least one non... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_with_non_alphanumeric(strings: List[str]) -> List[str]:
"""
Filters out strings from a list that do not contain any non-alphanumeric characters.
Args:
strings (List[str]):... |
code_exercise_27k | 12,331 | Write a Python function to perform binary search on a sorted list to find the index of a target element. If the target element is found, return its index; otherwise, return -1. The function should handle edge cases such as an empty list or a target element that is not present in the list.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def binary_search(arr: List[Any], target: Any) -> int:
"""
Performs binary search to find the index of a target element in a sorted list.
Args:
arr (List[Any]): The sorted list to searc... |
code_exercise_27k | 12,334 | Write a Python function to calculate the sum of all multiples of 3 and 5 below a given number. The function should handle the input validation and ensure that the target number is a positive integer. The code should start with:
```python
def sum_of_multiples(target: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_multiples(target: int) -> int:
"""
Calculate the sum of all multiples of 3 and 5 below a given number.
Args:
target (int): The upper limit number.
Returns:
int: The sum of the multiples of 3 ... |
code_exercise_27k | 12,337 | Write a Python function to calculate the sum of the digits of the factorial of a given non-negative integer. The function should handle the calculation efficiently, especially for larger values of `n`. The code should start with:
```python
def factorial_digit_sum(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_digit_sum(n: int) -> int:
"""
Calculate the sum of the digits of the factorial of a given non-negative integer.
Args:
n (int): The non-negative integer for which the factorial digit sum needs to be cal... |
code_exercise_27k | 12,338 | Create a Python decorator function that modifies the result of a wrapped function by adding a prefix to it. The decorator should accept a prefix string as an argument and append it to the beginning of the result returned by the wrapped function. The code should start with:
```python
def add_prefix(prefix: str):
""... | Here is the Python code to solve the given problem in the markdown block:
```python
def add_prefix(prefix: str):
"""
Decorator function that adds a prefix to the result of a wrapped function.
Args:
prefix (str): The prefix to be added to the result.
Returns:
function: The decorated fun... |
code_exercise_27k | 12,339 | Write a Python function to determine if a given number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding the number itself). The function should return `True` if the number is perfect and `False` otherwise. The code should start with:
```python
def is... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_number(number: int) -> bool:
"""
Determine if a given number is a perfect number.
A perfect number is a positive integer that is equal to the sum of its proper divisors.
Proper divisors of a number are po... |
code_exercise_27k | 12,341 | Write a Python function to filter out strings from a given list that do not contain any printable characters. A printable character is defined as any character that is not a control character (i.e., it has a visible representation). The function should return a new list containing only the strings that have at least on... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_printable_strings(strings: List[str]) -> List[str]:
"""
Filter out strings that do not contain any printable characters.
Args:
strings (List[str]): A list of strings to be filtere... |
code_exercise_27k | 12,343 | Write a Python function to generate the hailstone sequence for a given positive integer. The hailstone sequence is defined as follows:
- Start with a positive integer `n`.
- If `n` is even, the next number in the sequence is `n // 2`.
- If `n` is odd, the next number in the sequence is `3 * n + 1`.
- Repeat the process... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_hailstone_sequence(n: int) -> List[int]:
"""
Generate the hailstone sequence for a given positive integer.
The hailstone sequence is generated by the following rules:
- If the curre... |
code_exercise_27k | 12,351 | Write a Python function to determine if a given number is a palindrome. A palindrome is a number that reads the same backward as forward. The function should handle both positive and negative integers. The code should start with:
```python
def is_palindrome_number(number: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome_number(number: int) -> bool:
"""
Determine if the given number is a palindrome.
A palindrome is a number that reads the same backward as forward.
Args:
number (int): The number to check.
... |
code_exercise_27k | 12,356 | Write a Python function to generate the first n Catalan numbers efficiently using dynamic programming. The Catalan numbers are a sequence of natural numbers that appear in various combinatorial problems. The nth Catalan number can be computed using the formula:
\[ C_n = \sum_{i=0}^{n-1} C_i \cdot C_{n-1-i} \]
where \... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_catalan_numbers(n: int) -> List[int]:
"""
Generate the first n Catalan numbers using dynamic programming.
Args:
n (int): The number of Catalan numbers to generate.
Returns:... |
code_exercise_27k | 12,357 | Implement the counting sort algorithm to sort a list of non-negative integers. The function should handle lists with elements ranging from 0 to the maximum value present in the list. The code should start with:
```python
from typing import List
def counting_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def counting_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of non-negative integers using the counting sort algorithm.
Args:
arr (List[int]): A list of non-negative integers to be sort... |
code_exercise_27k | 12,370 | Write a Python function to implement the Radix Sort algorithm to sort a list of non-negative integers. The function should handle lists containing only non-negative integers and return the sorted list in ascending order. The function should start with:
```python
from typing import List
def radix_sort(lst: List[int]) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def radix_sort(lst: List[int]) -> List[int]:
"""
Sorts a list of non-negative integers using the Radix Sort algorithm.
Parameters:
lst (List[int]): A list of non-negative integers to be sorted.
... |
code_exercise_27k | 12,371 | Write a Python function to filter out strings from a given list that do not contain any lowercase letters. The function should return a new list containing only the strings that have at least one lowercase letter. The code should start with:
```python
from typing import List
def filter_strings_with_lowercase(strings: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_with_lowercase(strings: List[str]) -> List[str]:
"""
Filters out strings from a list that do not contain any lowercase letters.
Args:
strings (List[str]): A list of string... |
code_exercise_27k | 12,377 | Write a Python function to calculate the number of trailing zeros in the factorial of a given number. The function should efficiently compute the result without explicitly calculating the factorial, which can be very large and impractical for large inputs.
The code should start with:
```python
def count_trailing_zeros... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_trailing_zeros(n: int) -> int:
"""
Calculate the number of trailing zeros in the factorial of a given number.
Args:
n (int): The input number.
Returns:
int: The number of trailing zeros in the... |
code_exercise_27k | 12,379 | Write a Python decorator function that adds a prefix and suffix to the result of a wrapped function. The decorator should be flexible enough to accept any prefix and suffix as arguments. The code should start with:
```python
def add_prefix_suffix(prefix: str, suffix: str):
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def add_prefix_suffix(prefix: str, suffix: str):
"""
A decorator function that adds a specified prefix and suffix to the result of a wrapped function.
Args:
prefix (str): The prefix to be added to the result.
... |
code_exercise_27k | 12,381 | Write a Python function to determine whether a given number is prime or composite. The function should return `True` if the number is prime and `False` if the number is composite. The function should handle edge cases such as negative numbers, 0, and 1, which are not considered prime.
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(number: int) -> bool:
"""
Determine whether a given number is prime or composite.
Args:
number (int): The number to check.
Returns:
bool: True if the number is prime, False if the number is... |
code_exercise_27k | 12,383 | Write a Python function to filter out and return a list of numbers that are divisible by a given divisor from a given list of numbers. The function should handle both positive and negative numbers, as well as zero. The code should start with:
```python
from typing import List
def get_divisible_numbers(numbers: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_divisible_numbers(numbers: List[int], divisor: int) -> List[int]:
"""
Returns a list of numbers from the given list that are divisible by the specified divisor.
Args:
numbers (List[i... |
code_exercise_27k | 12,388 | Write a Python function to generate a new list that contains the length of each word in a given list of words. The function should handle an empty list and words of varying lengths. The code should start with:
```python
from typing import List
def word_lengths(words: List[str]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def word_lengths(words: List[str]) -> List[int]:
"""
Generate a new list that contains the length of each word in the given list.
Args:
words (List[str]): A list of words.
Returns:
... |
code_exercise_27k | 12,390 | Write a Python function to generate the first n Bell numbers using an iterative approach. The Bell numbers represent the number of ways to partition a set of n elements. The function should start with:
```python
def generate_bell_numbers(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_bell_numbers(n: int) -> List[int]:
"""
Generate the first n Bell numbers using an iterative approach.
Args:
n (int): Number of Bell numbers to generate.
Returns:
Li... |
code_exercise_27k | 12,391 | Write a Python function to filter out strings starting with a vowel from a given list of strings. The function should return a new list containing only the strings that start with a vowel (case insensitive).
The code should start with:
```python
from typing import List
def filter_strings_starting_with_vowel(strings: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_starting_with_vowel(strings: List[str]) -> List[str]:
"""
Filter out strings starting with a vowel from a list of strings.
Args:
strings (List[str]): A list of strings to ... |
code_exercise_27k | 12,394 | Write a Python function to find the sum of all numbers below a given limit that are divisible by 3 or 5. The function should be efficient and handle large limits.
The code should start with:
```python
def sum_of_multiples(limit: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_multiples(limit: int) -> int:
"""
Calculate the sum of all numbers below a given limit that are divisible by 3 or 5.
Args:
limit (int): The upper limit until which the numbers are considered.
Returns... |
code_exercise_27k | 12,395 | Write a Python function to calculate the sum of the squares of the numbers in a given list. The function should take a list of integers as input and return the sum of the squares of those integers. The code should start with:
```python
from typing import List
def sum_of_squares(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_squares(numbers: List[int]) -> int:
"""
Calculate the sum of the squares of the numbers in the given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int... |
code_exercise_27k | 12,401 | Write a Python function to filter out numbers from a given list that are divisible by a specified divisor. The function should return a new list containing only the numbers that meet the divisibility condition. The code should start with:
```python
from typing import List
def get_divisible_numbers(numbers: List[int], ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_divisible_numbers(numbers: List[int], divisor: int) -> List[int]:
"""
Returns a new list containing the numbers from the input list that are divisible by the specified divisor.
Args:
... |
code_exercise_27k | 12,409 | Write a Python function to filter out words from a given list that contain a specified letter. The function should return a new list containing only the words that include the specified letter. The function should be case-insensitive, meaning it should consider both uppercase and lowercase versions of the specified let... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_words_by_letter(input_list: List[str], letter: str) -> List[str]:
"""
Filters out words from the input list that contain the specified letter.
Args:
input_list (List[str]): A list... |
code_exercise_27k | 12,411 | Write a Python function to reverse a given list of numbers without using slicing or any built-in reverse functions. The function should create a new list that contains the numbers in reverse order by iterating through the original list from the end to the beginning.
The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list_manual(numbers: List[int]) -> List[int]:
"""
Returns a new list containing the numbers in reverse order without using slicing or built-in reverse functions.
Args:
numbers (L... |
code_exercise_27k | 12,413 | Write a Python function to generate the first `n` prime palindromes. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A palindrome is a number that reads the same backward as forward. The function should start with:
```python
from typing import List
def generate_... | 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 a number is prime.
Args:
num (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
if... |
code_exercise_27k | 12,416 | Write a Python function to convert a list of words to a new list that contains the words in uppercase, while preserving the original list. The function should handle an empty list gracefully and ensure that each word in the input list is a string. The code should start with:
```python
from typing import List
def conve... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_to_uppercase(words: List[str]) -> List[str]:
"""
Convert a list of words to uppercase and return a new list containing the uppercase versions of the words.
Args:
words (List[str]... |
code_exercise_27k | 12,417 | Write a Python function to calculate the nth term of an arithmetic progression given the first term, common difference, and n. The function should handle both integer and floating-point inputs for the first term and common difference. The code should start with:
```python
def nth_term_of_ap(first_term: float, common_di... | Here is the Python code to solve the given problem in the markdown block:
```python
def nth_term_of_ap(first_term: float, common_difference: float, n: int) -> float:
"""
Calculate the nth term of an arithmetic progression.
Args:
first_term (float): The first term of the arithmetic progression.
... |
code_exercise_27k | 12,419 | Write a Python function to calculate the square roots of numbers in a given list and return a new list containing these square roots. The function should handle the following cases:
1. If the input is not a list, raise a `TypeError`.
2. If any element in the list is negative, raise a `ValueError`.
3. The function shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
from typing import List
def calculate_square_roots(numbers: List[int]) -> List[float]:
"""
Calculate the square roots of numbers in a given list and return a new list containing these square roots.
Args:
... |
code_exercise_27k | 12,423 | Write a Python function to reverse each string in a given list of strings and return a new list containing the reversed strings. The function should handle lists of varying lengths and strings of varying lengths. The code should start with:
```python
from typing import List
def reverse_strings(strings: List[str]) -> L... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def reverse_strings(strings: List[str]) -> List[str]:
"""
Reverses each string in the given list of strings and returns a new list containing the reversed strings.
Args:
strings (List[str]... |
code_exercise_27k | 12,424 | Write a Python function to filter out and return only the positive numbers from a given list of integers. The function should handle an empty list and return an empty list in such cases. The function should also handle lists with all negative numbers and return an empty list. The code should start with:
```python
from ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_positive_numbers(numbers: List[int]) -> List[int]:
"""
Filters out and returns only the positive numbers from the given list of integers.
Args:
numbers (List[int]): A list of inte... |
code_exercise_27k | 12,429 | Write a Python function to calculate the value of the Euler totient function (phi function) for a given positive integer `n`. The Euler totient function, denoted as φ(n), counts the number of integers up to `n` that are coprime with `n`. Two numbers are coprime if their greatest common divisor (gcd) is 1.
The code sho... | Here is the Python code to solve the given problem in the markdown block:
```python
def euler_totient_function(n: int) -> int:
"""
Calculate the Euler's totient function (phi function) for a given positive integer.
The Euler's totient function, φ(n), counts the number of integers up to n that are coprime w... |
code_exercise_27k | 12,433 | Write a Python function to create a new list that contains the numbers from the original list multiplied by a specific factor. The function should handle both integer and floating-point numbers in the original list and the factor. The code should start with:
```python
from typing import List, Union
def multiply_list(o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def multiply_list(original_list: List[Union[int, float]], factor: Union[int, float]) -> List[Union[int, float]]:
"""
Multiply each number in the original list by the given factor.
Args:
... |
code_exercise_27k | 12,434 | Write a Python function to generate the first `n` ugly numbers. An ugly number is defined as a positive number whose prime factors are only 2, 3, or 5. The sequence of ugly numbers starts with 1, and every subsequent number in the sequence is formed by multiplying an existing ugly number by either 2, 3, or 5.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_ugly_numbers(n: int) -> List[int]:
"""
Generate the first n ugly numbers.
An ugly number is defined as a positive number whose prime factors are only 2, 3, or 5.
The sequence of ugl... |
code_exercise_27k | 12,443 | Write a Python decorator function that restricts the number of times a wrapped function can be called based on user roles and specific conditions. The decorator should take three parameters: `role` (a string representing the user role), `max_calls` (an integer representing the maximum number of times the function can b... | Here is the Python code to solve the given problem in the markdown block:
```python
def restrict_call_count(role: str, max_calls: int, condition=None) -> callable:
"""
Decorator that restricts the number of times a wrapped function can be called based on user roles and conditions.
Args:
role (str):... |
code_exercise_27k | 12,445 | Implement the comb sort algorithm to sort a list of numbers. The comb sort algorithm is an improvement over the bubble sort algorithm. It works by comparing elements that are a certain distance apart (called the "gap") and then reducing this gap over multiple passes until the list is sorted.
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 comb_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of numbers using the comb sort algorithm.
The comb sort algorithm is an improvement over the bubble sort algorithm. It works by comparing... |
code_exercise_27k | 12,449 | Write a Python function to create a new list that contains the numbers squared only if they are even. The function should take a list of integers as input and return a new list with the squared values of the even numbers from the input list. The function should handle the case where the input is not a list by raising a... | 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]:
"""
Squares even numbers from a given list and returns a new list.
Args:
numbers (List[int]): A list of integers.
Returns:
... |
code_exercise_27k | 12,456 | Write a Python function to generate the first `n` triangular numbers that are also square numbers. A triangular number is a number that can be represented as the sum of the first `k` natural numbers, and a square number is a number that is the square of an integer. The function should start with:
```python
def generate... | Here is the Python code to solve the given problem in the markdown block:
```python
def generate_triangular_square_numbers(n: int) -> list:
"""
Generate the first n triangular numbers that are also square numbers.
Args:
n (int): The number of triangular square numbers to generate.
Returns:
... |
code_exercise_27k | 12,461 | Write a Python function to replace all vowels in a list of names with a specified character. The function should return a new list containing the modified names. The code should start with:
```python
from typing import List
def replace_vowels_in_names(names: List[str], replace_char: str) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def replace_vowels_in_names(names: List[str], replace_char: str) -> List[str]:
"""
Replaces all vowels in a list of names with a specified character.
Args:
names (List[str]): A list of names... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.