source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
code_exercise_27k | 22,869 | Write a Python function to remove all uppercase letters from a given string. The function should iterate through each character in the string, check if it is an uppercase letter, and if not, append it to a new string. The function should return the new string with all uppercase letters removed.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_uppercase_letters(string: str) -> str:
"""
Remove all uppercase letters from a given string.
Args:
string (str): The input string from which uppercase letters are to be removed.
Returns:
str:... |
code_exercise_27k | 22,872 | Write a Python function to remove all characters except numbers from a given string. The function should return a new string containing only the numeric characters from the input string. The code should start with:
```python
def remove_non_numbers(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_non_numbers(string: str) -> str:
"""
Remove all characters except numbers from a given string.
Args:
string (str): The input string to process.
Returns:
str: The resulting string with only nu... |
code_exercise_27k | 22,875 | Write a Python function to check if a given string contains only uppercase letters. The function should return `True` if all characters in the string are uppercase, and `False` otherwise. The function should handle strings with any combination of characters, including spaces, numbers, and special characters.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_uppercase_string(string: str) -> bool:
"""
Check if the input string contains only uppercase letters.
Args:
string (str): The string to be checked.
Returns:
bool: True if all characters in the st... |
code_exercise_27k | 22,877 | Write a Python function to check if a given string is an anagram of another string, considering case sensitivity and ignoring whitespace. The function should return `True` if the two strings are anagrams, and `False` otherwise. The code should start with:
```python
def is_anagram(str1: str, str2: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_anagram(str1: str, str2: str) -> bool:
"""
Check if a given string is an anagram of another string, considering case sensitivity and ignoring whitespace.
Args:
str1 (str): The first string to compare.
... |
code_exercise_27k | 22,879 | Write a Python function to reverse the order of characters in a given string using a recursive approach. The function should handle strings of any length, including empty strings. The code should start with:
```python
def reverse_string_recursive(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string_recursive(string: str) -> str:
"""
Reverse the order of characters in a given string using recursion.
Args:
string (str): The input string to be reversed.
Returns:
str: The reversed s... |
code_exercise_27k | 22,882 | Write a Python function to count the number of inversions in a given array. An inversion is defined as a pair of indices (i, j) such that i < j and arr[i] > arr[j]. The function should use a modified merge sort algorithm to efficiently count the inversions.
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 count_inversions(arr: List[int]) -> int:
"""
Counts the number of inversions in a given array using a modified merge sort algorithm.
An inversion occurs when two elements in an array, say i and ... |
code_exercise_27k | 22,885 | Write a Python function to remove all whitespace characters except newlines from a given string. The function should handle various types of whitespace characters such as spaces, tabs, and carriage returns, but should preserve newline characters. The code should start with:
```python
def remove_whitespace_except_newlin... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_whitespace_except_newlines(string: str) -> str:
"""
Remove all whitespace characters except newlines from a given string.
Args:
string (str): The input string from which to remove whitespace characters.
... |
code_exercise_27k | 22,888 | Write a Python function to calculate the determinant of a given square matrix. The function should handle matrices of any size, including 1x1, 2x2, and larger square matrices. The function should raise a `ValueError` if the input matrix is not square.
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 calculate_determinant(matrix: List[List[float]]) -> float:
"""
Calculate the determinant of a given square matrix.
Args:
matrix (List[List[float]]): The square matrix for which the deter... |
code_exercise_27k | 22,892 | Write a Python function to implement the quicksort algorithm. The function should sort a list of integers in ascending order. The quicksort algorithm should be implemented using the last element as the pivot for simplicity. The code should start with:
```python
from typing import List
def quicksort(arr: List[int]) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the quicksort algorithm.
Parameters:
arr (List[int]): The list of integers to be sorted.
Returns:
List[int... |
code_exercise_27k | 22,893 | Write a Python function to find the length of the last word in a given string. The function should handle cases where the string is empty or contains only whitespace. The code should start with:
```python
def length_of_last_word(string: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def length_of_last_word(string: str) -> int:
"""
Returns the length of the last word in the given string.
Args:
string (str): The input string.
Returns:
int: The length of the last word, or 0 if the str... |
code_exercise_27k | 22,894 | Write a Python function to find the longest common subsequence (LCS) between two given strings. The function should return the length of the LCS.
The code should start with:
```python
def longest_common_subsequence_length(str1: str, str2: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_subsequence_length(str1: str, str2: str) -> int:
"""
Calculate the length of the longest common subsequence between two strings.
Args:
str1 (str): The first string.
str2 (str): The second ... |
code_exercise_27k | 22,896 | Write a Python function to convert a given string to its binary representation. Each character in the string should be converted to its 8-bit binary equivalent. The function should return the concatenated binary string.
The code should start with:
```python
def string_to_binary(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def string_to_binary(string: str) -> str:
"""
Convert a string to its binary representation. Each character is converted to an 8-bit binary string.
Args:
string (str): The input string to be converted.
Returns:... |
code_exercise_27k | 22,898 | Write a Python function to check if a given string contains only alphabets and spaces. The function should return `True` if the string meets the criteria, and `False` otherwise. The code should start with:
```python
def contains_only_alphabets_and_spaces(input_string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def contains_only_alphabets_and_spaces(input_string: str) -> bool:
"""
Check if a string contains only alphabets and spaces.
Args:
input_string (str): The input string to be checked.
Returns:
bool: True... |
code_exercise_27k | 22,902 | Write a Python function to implement the Merge Sort algorithm. The function should take a list of comparable elements as input and return the sorted list in ascending order. The function should be implemented using a recursive approach, where the list is divided into two halves, each half is sorted recursively, and the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(arr: List[int]) -> List[int]:
"""
Sorts the given array using the Merge Sort algorithm.
Args:
arr (List[int]): An unsorted list of integers.
Returns:
List[int]: T... |
code_exercise_27k | 22,903 | Write a Python function to check if a given string is a valid time in the format `hh:mm:ss`. The function should ensure that the hours, minutes, and seconds are within their respective valid ranges. The code should start with:
```python
import re
def is_valid_time(time_str: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_valid_time(time_str: str) -> bool:
"""
Check if a given string is a valid time in the format hh:mm:ss.
Parameters:
time_str (str): The string to be validated.
Returns:
bool: True if th... |
code_exercise_27k | 22,910 | Write a Python function to remove all characters except alphabets, digits, and spaces from a given string. The function should return the modified string. The code should start with:
```python
def remove_special_characters(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_special_characters(string: str) -> str:
"""
Removes all characters except alphabets, digits, and spaces from a given string.
Args:
string (str): The input string from which special characters are to be re... |
code_exercise_27k | 22,912 | Write a Python function to implement the Quick Sort algorithm. The function should sort a list of comparable elements in ascending order. The code should start with:
```python
from typing import List
def quick_sort(arr: List) -> List:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quick_sort(arr: List) -> List:
"""
Sorts a list in ascending order using the Quick Sort algorithm.
Parameters:
arr (list): A list of comparable elements to be sorted.
Returns:
list:... |
code_exercise_27k | 22,914 | Write a Python function to calculate the average of a given list of numbers. The function should handle the following cases:
1. If the input is not a list, raise a `ValueError` with the message "Input must be a list".
2. If the list is empty, raise a `ValueError` with the message "List cannot be empty".
3. Otherwise, c... | 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:
"""
Calculates the average of a given list of numbers.
Args:
numbers (List[float]): A list of numbers.
Returns:
float: The ... |
code_exercise_27k | 22,919 | Write a Python function to remove all consecutive duplicate characters from a given string. The function should iterate through the string and remove any character that appears consecutively more than once. The code should start with:
```python
def remove_consecutive_duplicates(input_str: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def remove_consecutive_duplicates(input_str: str) -> str:
"""
Removes all consecutive duplicate characters from a given string.
Args:
input_str (str): The input string from which consecutive duplicate characters are... |
code_exercise_27k | 22,920 | Write a Python function to count the frequency of each element in a given list. The function should return a dictionary where the keys are the elements of the list and the values are their respective frequencies. The code should start with:
```python
from typing import List, Dict
def count_element_frequency(lst: List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_element_frequency(lst: List[int]) -> Dict[int, int]:
"""
Count the frequency of each element in a given list.
Args:
lst (List[int]): The input list to count the frequency of ... |
code_exercise_27k | 22,921 | Write a Python function to find the largest multiple of a given number within a rolling window of size 7 in a list of integers. The function should return the largest multiple found within any of the rolling windows. If no multiples are found within any window, the function should return `None`.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def largest_multiple_in_window(num: int, arr: List[int]) -> Optional[int]:
"""
Find the largest multiple of a given number within a rolling window of size 7.
Args:
num (int): The n... |
code_exercise_27k | 22,924 | Write a Python function to check if a given string contains any consecutive repeating characters. The function should return `True` if there are any such characters and `False` otherwise. The code should start with:
```python
def has_consecutive_repeating_chars(s: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def has_consecutive_repeating_chars(s: str) -> bool:
"""
Check if a string contains consecutive repeating characters.
Args:
s (str): The input string to check.
Returns:
bool: True if the string contains... |
code_exercise_27k | 22,925 | Write a Python function to find the smallest power of 3 within a rolling window of size 8 from a given list of integers. The function should return the smallest power of 3 found within the window and its index. If no power of 3 is found, the function should return `None` for both the power and the index.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def smallest_power_of_3_in_window(window: List[int]) -> Tuple[int, int]:
"""
Finds the smallest power of 3 within a rolling window of size 8.
Args:
window (List[int]): A list of integ... |
code_exercise_27k | 22,927 | Write a Python function to extract all dates from a given string in the format `mm/dd/yyyy` or `dd/mm/yyyy`. The function should be able to handle both formats and return a list of all dates found in the input string.
The code should start with:
```python
import re
from typing import List
def extract_dates(string: st... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
from typing import List
def extract_dates(string: str) -> List[str]:
"""
Extracts all dates from a given string in the format `mm/dd/yyyy` or `dd/mm/yyyy`.
Args:
string (str): The input string from which ... |
code_exercise_27k | 22,929 | Write a Python function to find the ASCII value of each character in a given string. The function should return a list of integers where each integer represents the ASCII value of the corresponding character in the string. The code should start with:
```python
from typing import List
def find_ascii_values(string: str)... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_ascii_values(string: str) -> List[int]:
"""
Finds the ASCII value of each character in the given string.
Args:
string (str): The input string for which ASCII values need to be found... |
code_exercise_27k | 22,932 | Write a Python function to check if two lists are equal, considering that the lists may contain nested lists. The function should return `True` if the two lists are equal, and `False` otherwise. Two lists are considered equal if they have the same structure and the same elements at corresponding positions, including ne... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def are_lists_equal_nested(list1: List[Any], list2: List[Any]) -> bool:
"""
Check if two lists are equal, considering that the lists may contain nested lists.
Parameters:
list1 (List[Any]):... |
code_exercise_27k | 22,943 | Write a Python function to find the first non-repeating element in a given list of integers. If there is no non-repeating element, the function should return `None`. The function should be efficient and handle edge cases such as an empty list or a list with all elements repeating.
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, Optional
def find_first_non_repeating_element(lst: List[int]) -> Optional[int]:
"""
Find the first non-repeating element in a given list of integers.
Args:
lst (List[int]): The list of inte... |
code_exercise_27k | 22,947 | Write a Python function to check if a given list is symmetric. A list is symmetric if it reads the same backward as forward. The function should return `True` if the list is symmetric and `False` otherwise. The code should start with:
```python
from typing import List
def is_symmetric(lst: List[int]) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_symmetric(lst: List[int]) -> bool:
"""
Check if a given list is symmetric.
Args:
lst (List[int]): The list to check for symmetry.
Returns:
bool: True if the list is symme... |
code_exercise_27k | 22,948 | Write a Python function to add a new user to a bookmarking platform. The function should validate the input parameters and ensure that neither the username nor the email is already registered. If the input is valid and the user is not already registered, the function should add the user to the platform and return `True... | Here is the Python code to solve the given problem in the markdown block:
```python
def add_user_to_platform(username: str, email: str) -> bool:
"""
Adds a new user to the bookmarking platform.
Args:
username (str): The username of the user.
email (str): The email address of the user.
... |
code_exercise_27k | 22,949 | Write a Python function to find the smallest absolute difference between two adjacent elements in a given list using a rolling window of size 3. The function should return the smallest difference found within any window of size 3 in the list.
The code should start with:
```python
from typing import List
def find_smal... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_difference_in_window(lst: List[int]) -> int:
"""
Finds the smallest absolute difference between two adjacent elements in a given list using a rolling window of size 3.
Args:
... |
code_exercise_27k | 22,960 | Write a Python function to add a new bookmark to a user's collection of bookmarks. The collection is represented as a dictionary where each key is a unique identifier for a bookmark, and the value is another dictionary containing the bookmark's details (title, URL, and description). The function should ensure that the ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def add_bookmark(collection: Dict[str, Dict[str, str]], bookmark: Dict[str, str]) -> Dict[str, Dict[str, str]]:
"""
Add a new bookmark to a user's collection of bookmarks.
Args:
collection (... |
code_exercise_27k | 22,963 | Write a Python function to remove a bookmark from a user's collection. The function should handle the removal of the bookmark and return a boolean indicating whether the removal was successful. If the bookmark does not exist in the collection, the function should raise a `KeyError`.
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_bookmark(user_collection: dict, bookmark: str) -> bool:
"""
Remove a bookmark from a user's collection.
Args:
user_collection (dict): A dictionary representing the user's collection of bookmarks.
... |
code_exercise_27k | 22,969 | Write a Python function to search for bookmarks based on keywords. The function should take a list of dictionaries representing bookmarks and a keyword string as input. Each bookmark dictionary contains 'title' and 'description' keys. The function should return a list of bookmarks that contain the keyword in their titl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def search_bookmarks(bookmarks: List[Dict[str, str]], keyword: str) -> List[Dict[str, str]]:
"""
Search for bookmarks based on a keyword.
Args:
bookmarks (List[Dict[str, str]]): A list... |
code_exercise_27k | 22,970 | Write a Python function to find the length of the longest strictly increasing subsequence within a rolling window of size `k` for a given list of integers. The function should return the length of the longest subsequence found within any valid window of size `k`.
The code should start with:
```python
from typing impor... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence_in_window(arr: List[int], k: int) -> int:
"""
Find the length of the longest strictly increasing subsequence within a rolling window of size k.
Args:
arr (... |
code_exercise_27k | 22,971 | Write a Python function to check if a given list is sorted in descending order. The function should return `True` if the list is sorted in descending order, and `False` otherwise. The code should start with:
```python
from typing import List
def is_sorted_descending(lst: List[int]) -> bool:
```
| 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]): The input list to be checked.
Returns:
bool: Tr... |
code_exercise_27k | 22,975 | Write a Python function to determine if a given list of integers forms a valid arithmetic sequence. An arithmetic sequence is a sequence of numbers in which the difference between consecutive elements is constant. The function should return `True` if the list is a valid arithmetic sequence and `False` otherwise.
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_arithmetic_sequence(lst: List[int]) -> bool:
"""
Determines if a given list of integers forms a valid arithmetic sequence.
Args:
lst (List[int]): The list of integers to check.
R... |
code_exercise_27k | 22,980 | Write a Python function to sort a list using the merge sort algorithm. The function should be able to handle lists of integers and return the sorted list in ascending order. The code should start with:
```python
from typing import List
def merge_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(arr: List[int]) -> List[int]:
"""
Sorts a list using the merge sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
List[int]: The s... |
code_exercise_27k | 22,988 | Write a Python function to check if a given list contains any substring which is a palindrome with a length of 5 within a rolling window of size 6. The function should return `True` if such a palindrome exists, otherwise `False`.
The code should start with:
```python
from typing import List
def contains_palindrome_in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_palindrome(string: str) -> bool:
"""
Check if a given string is a palindrome.
Parameters:
string (str): The string to be checked.
Returns:
bool: True if the string is a palindrom... |
code_exercise_27k | 22,992 | Write a Python function to find the largest multiple of a given number within a rolling window of a specified size. The function should take two arguments: the number to find multiples of and the rolling window containing a list of numbers. The function should return the largest multiple of the given number within the ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_largest_multiple_in_window(number: int, window: List[int]) -> Optional[int]:
"""
Find the largest multiple of a given number within a rolling window of size 8.
Args:
numbe... |
code_exercise_27k | 22,993 | Write a Python function to calculate the sum of all even numbers within a given range. The function should handle both inclusive and exclusive ranges and ensure that the start of the range is less than or equal to the end of the range. If the start is greater than the end, the function should raise a `ValueError`.
The... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_even_numbers(start: int, end: int) -> int:
"""
Calculates the sum of all even numbers in a given range.
Args:
start (int): The starting number of the range (inclusive).
end (int): The ending numbe... |
code_exercise_27k | 23,000 | Write a Python function to find the maximum sum subarray in a given list of integers. The function should return a tuple containing the start index, end index, and the maximum sum of the subarray. The code should start with:
```python
from typing import List, Tuple
def find_max_sum_subarray(arr: List[int]) -> Tuple[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_sum_subarray(arr: List[int]) -> Tuple[int, int, int]:
"""
Finds the maximum sum subarray in a given list of integers.
Args:
arr (List[int]): The input list of integers.
... |
code_exercise_27k | 23,001 | Write a Python function to calculate the sum of the digits of a given number. The function should handle both positive and negative integers. If the number is negative, the function should ignore the negative sign and only sum the digits.
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 whose digits need to be summed.
Returns:
int: The sum of the digits.
Ex... |
code_exercise_27k | 23,003 | Write a Python function to find the minimum value in a given list of integers. If the list is empty, the function should raise a `ValueError` with the message "The list is empty". The function should start with:
```python
from typing import List
def find_minimum(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_minimum(numbers: List[int]) -> int:
"""
Finds the minimum value in a given list of integers.
Args:
numbers (List[int]): The list of integers to search for the minimum value.
Re... |
code_exercise_27k | 23,004 | Write a Python function to allow users to comment on bookmarks. The function should take a bookmark dictionary and a comment string as input, and return the modified bookmark dictionary with the new comment added to the list of existing comments. The bookmark dictionary should have at least the following keys:
- 'url':... | Here is the Python code to solve the given problem in the markdown block:
```python
def add_comment(bookmark: dict, comment: str) -> dict:
"""
Add a comment to a bookmark.
Args:
bookmark (dict): The bookmark dictionary containing the bookmark details.
It should have at least the followi... |
code_exercise_27k | 23,005 | 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:
"""
Counts 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.
Args:
s... |
code_exercise_27k | 23,008 | 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 numbers less than 2, which are not prime by definition.
The code should start with:
```python
def is_prime(num: int) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(num: int) -> bool:
"""
Determine if a given number is a prime number.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
""... |
code_exercise_27k | 23,011 | Write a Python function to determine if a given number is a Fibonacci number. The function should return `True` if the number is a Fibonacci number and `False` otherwise. The function should be efficient and handle edge cases such as negative numbers and zero.
The code should start with:
```python
def is_fibonacci_num... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_fibonacci_number(n: int) -> bool:
"""
Determine if a given number is a Fibonacci number.
Parameters:
n (int): The number to check.
Returns:
bool: True if the number is a Fibonacci number, False o... |
code_exercise_27k | 23,012 | Write a Python function to calculate the factorial of a given positive integer using a recursive approach. The function should handle the base case and recursive case appropriately. The code should start with:
```python
def factorial_recursive(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_recursive(n: int) -> int:
"""
Calculate the factorial of a given positive integer using a recursive approach.
Args:
n (int): The positive integer for which to calculate the factorial.
Returns:
... |
code_exercise_27k | 23,017 | Write a Python function to generate a report of a user's bookmarking activity. The function should take a list of dictionaries representing bookmarks as input and return a formatted string report. Each dictionary in the list should have the following keys:
- `title` (str): The title of the bookmark.
- `url` (str): The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def generate_bookmark_report(bookmarks: List[Dict[str, str]]) -> str:
"""
Generates a report of a user's bookmarking activity.
Args:
bookmarks (List[Dict[str, str]]): A list of diction... |
code_exercise_27k | 23,019 | Write a Python function to find the maximum and minimum values in a given list of numbers. The function should handle edge cases such as an empty list and non-list inputs by raising appropriate exceptions. The code should start with:
```python
from typing import List, Tuple
def find_max_min(lst: List[int]) -> Tuple[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_min(lst: List[int]) -> Tuple[int, int]:
"""
Finds the maximum and minimum values in a given list of integers.
Args:
lst (List[int]): A list of integers.
Returns:
... |
code_exercise_27k | 23,031 | Write a Python function to reverse a given string without using the built-in `reverse()` method or slicing. The function should handle both non-empty and empty strings. The code should start with:
```python
def reverse_string(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverse a given string without using the built-in reverse() method or slicing.
Args:
string (str): The string to be reversed.
Returns:
str: The reversed strin... |
code_exercise_27k | 23,032 | Write a Python function to remove duplicate prime numbers from a list within a rolling window of size 5. The function should consider only prime numbers and maintain the order of their first appearance in the list. The code should start with:
```python
from typing import List
def remove_duplicate_prime_numbers_in_wind... | 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
"""
... |
code_exercise_27k | 23,036 | Write a Python function to find the largest common prefix of a list of given strings. The function should return an empty string if no common prefix exists.
The code should start with:
```python
from typing import List
def find_common_prefix_of_list(strings: List[str]) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_prefix_of_list(strings: List[str]) -> str:
"""
Find the largest common prefix of a list of given strings.
Args:
strings (List[str]): A list of strings to find the common pref... |
code_exercise_27k | 23,037 | Write a Python function to validate whether a given string is a valid email address according to a specific regular expression pattern. The function should return `True` if the email is valid and `False` otherwise. The code should start with:
```python
import re
def is_valid_email(email: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_valid_email(email: str) -> bool:
"""
Validates whether a given string is a valid email address.
Args:
email (str): The string to be checked for email validity.
Returns:
bool: True if t... |
code_exercise_27k | 23,039 | Write a Python function to remove all duplicates from a given list of integers while maintaining the order of the first occurrence of each element. The function should start with:
```python
from typing import List
def remove_duplicates_keep_order(nums: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def remove_duplicates_keep_order(nums: List[int]) -> List[int]:
"""
Removes duplicates from a list of integers while maintaining the order of the first occurrence of each element.
Args:
nu... |
code_exercise_27k | 23,040 | Write a Python function to find the smallest common multiple (LCM) of three given integers. The function should be able to handle both positive and negative integers, and should return the absolute value of the LCM. The code should start with:
```python
def find_lcm_of_three(a: int, b: int, c: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_lcm_of_three(a: int, b: int, c: int) -> int:
"""
Calculate the smallest common multiple (LCM) of three given integers.
Args:
a (int): First integer.
b (int): Second integer.
c (int): Third i... |
code_exercise_27k | 23,042 | Write a Python function to remove duplicates from a given list while preserving the order of the elements. The function should efficiently handle the removal of duplicates using a set to track seen elements and a list to maintain the order.
The code should start with:
```python
from typing import List
def remove_dupl... | 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 elements.
Args:
lst (List[int]): The input list from wh... |
code_exercise_27k | 23,046 | Write a Python function to calculate the area of a triangle given its base and height. The function should handle non-positive inputs by raising a `ValueError`.
The code should start with:
```python
def triangle_area(base: float, height: float) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def triangle_area(base: float, height: float) -> float:
"""
Calculate the area of a triangle given its base and height.
Args:
base (float): The base length of the triangle.
height (float): The height of the ... |
code_exercise_27k | 23,052 | Write a Python function to remove all punctuation from a given string. The function should handle various types of punctuation and return the cleaned string. The code should start with:
```python
import string
def remove_punctuation(text: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
import string
def remove_punctuation(text: str) -> str:
"""
Removes all punctuation from a given string.
Args:
text (str): The input string from which punctuation needs to be removed.
Returns:
str: T... |
code_exercise_27k | 23,053 | Write a Python function to find the middle element(s) of a given list. If the list has an odd number of elements, return the single middle element. If the list has an even number of elements, return the two middle elements. The function should handle lists of any length, including 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_middle_elements(lst: List[int]) -> List[int]:
"""
Finds the middle element(s) of a given list.
Args:
lst (List[int]): The list to find the middle element(s) from.
Returns:
... |
code_exercise_27k | 23,055 | 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:
"""
Counts the number of vowels in the given string, considering both uppercase and lowercase vowels.
Args:
string (str): The string to count vowels from.
Returns:
... |
code_exercise_27k | 23,057 | Write a Python function to remove all duplicate characters from a given string while preserving the order of the first occurrence of each character. The function should handle any string input, including empty strings and strings with all unique characters. The code should start with:
```python
def remove_duplicate_cha... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_duplicate_chars(string: str) -> str:
"""
Removes duplicate characters from a given string while preserving the order of their first occurrence.
Args:
string (str): The input string from which to remove du... |
code_exercise_27k | 23,058 | Write a Python function to convert a given binary number represented as a string to its decimal equivalent. The function should handle binary numbers of any length and should raise appropriate exceptions for invalid inputs, such as non-string inputs or binary strings containing characters other than '0' and '1'.
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
def binary_to_decimal(binary_number: str) -> int:
"""
Convert a binary number represented as a string to its decimal equivalent.
Args:
binary_number (str): The binary number to be converted.
Returns:
in... |
code_exercise_27k | 23,067 | Write a Python function to determine whether a given number is a perfect square or not. The function should use the binary search algorithm to efficiently check if the number is a perfect square.
The code should start with:
```python
def is_perfect_square(n: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_square(n: int) -> bool:
"""
Determines whether a given number is a perfect square using the binary search algorithm.
Args:
n (int): The number to check.
Returns:
bool: True if the number ... |
code_exercise_27k | 23,069 | Write a Python function to find the common elements between two given lists, ensuring that the function returns a list of unique common elements in the order they appear in the first list. The function should handle lists of any data type and should be efficient even for large lists.
The code should start with:
```pyt... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def find_common_elements(list1: List[Any], list2: List[Any]) -> List[Any]:
"""
Finds the common elements between two lists and returns them in the order they appear in the first list.
Args:
... |
code_exercise_27k | 23,072 | Write a Python function to convert a given decimal number to its binary representation. The function should handle non-negative integers only. If the input is 0, the function should return '0'. The function should not use Python's built-in `bin()` function or any other built-in conversion functions.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(decimal_num: int) -> str:
"""
Convert a non-negative decimal number to its binary representation.
Args:
decimal_num (int): The non-negative decimal number to convert.
Returns:
str:... |
code_exercise_27k | 23,078 | Write a Python function to sort a given list of integers in ascending order using the bubble sort algorithm. The function should handle lists of any length, including empty lists. The code should start with:
```python
from typing import List
def bubble_sort(nums: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(nums: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the bubble sort algorithm.
Args:
nums (List[int]): The list of integers to be sorted.
... |
code_exercise_27k | 23,080 | Write a Python function to find the smallest power of 4 within a rolling window of size 9, considering only elements divisible by 8. The function should return the smallest power of 4 found within any valid window, or -1 if no such element exists.
The code should start with:
```python
from typing import List
def smal... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def smallest_power_of_4_in_window(numbers: List[int]) -> int:
"""
Finds the smallest power of 4 within a rolling window of size 9,
considering only elements divisible by 8.
Args:
numbers... |
code_exercise_27k | 23,081 | Write a Python function to find the middle element of a given list. If the list has an even number of elements, return the element at the index just before the middle. The function should handle lists of any data type and should raise a `ValueError` if the input list is empty.
The code should start with:
```python
fro... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def find_middle_element(lst: List[Any]) -> Any:
"""
Find the middle element of a given list. If the list has an even number of elements,
return the element at the index just before the middle.
... |
code_exercise_27k | 23,083 | Write a Python function to count the number of words with a specific length in a given sentence. The function should handle punctuation and ignore it when determining word lengths. The code should start with:
```python
def count_words_with_length(sentence: str, length: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import string
def count_words_with_length(sentence: str, length: int) -> int:
"""
Counts the number of words with a specific length in a given sentence, ignoring punctuation.
Args:
sentence (str): The sentence to a... |
code_exercise_27k | 23,086 | Write a Python function to find the length of the longest continuous subsequence of identical elements in a given list. The function should handle lists of any type of elements, including integers, strings, and other comparable types. The code should start with:
```python
from typing import List, Any
def longest_ident... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def longest_identical_subsequence_length(lst: List[Any]) -> int:
"""
Find the length of the longest continuous subsequence of identical elements in a given list.
Args:
lst: A list of el... |
code_exercise_27k | 23,087 | Write a Python function to calculate the sum of digits in 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 digits in a given number.
Args:
number (int): The number for which the sum of digits needs to be calculated.
Returns:
int: The sum of dig... |
code_exercise_27k | 23,090 | Write a Python function to find the prime factors of a given number. The function should return a list of prime factors in ascending order. The code should start with:
```python
from typing import List
def find_prime_factors(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_prime_factors(n: int) -> List[int]:
"""
Find the prime factors of a given number.
Args:
n (int): The number for which prime factors need to be found.
Returns:
List[int]... |
code_exercise_27k | 23,096 | Write a Python function to find the maximum subarray sum of a given list of integers. The function should handle both positive and negative integers and return the maximum sum of any contiguous subarray within the list. The function should raise a `ValueError` if the input list is empty.
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 max_subarray_sum(arr: List[int]) -> int:
"""
Finds the maximum subarray sum of a given list of integers.
Args:
arr (List[int]): A list of integers.
Returns:
int: The maximum... |
code_exercise_27k | 23,098 | Write a Python function to filter a list of bookmarks based on specific metadata fields. Each bookmark is represented as a dictionary with keys such as 'title', 'author', 'date', etc. The function should take a list of bookmarks and a dictionary of filters as input, and return a list of bookmarks that match all the spe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def filter_bookmarks(bookmarks: List[Dict[str, str]], filters: Dict[str, str]) -> List[Dict[str, str]]:
"""
Filter bookmarks based on specific metadata fields.
Args:
bookmarks (list): ... |
code_exercise_27k | 23,102 | Write a Python function to determine if a given string is a valid palindrome, ignoring spaces, punctuation, and case sensitivity. The function should return `True` if the string is a palindrome and `False` otherwise. The code should start with:
```python
def is_valid_palindrome(s: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_valid_palindrome(s: str) -> bool:
"""
Determine if a given string is a valid palindrome, ignoring spaces, punctuation, and case sensitivity.
Args:
s (str): The string to check.
Returns:
bool: Tru... |
code_exercise_27k | 23,104 | Write a Python function to find the missing number in a given list of consecutive integers from 1 to n. The list contains all integers from 1 to n except one missing number. The function should efficiently determine the missing number using mathematical operations.
The code should start with:
```python
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_missing_number(numbers: List[int]) -> int:
"""
Find the missing number in a list of consecutive integers from 1 to n.
Args:
numbers (List[int]): A list of integers in consecutive or... |
code_exercise_27k | 23,111 | Write a Python function to convert a given decimal number to its hexadecimal representation. The function should handle non-negative integers and return the hexadecimal string in uppercase letters. The code should start with:
```python
def decimal_to_hexadecimal(decimal_num: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_hexadecimal(decimal_num: int) -> str:
"""
Converts a given decimal number to its hexadecimal representation.
Args:
decimal_num (int): The decimal number to convert. Must be a non-negative integer.
... |
code_exercise_27k | 23,127 | Write a Python function to calculate the power of a given base and exponent using an iterative approach. The function should handle both positive and negative exponents, as well as the case where the exponent is zero. The code should start with:
```python
def calculate_power_iterative(base: int | float, exponent: int) ... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_power_iterative(base: int | float, exponent: int) -> int | float:
"""
Calculate the power of a given base and exponent using an iterative approach.
Args:
base (int | float): The base number.
ex... |
code_exercise_27k | 23,129 | Write a Python function to find the second largest number in a given list of integers. The function should handle edge cases such as lists with fewer than two elements and lists where all elements are the same. The code should start with:
```python
from typing import List
def find_second_largest(numbers: List[int]) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_second_largest(numbers: List[int]) -> int:
"""
Finds the second largest number in a given list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
int:... |
code_exercise_27k | 23,132 | Write a Python function to calculate the number of all possible combinations of N elements taken K at a time without using the `math.comb` function. The function should use the formula for combinations, which is given by:
\[ C(N, K) = \frac{N!}{K! \times (N - K)!} \]
The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def factorial(n: int) -> int:
"""
Calculate the factorial of a given number n.
Args:
n (int): The number to calculate the factorial for.
Returns:
int: The factorial of the numbe... |
code_exercise_27k | 23,137 | Write a Python function to find the missing numbers in a given list of consecutive integers from 1 to n. The function should return a list of integers that are missing from the given list.
The code should start with:
```python
from typing import List
def find_missing_numbers(n: int, lst: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_missing_numbers(n: int, lst: List[int]) -> List[int]:
"""
Finds the missing numbers in the list of consecutive integers from 1 to n.
Args:
n (int): The maximum integer value in the ... |
code_exercise_27k | 23,140 | Write a Python function to generate a list of "top users" based on their contribution to the platform. The function should take a dictionary of user data and an optional integer parameter specifying the number of top users to return. The dictionary keys are user IDs, and the values are their contributions. The function... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Tuple
def generate_top_users_list(users_data: Dict[str, int], num_users: int = 10) -> List[Tuple[str, int]]:
"""
Generates a list of top users based on their contribution to the platform.
Arg... |
code_exercise_27k | 23,144 | Write a Python function to calculate the perimeter of a rectangle given its length and width. The function should handle non-negative inputs and return the perimeter as a float. The code should start with:
```python
def calculate_perimeter(length: float, width: float) -> float:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_perimeter(length: float, width: float) -> float:
"""
Calculates the perimeter of a rectangle given its length and width.
Args:
length (float): The length of the rectangle.
width (float): The wi... |
code_exercise_27k | 23,145 | Write a Python function to filter bookmarks based on their popularity within a specific time period and return the top n most popular bookmarks. The function should take a list of dictionaries representing bookmarks, a start date, an end date, and an integer n as inputs. Each bookmark dictionary contains "url", "title"... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
import datetime
def filter_and_sort_bookmarks(bookmarks: List[Dict], start_date: datetime.date, end_date: datetime.date, n: int) -> List[Dict]:
"""
Filters bookmarks based on their popularity withi... |
code_exercise_27k | 23,147 | Write a Python function to determine whether 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 itself). The function should return `True` if the number is a perfect number and `False` otherwise.
The code should start with:
```python
de... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_number(num: int) -> bool:
"""
Determine whether 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 itself).
For example, ... |
code_exercise_27k | 23,149 | Write a Python function to find the kth smallest element in a given list of integers efficiently. The function should handle edge cases such as an empty list or an invalid k value. The code should start with:
```python
from typing import List
def find_kth_smallest(nums: List[int], k: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import heapq
def find_kth_smallest(nums: List[int], k: int) -> int:
"""
Finds the kth smallest element in a given list of integers efficiently.
Args:
nums (List[int]): A list of integers.
... |
code_exercise_27k | 23,158 | Write a Python function to calculate the product of all elements in a given list of numbers. The function should handle both integer and floating-point numbers. If the input list is empty, the function should raise a `ValueError` with a specific message.
The code should start with:
```python
from typing import List
d... | 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:
"""
Calculate the product of all elements in a list of numbers.
Args:
numbers (List[float]): A list of numbers (integers or floats).
... |
code_exercise_27k | 23,160 | Write a Python function to calculate the total number of bookmarks added by each user from a list of bookmark dictionaries. Each bookmark dictionary contains two keys: 'user' (representing the user who added the bookmark) and 'url' (representing the bookmarked URL). The function should return a dictionary where the key... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def calculate_bookmark_count(bookmarks: List[Dict[str, str]]) -> Dict[str, int]:
"""
Calculate the total number of bookmarks added by each user.
Args:
bookmarks (list): A list of dicti... |
code_exercise_27k | 23,165 | Write a Python function to find the first non-repeated character in a given string. The function should return the character if it exists, otherwise return `None`. The code should start with:
```python
def find_first_non_repeated_char(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_first_non_repeated_char(string: str) -> str:
"""
Find the first non-repeated character in a given string.
Args:
string (str): The input string.
Returns:
str: The first non-repeated character, o... |
code_exercise_27k | 23,166 | Write a Python function to calculate the number of all possible combinations of N elements taken 2 at a time. The function should use the mathematical formula for combinations, which is \( C(n, r) = \frac{n!}{r!(n-r)!} \), where \( n \) is the total number of elements and \( r \) is the number of elements to choose at ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def find_combinations(n: int) -> int:
"""
Calculates the number of all possible combinations of N elements taken 2 at a time.
Args:
n (int): The number of elements.
Returns:
int: The number... |
code_exercise_27k | 23,171 | Write a Python function to calculate the area of a trapezoid given its bases and height. The function should handle invalid inputs by raising a `ValueError` if any of the inputs are less than or equal to zero.
The code should start with:
```python
def calculate_trapezoid_area(base1: float, base2: float, height: float)... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_trapezoid_area(base1: float, base2: float, height: float) -> float:
"""
Calculate the area of a trapezoid given its bases and height.
Args:
base1 (float): The length of the first base of the trapezoid.... |
code_exercise_27k | 23,176 | Write a Python function to calculate the sum of an arbitrary number of numbers. The function should accept a variable number of arguments and return the sum of all the provided numbers. The function should handle both integers and floating-point numbers.
The code should start with:
```python
from typing import Union, ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union, List
def calculate_sum_of_numbers(*args: Union[int, float]) -> Union[int, float]:
"""
Calculate the sum of an arbitrary number of numbers.
Args:
*args (int or float): A variable number of ... |
code_exercise_27k | 23,180 | Write a Python function to customize the appearance and layout of a bookmarking platform. The function should accept two parameters: `appearance` and `layout`. The `appearance` parameter should be one of the following strings: "light", "dark", or "colorful". The `layout` parameter should be one of the following strings... | Here is the Python code to solve the given problem in the markdown block:
```python
def customize_platform(appearance: str, layout: str) -> str:
"""
Customize the appearance and layout of the bookmarking platform.
Args:
appearance (str): The desired appearance of the platform.
Options: ... |
code_exercise_27k | 23,183 | Write a Python function to determine whether a given number is a perfect square using the binary search algorithm. 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(n: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_perfect_square(n: int) -> bool:
"""
Determine whether a given number is a perfect square using binary search.
Args:
n (int): The input number to check.
Returns:
bool: True if the number is a perf... |
code_exercise_27k | 23,186 | Write a Python function to find the frequency of each element in a given list. The function should return a dictionary where the keys are the unique elements in the list and the values are the frequencies of those elements. The code should start with:
```python
from typing import List, Dict
def find_frequency(lst: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def find_frequency(lst: List[int]) -> Dict[int, int]:
"""
Finds the frequency of each element in a given list.
Args:
lst (List[int]): A list of integers.
Returns:
Dict[int... |
code_exercise_27k | 23,188 | Write a Python function to implement the Ternary Search algorithm. The function should find the index of a target element in a sorted array. If the target element is not found, the function should return -1. The code should start with:
```python
from typing import List
def ternary_search(arr: List[int], target: int) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def ternary_search(arr: List[int], target: int) -> int:
"""
Perform Ternary Search to find the index of the target in a sorted array.
Args:
arr (List[int]): A sorted list of integers.
... |
code_exercise_27k | 23,190 | Write a Python function to check whether a given string is a valid parenthesis combination or not. The string may contain any combination of the following characters: `(`, `)`, `[`, `]`, `{`, `}`. The function should return `True` if the string is a valid parenthesis combination, and `False` otherwise. A valid parenthe... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_valid_parenthesis(string: str) -> bool:
"""
Check whether a given string is a valid parenthesis combination or not.
The function takes a string as the input and checks if the string is a valid parenthesis combination... |
code_exercise_27k | 23,191 | Write a Python function to calculate the factorial of a given non-negative integer using an iterative approach. The function should handle edge cases such as zero and should raise a `ValueError` for negative inputs.
The code should start with:
```python
def factorial(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculate the factorial of a given non-negative integer.
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 is... |
code_exercise_27k | 23,192 | Write a Python function to calculate the area of a square given its side length. The function should handle input validation to ensure the side length is a non-negative number. If the input is invalid, the function should raise a `ValueError` with an appropriate message.
The code should start with:
```python
def calcu... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_square_area(side_length: float) -> float:
"""
Calculate the area of a square given its side length.
Args:
side_length (float): The length of the side of the square.
Returns:
float: The are... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.