source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
code_exercise_27k
13,346
Write a Python function to calculate the product of all numbers in a given list, handling edge cases such as an empty list and a list containing zeros. The function should return the product as a float. The code should start with: ```python from typing import List def calculate_product(numbers: List[float]) -> float: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_product(numbers: List[float]) -> float: """ Calculates the product of all numbers in the given list. Args: numbers (List[float]): A list of numbers. Returns: float...
code_exercise_27k
13,349
Write a Python function to calculate the difference between the largest and smallest numbers in a given list of integers. The function should handle an empty list by raising a `ValueError`. The code should start with: ```python from typing import List def find_difference(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_difference(numbers: List[int]) -> int: """ Calculate the difference between the largest and smallest number in a list of integers. Args: numbers (List[int]): A list of integers. ...
code_exercise_27k
13,351
Write a Python function that takes a dictionary and adds a key-value pair to it. The function should have default arguments for the dictionary, key, and value. If the dictionary is not provided, an empty dictionary should be created. If the key is not provided, a default key of 'default_key' should be used. If the valu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict, Optional def add_key_value(dictionary: Optional[Dict[Any, Any]] = None, key: Optional[Any] = None, value: Optional[Any] = None) -> Dict[Any, Any]: """ Adds a key-value pair to a dictionary with def...
code_exercise_27k
13,353
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 spaces and be case-insensitive. The code should start w...
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. Args: str1 (str): The first string. str2 (str): The second string. Returns: bool: True if...
code_exercise_27k
13,355
Write a Python function to sort a list of numbers in descending order. The function should handle an optional default argument of an empty list. The function should return a new list with the numbers sorted in descending order. The code should start with: ```python from typing import List def sort_numbers(numbers: Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_numbers(numbers: List[int] = None) -> List[int]: """ Sorts a list of numbers in descending order. Args: numbers (List[int], optional): The list of numbers to be sorted. Defaults to ...
code_exercise_27k
13,358
Write a Python function to sort a list of numbers in ascending order using the merge sort algorithm. The function should be efficient and should not modify the original list. The code should start with: ```python from typing import List def merge_sort(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def merge_sort(numbers: List[int]) -> List[int]: """ Sorts a list of numbers in ascending order using the merge sort algorithm. Args: numbers (List[int]): A list of numbers to be sorted. ...
code_exercise_27k
13,364
Write a Python function to calculate the area of a square, with a default argument of 1 for the side length. The function should raise a `ValueError` if the side length is not a positive number. The code should start with: ```python def calculate_square_area(side_length: float = 1) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_square_area(side_length: float = 1) -> float: """ Calculate the area of a square. Parameters: side_length (float or int): The length of the side of the square. Default is 1. Returns: float: The ar...
code_exercise_27k
13,366
Write a Python function to reverse each string in a given list of strings and return a new list with the reversed strings. The function should handle an optional argument with a default value of an empty list. The code should start with: ```python from typing import List def reverse_strings(string_list: List[str] = []...
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def reverse_strings(string_list: List[str] = []) -> List[str]: """ Reverse each string in the given list and return a new list with the reversed strings. Args: string_list (List[str], opti...
code_exercise_27k
13,372
Write a Python function to reverse each string in a given list of strings and return a new list with 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]) -> List[st...
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 a given list of strings and returns a new list with the reversed strings. Args: strings (List[str]): A list ...
code_exercise_27k
13,373
Write a Python function to find the smallest missing positive integer in a given list of integers. The function should return the smallest positive integer that is not present in the list. If the list contains all positive integers from 1 to the maximum number in the list, the function should return the next largest po...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_smallest_missing_positive(numbers: List[int]) -> int: """ Find the smallest missing positive integer in a given list of integers. Args: numbers (List[int]): The input list of intege...
code_exercise_27k
13,374
Write a Python function to count the number of words in a given string, considering various edge cases such as multiple spaces, leading and trailing spaces, and empty strings. The function should handle these cases efficiently and return the correct count of words. The code should start with: ```python def count_words...
Here is the Python code to solve the given problem in the markdown block: ```python def count_words(string: str) -> int: """ Count the number of words in a given string. This function handles various edge cases such as multiple spaces, leading and trailing spaces, and empty strings. It returns the corr...
code_exercise_27k
13,375
Write a Python function to remove duplicates from a given list while preserving the order of the elements. The function should return a new list with all duplicate elements removed, maintaining the first occurrence of each element in the original list. The code should start with: ```python from typing import List def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_preserve_order(lst: List[int]) -> List[int]: """ Remove duplicates from a list while preserving the order of the first occurrence of each element. Args: lst (List[int])...
code_exercise_27k
13,377
Write a Python function to generate the Fibonacci series up to a given limit. The function should return a list of Fibonacci numbers that do not exceed the specified limit. The function should handle invalid input by raising a `ValueError`. The code should start with: ```python from typing import List def generate_fi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_fibonacci_series(limit: int) -> List[int]: """ Generates the Fibonacci series up to the given limit. Args: limit (int): An integer representing the upper limit for the Fibonacci...
code_exercise_27k
13,378
Write a Python function to filter out numbers from a given list of integers that are divisible by a specified divisor. The function should return a new list containing only the numbers that are divisible by the given divisor. The code should start with: ```python from typing import List def filter_divisible_numbers(nu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_divisible_numbers(numbers: List[int], divisor: int) -> List[int]: """ Returns a new list containing only the numbers from the input list that are divisible by the specified divisor. Args:...
code_exercise_27k
13,384
Write a Python function that takes a list of numbers and returns a new list with each number squared. The function should have a default argument of an empty list. Additionally, ensure that the function handles cases where the input list contains non-numeric elements by raising a `ValueError`. The code should start wi...
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]: """ Squares the numbers in a given list and returns a new list. Args: numbers (List[int]): A list of numbers. Default is an empty li...
code_exercise_27k
13,385
Write a Python function to calculate the sum of positive numbers in a given list, ignoring any negative numbers. The function should handle an empty list by returning 0. The code should start with: ```python from typing import List def sum_positive_numbers(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_positive_numbers(numbers: List[int]) -> int: """ Calculate the sum of positive numbers in a given list, ignoring any negative numbers. Args: numbers (List[int]): A list of integers. ...
code_exercise_27k
13,386
Write a Python function to determine if a given number is a power of two. The function should have a default argument of 1. The function should return `True` if the number is a power of two, and `False` otherwise. The function should handle edge cases such as zero and negative numbers. The code should start with: ```p...
Here is the Python code to solve the given problem in the markdown block: ```python def is_power_of_two(number: int = 1) -> bool: """ Determine if a given number is a power of two. Parameters: - number (int): The number to be checked (default is 1). Returns: - bool: True if the number is a pow...
code_exercise_27k
13,388
Write a Python function to sort a list of strings in descending order of their length. The function should return a new list with the strings sorted by their length, from the longest to the shortest. The original list should not be modified. The code should start with: ```python from typing import List def sort_strin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_length(strings: List[str]) -> List[str]: """ Sorts a list of strings in descending order of their length. Args: strings (List[str]): List of strings to be sorted. Re...
code_exercise_27k
13,392
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 a given list. Args: numbers (List[int]): The list of integers to be processed. Re...
code_exercise_27k
13,393
Write a Python function to find the longest word in a given string, with a default argument of an empty string. If multiple words have the same longest length, the first occurrence should be returned. If no words are found, an empty string should be returned. The code should start with: ```python def find_longest_word...
Here is the Python code to solve the given problem in the markdown block: ```python def find_longest_word(string: str = "") -> str: """ Finds the longest word in a given string. Parameters: string (str): The input string to search for the longest word. Default is an empty string. Returns: str:...
code_exercise_27k
13,403
Write a Python function to calculate the average of a list of numbers. The function should handle empty lists by returning 0.0 and should raise a `ValueError` if the input is not a list. The code should start with: ```python from typing import List def calculate_average(numbers: List[float]) -> float: ```
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. Args: numbers (List[float]): A list of numbers. Returns: float: The average...
code_exercise_27k
13,406
Write a Python function to count the number of vowels in a given string, considering both uppercase and lowercase vowels. The function should have a default argument of an empty 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: """ Count the number of vowels in a given string. Args: string (str): The string to count vowels in. Defaults to an empty string. Returns: int: The count of ...
code_exercise_27k
13,415
Write a Python function to remove duplicate elements from a given list of numbers while preserving the order of the first occurrence of each element. The function should handle an optional default argument of an empty list. The code should start with: ```python from typing import List def remove_duplicates(numbers: Li...
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def remove_duplicates(numbers: List[int] = []) -> List[int]: """ Removes duplicate elements from a list of numbers while preserving the order of their first occurrence. Args: numbers (List...
code_exercise_27k
13,422
Write a Python function to sort a list of numbers in descending order using the merge sort algorithm. The function should return a new list with the numbers sorted in descending order. The merge sort algorithm should be implemented from scratch without using Python's built-in sorting functions. 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 merge_sort_descending(numbers: List[int]) -> List[int]: """ Sorts a list of numbers in descending order using the merge sort algorithm. Args: numbers (List[int]): List of numbers to be s...
code_exercise_27k
13,423
Write a Python function to find the largest element in a given list. If the list is empty, the function should raise a `ValueError`. The function should be designed to handle both integer and floating-point numbers. The code should start with: ```python from typing import List, Union def find_largest_element(lst: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_largest_element(lst: List[Union[int, float]]) -> Union[int, float]: """ Finds the largest element in a given list of integers or floating-point numbers. Args: lst (List[Union...
code_exercise_27k
13,425
Write a Python function that takes a dictionary as input and returns a list of its keys sorted in alphabetical order. The function should have a default argument of an empty dictionary. Additionally, the function should handle cases where the dictionary is empty or contains non-string keys by returning an empty list in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Union def get_sorted_keys(dictionary: Dict[str, Union[int, str]] = {}) -> List[str]: """ Returns a list of keys from the given dictionary sorted in alphabetical order. Args: dictionar...
code_exercise_27k
13,426
Write a Python function to sort a list of strings in alphabetical order, ignoring case, and return a new list with the sorted strings. The function should handle an empty list gracefully and ensure that the original list is not modified. The code should start with: ```python from typing import List def sort_strings_i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_ignore_case(strings: List[str]) -> List[str]: """ Sorts a list of strings in alphabetical order, ignoring case. Args: strings (List[str]): A list of strings to be sorted. ...
code_exercise_27k
13,431
Write a Python function to calculate the area of a triangle using Heron's formula. The function should accept three side lengths as arguments and return the area of the triangle. If the side lengths do not form a valid triangle, the function should raise a `ValueError`. 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 Union import math def calculate_triangle_area_heron(a: Union[int, float], b: Union[int, float], c: Union[int, float]) -> float: """ Calculates the area of a triangle using Heron's formula. Args: ...
code_exercise_27k
13,437
Write a Python function to sort a list of strings in reverse alphabetical order. The function should handle an optional default argument of an empty list. Additionally, the function should raise appropriate `TypeError` exceptions if the input is not a list or if any element in the list is not a string. The code should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_sort_strings(string_list: List[str] = []) -> List[str]: """ Sorts a list of strings in reverse alphabetical order. Args: string_list (List[str]): A list of strings. Default is an...
code_exercise_27k
13,440
Write a Python function to calculate the cube of a number, with a default argument of 1. The function should handle both integer and floating-point inputs. The code should start with: ```python def calculate_cube(num: int | float = 1) -> int | float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_cube(num: int | float = 1) -> int | float: """ Calculates the cube of a number. Parameters: num (int or float): The number to calculate the cube for. Default is 1. Returns: int or float: The cube ...
code_exercise_27k
13,446
Write a Python function to find the second largest element in a given list. If the list is empty or contains less than two elements, the function should return `None`. The function should handle lists with duplicate elements and should not modify the original list. The code should start with: ```python from typing impo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def find_second_largest(lst: List[Union[int, float]]) -> Union[int, float, None]: """ Finds the second largest element in a given list. Args: lst (List[Union[int, float]]): The list t...
code_exercise_27k
13,447
Write a Python function to implement the merge sort algorithm. The function should take a list of integers as input and return the sorted list. The merge sort algorithm should be implemented using a divide-and-conquer approach, where the input list is divided into two halves, each half is sorted recursively, and then t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts an array using the merge sort algorithm. Merge sort is a divide-and-conquer algorithm that works by dividing the input array into two halve...
code_exercise_27k
13,449
Write a Python function to calculate the product of all positive numbers in a given list. If the list contains no positive numbers, the function should return 1. The function should handle both integers and floating-point numbers. The code should start with: ```python from typing import List def product_of_positive_n...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def product_of_positive_numbers(numbers: List[int | float]) -> int | float: """ Calculate the product of all positive numbers in a given list. Args: numbers (List[int | float]): A list of nu...
code_exercise_27k
13,452
Write a Python function to calculate the volume of a cone given its radius and height, with default arguments for both radius and height. The function should raise a `ValueError` if either the radius or the height is negative. The code should start with: ```python import math def calculate_cone_volume(radius: float = ...
Here is the Python code to solve the given problem in the markdown block: ```python import math def calculate_cone_volume(radius: float = 1, height: float = 1) -> float: """ Calculate the volume of a cone given its radius and height. Args: radius (float, optional): The radius of the base of the co...
code_exercise_27k
13,453
Write a Python function to sort the values of a given dictionary in ascending order. The function should handle an empty dictionary as a default argument and return an empty list if the dictionary is empty. The function should also handle dictionaries with non-comparable values by raising a `TypeError`. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Any def sort_dict_values(dictionary: Dict[Any, Any] = {}) -> List[Any]: """ Sorts the values of a given dictionary in ascending order. Args: dictionary (Dict[Any, Any], optional): The...
code_exercise_27k
13,456
Write a Python function to check if a given number is divisible by a specified divisor, with a default divisor of 1. The function should handle both integer and floating-point numbers. The code should start with: ```python def is_divisible(number: float, divisor: float = 1) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_divisible(number: float, divisor: float = 1) -> bool: """ Check if a number is divisible by a given divisor. Parameters: number (float): The number to be checked for divisibility. divisor (float, opti...
code_exercise_27k
13,464
Write a Python function to filter a list of numbers and return a new list containing only the numbers that have at least two digits. The function should handle both positive and negative numbers. The code should start with: ```python from typing import List def filter_numbers_with_two_digits(numbers: List[int]) -> Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_numbers_with_two_digits(numbers: List[int]) -> List[int]: """ Filter a list of numbers to return only numbers with at least two digits. Args: numbers (List[int]): A list of intege...
code_exercise_27k
13,476
Write a Python function to calculate the logarithm of a number with a default base of 1. The function should handle the following cases: 1. If the base is 1, the function should return the number itself. 2. If the base is not 1, the function should return the logarithm of the number with the specified base. 3. The func...
Here is the Python code to solve the given problem in the markdown block: ```python import math def calculate_logarithm(number: float, base: float = 1) -> float: """ Calculates the logarithm of a number with a default base of 1. Parameters: number (float): The number for which the logarithm is cal...
code_exercise_27k
13,480
Write a Python function to check if a given list is sorted in descending order. The function should handle an empty list as a default argument and return `True` for an empty list. The function should also handle lists with duplicate elements. The code should start with: ```python from typing import List def is_sorted...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_sorted_descending(lst: List[int] = []) -> bool: """ Check if a given list is sorted in descending order. Args: lst (List[int], optional): The list to be checked. Defaults to an empty ...
code_exercise_27k
13,483
Write a Python function to find and return a list of strings that have the highest number of characters from a given list of strings. If there are multiple strings with the same highest length, include all of them in the result. The code should start with: ```python from typing import List def get_longest_strings(str...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_longest_strings(strings: List[str]) -> List[str]: """ Returns a list of strings that have the highest number of characters from the given list. Args: strings (List[str]): A list of s...
code_exercise_27k
13,489
Write a Python function to sort a list of strings in descending order of their number of words. Each string in the list can contain multiple words separated by spaces. The function should return a new list with the strings sorted based on the number of words in each string, from the highest to the lowest. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_word_count(strings: List[str]) -> List[str]: """ Sorts a list of strings in descending order of their number of words. Args: strings (List[str]): A list of strings to be ...
code_exercise_27k
13,493
Write a Python function to find the common elements between two lists and return a new list containing these common elements. The function should handle the case where the input lists are empty by default. The function should also ensure that the common elements are returned in the order they appear in the second list....
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(list1: List[int] = [], list2: List[int] = []) -> List[int]: """ Find and return a new list with the common elements between list1 and list2. Args: list1 (List[int], ...
code_exercise_27k
13,502
Write a Python function to calculate the area of a trapezoid with default arguments for the bases and height. The function should handle negative input values by raising a `ValueError`. The code should start with: ```python def calculate_trapezoid_area(base1: float = 0, base2: float = 0, height: float = 0) -> float: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_trapezoid_area(base1: float = 0, base2: float = 0, height: float = 0) -> float: """ Calculate the area of a trapezoid given its two bases and height. Args: base1 (float, optional): The length of the fi...
code_exercise_27k
13,503
Write a Python function to round each number in a given list to the nearest whole number and return a new list containing the rounded numbers. The function should handle various types of numeric inputs, including integers and floating-point numbers. The function should also raise a `TypeError` if the input is not a lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def round_numbers(numbers: List[float]) -> List[int]: """ Round each number in the input list to the nearest whole number and return a new list of rounded numbers. Args: numbers (List[float]...
code_exercise_27k
13,504
Write a Python function to count the number of words in a given string, considering that words are separated by any number of whitespace characters (spaces, tabs, newlines). The function should handle an empty string as a default argument and return 0 in that case. The function should also handle strings with multiple ...
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. Words are defined as sequences of characters separated by any number of whitespace characters. Args: sentence (str...
code_exercise_27k
13,516
Write a Python function to count the number of numbers in a given list that have a decimal part. The function should handle both integers and floating-point numbers. The code should start with: ```python from typing import List def count_numbers_with_decimal(numbers: List[float]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_numbers_with_decimal(numbers: List[float]) -> int: """ Counts the number of numbers in a given list that have a decimal part. Args: numbers (List[float]): A list of numbers (intege...
code_exercise_27k
13,527
Write a Python function to calculate the sum of all numbers in a list, with a default argument of an empty list. The function should handle the following: 1. Validate that the input is a list. 2. Validate that all elements in the list are numbers (either integers or floats). 3. Calculate and return the sum of the numbe...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_sum(numbers=[]): """ Calculate the sum of all numbers in a list. Args: numbers (list, optional): A list of numbers. Defaults to an empty list. Returns: float: The sum of all numbers in the...
code_exercise_27k
13,534
Write a Python function to sort the key-value pairs of a dictionary by the keys in ascending order. The function should handle an empty dictionary as a default argument and return a list of tuples representing the sorted key-value pairs. The code should start with: ```python from typing import Dict, List, Tuple def so...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List, Tuple def sort_dictionary_by_key(input_dict: Dict[str, int] = {}) -> List[Tuple[str, int]]: """ Sorts the key-value pairs in a dictionary by the keys in ascending order. Args: input_d...
code_exercise_27k
13,542
Write a Python function to count the number of numbers in a given list that are both negative and odd. The function should take a list of integers as input and return an integer representing the count of such numbers. The code should start with: ```python from typing import List def count_negative_odd_numbers(numbers:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_negative_odd_numbers(numbers: List[int]) -> int: """ Counts the number of numbers in a given list that are both negative and odd. Args: numbers (List[int]): A list of integers. ...
code_exercise_27k
13,548
Write a Python function to check if a given list is sorted in ascending order without using the `sort()` method. The function should handle lists of any length, including empty lists, and should return `True` if the list is sorted in ascending order and `False` otherwise. The function should be designed to handle the d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_ascending_sorted(lst: List[int] = None) -> bool: """ Checks if a given list is sorted in ascending order. Parameters: lst (List[int]): The list to check for ascending sort. Defaults to No...
code_exercise_27k
13,555
Write a Python function to find the common elements between two lists, ensuring that the function handles duplicate elements correctly and returns a list with unique common elements in the order they first appear in the first list. The function should have default arguments of two empty lists. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(list1: List[int] = [], list2: List[int] = []) -> List[int]: """ Returns a new list with elements that appear in both input lists, preserving the order of their first appearance i...
code_exercise_27k
13,563
Write a Python function to calculate the sum of two numbers, ensuring that the function handles both integer and floating-point inputs. Additionally, the function should be able to handle cases where the inputs are provided as strings that represent valid numbers. The function should return the sum as a float if either...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_sum(a, b): """ Calculate the sum of two numbers, handling both integer and floating-point inputs. If the inputs are provided as strings that represent valid numbers, they will be converted accordingly. The ...
code_exercise_27k
13,569
Write a Python function to calculate the perimeter of a rectangle given its length and width. The function should be able to handle both integer and floating-point inputs. The code should start with: ```python def calculate_rectangle_perimeter(length: float, width: float) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_rectangle_perimeter(length: float, width: float) -> float: """ Calculates the perimeter of a rectangle. Args: length (float): The length of the rectangle. width (float): The width of the rectan...
code_exercise_27k
13,570
Write a Python function to divide each element in a given list of numbers by a specified divisor and return a new list with the results. The function should handle both integer and floating-point numbers. The code should start with: ```python from typing import List def divide_list_elements(numbers: List[float], divis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def divide_list_elements(numbers: List[float], divisor: float) -> List[float]: """ Divides each element in the given list of numbers by the specified divisor and returns a new list with the results. ...
code_exercise_27k
13,571
Write a Python function to find the maximum number in a given list of integers. The function should handle the case where the list is empty by raising a `ValueError`. The code should start with: ```python from typing import List def find_max_number(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_number(numbers: List[int]) -> int: """ Find the maximum number in a given list of integers. Args: numbers (List[int]): A list of integers from which the maximum number is to be ...
code_exercise_27k
13,573
Write a Python function to filter and return a list of strings that have the lowest number of words from a given list of strings. The function should handle cases where multiple strings have the same lowest number of words. The code should start with: ```python from typing import List def filter_strings_with_lowest_w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_strings_with_lowest_words(strings: List[str]) -> List[str]: """ Returns a new list of strings that have the lowest number of words from the given list. Args: strings (List[str]): ...
code_exercise_27k
13,579
Write a Python function to round up each number in a given list to the nearest whole number and return a new list with the rounded numbers. The function should handle both positive and negative numbers. The code should start with: ```python from typing import List import math def round_up_numbers(numbers: List[float])...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def round_up_numbers(numbers: List[float]) -> List[int]: """ Rounds up each number in the given list to the nearest whole number and returns a new list with the rounded numbers. Args: ...
code_exercise_27k
13,584
Write a Python function to find and return a list of strings from a given list that have the highest number of uppercase letters. If multiple strings have the same highest number of uppercase letters, all such strings should be included in the result. The code should start with: ```python from typing import List def ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_strings_with_most_uppercase(strings: List[str]) -> List[str]: """ Returns a list of strings from the input list that have the highest number of uppercase letters. Args: strings (List...
code_exercise_27k
13,587
Write a Python function to calculate the average of a list of numbers, ensuring that the function handles edge cases such as an empty list by returning `None` instead of raising an exception. The function should also handle lists containing non-numeric values by ignoring those values and calculating the average of the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def calculate_average(numbers: List[Union[int, float]]) -> Union[float, None]: """ Calculate the average of a list of numbers, ignoring non-numeric values. Args: numbers (list): A...
code_exercise_27k
13,590
Write a Python function to find the longest word in a given list of words. If there are multiple words with the same maximum length, return the first one encountered. The function should raise a `ValueError` if the input list is empty. The code should start with: ```python from typing import List def find_longest_wor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_longest_word(words: List[str]) -> str: """ Finds the longest word in a given list of words. Args: words (List[str]): A list of strings representing words. Returns: str:...
code_exercise_27k
13,593
Write a Python function to multiply each element in a given list of numbers by a specified factor and return a new list with the results. The function should handle both positive and negative factors, as well as the case where the factor is zero. The code should start with: ```python from typing import List def multip...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def multiply_list_elements(numbers: List[int], factor: int) -> List[int]: """ Multiplies each element in the given list of numbers by the specified factor and returns a new list with the results. Ar...
code_exercise_27k
13,594
Write a Python function to reverse a given string. The function should handle strings of any length, including empty strings. The code should start with: ```python def reverse_string(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses a given string. Args: string (str): The string to be reversed. Returns: str: The reversed string. """ # Use slicing to reverse the string ...
code_exercise_27k
13,597
Write a Python function to check if a given number is prime. The function should return `True` if the number is prime and `False` otherwise. The function should handle special cases such as numbers less than or equal to 1, which are not prime. The code should start with: ```python def is_prime(n: int) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Check if a given number is prime. Parameters: n (int): The number to be checked. Returns: bool: True if the number is prime, False otherwise. """ # Handle special case...
code_exercise_27k
13,599
Write a Python function to count the number of vowels in a given string, considering both uppercase and lowercase vowels. 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: """ Count the number of vowels in a given string. This function iterates through each character in the input string and counts how many of them are vowels. The function is case-inse...
code_exercise_27k
13,603
Write a Python function to find the second smallest number in a given list of integers. The function should handle lists with at least two elements and should return the second smallest number. If the list has fewer than two elements, the function should raise a `ValueError`. 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 find_second_smallest(numbers: List[int]) -> int: """ Finds and returns the second smallest number in a list of integers. Args: numbers (List[int]): A list of integers. Returns: ...
code_exercise_27k
13,608
Write a Python function to convert a given string to title case. Title case means that the first letter of each word in the string is capitalized, and all other letters are in lowercase. The function should handle strings with multiple spaces between words and leading/trailing spaces. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_to_title_case(s: str) -> str: """ Convert a given string to title case. Title case means that the first letter of each word in the string is capitalized, and all other letters are in lowercase. This function...
code_exercise_27k
13,617
Write a Python function to divide each number in a given list by a specified divisor and return a new list with the divided values. The function should handle both integer and floating-point numbers and ensure that the result is accurate. The code should start with: ```python from typing import List def divide_list_by...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def divide_list_by_divisor(numbers: List[float], divisor: float) -> List[float]: """ Divides each number in the input list by the specified divisor and returns a new list with the divided values. Ar...
code_exercise_27k
13,624
Write a Python function to validate a credit card number using the Luhn algorithm. The function should take a string as input and return `True` if the credit card number is valid, and `False` otherwise. The function should handle any non-digit characters in the input string by removing them before validation. The code...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_credit_card(number: str) -> bool: """ Validates a credit card number using the Luhn algorithm. Args: number (str): The credit card number as a string, which may contain non-digit characters. Return...
code_exercise_27k
13,626
Write a Python function to rotate a list of elements by a specified amount. The rotation can be either to the right or to the left based on the sign of the specified amount. If the specified amount is positive, the list should be rotated to the right. If the specified amount is negative, the list should be rotated to t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def rotate_list(lst: List[int], amount: int) -> List[int]: """ Rotates a list of elements by a specified amount. Parameters: lst (List[int]): The list to be rotated. amount (int): The amount...
code_exercise_27k
13,635
Write a Python function to divide each number in a given list by a specified divisor and return a new list with the results. The function should handle cases where the divisor is zero by returning the original list unchanged. The code should start with: ```python from typing import List def divide_list_by_divisor(num...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def divide_list_by_divisor(numbers: List[int], divisor: int) -> List[int]: """ Divides each number in the given list by the specified divisor and returns a new list with the results. If the divisor i...
code_exercise_27k
13,639
Write a Python function to filter a list of strings and return a new list containing only the strings with the lowest number of special characters. Special characters are defined as any character in the set `"!@#$%^&*()_-+=<>?/\|{}[]~"`. The function should start with: ```python from typing import List def filter_stri...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_special_chars(string: str) -> int: """ Counts the number of special characters in a given string. Args: string (str): The input string to count special characters in. Returns:...
code_exercise_27k
13,646
Write a Python function to convert a string to an integer, handling the `ValueError` that occurs when the string cannot be converted to an integer. The function should return `None` if the conversion fails. The code should start with: ```python def safe_string_to_integer(string: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def safe_string_to_integer(string: str) -> int: """ Converts a string to an integer, returning None if the conversion fails. Args: string (str): The string to convert to an integer. Returns: int: The in...
code_exercise_27k
13,649
Write a Python function to determine if a given number is a perfect square. The function should handle both integer and floating-point inputs. The function should return `True` if the number is a perfect square and `False` otherwise. The code should start with: ```python def is_perfect_square(num: float) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_perfect_square(num: float) -> bool: """ Determine if a given number is a perfect square. Args: num (float): The number to check. Returns: bool: True if the number is a perfect square, False other...
code_exercise_27k
13,656
Write a Python function to concatenate two strings and handle the `TypeError` if either of the inputs is not a string. The function should raise a `TypeError` with a specific message if the inputs are not strings. The code should start with: ```python def concatenate_strings(input1: str, input2: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def concatenate_strings(input1: str, input2: str) -> str: """ Concatenates two strings. Args: input1 (str): The first input string. input2 (str): The second input string. Returns: str: The conca...
code_exercise_27k
13,658
Write a Python function to calculate the Euclidean distance between two points in a multi-dimensional space. The function should handle points with any number of dimensions and ensure that the dimensions of the two points are equal. If the dimensions are not equal, the function should raise a `ValueError`. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def euclidean_distance(point1: List[float], point2: List[float]) -> float: """ Calculates the Euclidean distance between two points in a multi-dimensional space. Args: point1 (Li...
code_exercise_27k
13,660
Write a Python function to find the ASCII value of a given character. The function should handle input validation to ensure that the input is a single character string. If the input is not a single character string, the function should raise a `TypeError`. The code should start with: ```python def find_ascii_value(cha...
Here is the Python code to solve the given problem in the markdown block: ```python def find_ascii_value(character: str) -> int: """ Return the ASCII value of a given character. Args: character (str): The character for which the ASCII value is to be found. Returns: int: The ASCII value...
code_exercise_27k
13,668
Write a Python function to check if a given string is a valid URL. The function should validate the URL based on the following criteria: 1. The URL must start with `http://`, `https://`, `ftp://`, or `ftps://`. 2. The URL can include a domain name, an IP address, or the special case `localhost`. 3. The domain name shou...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_url(url: str) -> bool: """ Check if the given string is a valid URL. Parameters: url (str): The string to be checked. Returns: bool: True if the string is a valid URL, False otherwise. ...
code_exercise_27k
13,671
Write a Python function that takes two parameters and returns their sum. The function should be implemented using a lambda function. Additionally, write a function to test the lambda function with various inputs, ensuring it works correctly for different types of numbers (integers, floats) and edge cases (negative numb...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union # Define the lambda function sum_two_numbers = lambda x, y: x + y # Define the test function def test_sum_two_numbers(): """ Test the lambda function sum_two_numbers with various inputs. This func...
code_exercise_27k
13,673
Write a Python function to determine if a given number is even or odd. The function should return a string "even" if the number is even and "odd" if the number is odd. The function should be implemented using a lambda function. The code should start with: ```python is_even_or_odd = lambda num: ... ```
Here is the Python code to solve the given problem in the markdown block: ```python is_even_or_odd = lambda num: "even" if num % 2 == 0 else "odd" """ This lambda function takes a number as input and checks if it is even or odd. Parameters: - num (int): The input number to be checked. Returns: - str: Returns "even" ...
code_exercise_27k
13,681
Create a Python function to calculate the cube of a number using a lambda function. The function should be named `cube` and should take a single numeric argument. The function should return the cube of the input number. Additionally, write a function `calculate_power` that takes two arguments: a base number and an expo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union # Define the lambda function for cube calculation cube = lambda x: x ** 3 def calculate_power(base: Union[int, float], exponent: int) -> Union[int, float]: """ Calculate the power of a base number rais...
code_exercise_27k
13,682
Write a Python function to check if a given string starts with an uppercase letter. The function should return `True` if the string starts with an uppercase letter, otherwise `False`. The function should handle edge cases such as empty strings and non-alphabetic characters. The code should start with: ```python def st...
Here is the Python code to solve the given problem in the markdown block: ```python def starts_with_uppercase(s: str) -> bool: """ Check if the given string starts with an uppercase letter. Args: s (str): The string to check. Returns: bool: True if the string starts with an uppercase l...
code_exercise_27k
13,689
Write a Python function to convert a given string to uppercase using a lambda function. The function should take a single string as input and return the uppercase version of that string. The code should start with: ```python from typing import Callable def create_uppercase_lambda() -> Callable[[str], str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Callable def create_uppercase_lambda() -> Callable[[str], str]: """ Creates and returns a lambda function that converts a given string to uppercase. Returns: Callable[[str], str]: A lambda functi...
code_exercise_27k
13,692
Write a Python function to check if a given number is divisible by both 3 and 5. The function should be implemented using a lambda function. The code should start with: ```python is_divisible_by_3_and_5 = lambda number: ... ```
Here is the Python code to solve the given problem in the markdown block: ```python is_divisible_by_3_and_5 = lambda number: number % 3 == 0 and number % 5 == 0 """ A lambda function that checks if a given number is divisible by both 3 and 5. Parameters: number (int): The number to be checked for divisibility. R...
code_exercise_27k
13,701
Write a Python function to reverse a given string using a lambda function. The function should take a string as input and return the reversed string. The code should start with: ```python reverse_string = lambda s: s[::-1] ```
Here is the Python code to solve the given problem in the markdown block: ```python reverse_string = lambda s: s[::-1] """ Lambda function to reverse a string. Parameters: - s (str): The string to be reversed. Returns: - str: The reversed string. Explanation: The lambda function takes a string `s` as input and uses...
code_exercise_27k
13,702
Write a Python function to calculate the area of a rectangle given its length and width. The function should be implemented using a lambda function. Additionally, write a function to calculate the perimeter of the rectangle using a lambda function. The code should start with: ```python from typing import Union calcula...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union # Lambda function to calculate the area of a rectangle calculate_area = lambda length, width: length * width # Lambda function to calculate the perimeter of a rectangle calculate_perimeter = lambda length, wid...
code_exercise_27k
13,707
Write a Python function to find the maximum of three numbers using a lambda function. The function should take three numbers as input and return the maximum of the three. The code should start with: ```python max_of_three = lambda x, y, z: ... ```
Here is the Python code to solve the given problem in the markdown block: ```python max_of_three = lambda x, y, z: max(x, y, z) """ A lambda function to find the maximum of three numbers. Parameters: - x: The first number to compare. - y: The second number to compare. - z: The third number to compare. Returns: The m...
code_exercise_27k
13,713
Write a Python function to convert a list of strings to lowercase. The function should take a list of strings as input and return a new list where each string is converted to lowercase. The function should be implemented using a lambda function and a list comprehension. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List lowercase_strings = lambda strings: [s.lower() for s in strings] # Docstring """ Convert a list of strings to lowercase. Args: strings (List[str]): A list of strings to be converted to lowercase. Returns:...
code_exercise_27k
13,716
Write a Python function to calculate the average of numbers in a list. The function should handle an empty list by returning `None` instead of raising an error. The code should start with: ```python from typing import List, Optional def calculate_average(numbers: List[int]) -> Optional[float]: ```
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 numbers in a list. If the list is empty, return None. Args: numbers (List[int]): A list o...
code_exercise_27k
13,717
Write a Python function to sort a list of strings in alphabetical order using a lambda function. The function should take a list of strings as input and return the sorted list. The code should start with: ```python from typing import List def sort_strings(strings: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings(strings: List[str]) -> List[str]: """ Sorts a list of strings in alphabetical order using a lambda function. Args: strings (List[str]): The list of strings to be sorted. ...
code_exercise_27k
13,719
Write a Python function to check if a number is a prime number. The function should be efficient and handle edge cases such as 1 and negative numbers. The code should start with: ```python def is_prime(num: int) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(num: int) -> bool: """ Check if a given number is a prime number. Args: num (int): The number to be checked for primality. Returns: bool: True if the number is prime, False otherwise. ...
code_exercise_27k
13,726
Write a Python function to remove duplicates from a given list while preserving the order of the elements. The function should take a list as input and return a new list with all duplicate elements removed. The code should start with: ```python from typing import List def remove_duplicates_preserve_order(lst: List[int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_preserve_order(lst: List[int]) -> List[int]: """ Removes duplicates from the given list while preserving the order of the elements. Args: lst (List[int]): The list from...
code_exercise_27k
13,729
Write a Python function to check if a given string contains only digits. The function should return `True` if all characters in the string are digits, and `False` otherwise. The code should start with: ```python def is_only_digits(s: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_only_digits(s: str) -> bool: """ Check if a given string contains only digits. This function iterates through each character in the string and checks if it is a digit. It uses the `isdigit()` method of strings to...
code_exercise_27k
13,731
Write a Python function to find the index of the first occurrence of a given element in a list. If the element is not found, the function should return `-1`. The function should handle cases where the list is empty or the element is not present in the list. The code should start with: ```python from typing import List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_first_occurrence(lst: List[int], element: int) -> int: """ Find the index of the first occurrence of a given element in a list. Args: lst (List[int]): The list in which to search fo...
code_exercise_27k
13,734
Write a Python function to calculate the factorial of a given number using a lambda function. The function should handle both positive integers and the special case where the input is 0. The code should start with: ```python def factorial_lambda(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def factorial_lambda(n: int) -> int: """ Calculate the factorial of a given number using a lambda function. Parameters: n (int): The input number whose factorial is to be calculated. Returns: int: The factorial...
code_exercise_27k
13,737
Write a Python function to apply a given function to all elements of a list and return the resulting list. The function should be flexible enough to handle different types of functions and lists. The code should start with: ```python from typing import List, Callable def apply_function_to_list(lst: List, func: Callabl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Callable def apply_function_to_list(lst: List, func: Callable) -> List: """ Applies a given function to all elements of a list and returns the resulting list. Args: lst (List): The list of ...
code_exercise_27k
13,741
Write a Python function to check 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 case and any non-alphabetic characters. The code should sta...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(s1: str, s2: str) -> bool: """ Check if two strings are anagrams of each other, ignoring case and non-alphabetic characters. Args: s1 (str): The first string to check. s2 (str): The second str...
code_exercise_27k
13,746
Write a Python function to calculate the sum of squares of all even numbers in a given list. The function should be implemented using a list comprehension and the `sum` function. The code should start with: ```python from typing import List def sum_of_squares_of_evens(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_of_evens(numbers: List[int]) -> int: """ Calculate the sum of squares of all even numbers in the given list. Args: numbers (List[int]): The list of integers from which to ...
code_exercise_27k
13,750
Write a Python function to find the largest prime number in a given list of integers. The function should return `None` if no prime numbers are present in the list. The code should start with: ```python from typing import List, Optional def find_largest_prime(numbers: List[int]) -> Optional[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def is_prime(num: int) -> bool: """ Helper function to check if a number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the numbe...
code_exercise_27k
13,754
Write a Python function to check if a given date is in the future. The function should take a `datetime.datetime` object as input and return `True` if the date is in the future, and `False` otherwise. The code should start with: ```python import datetime def is_future_date(date: datetime.datetime) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def is_future_date(date: datetime.datetime) -> bool: """ Check if a given date is in the future. Args: date (datetime.datetime): The date to be checked. Returns: bool: True if the date ...
code_exercise_27k
13,756
Write a Python function to calculate the sum of two numbers using a lambda function. The function should be able to handle both integers and floating-point numbers. The code should start with: ```python from typing import Union add = lambda x: lambda y: x + y ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import Union add = lambda x: lambda y: x + y """ This lambda function takes in two numbers and returns their sum using currying. Parameters: - x (Union[int, float]): The first number. - y (Union[int, float]): The second...