source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 13,629 | Write a Python function to count the number of nodes in a binary tree that have both left and right children. The binary tree is represented as a nested dictionary where each node has keys "data", "left", and "right". The "left" and "right" keys point to the left and right child nodes, respectively. If a node does not ... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_nodes_with_both_children(node: dict) -> int:
"""
Count the number of nodes in a binary tree that have both left and right children.
Args:
node (dict): The root node of the binary tree represented as a dict... |
python_sharegpt_23k | 13,632 | Write a Python function to sort a hashtable by its values in descending order. The values are strings and the keys are integers. The hashtable may contain up to 1 million key-value pairs. Optimize your solution for time complexity.
The code should start with:
```python
from typing import Dict, List, Tuple
def sort_ha... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Tuple
def sort_hashtable_by_values(hashtable: Dict[int, str]) -> Dict[int, str]:
"""
Sorts a hashtable by its values in descending order. The values are strings and the keys are integers.
Arg... |
python_sharegpt_23k | 13,640 | Write a Python function to check if the parentheses, curly brackets, square brackets, and angle brackets in a given expression are balanced. The function should return `True` if all the brackets are balanced and `False` otherwise. The code should start with:
```python
def is_balanced(expression: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_balanced(expression: str) -> bool:
"""
Check if the parentheses, curly brackets, square brackets, and angle brackets in a given expression are balanced.
Args:
expression (str): The expression to check for bal... |
python_sharegpt_23k | 13,641 | Write a Python function to count the number of words in a given string, ignoring punctuation marks. A word is defined as a sequence of characters separated by whitespace (spaces, tabs, or newlines). The function should start with:
```python
from typing import List
def count_words_ignore_punctuation(input_string: str) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import string
def count_words_ignore_punctuation(input_string: str) -> int:
"""
Counts the number of words in a given string, ignoring punctuation marks.
Args:
input_string (str): The input ... |
python_sharegpt_23k | 13,643 | Write a Python function to find the sum of all numbers in a given array, ensuring that the function uses a constant amount of space and has a time complexity of O(n), where n is the number of elements in the array. The function should handle negative numbers correctly and return the correct sum.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_total_sum(arr: List[int]) -> int:
"""
Calculate the sum of all numbers in the given array.
Args:
arr (List[int]): The array of integers to sum.
Returns:
int: The sum of... |
python_sharegpt_23k | 13,646 | Write a Python function to reverse a given string without using any built-in string reversal functions or methods. The function should iterate through the string in reverse order and construct the reversed string character by character.
The code should start with:
```python
def reverse_string(input_string: str) -> str... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(input_string: str) -> str:
"""
Reverses the given string without using any built-in string reversal functions or methods.
Args:
input_string (str): The string to be reversed.
Returns:
... |
python_sharegpt_23k | 13,651 | Write a Python function to find the minimum value in a list of integers using recursion, with the following constraints:
1. The function should be implemented using recursion instead of a loop.
2. The function should have a time complexity of O(n) where n is the length of the input.
3. The function should not use any b... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_minimum_recursive(numbers: List[int]) -> int:
"""
Find the minimum value in a list of integers using recursion.
Args:
numbers (List[int]): The list of integers from which to find th... |
python_sharegpt_23k | 13,652 | Write a Python function to sort an array of numbers from least to greatest without using any built-in sorting functions or libraries. The input array will always contain at least 2 numbers and at most 10^6 numbers. All numbers in the array will be distinct and will be in the range of -10^9 to 10^9. The function should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_sort(arr: List[int]) -> List[int]:
"""
Sorts an array of numbers from least to greatest using the Merge Sort algorithm.
Args:
arr (List[int]): The array of distinct integers to be... |
python_sharegpt_23k | 13,654 | Write a Python function to determine if all elements of a smaller list `B` are present in a larger list `A` in the same order, and to find the starting index of the first occurrence of `B` in `A`. The function should return a tuple containing a boolean value indicating whether `B`'s elements are all present in `A` in t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_subset_in_order(A: List[int], B: List[int]) -> Tuple[bool, int]:
"""
Determines if all elements of list B are present in list A in the same order
and returns the starting index of the... |
python_sharegpt_23k | 13,663 | Write a Python function to count the number of occurrences of a target integer in a given list of integers. The list will always have at least 100 elements and will not exceed 1000 elements. The target integer will always be within the range of -1000 to 1000. The function should handle cases where the list contains dup... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_occurrences(lst: List[int], target: int) -> int:
"""
Counts the number of occurrences of a target integer in a given list of integers.
Args:
lst (List[int]): The list of integers t... |
python_sharegpt_23k | 13,665 | Write a Python function to determine if a given integer is a prime number or a composite number. The function should return a string indicating whether the number is "prime" or "composite". The code should start with:
```python
def check_prime_or_composite(num: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def check_prime_or_composite(num: int) -> str:
"""
Determines if a given integer is a prime number or a composite number.
Args:
num (int): The integer to check.
Returns:
str: A string indicating whether... |
python_sharegpt_23k | 13,669 | Write a Python function to reverse a given string using recursion. The function should return the reversed string. The code should start with:
```python
def reverse_string(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def reverse_string(s: str) -> str:
"""
Reverses the given string using recursion.
Args:
s (str): The string to be reversed.
Returns:
str: The reversed string.
"""
# Base case: if the string is... |
python_sharegpt_23k | 13,672 | Write a Python function to group email addresses by their domain and count the number of unique email addresses in each domain. The function should return a dictionary where the keys are domain names and the values are the counts of unique email addresses for each domain.
The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_unique_email_domains(emails: List[str]) -> Dict[str, int]:
"""
Groups email addresses by their domain and counts the number of unique email addresses in each domain.
Args:
em... |
python_sharegpt_23k | 13,673 | Write a Python function to remove all occurrences of a specific character from a given string without using any built-in string manipulation functions or libraries. The function should iterate through the string and construct a new string that excludes the specified character.
The code should start with:
```python
def... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_char(string: str, char: str) -> str:
"""
Removes all occurrences of a specific character from the given string.
Args:
string (str): The input string from which to remove the character.
char (str):... |
python_sharegpt_23k | 13,678 | Write a Python function to reverse the elements of a given array in-place without using any built-in reverse functions or additional data structures. The function should modify the original array and return it. The code should start with:
```python
from typing import List
def reverse_array_in_place(arr: List[int]) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array_in_place(arr: List[int]) -> List[int]:
"""
Reverses the elements of a given array in-place without using any built-in reverse functions or additional data structures.
Args:
... |
python_sharegpt_23k | 13,679 | Write a Python function to find all duplicates in an array of positive integers where each integer is between 1 and 10^6, inclusive. The array may contain up to 10^9 elements. Return the duplicates in descending order.
The code should start with:
```python
from typing import List
def find_duplicates(arr: List[int]) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_duplicates(arr: List[int]) -> List[int]:
"""
Find all duplicates in an array of positive integers and return them in descending order.
Args:
arr (List[int]): The array of positive i... |
python_sharegpt_23k | 13,683 | Write a Python function to merge two dictionaries into a single dictionary. The merged dictionary should include all keys from both input dictionaries, and in case of duplicate keys, the value from the second dictionary should overwrite the value from the first dictionary. The merged dictionary should be sorted in asce... | Here is the Python code to solve the given problem in the markdown block:
```python
def merge_dictionaries(a: dict, b: dict) -> dict:
"""
Merge two dictionaries into a single dictionary. The merged dictionary includes all keys from both input dictionaries.
In case of duplicate keys, the value from the secon... |
python_sharegpt_23k | 13,687 | Write a Python function to sort an array of integers in descending order using the bubble sort technique. The array can contain up to 10^6 elements. The time complexity of the solution should be O(n^2), and the space complexity should be O(1). The code should start with:
```python
from typing import List
def bubble_so... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in descending order using the bubble sort technique.
Args:
arr (List[int]): The array of integers to b... |
python_sharegpt_23k | 13,688 | Write a Python function to reverse a given string without using any built-in functions or methods for string manipulation (e.g., `reverse()`, `slice()`, etc.). You can only use basic string operations and loops. The time complexity of your solution should be O(n), where n is the length of the input string.
The code sh... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(input_string: str) -> str:
"""
Reverses the input string without using any built-in functions or methods for string manipulation.
Args:
input_string (str): The string to be reversed.
Returns:... |
python_sharegpt_23k | 13,690 | Write a Python function to find all integer solutions (x, y, z) for the equation \( x^4 + y^4 = z^4 \) within a given range [1, n]. The function should return a list of tuples, where each tuple represents a valid solution (x, y, z).
The code should start with:
```python
from typing import List, Tuple
def find_solutio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_solutions(n: int) -> List[Tuple[int, int, int]]:
"""
Find all integer solutions (x, y, z) for the equation x^4 + y^4 = z^4 within the range [1, n].
Args:
n (int): The upper l... |
python_sharegpt_23k | 13,693 | Write a Python function to sort a list of numbers in ascending order using the bubble sort algorithm. The function should not use any built-in sorting functions or methods. The code should start with:
```python
from typing import List
def bubble_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 bubble_sort(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the bubble sort algorithm.
Args:
numbers (List[int]): The list of numbers to be sorted.... |
python_sharegpt_23k | 13,695 | Write a Python function to compute the number of ways to select `k` items from a list of `n` options, where each option has a unique ID. The function should use a recursive approach to count the number of valid combinations. The code should start with:
```python
from typing import List
def compute_ways(options: List[s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def compute_ways(options: List[str], k: int) -> int:
"""
Compute the number of ways to select k items from a list of n options.
Args:
options (List[str]): A list of options, each with a uniq... |
python_sharegpt_23k | 13,697 | Write a Python function to split a given string of tasks into a list of tasks, while removing any additional instructions enclosed in parentheses. The function should handle multiple sets of instructions within a single task and should also handle various delimiters such as commas, semicolons, and periods. The code sho... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def split_tasks(string: str) -> List[str]:
"""
Splits a given string of tasks into a list of tasks, removing any additional instructions enclosed in parentheses.
Args:
string (str)... |
python_sharegpt_23k | 13,708 | Write a Python function to perform bubble sort on an array of integers in ascending order. The function should not use any built-in sorting functions or libraries. The input array can contain duplicate elements and may have a length of up to 10^6. Your implementation should have a time complexity of O(n^2), where n is ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in ascending order using the bubble sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 13,710 | Write a Python function named `parse_urls` that takes a single string parameter representing a URL and returns a dictionary of query parameters extracted from the URL. The URL can contain multiple query parameters separated by "&". Each query parameter is represented by a key-value pair, with the key and value separate... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
from urllib.parse import unquote
def parse_urls(url: str) -> Dict:
"""
Parses the query parameters from a given URL and returns a dictionary.
Args:
url (str): The URL containing query parame... |
python_sharegpt_23k | 13,711 | Write a Python function to calculate the sum of all positive numbers in a given list without using any built-in functions such as `sum()` or `reduce()`. The function should be efficient enough to handle lists containing up to \(10^6\) numbers.
The code should start with:
```python
from typing import List
def calculat... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_sum_of_positive_numbers(numbers: List[int]) -> int:
"""
Calculate the sum of all positive numbers in a given list.
Args:
numbers (List[int]): A list of integers.
Returns:
... |
python_sharegpt_23k | 13,719 | Write a Python function that creates a callback mechanism. The callback function should take three arguments: a string, a number, and a boolean flag. The callback should store the input data and emit it when triggered. The callback can only be triggered once, and the flag must be set to `True` before the callback can b... | Here is the Python code to solve the given problem in the markdown block:
```python
def create_callback(input_string: str, input_number: int, input_flag: bool) -> callable:
"""
Creates a callback function that stores input data and emits it when triggered.
The callback can only be triggered once, and the fl... |
python_sharegpt_23k | 13,727 | Write a Python function to remove duplicates from a given list without using built-in methods, except for the loop. The original list should not be modified, and the resulting list should maintain the original order of elements. The time complexity of your solution should be O(n) and the space complexity should be O(1)... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_place(lst: List[int]) -> List[int]:
"""
Removes duplicates from the given list while maintaining the original order of elements.
The function modifies the list in place and r... |
python_sharegpt_23k | 13,736 | Write a Python function to find the longest word that can be constructed from a given set of characters, considering that each character in the set can be used multiple times. The function should handle case-insensitivity and have a time complexity of O(n), where n is the length of the dictionary.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Set
def find_longest_word(dictionary: List[str], characters: Set[str]) -> str:
"""
Finds the longest word in the dictionary that can be constructed using the given set of characters.
Each character ... |
python_sharegpt_23k | 13,738 | Write a Python function to determine whether a given number is a prime number using the Sieve of Eratosthenes algorithm. If the number is not prime, the function should return the smallest prime factor of the number. The function should efficiently handle numbers up to \(10^9\) and be optimized for performance.
The co... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime_or_smallest_prime_factor(n: int) -> int:
"""
Determines whether a given number is a prime number using the Sieve of Eratosthenes algorithm.
If the number is not prime, returns the smallest prime fac... |
python_sharegpt_23k | 13,739 | Write a Python function to find and return a list of prime numbers between a given range `[start, end]`. The function should use a maximum of 3 variables and should not use any built-in functions or libraries related to prime numbers. The code should start with:
```python
from typing import List
def find_primes_in_ran... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_primes_in_range(start: int, end: int) -> List[int]:
"""
Find and return a list of prime numbers between the given range [start, end].
Args:
start (int): The starting number of the r... |
python_sharegpt_23k | 13,742 | Write a Python function to find the length of the longest increasing subsequence (LIS) in a given list of positive integers. An increasing subsequence is a sequence of elements where each element is greater than the previous element. The function should use a dynamic programming approach to solve the problem efficientl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_increasing_subsequence(nums: List[int]) -> int:
"""
Calculate the length of the longest increasing subsequence in a given list of positive integers.
Args:
nums (List[int]): A lis... |
python_sharegpt_23k | 13,744 | Write a Python function to find the common elements between two tuples. The function should return a new tuple containing the common elements in the order they appear in the first tuple. The code should start with:
```python
from typing import Tuple
def find_common_elements(tuple1: Tuple, tuple2: Tuple) -> Tuple:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Tuple
def find_common_elements(tuple1: Tuple, tuple2: Tuple) -> Tuple:
"""
Find the common elements between two tuples and return them in a new tuple.
The common elements are returned in the order they ap... |
python_sharegpt_23k | 13,747 | Write a Python function to modify a given list of integers such that all elements are greater than five. If an element is already greater than or equal to five, it should remain unchanged. The function should start with:
```python
from typing import List
def ensure_elements_greater_than_five(vector: List[int]) -> List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def ensure_elements_greater_than_five(vector: List[int]) -> List[int]:
"""
Modifies the given list such that all elements are greater than five.
If an element is already greater than or equal to five... |
python_sharegpt_23k | 13,752 | Write a Python function to capitalize the first letter of every word in a given sentence, excluding the words 'and' and 'the'. Additionally, the function should remove any punctuation marks at the end of each word before capitalizing it. The sentence may contain multiple spaces between words and may end with a punctuat... | Here is the Python code to solve the given problem in the markdown block:
```python
def capitalize_sentence(sentence: str) -> str:
"""
Capitalizes the first letter of every word in a given sentence, excluding the words 'and' and 'the'.
Additionally, removes any punctuation marks at the end of each word befo... |
python_sharegpt_23k | 13,753 | Write a Python function to generate a sorted dataset of size N using a random sample, where N is a positive integer less than or equal to 10^6. The function should ensure that the dataset is sorted in ascending order. The code should start with:
```python
from typing import List
import random
def generate_sorted_datas... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_sorted_dataset(N: int) -> List[int]:
"""
Generates a sorted dataset of size N using a random sample of unique integers.
Args:
N (int): The size of the dataset to g... |
python_sharegpt_23k | 13,758 | Write a Python function to multiply two integers using bit manipulation without using the `*` operator. The function should handle both positive and negative integers. The code should start with:
```python
def multiply_using_bitwise(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def multiply_using_bitwise(a: int, b: int) -> int:
"""
Multiplies two integers using bit manipulation without using the '*' operator.
Args:
a (int): The first integer.
b (int): The second integer.
Retur... |
python_sharegpt_23k | 13,759 | Write a Python function to determine if two strings contain the same characters, regardless of their order, while considering uppercase and lowercase characters as the same, ignoring any whitespace characters, and handling special characters and symbols correctly. The function should also handle strings with multiple o... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Determines if two strings are anagrams, considering uppercase and lowercase characters as the same,
ignoring any whitespace characters, and handling special characters and ... |
python_sharegpt_23k | 13,764 | Write a Python function to compute the Nth Fibonacci number efficiently, where N can be any positive integer less than or equal to \(10^9\). The function should have a time complexity of \(O(\log N)\) and a space complexity of \(O(1)\). The function should use matrix exponentiation to achieve the desired performance.
... | Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci(N: int) -> int:
"""
Compute the Nth Fibonacci number using matrix exponentiation.
Args:
N (int): The position in the Fibonacci sequence to compute.
Returns:
int: The Nth Fibonacci number.
... |
python_sharegpt_23k | 13,767 | Write a Python function that iterates over a given list and prints each item, but skips any items that are divisible by 3. Additionally, the function should stop looping and exit if it encounters the number 7 in the list. The function should start with:
```python
from typing import List
def process_list(lst: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def process_list(lst: List[int]) -> None:
"""
Iterates over a list and prints each item, but skips any items that are divisible by 3.
Additionally, the function stops looping and exits if it encounte... |
python_sharegpt_23k | 13,769 | Write a Python function `reverse_list` that takes a list as input and returns a new list with the elements in reverse order. The original list should remain unchanged. Additionally, write a function `print_first_last` that takes a list as input and prints the first and last value of the list. Ensure that the time compl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list(input_list: List[int]) -> List[int]:
"""
Returns a new list with the elements of the input list in reverse order.
The original list remains unchanged.
Args:
input_list (... |
python_sharegpt_23k | 13,773 | Write a Python function to delete the last occurrence of a specified value from a list without using any built-in functions or methods. The function should return the modified list. If the value is not found in the list, the function should return the original list without any modifications. The function must adhere to... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def delete_last_occurrence(lst: List[int], value: int) -> List[int]:
"""
Deletes the last occurrence of a specified value from a list and returns the modified list.
If the value is not found, the ori... |
python_sharegpt_23k | 13,777 | Write a Python function to print the characters of a given string in reverse order using nested for loops. You are not allowed to use any built-in string manipulation functions or methods. The function should start with:
```python
def print_reverse_string(s: str) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_reverse_string(s: str) -> None:
"""
Prints the characters of the given string in reverse order using nested for loops.
Args:
s (str): The input string whose characters need to be printed in reverse order.
... |
python_sharegpt_23k | 13,778 | Write a Python function to hide specified words in a given text by replacing them with a specified symbol, such as asterisks (*). The function should ensure that the hidden words cannot be easily guessed by preserving the length of the words. The function should handle cases where the words to hide appear as part of an... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def hide_words(text: str, words_to_hide: List[str], symbol: str = '*') -> str:
"""
Hides specified words in a given text by replacing them with a specified symbol.
Args:
text (str)... |
python_sharegpt_23k | 13,779 | Write a Python function to calculate the sum of all prime numbers in a given list of integers. The function should include a helper function to check if a number is prime. The code should start with:
```python
from typing import List
def sum_of_primes(lst: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 13,780 | Write a Python function to sort a dictionary containing names and ages of people by the sum of the length of the names and ages in descending order. If two or more people have the same sum of lengths, sort them alphabetically by name in ascending order. The function should return the sorted dictionary.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_dictionary_by_length_and_name(dictionary: Dict[str, int]) -> Dict[str, int]:
"""
Sorts a dictionary containing names and ages by the sum of the length of the names and ages in descending order.
... |
python_sharegpt_23k | 13,784 | Write a Python function to calculate the product of two non-negative integers using only bitwise operations. The function should not use the multiplication operator (`*`) and should be implemented using a single bitwise operation.
The code should start with:
```python
def bitwise_multiply(num1: int, num2: int) -> int:... | Here is the Python code to solve the given problem in the markdown block:
```python
def bitwise_multiply(num1: int, num2: int) -> int:
"""
Calculate the product of two non-negative integers using only bitwise operations.
Args:
num1 (int): The first non-negative integer.
num2 (int): The seco... |
python_sharegpt_23k | 13,785 | Write a Python function to calculate the sum of even numbers in a given list. The function should raise a `ValueError` if any element in the list is negative and a `TypeError` if any element is not an integer. The function should handle these exceptions gracefully and provide appropriate error messages. The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_even_numbers(lst: List[int]) -> int:
"""
Calculate the sum of even numbers in a given list.
Args:
lst (List[int]): The list of integers to process.
Returns:
int: The ... |
python_sharegpt_23k | 13,786 | Write a Python function to find the second largest number in a given list of integers without using any built-in functions or methods to find the maximum or second maximum value. The function should handle edge cases such as an empty list or a list with fewer than two elements by returning `None`.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_second_largest(arr: List[int]) -> Optional[int]:
"""
Find the second largest number in a given list of integers without using any built-in functions or methods.
Args:
arr ... |
python_sharegpt_23k | 13,787 | Write a Python function to calculate the sum and average of all elements in a given array of integers. The function should not use any built-in functions or libraries to calculate the sum or average. The function should start with:
```python
from typing import List, Tuple
def calculate_sum_and_average(arr: List[int]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def calculate_sum_and_average(arr: List[int]) -> Tuple[int, float]:
"""
Calculate the sum and average of all elements in the given array.
Args:
arr (List[int]): The array of integers ... |
python_sharegpt_23k | 13,788 | Write a Python function to determine if one array is a subsequence of another array. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. The function should have a time complexity of O(n), where n is the length of th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_subsequence(arr1: List[int], arr2: List[int]) -> bool:
"""
Determines if one array is a subsequence of another array.
Args:
arr1 (List[int]): The main array to check against.
... |
python_sharegpt_23k | 13,790 | Write a Python function to convert a given list into a NumPy array, find the sum of all elements, and then sort the array in descending order. The function should ensure that the input list contains only positive integers and has a length of at least 5. If the input list does not meet these requirements, the function s... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import List, Union
def process_and_sort_array(lst: List[int]) -> Union[np.ndarray, str]:
"""
Convert a given list into a NumPy array, find the sum of all elements, and sort the array in descending... |
python_sharegpt_23k | 13,792 | Write a Python function to find the maximum value in a list of integers using recursion and binary search. The function should not use any built-in functions or libraries to find the maximum value. The time complexity of the solution should be O(n), where n is the length of the list.
The code should start with:
```pyt... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_recursive(arr: List[int]) -> int:
"""
Find the maximum value in a list of integers using recursion and binary search.
Args:
arr (List[int]): The list of integers from which to f... |
python_sharegpt_23k | 13,796 | Write a Python function to determine the specific data type of a given value and convert it to an integer if possible. If the conversion is not possible, return `None`. The function should handle common data types such as `bool`, `int`, `float`, `str`, and `NoneType`.
The code should start with:
```python
def convert_... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_integer(value) -> int:
"""
Determine the data type of the given value and convert it to an integer if possible.
If the conversion is not possible, return None.
Args:
value: The value to be convert... |
python_sharegpt_23k | 13,798 | Write a Python function to find the most common element(s) in a given list of integers. If there are multiple elements with the same maximum frequency, return all of them. The function should have a time complexity of O(n), where n is the length of the input list, and should not use any built-in sorting or counting fun... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def most_common(lst: List[int]) -> List[int]:
"""
Find the most common element(s) in a list of integers.
Args:
lst (List[int]): The list of integers from which to find the most common elemen... |
python_sharegpt_23k | 13,803 | Write a Python function to calculate the sum of the first `n` prime numbers. The function should take an integer `n` as input and return the sum of the first `n` prime numbers. The code should start with:
```python
def sum_of_first_n_primes(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_first_n_primes(n: int) -> int:
"""
Calculate the sum of the first n prime numbers.
Args:
n (int): The number of prime numbers to sum.
Returns:
int: The sum of the first n prime numbers.
"... |
python_sharegpt_23k | 13,808 | Write a Python function to reverse the order of nodes in a singly linked list with a time complexity of O(n) and using constant extra space. The function should adhere to the following constraints:
- Do not create a new linked list.
- Do not use recursion.
- Do not modify the existing nodes' values.
- Do not swap node ... | Here is the Python code to solve the given problem in the markdown block:
```python
class ListNode:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def reverse_linked_list(head: ListNode) -> ListNode:
"""
Reverses the order of nodes in a singly linked list.
... |
python_sharegpt_23k | 13,811 | Write a Python function to implement a binary search algorithm that searches for a specific element in a sorted list. The function should return the index of the first occurrence of the element if found, or -1 if not found. The function should handle duplicate elements and maintain a time complexity of O(log n).
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search_first_occurrence(arr: List[int], target: int) -> int:
"""
Searches for the first occurrence of a target element in a sorted list using binary search.
Args:
arr (List[int]):... |
python_sharegpt_23k | 13,813 | Write a Python function to fill a given MxN matrix with zeroes in a spiral pattern starting from the top-left corner and moving clockwise. The function should not use any extra space or modify the input matrix directly. The function should return the modified matrix.
The code should start with:
```python
from typing i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def fill_matrix_with_zeros(matrix: List[List[int]]) -> List[List[int]]:
"""
Fill a given MxN matrix with zeroes in a spiral pattern starting from the top-left corner and moving clockwise.
Args:
... |
python_sharegpt_23k | 13,818 | Implement a sorting algorithm that can handle both positive and negative integers efficiently. The algorithm should have a time complexity of O(n log n) and should handle duplicate values efficiently. You are not allowed to use any built-in sorting functions or libraries. The input will always be a list of integers, bo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Merge Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
Lis... |
python_sharegpt_23k | 13,825 | Write a Python function to find the maximum element in a given list without using the built-in `max()` function. The list may contain negative numbers and may be very large (up to \(10^6\) elements). The time complexity of your solution should be \(O(n)\) and the space complexity should be \(O(1)\).
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_element(lst: List[int]) -> int:
"""
Find the maximum element in a list without using the built-in max() function.
Args:
lst (List[int]): The list of integers from which to find ... |
python_sharegpt_23k | 13,827 | Write a Python function to find the index of the first occurrence of a given item in a list. The function should have a time complexity of O(n) and should not use any built-in functions or methods other than basic operations like `len()`.
The code should start with:
```python
from typing import List
def find_first_oc... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_first_occurrence(my_list: List[int], item_to_find: int) -> int:
"""
Find the index of the first occurrence of a given item in a list.
Args:
my_list (List[int]): The list in which to... |
python_sharegpt_23k | 13,833 | Write a Python function to find all prime numbers between two given integers (inclusive). The function should handle various edge cases such as negative input numbers, non-integer input, and the starting number being greater than the ending number. Additionally, if the ending number is less than 2, the function should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
python_sharegpt_23k | 13,835 | Write a Python function to find the maximum element in a 2D array of size `n x m`, where `n` is the number of rows and `m` is the number of columns. The array contains integers, which can be both positive and negative. The algorithm should have a time complexity of O(n*m) and a space complexity of O(1).
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_element(arr: List[List[int]]) -> int:
"""
Find the maximum element in a 2D array of size n x m.
Args:
arr (List[List[int]]): A 2D list of integers.
Returns:
int: Th... |
python_sharegpt_23k | 13,838 | Write a Python function to find the maximum number in a given array without using any built-in functions or methods to determine the maximum. If the array is empty, the function should return `None`.
The code should start with:
```python
from typing import List, Optional
def find_max_in_array(arr: List[int]) -> Optio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_max_in_array(arr: List[int]) -> Optional[int]:
"""
Find the maximum number in a given array without using built-in functions or methods.
Args:
arr (List[int]): The array o... |
python_sharegpt_23k | 13,841 | Write a Python function to calculate the sum of all positive even numbers from a given list, ensuring that the function has a time complexity of O(n), where n is the length of the list. The function should ignore any duplicate numbers in the list and should not use the built-in `sum()` function or any other external li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_positive_even_numbers(numbers: List[int]) -> int:
"""
Calculate the sum of all positive even numbers from a given list, ignoring duplicates.
Args:
numbers (List[int]): The list of in... |
python_sharegpt_23k | 13,846 | Write a Python function to retrieve the first `n` items from a given list in reverse order without modifying the original list. The function should handle cases where `n` is greater than the length of the list by returning the entire list in reverse order. If `n` is less than or equal to 0, the function should return a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_first_n_items_reversed(original_list: List[int], n: int) -> List[int]:
"""
Retrieves the first n items from the given list in reverse order without modifying the original list.
Args:
... |
python_sharegpt_23k | 13,851 | Write a Python function to find the nth prime number in ascending order using only O(1) space and with a time complexity of O(n^2). The function should be efficient in terms of both space and time complexity.
The code should start with:
```python
import math
def find_nth_prime(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import math
def find_nth_prime(n: int) -> int:
"""
Find the nth prime number in ascending order using O(1) space and O(n^2) time complexity.
Args:
n (int): The position of the prime number to find.
Returns:
... |
python_sharegpt_23k | 13,854 | Write a Python function to calculate the average of the sum of all odd prime numbers in a given array. The function should filter out the odd prime numbers, compute their sum, and then find the average of this sum. The code should start with:
```python
from typing import List
def average_of_odd_prime_sum(arr: List[int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False oth... |
python_sharegpt_23k | 13,864 | Write a Python function to generate a random sequence of numbers between 0 and 10,000, calculate the square root of each number, and then sort the square roots in ascending order without using any built-in sorting functions or libraries. The function should return the sorted list of square roots.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def sort_square_roots(n: int) -> List[float]:
"""
Generates a random sequence of n numbers between 0 and 10,000, calculates the square root of each number,
and sorts the square roots in... |
python_sharegpt_23k | 13,867 | Write a Python function to compute the sum of two numbers and then multiply the result by a third number. The function should handle the following:
1. Calculate the sum of two input numbers.
2. Multiply the sum by a third input number.
3. Return the result of the multiplication.
The code should start with:
```python
d... | Here is the Python code to solve the given problem in the markdown block:
```python
def compute_sum_and_multiply(x: int, y: int, z: int) -> int:
"""
Computes the sum of two numbers and then multiplies the result by a third number.
Args:
x (int): The first number.
y (int): The second number.... |
python_sharegpt_23k | 13,879 | Write a Python function to reverse the order of elements in a given list and remove any duplicates, using only constant space complexity and a time complexity of O(n). The function should modify the input list in place and return the modified list.
The code should start with:
```python
from typing import List
def rev... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_and_remove_duplicates(lst: List[int]) -> List[int]:
"""
Reverses the order of elements in the given list and removes any duplicates,
using only constant space complexity and a time comple... |
python_sharegpt_23k | 13,886 | Implement the merge sort algorithm to sort a given array of integers in ascending order. The array will contain at least 1 million elements and can have duplicate numbers. Additionally, you are not allowed to use any built-in sorting functions or libraries.
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 merge_sort(arr: List[int]) -> List[int]:
"""
Sorts a given array of integers in ascending order using the merge sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.... |
python_sharegpt_23k | 13,891 | Write a Python function to compare two strings without using any built-in functions, loops, or recursion. The function should have a time complexity of O(n), where n is the length of the longer string. The function should return `True` if the strings are identical and `False` otherwise.
The code should start with:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(string1: str, string2: str) -> bool:
"""
Compares two strings without using any built-in functions, loops, or recursion.
Args:
string1 (str): The first string to compare.
string2 (str): T... |
python_sharegpt_23k | 13,892 | Write a Python function to create an array from a given string containing only lowercase alphabets, sort it in ascending order, and remove any duplicate elements. The function should have a time complexity of O(n log n) and a space complexity of O(n), where n is the length of the input string.
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 sort_and_remove_duplicates(string: str) -> List[str]:
"""
Create an array from a given string containing only lowercase alphabets,
sort it in ascending order, and remove any duplicate elements.
... |
python_sharegpt_23k | 13,894 | Write a Python function to reverse the order of elements in a given array without using any built-in functions or methods, and also without creating any additional arrays or data structures. The function should modify the input array in place.
The code should start with:
```python
from typing import List
def reverse_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array_in_place(arr: List[int]) -> None:
"""
Reverses the order of elements in the given array in place.
Args:
arr (List[int]): The array to be reversed.
Returns:
Non... |
python_sharegpt_23k | 13,904 | Write a Python function to find the number of distinct words in a given sentence, ignoring case sensitivity and excluding any punctuation marks. Additionally, implement a feature to remove any common English stop words from the sentence before counting the distinct words. The function should start with:
```python
from ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import string
def count_distinct_words(sentence: str, stop_words: List[str]) -> int:
"""
Counts the number of distinct words in a given sentence, ignoring case sensitivity,
excluding punctuation, and... |
python_sharegpt_23k | 13,906 | Write a Python function to find the largest prime number in a given array of numbers. If there are no prime numbers in the array, return -1. The function should start with:
```python
from typing import List
def find_largest_prime(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def is_prime(num: int) -> bool:
"""
Check if a given number is a prime number.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number ... |
python_sharegpt_23k | 13,907 | Write a Python function to replace each element in a list of integers with its square if the number is even, and with its cube if the number is odd. Each result should be rounded to the nearest whole number. The list can contain up to 10^7 elements.
The code should start with:
```python
from typing import List
def re... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def replace_numbers(lst: List[int]) -> List[int]:
"""
Replace each element in the list with its square if the number is even, and with its cube if the number is odd.
Each result is rounded to the nea... |
python_sharegpt_23k | 13,910 | Write a Python function to replace each element in a given 2D array of non-negative integers with the greatest element found on its row. The function should modify the input array in-place and run in O(n) time complexity, where n is the total number of elements in the input array. Additionally, the function should mini... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def replace_with_greatest(matrix: List[List[int]]) -> List[List[int]]:
"""
Replace each element in the given 2D array with the greatest element found on its row.
Args:
matrix (List[List[int]... |
python_sharegpt_23k | 13,918 | Write a Python function to find the common elements between two given lists with a time complexity of O(n) and a space complexity of O(1), where n is the length of the longer list. The function should not use any built-in functions or methods for finding intersections or common elements between lists. The function shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the common elements between two lists with a time complexity of O(n) and a space complexity of O(1).
Args:
... |
python_sharegpt_23k | 13,920 | Write a Python function to calculate the cumulative sum of an array, but skip the first two elements in the calculation. The function should return an error message if the array has fewer than three elements. The code should start with:
```python
from typing import List
def cumulative_sum_skip_first_two(array: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cumulative_sum_skip_first_two(array: List[int]) -> List[int]:
"""
Calculate the cumulative sum of an array, skipping the first two elements.
Args:
array (List[int]): The input array of i... |
python_sharegpt_23k | 13,924 | Write a Python function to calculate the amount of tax to be paid based on the inputs of income, country, and marital status. The tax rates and income brackets for each country are predefined. The function should also validate the inputs, ensuring that the income is a positive integer and the country and marital status... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_tax(income: int, country: str, marital_status: str) -> float:
"""
Calculate the amount of tax to be paid based on the income, country, and marital status.
Args:
income (int): The income amount.
... |
python_sharegpt_23k | 13,928 | Write a Python function to reverse the order of an array without using any built-in functions or libraries. The input array will always contain at least 10 elements and at most 1000 elements. The time complexity of your solution should be O(n), where n is the length of the input array.
The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array(arr: List[int]) -> List[int]:
"""
Reverses the order of the input array without using any built-in functions or libraries.
Args:
arr (List[int]): The input array to be reve... |
python_sharegpt_23k | 13,930 | Write a Python function to generate a list of the first `n` Fibonacci numbers, reverse the list, and calculate the sum of the reversed list. The function should return the reversed list and the sum.
The code should start with:
```python
from typing import List, Tuple
def fibonacci_reverse_and_sum(n: int) -> Tuple[Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def fibonacci_reverse_and_sum(n: int) -> Tuple[List[int], int]:
"""
Generate a list of the first n Fibonacci numbers, reverse the list, and calculate the sum of the reversed list.
Args:
... |
python_sharegpt_23k | 13,931 | Write a Python function to create a copy of a given list while excluding any duplicate elements from the original list. The function should use list comprehension to achieve this. The code should start with:
```python
from typing import List
def remove_duplicates_with_list_comprehension(original_list: List[int]) -> Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_with_list_comprehension(original_list: List[int]) -> List[int]:
"""
Creates a copy of the given list, excluding any duplicate elements.
Args:
original_list (List[int]):... |
python_sharegpt_23k | 13,936 | Write a Python function to determine if a given target element is present in a sorted array using a custom binary search algorithm. The array is guaranteed to be sorted in ascending order. The time complexity of the solution should be O(log n), where n is the size of the array. You are not allowed to use the built-in b... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_element_in_sorted_array(arr: List[int], target: int) -> bool:
"""
Determines if a given target element is present in a sorted array using a custom binary search algorithm.
Args:
arr (... |
python_sharegpt_23k | 13,937 | Write a Python function to swap the places of the first and last elements in a list of integers, without using any additional variables or built-in functions. The function should have a time complexity of O(1) and should not modify the original list. The function should return a new list with the swapped elements.
The... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def swap_first_last(lst: List[int]) -> List[int]:
"""
Swaps the places of the first and last elements in a list of integers without using any additional variables or built-in functions.
The function ... |
python_sharegpt_23k | 13,940 | Write a Python function to create a dictionary from two lists, one containing keys and the other containing values. The function should handle the following cases:
1. Ignore keys that are empty strings or have a length greater than 10 characters.
2. If a value is a function, execute the function and use its return valu... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any, Callable
def create_dictionary(keys: List[str], values: List[Any]) -> dict:
"""
Creates a dictionary from two lists, one containing keys and the other containing values.
Handles cases where key... |
python_sharegpt_23k | 13,948 | Write a Python function to remove duplicates from a given list and sort the resulting list in ascending order using a custom sorting algorithm. The custom sorting algorithm must have a time complexity of O(n log n) or better and should not use any additional data structures. The function should start with:
```python
f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(input_list: List[int]) -> List[int]:
"""
Removes duplicates from the input list and sorts the resulting list in ascending order using a custom sorting algorithm.
Args:... |
python_sharegpt_23k | 13,954 | Write a Python function to calculate the sum of all positive integers greater than 1000 from a given list of integers. The function should only consider integers greater than 1000 and ignore any other values, including non-integer types.
The code should start with:
```python
from typing import List
def sum_positive_i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_positive_integers(numbers: List[int]) -> int:
"""
Calculate the sum of all positive integers greater than 1000 from a given list of integers.
Args:
numbers (List[int]): A list of int... |
python_sharegpt_23k | 13,956 | Write a Python function to filter out prime numbers from a given list of integers. The function should ensure that the input list contains between 5 and 100 integers and raise an exception if any non-integer values are found in the input list. The function should return a new list containing only the prime numbers from... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_prime_numbers(numbers: List[int]) -> List[int]:
"""
Filters out prime numbers from a given list of integers.
Args:
numbers (List[int]): A list of integers to filter prime numbers ... |
python_sharegpt_23k | 13,958 | Write a Python function to determine if a given number is a prime number, find the smallest prime number greater than the given number, and calculate the sum of all prime numbers smaller than the given number. The function should return a tuple containing these three values: a boolean indicating if the given number is ... | 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 a prime number.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
"""
if... |
python_sharegpt_23k | 13,960 | Write a Python function that takes an array of strings and returns a dictionary whose keys are the strings and the values are the lengths of the strings. The function should also remove any duplicate strings from the input array before processing it. Finally, the function should sort the dictionary based on the values ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_string_lengths(arr: List[str]) -> Dict[str, int]:
"""
Takes an array of strings, removes duplicates, creates a dictionary with string lengths,
and returns the dictionary sorted by stri... |
python_sharegpt_23k | 13,961 | Write a Python function to convert all lowercase characters in a given string to uppercase and all uppercase characters to lowercase, while leaving other characters unchanged. The function should maintain the order of characters in the original string and handle Unicode characters correctly. The function should not use... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_case(s: str) -> str:
"""
Converts all lowercase characters in the input string to uppercase and all uppercase characters to lowercase,
while leaving other characters unchanged. The function maintains the order of... |
python_sharegpt_23k | 13,964 | Write a Python function to determine if a given string is a palindrome. The function should ignore spaces, punctuation, and capitalization when checking for palindromes. The function should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the input string.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(s: str) -> bool:
"""
Determines if a given string is a palindrome, ignoring spaces, punctuation, and capitalization.
Args:
s (str): The input string to check.
Returns:
bool: True if th... |
python_sharegpt_23k | 13,968 | Write a Python function to assign the value 1 to all elements of a given 6x6 array that are divisible by 5, and then return the sum of all the assigned elements. The function should handle any 6x6 array of integers.
The code should start with:
```python
from typing import List
def sum_of_assigned_elements(arr: List[L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_assigned_elements(arr: List[List[int]]) -> int:
"""
Assign the value 1 to all elements of a 6x6 array that are divisible by 5, and return the sum of all assigned elements.
Args:
a... |
python_sharegpt_23k | 13,971 | Write a Python function to perform a binary search on a sorted list of integers and return the index of the target element if it exists in the list, otherwise return -1. The function should be efficient and should be implemented using the binary search algorithm. The code should start with:
```python
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search(arr: List[int], target: int) -> int:
"""
Perform a binary search on a sorted list to find the index of the target element.
Args:
arr (List[int]): A sorted list of integers.... |
python_sharegpt_23k | 13,972 | Write a Python function to find the kth smallest element from a given array, where k is a positive integer less than or equal to the length of the array. Do not use the built-in `min()` or `sorted()` functions. The function should efficiently find the kth smallest element by partially sorting the array.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_kth_smallest(arr: List[int], k: int) -> int:
"""
Find the kth smallest element in the given array without using built-in min() or sorted() functions.
Args:
arr (List[int]): The arra... |
python_sharegpt_23k | 13,975 | Write a Python function to classify and print the data types of a list of given pieces of data. The function should handle various data types including strings, floats, lists, dictionaries, tuples, booleans, and None. The function should start with:
```python
from typing import List, Any
def classify_data_types(data: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def classify_data_types(data: List[Any]) -> None:
"""
Classify and print the data types of a list of given pieces of data.
Args:
data (List[Any]): A list containing various pieces of da... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.