source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 57 | Write a Python function to find the sum of all unique prime numbers in a given array. The function should adhere to the following constraints:
1. You cannot use any built-in functions or libraries to calculate the sum or determine whether a number is prime.
2. The array can contain negative numbers as well.
3. The arra... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(num):
"""
Helper function to determine if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
"""
if ... |
python_sharegpt_23k | 58 | Write a Python function to recursively find the sum of all even numbers in a given list, which may contain nested lists. The function should handle negative numbers and return the sum of all even numbers found. The function should be implemented using recursion and should not use any loops.
The code should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def sum_even_numbers_recursive(lst: Union[List, int]) -> int:
"""
Recursively finds the sum of all even numbers in a given list, which may contain nested lists.
Args:
lst (Union[List,... |
python_sharegpt_23k | 59 | Write a Python function to remove duplicates from a given list while maintaining the order of the original list and keeping track of the count of each duplicate element. The function should return a new list where each element is a tuple containing the unique elements from the original list along with their respective ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def remove_duplicates_with_counts(lst: List[int]) -> List[Tuple[int, int]]:
"""
Removes duplicates from the given list while maintaining the order of the original list and keeping track of the cou... |
python_sharegpt_23k | 63 | Write a Python function to find the maximum value in a given list of integers using a recursive approach. The function should not use any loops, built-in functions (except for `len`), or methods to solve the problem. The time complexity of the function should be O(n), where n is the length of the list.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_recursive(lst: List[int]) -> int:
"""
Find the maximum value in a given list of integers using a recursive approach.
Args:
lst (List[int]): The list of integers from which to fi... |
python_sharegpt_23k | 77 | Write a Python function to generate an NxN array where each element is equal to 1, ensuring that each row and column sum up to N and have a unique sum. The function should achieve this with a time complexity less than O(N^2) and a space complexity less than O(N^2).
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 generate_unique_sum_array(N: int) -> List[List[int]]:
"""
Generate an NxN array where each element is 1, ensuring that each row and column sums up to N and has a unique sum.
Args:
N (int... |
python_sharegpt_23k | 79 | Write a Python function to count the total number of elements in a given multi-dimensional array. The array may contain integers, nested arrays, and other data types such as strings or booleans. The elements can be located at any depth within the array structure, with a maximum depth of 10 levels. The function should b... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def count_elements(arr: Union[List, int, str, bool]) -> int:
"""
Count the total number of elements in a given multi-dimensional array.
Args:
arr (Union[List, int, str, bool]): The ar... |
python_sharegpt_23k | 82 | Write a Python function to generate an output string by taking two input strings, `str1` and `str2`, where the length of `str1` is equal to the length of `str2`. The function should create an output string containing alternate characters from each string, ensuring that the output string does not contain any repeated ch... | Here is the Python code to solve the given problem in the markdown block:
```python
def generate_sorted_alternate_string(str1: str, str2: str) -> str:
"""
Generates an output string by taking alternate characters from two input strings,
ensuring no repeated characters and sorting the result in ascending ord... |
python_sharegpt_23k | 85 | Write a Python function to sort a given list of integers in ascending order using the Bubble Sort algorithm. The function should handle lists with duplicate elements and have a time complexity of O(n^2). The code should start with:
```python
from typing import List
def bubble_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 bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
python_sharegpt_23k | 91 | Write a Python function to extract and return a sorted list of unique even numbers from a given list. The function should have a time complexity of O(n) and should not use any additional data structures. The function should start with:
```python
from typing import List
def get_sorted_unique_even_numbers(arr: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_sorted_unique_even_numbers(arr: List[int]) -> List[int]:
"""
Extracts and returns a sorted list of unique even numbers from the given list.
Args:
arr (List[int]): The input list of i... |
python_sharegpt_23k | 95 | Write a Python function to find the second maximum element in a given array without using any built-in sorting functions or methods. The time complexity of your solution should be less than O(n^2). The function should handle edge cases such as arrays with fewer than two elements and arrays with duplicate maximum values... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_second_max(arr: List[int]) -> Optional[int]:
"""
Finds the second maximum element in a given array without using any built-in sorting functions or methods.
Args:
arr (List... |
python_sharegpt_23k | 100 | Write a Python function to remove all occurrences of a given element from a list while maintaining the original order of the remaining elements. The function should have a time complexity of O(n) and should not use any built-in functions or libraries to remove the elements. Additionally, you are not allowed to create a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_element(nums: List[int], element: int) -> List[int]:
"""
Removes all occurrences of a given element from a list while maintaining the original order of the remaining elements.
The function... |
python_sharegpt_23k | 104 | Write a Python function to reorder a given array such that all prime numbers appear before non-prime numbers while maintaining the relative order of prime and non-prime numbers. The function should have a time complexity of O(n), where n is the size of the input array.
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 is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
if... |
python_sharegpt_23k | 112 | Write a Python function to find the smallest number in an array of numbers without using any built-in functions or methods, and adhering to the following constraints:
1. The solution should have a time complexity of O(n), where n is the number of elements in the array.
2. The solution should handle negative numbers in ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
from functools import reduce
def find_smallest_number(numbers: List[float]) -> Optional[float]:
"""
Find the smallest number in an array of numbers without using built-in functions or methods.
... |
python_sharegpt_23k | 113 | Write a Python function to calculate the factorial of a non-negative integer iteratively, without using the built-in `math.factorial()` function. The function should have a time complexity of O(n) and a constant space complexity, meaning the space used by the function should not depend on the input size. Additionally, ... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculate the factorial of a non-negative integer iteratively.
Args:
n (int): The non-negative integer for which to calculate the factorial.
Returns:
int: The factorial... |
python_sharegpt_23k | 121 | Write a Python function to find the index of the last occurrence of a given item in an array. If the item appears multiple times in the array, return the index of the last occurrence. The function should not use any built-in functions or libraries that directly solve the problem, such as the "index" method in Python. A... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_last_index(arr: List[int], item: int) -> int:
"""
Find the index of the last occurrence of a given item in an array.
Args:
arr (List[int]): The array to search in.
item (int... |
python_sharegpt_23k | 125 | Write a Python function to implement the Odd-Even Sort algorithm recursively. The function should take an array of integers as input and return the sorted array. The function should handle arrays of any length, including arrays with duplicate elements, negative numbers, and floating-point numbers. The function should u... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def odd_even_sort(arr: List[int]) -> List[int]:
"""
Sorts an array using the Odd-Even Sort algorithm recursively.
Args:
arr (List[int]): The array of integers to be sorted.
Returns:
... |
python_sharegpt_23k | 133 | Write a Python function to convert a given binary string to its corresponding decimal number without using any built-in functions or libraries for binary-to-decimal conversion. The function must implement its own algorithm with a time complexity of O(n), where n is the length of the binary string. The function should s... | Here is the Python code to solve the given problem in the markdown block:
```python
def binary_to_decimal(binary_string: str) -> int:
"""
Convert a given binary string to its corresponding decimal number.
Args:
binary_string (str): The binary string to be converted.
Returns:
int: The d... |
python_sharegpt_23k | 134 | Write a Python function to find the index of a given word in a sentence. The sentence should contain at least 15 words and should not exceed a maximum length of 150 characters. The word should only be considered a match if it appears as a complete word, not as part of another word. Additionally, the word should be case... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_word_index(sentence: str, word: str) -> int:
"""
Find the index of the given word in the sentence. The sentence should contain at least 15 words and should not exceed 150 characters.
The word should only be consider... |
python_sharegpt_23k | 135 | Write a Python function to reverse a given string without using any built-in string reversal functions, slicing, or any additional data structures. The solution should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the string. The function should start with:
```python
def revers... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverses a given string without using any built-in string reversal functions, slicing, or additional data structures.
The solution has a time complexity of O(n) and a space complex... |
python_sharegpt_23k | 136 | Write a Python function to sort a given list of numbers in descending order and return the sum of the first and second elements of the sorted list as the largest number. The function should handle lists of any length, including empty lists and lists with only one element.
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
def sum_of_two_largest(numbers: List[int]) -> int:
"""
Sorts the given list of numbers in descending order and returns the sum of the first and second elements of the sorted list.
Args:
numb... |
python_sharegpt_23k | 137 | Write a Python function to calculate the nth term of the Fibonacci sequence efficiently using memoization. The function should be able to handle large numbers. The code should start with:
```python
def fibonacci_memoized(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci_memoized(n: int) -> int:
"""
Calculate the nth term of the Fibonacci sequence using memoization.
Args:
n (int): The position in the Fibonacci sequence to calculate.
Returns:
int: The nth t... |
python_sharegpt_23k | 139 | Write a Python function to calculate the square root of a given positive integer and round it to the nearest integer. The input number will always be a positive integer less than or equal to \(10^{18}\). The time complexity of your solution should be \(O(\log(N))\), where \(N\) is the input number. Additionally, your s... | Here is the Python code to solve the given problem in the markdown block:
```python
def rounded_sqrt(n: int) -> str:
"""
Calculate the square root of a given positive integer and round it to the nearest integer.
The input number will always be a positive integer less than or equal to 10^18.
The time com... |
python_sharegpt_23k | 140 | Write a Python function to create a two-dimensional array of specified dimensions, ensuring that all elements in the array are unique, in ascending order, and are prime numbers. The function should have a time complexity of O(n) or better, where n is the total number of elements in the array. The function should not us... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 141 | Write a Python function to generate the powerset of a given list, where the list may contain duplicate elements. The output should not contain any duplicate subsets. The function should return the powerset in lexicographical order.
Additionally, the function should only use constant extra space (O(1) space complexity)... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def powerset_with_duplicates(lst: List[int]) -> Tuple[List[int], List[List[int]]]:
"""
Generate the powerset of a given list, where the list may contain duplicate elements.
The output should n... |
python_sharegpt_23k | 147 | Write a Python function named `sort_and_sum` that takes in a list of integers as a parameter and returns the sum of the sorted set. The function should ensure that the resulting set is sorted in descending order, contains only unique elements greater than 1, and finally, outputs the sum of all the elements in the set.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_and_sum(lst: List[int]) -> int:
"""
Converts the given list of integers to a set, filters out elements less than or equal to 1,
sorts the set in descending order, and returns the sum of the ... |
python_sharegpt_23k | 149 | Write a Python function to find the number of unique prime numbers in a given list of integers, as well as the largest and smallest prime numbers in the list. The solution should have a time complexity of O(n), where n is the length of the list. Additionally, the solution should not use any built-in functions or librar... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import math
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime... |
python_sharegpt_23k | 152 | Write a Python function to find the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. The function should have a time complexity of O(log(min(a, b))) and a space complexity of O(1). It should handle negative input numbers correctly and return the GCD as a positive integer. The function should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two integers using the Euclidean algorithm.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
... |
python_sharegpt_23k | 154 | Write a Python function to sort a list of integers in descending order and remove any duplicate elements. The list may contain duplicates and have a length of up to \(10^6\). Implement an efficient algorithm that uses \(O(1)\) extra space and runs in \(O(n \log n)\) time complexity, where \(n\) is the length of the lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_and_remove_duplicates(arr: List[int]) -> List[int]:
"""
Sorts the given list of integers in descending order and removes any duplicate elements.
Args:
arr (List[int]): The list of i... |
python_sharegpt_23k | 156 | Write a Python function to sort a given list of numbers in ascending order using the bubble sort algorithm. You are not allowed to use the built-in `sort()` function or any other sorting algorithm. The function 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 given list of numbers in ascending order using the bubble sort algorithm.
Args:
numbers (List[int]): The list of numbers to be s... |
python_sharegpt_23k | 157 | Write a Python function to calculate the sum of all even prime numbers in a given list of positive integers. The list should contain at least 15 elements, and each element should be a prime number less than or equal to 100. The function should return the sum only if the list has more even prime numbers than odd prime n... | 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 a prime number.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
python_sharegpt_23k | 161 | Write a Python function to convert a given positive integer from decimal to binary representation without using any built-in functions or libraries for the conversion. The input number can be any positive integer, including numbers greater than 1000. The function should return the binary representation as a string.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(n: int) -> str:
"""
Converts a given positive integer to its binary representation without using built-in functions or libraries.
Args:
n (int): The positive integer to be converted to binary.
... |
python_sharegpt_23k | 164 | Write a Python function to calculate the area of a triangle given its base, height, and the lengths of its three sides. The function should also validate if the triangle is valid based on the triangle inequality theorem. The function should handle decimal inputs for the base, height, and side lengths. Additionally, the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union
def find_triangle_area(base: float, height: float, side1: float, side2: float, side3: float) -> Union[float, str]:
"""
Calculate the area of a triangle given its base, height, and the lengths of its thr... |
python_sharegpt_23k | 166 | Write a Python function to loop backwards through a given list, print each element in the reversed order, and calculate the sum of all the values in the reversed order. The function should start from the last element of the list and decrement the index until it reaches the first element. The function should return the ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_loop_and_sum(arr: List[int]) -> int:
"""
Loop backwards through the given list, print each element in the reversed order,
and calculate the sum of all the values in the reversed order.
... |
python_sharegpt_23k | 172 | Write a Python function to remove duplicates from a given list of integers while maintaining the order of the first occurrence of each element. The function should have a time complexity of O(n log n) or better and a memory usage of O(n) or better. You are not allowed to use any built-in Python functions or libraries t... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_duplicates_from_list(nums):
"""
Removes duplicates from a list of integers while maintaining the order of the first occurrence of each element.
Args:
nums (list): A list of integers from which duplicates ... |
python_sharegpt_23k | 181 | Write a Python function to remove duplicates from an unsorted array of integers in-place with a time complexity of O(n), where n is the number of elements in the array. The function should handle negative numbers and zero, and it should remove all occurrences of duplicates while maintaining the relative order of the re... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_place(arr: List[int]) -> List[int]:
"""
Removes duplicates from an unsorted array of integers in-place and returns the sorted array.
Args:
arr (List[int]): The unsor... |
python_sharegpt_23k | 184 | Write a Python function to find all prime numbers within a given list of integers and return them in ascending order. The function should ensure a time complexity of O(n) and remove all duplicate numbers from the list. The code should start with:
```python
from typing import List
def find_primes_in_list(lst: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_primes_in_list(lst: List[int]) -> List[int]:
"""
Finds all prime numbers within a given list of integers and returns them in ascending order.
The function ensures a time complexity of O(n) a... |
python_sharegpt_23k | 187 | Write a Python function to sort a list of numbers in ascending order using the Merge Sort algorithm. The function should have a time complexity of O(n log n) and handle duplicate numbers, preserving their relative order. The function must be implemented from scratch without using any built-in sorting functions or libra... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(lst: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Merge Sort algorithm.
Args:
lst (List[int]): The list of integers to be sorted.
Re... |
python_sharegpt_23k | 188 | Write a Python function to sort elements in an array in descending order using a custom merge sort algorithm. The function should have a time complexity of O(nlogn) and should not use any built-in sorting functions or libraries. The code should start with:
```python
from typing import List
def custom_merge_sort(arr: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_merge_sort(arr: List[int]) -> List[int]:
"""
Sorts the elements in the given array in descending order using a custom merge sort algorithm.
Args:
arr (List[int]): The array of int... |
python_sharegpt_23k | 194 | Write a Python function to sort an array in descending order using the bubble sort algorithm. The function should not use any additional data structures or built-in sorting functions. 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
fr... | 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 the given array in descending order using the bubble sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Re... |
python_sharegpt_23k | 196 | Write a Python function to swap the case of each character in a given string. The function should return a new string where all lowercase letters are replaced with their corresponding uppercase letters and vice versa. Non-alphabetic characters should remain unchanged.
The code should start with:
```python
def swap_cas... | Here is the Python code to solve the given problem in the markdown block:
```python
def swap_case(string: str) -> str:
"""
Swaps the case of each character in the given string.
Args:
string (str): The input string whose characters' cases need to be swapped.
Returns:
str: A new string w... |
python_sharegpt_23k | 201 | Write a Python function to remove all duplicate elements from a given array while preserving the original order of the elements. The function should return a new array. The code should start with:
```python
from typing import List
def remove_duplicates_preserve_order(data: 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_preserve_order(data: List[int]) -> List[int]:
"""
Removes all duplicate elements from the given array while preserving the original order of the elements.
Args:
data (L... |
python_sharegpt_23k | 202 | Write a Python function to extract the item at a specified index from a given list without using built-in list indexing or slicing methods. Instead, you can only use a loop and basic list manipulation techniques. Additionally, you cannot use the length of the list or the range function. The function should start with:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def get_item_at_index(my_list: List[Any], index: int) -> Any:
"""
Extracts the item at the specified index from the given list without using built-in indexing or slicing methods.
Args:
... |
python_sharegpt_23k | 205 | Implement a function that sorts a given list of numbers in ascending order using the merge sort algorithm. You are not allowed to use any built-in sorting functions or libraries. The function should follow these steps:
1. Divide the list into two halves.
2. Recursively sort each half using the merge sort algorithm.
3.... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(nums: List[int]) -> List[int]:
"""
Sorts a given list of numbers in ascending order using the merge sort algorithm.
Args:
nums (List[int]): The list of numbers to be sorted.
... |
python_sharegpt_23k | 207 | Write a Python function to check whether a string has all unique characters. The function should have a time complexity of O(n) and a space complexity of O(1). The function should not use any additional data structures or libraries.
The code should start with:
```python
def has_unique_chars(string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def has_unique_chars(string: str) -> bool:
"""
Check whether a string has all unique characters.
Args:
string (str): The input string to check for unique characters.
Returns:
bool: True if the string ha... |
python_sharegpt_23k | 208 | Write a Python function to delete an element from an array given its index. The index should be verified to be within the bounds of the array before performing the deletion. If the index is out of bounds, an appropriate error message should be displayed. The remaining elements in the array should be shifted to fill the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def delete_element_by_index(arr: List[int], index: int) -> List[int]:
"""
Deletes an element from the array at the specified index.
Args:
arr (List[int]): The array from which to delete the ... |
python_sharegpt_23k | 211 | Write a Python function to multiply two integers using only bitwise operations. The function should have a time complexity of O(log n), where n is the larger of the two input integers. Additionally, the function should not use any arithmetic operators (+, -, *, /) or any built-in multiplication functions. Furthermore, ... | Here is the Python code to solve the given problem in the markdown block:
```python
def multiply_bitwise(a: int, b: int) -> int:
"""
Multiplies two integers using only bitwise operations.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The product o... |
python_sharegpt_23k | 214 | Write a Python function to calculate the sum of all prime numbers between a given range `[start, end]`. Additionally, implement a function that finds all pairs of numbers from two lists that sum up to a given target value using nested loops.
The code should start with:
```python
from typing import List, Tuple
def sum... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
python_sharegpt_23k | 218 | Write a Python function to convert the case of each letter in a given string without using any built-in string methods or libraries. Specifically, uppercase letters should be converted to lowercase and vice versa. Non-alphabetic characters should remain unchanged.
The code should start with:
```python
def convert_case... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_case(s: str) -> str:
"""
Convert the case of each letter in the given string. Uppercase letters are converted to lowercase and vice versa.
Non-alphabetic characters remain unchanged.
Args:
s (str): T... |
python_sharegpt_23k | 223 | Write a Python function to create a dictionary from two lists of the same length, where the keys in the dictionary are obtained by converting the elements of the first list to uppercase and adding a suffix of "KEY". The values in the dictionary are obtained by converting the elements of the second list to integers and ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def create_sorted_dict(mylist1: List[str], mylist2: List[str]) -> Dict[str, int]:
"""
Create a dictionary from two lists with the same length, where the keys in the dictionary are obtained by conve... |
python_sharegpt_23k | 225 | Write a Python function to initialize an empty list and then add `n` unique items to it using a for loop. The range for the loop should be dynamically generated based on the value of `n`. Each item added to the list must be unique. If a duplicate item is encountered during the iteration, it should be skipped, and the l... | Here is the Python code to solve the given problem in the markdown block:
```python
def add_unique_items(n: int) -> list:
"""
Initialize an empty list and add n unique items to it using a for loop.
Args:
n (int): The number of unique items to add to the list.
Returns:
list: A list cont... |
python_sharegpt_23k | 229 | Write a Python function to calculate the product of all the prime numbers in a given list that are divisible by 4. The list may contain duplicate elements. The product should only include numbers that are divisible by 4. You are not allowed to use any built-in functions for finding the product. The list may have a maxi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 235 | Write a Python function to validate if a given input is a positive integer between 1 and 100, inclusive, and then determine if it is a prime number. The function should return a string indicating whether the number is a prime number or not.
The code should start with:
```python
def check_prime_and_validity(number: int... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_prime_and_validity(number: int) -> str:
"""
Validates if the given number is a positive integer between 1 and 100, inclusive,
and checks if it is a prime number. Returns a string indicating the result.
Args:
... |
python_sharegpt_23k | 241 | Write a Python function `optimized_sort(arr)` that takes an array `arr` as input and returns a new array with the elements sorted in ascending order, without any duplicates. The sorting algorithm used should have a time complexity of O(n log n) and a space complexity of O(1). You are not allowed to use any built-in sor... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def optimized_sort(arr: List[int]) -> List[int]:
"""
Sorts the input array in ascending order without any duplicates using a merge sort algorithm.
Args:
arr (List[int]): The input array to b... |
python_sharegpt_23k | 244 | Write a Python function to generate the Fibonacci sequence up to the nth number efficiently using matrix exponentiation, ensuring a runtime complexity of O(log n). Additionally, implement a function to check if a given number is a Fibonacci number using an efficient algorithm with a runtime complexity of O(1).
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
import math
def generate_fibonacci_sequence(n: int) -> list:
"""
Generate the Fibonacci sequence up to the nth number using matrix exponentiation.
Args:
n (int): The number of Fibonacci numbers t... |
python_sharegpt_23k | 246 | Write a Python function to find the minimum prime number in an unordered array of integers. The array may contain duplicates. Implement the solution without using any built-in functions or libraries that directly check for primality. The function should start with:
```python
from typing import List
def find_min_prime(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 250 | Write a Python function to sort a given set of strings in ascending alphabetical order without using any built-in sorting functions or libraries. The function should have a time complexity of O(nlogn). The function should start with:
```python
def sort_set(my_set: set) -> set:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sort_set(my_set: set) -> set:
"""
Sorts a given set of strings in ascending alphabetical order using a custom merge sort algorithm.
Args:
my_set (set): The set of strings to be sorted.
Returns:
set:... |
python_sharegpt_23k | 251 | Write a Python function to sort an array in descending order using a recursive merge sort algorithm. The function should have a time complexity of O(n log n) and an auxiliary space complexity of O(log n). The original array should remain unchanged, and the sorted array should be stored in a new array. The function shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts the given array in descending order using a recursive merge sort algorithm.
Args:
arr (List[int]): The array to be sorted.
... |
python_sharegpt_23k | 255 | Write a Python function to calculate the result of raising 2 to the power of `n`, where `n` is an integer between 1 and 10^9. The function should have a time complexity of O(log n) and should not use any built-in mathematical functions or operators for exponentiation. The code should start with:
```python
def power_of_... | Here is the Python code to solve the given problem in the markdown block:
```python
def power_of_two(n: int) -> int:
"""
Calculate the result of raising 2 to the power of n using binary exponentiation.
Args:
n (int): The exponent, an integer between 1 and 10^9.
Returns:
int: The result... |
python_sharegpt_23k | 256 | Write a Python function that takes a list of integers as input and returns a new list where each element is multiplied by 2. The function must adhere to the following constraints:
- You are not allowed to use any built-in list manipulation methods or functions such as `map()`, `list comprehension`, or `append()`.
- You... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def multiply_by_two(input_list: List[int]) -> List[int]:
"""
Multiplies each element in the input list by 2 and returns a new list with the results.
Args:
input_list (List[int]): The list of... |
python_sharegpt_23k | 258 | Write a Python function to calculate the average of three positive integers, ensuring that each integer is within the range of 1 to 100 (inclusive). If any of the numbers are negative or greater than 100, raise a `ValueError` with the message "Invalid input, all numbers must be positive integers within the range of 1 t... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_average(num1: int, num2: int, num3: int) -> float:
"""
Calculate the average of three positive integers within the range of 1 to 100.
If any of the numbers are not divisible by 5, round the average to the nearest wh... |
python_sharegpt_23k | 259 | Write a Python function to check if two strings are anagrams of each other without using any built-in string manipulation methods or libraries. The function should handle cases where the input strings contain special characters, whitespace, and have a maximum length of 100 characters. The function should also have a ti... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_anagram(str1: str, str2: str) -> bool:
"""
Check if two strings are anagrams of each other without using built-in string manipulation methods or libraries.
Args:
str1 (str): The first input string.
... |
python_sharegpt_23k | 265 | Write a Python function to find the largest number in a given array and its corresponding index. If multiple numbers have the same maximum value, return the index of the first occurrence. The function should have a time complexity of O(n) and use only a single loop to iterate through the array. The function should not ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_largest_number(arr: List[int]) -> Tuple[int, int]:
"""
Find the largest number in the given array and its corresponding index.
If multiple numbers have the same maximum value, return ... |
python_sharegpt_23k | 277 | Write a Python function to calculate the average of all positive integers in a given array. The function should exclude any negative numbers, zero, and any non-integer values. The function should also handle empty arrays gracefully by returning 0. The time complexity should be less than or equal to O(n), where n is the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def average_positive_numbers(arr: List[int]) -> float:
"""
Calculate the average of all positive integers in the given array.
Args:
arr (List[int]): The array of integers from which to calcu... |
python_sharegpt_23k | 282 | Write a Python function to reverse the order of words in a given string while maintaining the original order of characters within each word. The function should also remove any leading or trailing spaces and collapse multiple consecutive spaces into a single space. The function should have a time complexity of O(n) and... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_in_string(string: str) -> str:
"""
Reverses the order of words in a given string while maintaining the original order of characters within each word.
Leading and trailing spaces are removed, and multiple co... |
python_sharegpt_23k | 284 | Write a Python function to find the minimum and maximum values in a list of numbers without using built-in functions like `min()` and `max()`. The list will always have at least 10 numbers and may contain duplicates. The function should handle both positive and negative numbers. Additionally, the function should only i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_min_max(numbers: List[int]) -> Tuple[int, int]:
"""
Find the minimum and maximum values in a list of numbers without using built-in functions like min() and max().
Args:
numb... |
python_sharegpt_23k | 286 | Write a Python function to convert a given string to uppercase without using any built-in string manipulation functions or methods. The function should only use a single loop and have a time complexity of O(n), where n is the length of the input string. The function should also handle cases where the input string conta... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_uppercase(string: str) -> str:
"""
Converts a given string to uppercase without using any built-in string manipulation functions or methods.
Args:
string (str): The input string to be converted to upp... |
python_sharegpt_23k | 288 | Write a Python function to count the frequency of each character in a given string. The function should handle strings containing both uppercase and lowercase letters, special characters, and whitespace. The function should return a dictionary where the keys are characters and the values are the number of times each ch... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_character_frequencies(string: str) -> Dict[str, int]:
"""
Counts the frequency of each character in the given string.
Args:
string (str): The input string to analyze.
Returns:... |
python_sharegpt_23k | 293 | Write a Python function to find the maximum number in a given array of positive integers and return the indices of all occurrences of this maximum number in ascending order. If the array is empty, return an empty list. The function should have a time complexity of O(n), where n is the size of the input array.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_indices(nums: List[int]) -> List[int]:
"""
Finds the maximum number in the given array of positive integers and returns the indices of all its occurrences in ascending order.
Args:
... |
python_sharegpt_23k | 294 | Write a Python function that filters a list of dictionaries based on a specified key-value pair. The function should take three parameters: an integer representing the key, a string representing the value, and a list of dictionaries. The function should return a new list containing only the dictionaries from the input ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def filter_dictionaries(key: int, value: str, dictionaries: List[Dict[int, str]]) -> List[Dict[int, str]]:
"""
Filters a list of dictionaries based on a specified key-value pair.
Args:
... |
python_sharegpt_23k | 299 | Write a Python function to implement the bubble sort algorithm. The function should sort an array of integers in ascending order. The implementation should adhere to the following requirements:
1. The function should be named `bubble_sort`.
2. The function should take a single parameter, `nums`, which is a list of inte... | 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 an array of integers in ascending order using the bubble sort algorithm.
Args:
nums (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 301 | Write a Python function to sort a list of custom objects based on two properties: "name" (a string) and "age" (an integer). The sorting should be done based on the "name" property in ascending order. If two objects have the same "name", then the sorting should be based on the "age" property in ascending order. Addition... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, NamedTuple
class Person(NamedTuple):
name: str
age: int
def custom_sort(arr: List[Person], descending: bool = False) -> List[Person]:
"""
Sorts a list of Person objects based on the 'name' prop... |
python_sharegpt_23k | 302 | Write a Python function to find the longest palindromic substring in a given character sequence. The length of the sequence will not exceed 1000 characters. The function should have a time complexity less than O(n^2) and a space complexity less than O(n), where n is the length of the sequence. Additionally, you are not... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_palindromic_substring(s: str) -> str:
"""
Find the longest palindromic substring in the given character sequence.
Args:
s (str): The input character sequence.
Returns:
str: The longest palin... |
python_sharegpt_23k | 308 | Write a Python function to determine if two given strings are rotations of each other, considering rotations in both directions (left and right). A rotation is defined as moving the characters of a string to the left or right and wrapping the characters around. The function should return `True` if the strings are rotat... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_rotation(string1: str, string2: str) -> bool:
"""
Determines if two given strings are rotations of each other, considering rotations in both directions.
Args:
string1 (str): The first string to check.
... |
python_sharegpt_23k | 315 | Write a Python function to output all the elements from a given list in reverse order. The list must contain at least 7 elements and at most 12 elements. Each element in the list should be a string of exactly 4 characters. The output should be formatted in uppercase. The function should start with:
```python
from typin... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_and_uppercase(lst: List[str]) -> None:
"""
Outputs all the elements from a given list in reverse order with each element formatted in uppercase.
Args:
lst (List[str]): A list of ... |
python_sharegpt_23k | 317 | Write a Python function to find the indices of all occurrences of a given item in a nested array. The function should handle arrays that may contain duplicate values and nested arrays. If the given item is found within a nested array, the function should return the indices of both the nested array and the item within t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Any
def find_all_indices(arr: List[Any], x: Any) -> List[Tuple[int, ...]]:
"""
Find all occurrences of a given item in a nested array and return their indices.
Args:
arr (List[Any]):... |
python_sharegpt_23k | 322 | Implement the insertion sort algorithm to sort the given array of integers in descending order. The input array will have at least two elements and at most 10^5 elements. All elements in the array will be unique and range from -10^9 to 10^9. The implementation should be done using an in-place algorithm without using an... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insertion_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts the given array of integers in descending order using an in-place insertion sort algorithm.
Args:
arr (List[int]): The a... |
python_sharegpt_23k | 323 | Write a Python function to find the minimum of five given numbers. The function should handle the case where all five numbers are the same. The function should start with:
```python
from typing import List
def find_minimum_of_five(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_of_five(numbers: List[int]) -> int:
"""
Find the minimum of five given numbers.
Args:
numbers (List[int]): A list of exactly five integers.
Returns:
int: The ... |
python_sharegpt_23k | 328 | Write a Python function to find the maximum number in a list of length 10, ignoring any negative numbers and any numbers that are divisible by 3. The function should handle cases where the list may contain duplicates. The solution should have a time complexity of O(n) and should not use any built-in functions or librar... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_number(list_nums: List[int]) -> int:
"""
Find the maximum number in a list of length 10, ignoring negative numbers and numbers divisible by 3.
Args:
list_nums (List[int]): A lis... |
python_sharegpt_23k | 329 | Write a Python function to compute the square root of a given non-negative integer using only basic arithmetic operations (addition, subtraction, multiplication, and division). The function should not use any built-in square root functions or libraries. Additionally, you are not allowed to use any iterative methods (su... | Here is the Python code to solve the given problem in the markdown block:
```python
def compute_sqrt(x: int) -> int:
"""
Compute the square root of a given non-negative integer using only basic arithmetic operations.
Args:
x (int): The non-negative integer for which to compute the square root.
... |
python_sharegpt_23k | 330 | Write a Python function to remove duplicates from a given list while maintaining the original order of elements. The original list is sorted in descending order, and the resulting list should be sorted in ascending order. The solution should have a time complexity of O(n), where n is the length of the original list.
T... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(lst: List[int]) -> List[int]:
"""
Removes duplicates from the given list while maintaining the original order of elements,
and then sorts the resulting list in ascendin... |
python_sharegpt_23k | 338 | Write a Python function to merge two sorted lists into a single sorted list containing only unique elements. The function should have a time complexity of O(n+m), where n and m are the lengths of the two input lists respectively. You are not allowed to use any additional data structures or libraries.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists_unique(list1: List[int], list2: List[int]) -> List[int]:
"""
Merge two sorted lists into a single sorted list containing only unique elements.
Args:
list1 (List[int]):... |
python_sharegpt_23k | 347 | Write a Python function that checks if the first input number is a prime number and the second input number is a perfect square. The function should return `True` if both conditions are met, and `False` otherwise. Additionally, the function should raise a `ValueError` if either of the input numbers are negative or if t... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def check_prime_square(num1: int, num2: int) -> bool:
"""
Checks if the first number is a prime number and the second number is a perfect square.
Args:
num1 (int): The first number to check for primalit... |
python_sharegpt_23k | 350 | Write a Python function to find the maximum absolute difference between any two elements in a given sorted array of integers. The function should have a time complexity of O(n) and a space complexity of O(1).
The code should start with:
```python
from typing import List
def find_max_difference(arr: List[int]) -> int:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_difference(arr: List[int]) -> int:
"""
Calculate the maximum absolute difference between any two elements in a sorted array of integers.
Args:
arr (List[int]): A sorted list of ... |
python_sharegpt_23k | 352 | Write a Python function to calculate the sum of the cubes of all the odd numbers in a given list of integers. The function should not use any built-in functions or libraries for mathematical operations and should have a time complexity of O(n), where n is the length of the input list.
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_odd_cubes(nums: List[int]) -> int:
"""
Calculate the sum of the cubes of all the odd numbers in the given list.
Args:
nums (List[int]): A list of integers.
Returns:
i... |
python_sharegpt_23k | 355 | Write a Python function to calculate the factorial of a given number `n` using recursion and memoization. The function should handle invalid inputs (negative numbers or non-integer inputs) by raising an appropriate exception. The time complexity should be less than or equal to O(n) and the space complexity should be le... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_with_memoization(n: int) -> int:
"""
Calculate the factorial of a given number n using recursion and memoization.
Args:
n (int): The number to calculate the factorial for.
Returns:
int: Th... |
python_sharegpt_23k | 356 | Write a Python function to find all elements in a given list which appear more than twice, excluding the element 3. The function should also handle input validation by checking if the input is a list and if all elements in the list are integers. If the input is not a list or contains non-integer elements, the function ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_repeating_elements(nums: List[int]) -> List[int]:
"""
Find all elements in a given list which appear more than twice, excluding the element 3.
Args:
nums (List[int]): The list of in... |
python_sharegpt_23k | 357 | Write a Python function to calculate the square of a given number efficiently. Additionally, write a function to calculate the square root of a given number using the `math` module. Ensure that both functions handle edge cases such as negative numbers and zero. The code should start with:
```python
import math
def squ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def squared(num: float) -> float:
"""
Calculate the square of a given number.
Args:
num (float): The number to be squared.
Returns:
float: The square of the given number.
"""
return... |
python_sharegpt_23k | 359 | Write a Python function to calculate the absolute difference between two numbers using only basic arithmetic operations (`+`, `-`, `*`, `/`) and comparisons (`<`, `>`, `==`) a maximum of two times. The function should have a time complexity of O(1) and use a constant amount of space. The result should be rounded to the... | Here is the Python code to solve the given problem in the markdown block:
```python
def absolute_difference(a: float, b: float) -> int:
"""
Calculate the absolute difference between two numbers and round the result to the nearest integer.
Args:
a (float): The first number.
b (float): The se... |
python_sharegpt_23k | 360 | Write a Python function to flatten a given array of lists, but only include the elements that are divisible by both 2 and 3. Additionally, sort the elements in ascending order before returning the final result. The input array can contain multiple levels of nesting and can contain non-integer elements.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def flatten_and_filter_divisible_by_2_and_3(arr: List[Any]) -> List[int]:
"""
Flattens a given array of lists and filters out elements that are divisible by both 2 and 3.
The resulting elements ... |
python_sharegpt_23k | 371 | Write a Python function to generate a SQL query for selecting data from a table with the following specifications:
1. The table name should be provided as a parameter to the function.
2. The number of columns in the table should also be provided as a parameter.
3. The column names should be generated dynamically, rath... | Here is the Python code to solve the given problem in the markdown block:
```python
def generate_sql_query(table_name: str, num_columns: int, condition: str) -> str:
"""
Generates a SQL query for selecting data from a table with dynamically generated column names and a WHERE clause.
Args:
table_nam... |
python_sharegpt_23k | 373 | Implement a modified merge sort algorithm that uses a binary search to find the correct position for each element in the sorted subarray. Additionally, ensure that the algorithm has a time complexity of O(nlogn) and a space complexity of O(n).
The code should start with:
```python
from typing import List
def modified... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import bisect
def modified_merge_sort(arr: List[int]) -> List[int]:
"""
Implements a modified merge sort algorithm that uses binary search to find the correct position for each element in the sorted suba... |
python_sharegpt_23k | 376 | Write a Python function to count the number of unique elements in a given list of integers with a time complexity of O(n log n) and using only constant space. The function should handle negative numbers correctly and should not use any additional data structures such as sets or dictionaries.
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 count_unique_elements(lst: List[int]) -> int:
"""
Counts the number of unique elements in a given list of integers.
The function sorts the list in O(n log n) time and then iterates through the s... |
python_sharegpt_23k | 379 | Write a Python function to combine two given arrays, remove any duplicate elements, and return the combined array sorted in descending order. The function should handle arrays with duplicate elements, empty arrays, and arrays with negative numbers as input. 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 combine_and_sort_arrays(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Combines two arrays, removes duplicate elements, and returns the combined array sorted in descending order.
Args:
... |
python_sharegpt_23k | 396 | Write a Python function to reverse an array of positive integers and return a new array without modifying the original array. The reversed array should be sorted in descending order. The algorithm should have a time complexity of O(n), where n is the length of the input array.
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
def reverse_and_sort_descending(nums: List[int]) -> List[int]:
"""
Reverses an array of positive integers and returns a new array sorted in descending order without modifying the original array.
Arg... |
python_sharegpt_23k | 400 | Write a Python function to determine if all characters in a given string are uppercase letters. The function should return `True` if all characters in the string are uppercase letters from 'A' to 'Z', and `False` otherwise. The function should handle special characters, numbers, and whitespace by returning `False` for ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_all_uppercase(string: str) -> bool:
"""
Determines if all characters in the given string are uppercase letters.
Args:
string (str): The string to be checked.
Returns:
bool: True if all characters... |
python_sharegpt_23k | 403 | Write a Python function to determine if a given number \( N \) is a prime number. The function should adhere to the following constraints:
1. It should return `False` if \( N \) is less than 2, as prime numbers are greater than 1.
2. It should check for divisors from 2 up to the square root of \( N \) (inclusive). If a... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime(N: int) -> bool:
"""
Determine if a given number N is a prime number.
Args:
N (int): The number to check for primality.
Returns:
bool: True if N is a prime number, False otherw... |
python_sharegpt_23k | 407 | Write a Python function to check if two strings are anagrams, considering the strings can contain special characters and have a time complexity of O(n) and a space complexity of O(1). The function should be case-insensitive and should only consider alphabetic characters, ignoring any special characters or digits presen... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Check if two strings are anagrams, considering only alphabetic characters and ignoring case and special characters.
Args:
str1 (str): The first string to compare.
... |
python_sharegpt_23k | 408 | Write a Python function to sort a list of integers in descending order using the selection sort algorithm and remove any duplicate values that appear in the list. The function should start with:
```python
from typing import List
def sort_and_remove_duplicates(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 sort_and_remove_duplicates(lst: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using selection sort and removes any duplicate values.
Args:
lst (List[int]): Th... |
python_sharegpt_23k | 411 | Write a Python function to count the number of distinct integers in a given list. The function should have a time complexity of O(n) and a space complexity of O(n). The code should start with:
```python
from typing import List
def count_distinct_numbers(nums: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_distinct_numbers(nums: List[int]) -> int:
"""
Counts the number of distinct integers in a given list.
Args:
nums (List[int]): The list of integers to count distinct elements from.
... |
python_sharegpt_23k | 416 | Write a Python function to parse a given HTML string and extract the text content of all occurrences of a specified tag. The HTML string may contain nested tags and multiple occurrences of the specified tag. The solution should not use any built-in HTML parsing libraries or functions. The function should start with:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
def extract_tag_content(html: str, tag: str) -> str:
"""
Extracts the text content of all occurrences of a specified tag from an HTML string.
Args:
html (str): The HTML string to parse.
tag (str): The tag wh... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.