source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 10,288 | Write a Python function to delete duplicates from a given list while keeping track of the count of each duplicate element. The function should return a new list with the unique elements from the original list, along with their respective counts. The function should maintain the order of the original list while removing... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def delete_duplicates_with_count(lst: List[int]) -> List[Tuple[int, int]]:
"""
Deletes duplicates from a given list while keeping track of the count of each duplicate element.
Args:
l... |
python_sharegpt_23k | 10,289 | Write a Python function to recursively calculate the sum of all even numbers in a given list. The list may contain negative numbers, and the sum can be negative as well. The function should use recursion instead of loops.
The code should start with:
```python
from typing import List
def sum_even_numbers_recursive(lst... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_even_numbers_recursive(lst: List[int], index: int = 0) -> int:
"""
Recursively calculates the sum of all even numbers in a given list.
Args:
lst (List[int]): The list of integers to ... |
python_sharegpt_23k | 10,296 | Write a Python function to count the frequency of each unique character in a given string, ignoring case sensitivity and whitespace characters. The function should return a dictionary where the keys are the unique characters and the values are their respective frequencies. The function should have a time complexity of ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def character_frequency(string: str) -> Dict[str, int]:
"""
Counts the frequency of each unique character in a given string, ignoring case sensitivity and whitespace characters.
Args:
string... |
python_sharegpt_23k | 10,297 | Write a Python function to generate a list of all prime numbers between 1 and a given upper limit `n`, and then calculate the sum of all the prime numbers in the list. The function should be efficient and handle the upper limit `n` appropriately.
The code should start with:
```python
from typing import List
def sum_o... | 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 | 10,303 | Write a Python function to generate an NxN array where each element is 1, ensuring that each row and column sum up to N. The function should achieve this with a time complexity less than O(N^2).
The code should start with:
```python
from typing import List
def generate_array(N: int) -> List[List[int]]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_array(N: int) -> List[List[int]]:
"""
Generate an NxN array where each element is 1, ensuring that each row and column sum up to N.
Args:
N (int): The size of the NxN array.
... |
python_sharegpt_23k | 10,306 | Write a Python function to check whether a given input string is a palindrome or not. The function must meet the following requirements:
1. **In-place and Constant Space**: The solution should not use any additional data structures (such as arrays or stacks) and must solve the problem in-place with constant extra space... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(s: str) -> bool:
"""
Check whether the given input string is a palindrome.
The function handles case-insensitive palindromes, ignores punctuation and whitespace,
and considers numbers as valid characte... |
python_sharegpt_23k | 10,307 | Write a Python function to calculate the mean of a given list of numbers, ensuring the result is rounded to two decimal places. The input list can contain up to 1 million numbers. The function should handle edge cases such as an empty list and lists with very large numbers.
The code should start with:
```python
from t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_mean(numbers: List[float]) -> float:
"""
Calculate the mean of a given list of numbers and round it to two decimal places.
Args:
numbers (List[float]): A list of numbers (can c... |
python_sharegpt_23k | 10,308 | Write a Python function to determine if a given string is a palindrome, adhering to the following constraints:
- The function should not use any built-in string reversal functions or data structures (e.g., slicing, `reverse()` function, stack, etc.).
- The function should have a time complexity of O(n) and a space comp... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(string: str) -> bool:
"""
Determines if a given string is a palindrome without using built-in reversal functions or data structures.
Args:
string (str): The string to check for palindrome property.... |
python_sharegpt_23k | 10,310 | Write a Python function to filter out non-prime numbers from a given list of integers. The function should not use any built-in prime number checking functions or libraries. The code should start with:
```python
from typing import List
def filter_primes(numbers: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Helper function to check if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, ... |
python_sharegpt_23k | 10,315 | Write a Python function to flatten a nested list of integers and return the sum of all the elements. The nested list can contain integers and lists, and can have up to three levels of nesting. The final sum should only include the integers within the nested lists, and not the lists themselves.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def flatten_and_sum(nested_list: List[Union[int, List]]) -> int:
"""
Flattens a nested list of integers and returns the sum of all the elements.
Args:
nested_list (List[Union[int, Lis... |
python_sharegpt_23k | 10,316 | Write a Python function to find all intersecting elements between two given arrays with a time complexity of O(n), where n is the length of the arrays. The function must not use any built-in intersection functions or data structures, and it should be implemented using only constant extra space without modifying the inp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersecting_elements(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Find all intersecting elements between two arrays with a time complexity of O(n).
Args:
arr1 (List[int]): T... |
python_sharegpt_23k | 10,318 | 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 the solution should be less than O(n^2). The function should handle cases where the array has fewer than two elements by returning `None`.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_second_maximum(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 (... |
python_sharegpt_23k | 10,321 | Write a Python function to calculate the weighted average score of a list of numbers, where each number is multiplied by a corresponding weight. The weights are provided in a separate list of the same length. The function should handle empty lists by returning 0, and should skip numbers corresponding to non-positive we... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_weighted_average(numbers: List[float], weights: List[float]) -> float:
"""
Calculate the weighted average score of a list of numbers, where each number is multiplied by a corresponding weig... |
python_sharegpt_23k | 10,323 | Write a Python function to reorder a given array such that all non-prime numbers appear before all prime numbers while maintaining the relative order of prime and non-prime numbers. The function should use a helper function to determine if a number is prime.
The code should start with:
```python
from typing import Lis... | 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:
"""
Determine if a given number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False o... |
python_sharegpt_23k | 10,328 | Write a Python function to find the smallest element in a given array of positive integers using a divide and conquer approach. The array length is between 1 and 100, and the values are between 1 and 1000. The function should be efficient and handle the constraints provided.
The code should start with:
```python
from ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_element(arr: List[int]) -> int:
"""
Find the smallest element in a given array of positive integers using a divide and conquer approach.
Args:
arr (List[int]): The array of... |
python_sharegpt_23k | 10,330 | Write a Python function to find the smallest number in an array of numbers without using any built-in functions or methods. The solution should have a time complexity of O(n), where n is the number of elements in the array. It should handle negative numbers, arrays with duplicate numbers, and should not modify the orig... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_number(arr: List[int]) -> int:
"""
Find the smallest number in an array of integers without using any built-in functions or methods.
Args:
arr (List[int]): The array of int... |
python_sharegpt_23k | 10,333 | Write a Python function to find the position of a substring in a string, without using any built-in string search functions or regular expressions. The function should have a time complexity of O(n) or less and should handle the following additional requirements:
1. The function should be case-sensitive, meaning it sh... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_substring_positions(string: str, substrings: List[str]) -> List[List[int]]:
"""
Find all positions of each substring in the given string, including overlapping occurrences.
Args:
... |
python_sharegpt_23k | 10,339 | Write a Python function to calculate the Big O notation for a given algorithm's time complexity, which combines both recursive and iterative approaches. The function should return a string representation of the result. The algorithm's time complexity is determined by the time complexity of the recursive part, the time ... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_big_o(recursive_time_complexity: str, iterative_time_complexity: str, recursive_calls: int) -> str:
"""
Calculate the Big O notation for an algorithm that combines both recursive and iterative approaches.
Args... |
python_sharegpt_23k | 10,340 | Write a Python function to check if a value exists in a list of integers and return the number of occurrences of that value in the list. You are not allowed to use any built-in Python functions or libraries such as `in`, `count`, or `collections`. You must implement your own logic to achieve the desired result. The fun... | 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], value: int) -> int:
"""
Counts the number of occurrences of a specific value in a list of integers.
Args:
lst (List[int]): The list of integers to searc... |
python_sharegpt_23k | 10,341 | Write a Python function to implement the Odd-Even Sort algorithm. The function should take an array of numbers (integers, floating-point numbers, and negative numbers) as input and return the sorted array. The function should handle arrays of any length, including arrays with duplicate elements. The function should use... | 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[float]) -> List[float]:
"""
Sorts the given array using the Odd-Even Sort algorithm.
The Odd-Even Sort algorithm is an optimized version of the Bubble Sort algorithm.
... |
python_sharegpt_23k | 10,344 | Write a Python function to find the first non-repeating character in a given string. The function should not use any built-in Python functions or libraries, and it should handle cases where the input string contains special characters or numbers. The code should start with:
```python
def find_first_non_repeating_char(i... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_first_non_repeating_char(input_str: str) -> str:
"""
Find the first non-repeating character in the given string.
Args:
input_str (str): The input string to search for the first non-repeating character.
... |
python_sharegpt_23k | 10,351 | 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. Implement your own algorithm with a time complexity of O(n), where n is the length of the binary string.
The code should start with:
```python... | Here is the Python code to solve the given problem in the markdown block:
```python
def binary_to_decimal(binary: str) -> int:
"""
Convert a given binary string to its corresponding decimal number without using any built-in functions or libraries.
Args:
binary (str): The binary string to be convert... |
python_sharegpt_23k | 10,356 | 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), where n is the length of the string. The function 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 a given string without using any built-in string reversal functions, slicing, or additional data structures.
Args:
s (str): The string to be reversed.
Returns:
... |
python_sharegpt_23k | 10,357 | Write a Python function to generate a personalized message based on the provided name, age, and gender. The function should validate the gender to ensure it is either "male" or "female". If the gender is invalid, the function should raise a `ValueError` with an appropriate error message. The generated message should fo... | Here is the Python code to solve the given problem in the markdown block:
```python
def generate_message(name: str, age: int, gender: str) -> str:
"""
Generates a personalized message based on the provided name, age, and gender.
Args:
name (str): The name of the person.
age (int): The age o... |
python_sharegpt_23k | 10,360 | Write a Python function to find the minimum and maximum elements in a given list of numbers without using any built-in functions or methods for finding the minimum and maximum. The solution must have a time complexity of O(n), where n is the length of the list.
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, Tuple
def find_min_max(lst: List[int]) -> Tuple[int, int]:
"""
Find the minimum and maximum elements in a list of integers without using built-in functions or methods.
Args:
lst (List[int])... |
python_sharegpt_23k | 10,361 | Write a Python function to create a two-dimensional array of specified dimensions, ensuring that all elements in the array are unique and in ascending order. The function should have a time complexity of O(n) or better, where n is the total number of elements in the array.
The code should start with:
```python
from ty... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_2d_array(rows: int, cols: int) -> List[List[int]]:
"""
Create a two-dimensional array of specified dimensions with unique and ascending elements.
Args:
rows (int): The number of r... |
python_sharegpt_23k | 10,371 | Write a Python function to filter and return a list of all odd numbers greater than 10 and less than 100 from a given list of positive integers. The returned list should be sorted in ascending order. The function should handle cases where the input list is empty and should raise a `ValueError` if any negative numbers a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_odd_numbers(input_list: List[int]) -> List[int]:
"""
Filters and returns a list of all odd numbers greater than 10 and less than 100 from the given list of positive integers.
The returned ... |
python_sharegpt_23k | 10,373 | Write a Python function to count the number of unique prime numbers in a given 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 libraries to check for prime numbers. The code should start with:
```python
from... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def 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 | 10,375 | 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). The function should not use any built-in functions or modules that directly calculate the GCD.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd_euclidean(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
Args:
a (int): The first number.
b (int): The second number.
Retur... |
python_sharegpt_23k | 10,376 | 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(lst: List[int]) -> List[int]:
"""
Sorts the given list of integers in descending order and removes any duplicate elements.
Args:
lst (List[int]): The list of i... |
python_sharegpt_23k | 10,378 | Write a Python function to sort a given list of numbers in ascending order using the Bubble Sort algorithm. The function should not use any built-in sorting functions or other sorting algorithms. 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 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 | 10,379 | Write a Python function to generate the first n prime numbers using a more advanced primality testing algorithm, such as the Miller-Rabin primality test. The function should have a time complexity of O(n^2) or better and should not use any external libraries or functions for prime number generation. Additionally, the f... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_first_n_primes(n: int) -> list:
"""
Generate the first n prime numbers using the Miller-Rabin primality test.
Args:
n (int): The number of prime numbers to generate.
Returns:
list: A list contai... |
python_sharegpt_23k | 10,385 | Write a Python function to find the maximum value in a given array of positive integers without using any built-in functions or libraries. The function should have a time complexity of O(n) and should not use any extra space apart from the given array itself. The function 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 find_maximum(arr: List[int]) -> int:
"""
Find the maximum value in a given array of positive integers without using any built-in functions or libraries.
Args:
arr (List[int]): The array ... |
python_sharegpt_23k | 10,389 | Write a Python function to convert a given positive decimal number into its binary representation. The function should handle decimal numbers up to \(10^9\). The code should start with:
```python
def decimal_to_binary(decimal: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(decimal: int) -> str:
"""
Convert a given positive decimal number into its binary representation.
Args:
decimal (int): The decimal number to convert. Must be a positive integer less than or equ... |
python_sharegpt_23k | 10,390 | Write a Python function to find the smaller of two given integers without using any comparison operators (e.g., <, >, <=, >=), conditional statements (e.g., if-else, switch-case), arithmetic operators (e.g., +, -, *, /), or bitwise operators (e.g., &, |, ^, ~). You are only allowed to use basic arithmetic operations (e... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_smaller_num(a: int, b: int) -> int:
"""
Find the smaller of two integers without using comparison, arithmetic, or bitwise operators.
Args:
a (int): The first integer.
b (int): The second integer.
... |
python_sharegpt_23k | 10,397 | Write a Python function to find the first repeating element in an array of integers. The function should return the first element that appears more than once in the array. If no such element is found, the function should return -1. The function should have a time complexity of O(n).
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_first_repeating_element(arr: List[int]) -> int:
"""
Find the first repeating element in an array of integers.
Args:
arr (List[int]): The array of integers to search for the first re... |
python_sharegpt_23k | 10,398 | Write a Python function to find the maximum subarray sum in a given array using dynamic programming. The function should have a time complexity of O(n) and a space complexity of O(1), without using any extra data structures. The code should start with:
```python
from typing import List
def max_subarray_sum(arr: List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
"""
Find the maximum subarray sum in a given array using dynamic programming.
Args:
arr (List[int]): The input array of integers.
Returns:
... |
python_sharegpt_23k | 10,399 | Write a Python function to convert a date string from the format `MM/DD/YYYY` to `DD-MM-YYYY`, ensuring that the input date is valid and correctly handles leap years. The function should return the converted date string if the input is valid, otherwise it should return "Invalid date".
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_date(input_date: str) -> str:
"""
Converts a date string from the format MM/DD/YYYY to DD-MM-YYYY, ensuring the date is valid and handles leap years correctly.
Args:
input_date (str): The input date stri... |
python_sharegpt_23k | 10,405 | Write a Python function to sort a list of strings in alphabetical order while ignoring case sensitivity. The function should also handle strings with special characters and numbers, sorting them based on their ASCII values. You are not allowed to use any built-in sorting functions or libraries. Implement your own sorti... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_strings_ignore_case(lst: List[str]) -> List[str]:
"""
Sorts a list of strings in alphabetical order while ignoring case sensitivity.
Special characters and numbers are sorted based on their ... |
python_sharegpt_23k | 10,411 | Write a Python function to sort elements in an array in descending order using the merge sort algorithm, which has a time complexity of O(nlogn). The function should take a list of integers as input and return a new list with the elements sorted in descending order.
The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts the elements in the given array in descending order using the merge sort algorithm.
Args:
arr (List[int]): The array of inte... |
python_sharegpt_23k | 10,414 | Write a Python function to convert a given string to uppercase and reverse the order of the characters without using any built-in string reversal methods or functions. The function should handle both uppercase and lowercase letters, converting all lowercase letters to uppercase and leaving other characters unchanged.
... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_and_uppercase_string(input_string: str) -> str:
"""
Convert the given string to uppercase and reverse the order of the characters.
Args:
input_string (str): The input string to be processed.
Returns... |
python_sharegpt_23k | 10,418 | Write a Python function to determine whether a given number is a strong prime number. A strong prime number is defined as a prime number that satisfies the following conditions:
1. It is greater than 1.
2. It is not a perfect square.
3. It is not a multiple of the first five prime numbers (2, 3, 5, 7, and 11).
The fun... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_strong_prime(num: int) -> bool:
"""
Determine whether a given number is a strong prime number.
A strong prime number is defined as:
1. It is greater than 1.
2. It is not a perfect square.
3. It is not a m... |
python_sharegpt_23k | 10,421 | Write a Python function to remove all whitespace, punctuation, and digits from a given string, and convert all characters to uppercase. The function should use a translation table to achieve this efficiently.
The code should start with:
```python
import string
def clean_and_uppercase_string(text: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import string
def clean_and_uppercase_string(text: str) -> str:
"""
Removes all whitespace, punctuation, and digits from the given string and converts all characters to uppercase.
Args:
text (str): The input string... |
python_sharegpt_23k | 10,424 | Write a Python function to count the occurrences of a given character in a string, considering both uppercase and lowercase characters as separate entities. The function should return the total count of the specified character in the string.
The code should start with:
```python
def count_character_occurrences(string:... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_character_occurrences(string: str, character: str) -> int:
"""
Counts the occurrences of a specified character in a given string, considering both uppercase and lowercase characters as separate entities.
Args:
... |
python_sharegpt_23k | 10,426 | Write a Python function to find the maximum sum of any subarray within each row of a given two-dimensional array of integers. A subarray is defined as a contiguous block of elements in the same row. The function should return a list of maximum sums for each row, ensuring that each sum is a non-negative integer.
The co... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_maximum_sum_subarray(A: List[List[int]]) -> List[int]:
"""
Find the maximum sum of any subarray within each row of a given two-dimensional array.
Args:
A (List[List[int]]): A two-di... |
python_sharegpt_23k | 10,428 | 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.
The code should start with:
```python
from typing import List, Any
def get_item_at_index(my_list: List[An... | 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 | 10,430 | Write a Python function to calculate the factorial of a given non-negative integer. The function should handle invalid inputs gracefully by returning an appropriate error message. Additionally, there is a bug in the code that needs to be fixed. The bug is not related to error handling.
The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int or str:
"""
Calculate the factorial of a given non-negative integer.
Args:
n (int): The non-negative integer for which to calculate the factorial.
Returns:
int or str: The f... |
python_sharegpt_23k | 10,432 | 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 use bitwise operations to achieve the required space complexity.
The code should start with:
```python
def has_unique_characters(string: st... | Here is the Python code to solve the given problem in the markdown block:
```python
def has_unique_characters(string: str) -> bool:
"""
Check whether a string has all unique characters using bitwise operations.
Args:
string (str): The string to check for unique characters.
Returns:
boo... |
python_sharegpt_23k | 10,434 | Write a Python function to delete an element from a list given its index. The index should be verified to be within the bounds of the list before performing the deletion. If the index is out of bounds, an appropriate error message should be displayed. The remaining elements in the list should be shifted to fill the emp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def delete_element(arr: List[int], index: float) -> List[int]:
"""
Deletes an element from the given list at the specified index.
Args:
arr (List[int]): The list from which to delete the ele... |
python_sharegpt_23k | 10,435 | Write a Python function to flatten a given list of lists, where each inner list may contain any combination of integers, floats, strings, and dictionaries. The function should flatten the list by removing any nested lists and merging their elements into a single flat list. The final list should maintain the order of th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def flatten_nested_list(input_list: List[Any]) -> List[Any]:
"""
Flattens a given list of lists, where each inner list may contain any combination of integers, floats, strings, and dictionaries.
... |
python_sharegpt_23k | 10,436 | Write a Python function to find the intersection of two given arrays. The intersection is defined as the common elements between the two arrays, and the result should be returned in the order they appear in the first array. You are not allowed to use any built-in intersection functions or sets.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_intersection(list_one: List[int], list_two: List[int]) -> List[int]:
"""
Find the intersection of two lists of integers. The intersection is defined as the common elements
between the two li... |
python_sharegpt_23k | 10,438 | Write a Python function to compare two sorted lists and return a list with the common elements along with their indices in both lists. The lists can contain duplicates, and the common elements should be counted multiple times based on their occurrences in both lists. The function should not use any built-in functions o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_common_elements_with_indices(list_one: List[int], list_two: List[int]) -> Tuple[List[int], List[int], List[int]]:
"""
Compare two sorted lists and return a list with the common elements a... |
python_sharegpt_23k | 10,442 | Write a Python function to find the common items between two given arrays. The function should handle cases where one of the inputs is a string instead of an array. The function should not use any built-in functions or libraries to find the common items, and it should not modify the original arrays. The function should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def find_common_items(arr1: Union[List[int], str], arr2: Union[List[int], str]) -> List[int]:
"""
Find the common items between two given arrays. Handles cases where one of the inputs is a string.... |
python_sharegpt_23k | 10,443 | Write a Python function to generate the Fibonacci sequence up to the nth term, where n is a positive integer greater than 2. The function should also return the sum of all the even numbers in the Fibonacci sequence, but without using any loops or if statements. The function should start with:
```python
from typing impo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def fibonacci_sequence_and_even_sum(n: int) -> Tuple[List[int], int]:
"""
Generate the Fibonacci sequence up to the nth term and calculate the sum of all even numbers in the sequence.
Args:
... |
python_sharegpt_23k | 10,445 | Write a Python function to calculate the length of a given list without using the built-in `len()` function or the `len()` method of the list object. The function should only use basic list operations and logical steps, without relying on any external libraries or modules.
The code should start with:
```python
from ty... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_length(lst: List) -> int:
"""
Calculate the length of a given list without using the built-in len() function or the len() method of the list object.
Args:
lst (List): The list ... |
python_sharegpt_23k | 10,448 | Write a Python function to create a dictionary from two lists of the same length. The keys in the dictionary should be obtained by converting the elements of the first list to uppercase and appending the suffix "KEY". The values in the dictionary should be obtained by converting the elements of the second list to integ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def create_dictionary(mylist1: List[str], mylist2: List[str]) -> Dict[str, int]:
"""
Create a dictionary from two lists of the same length. The keys are obtained by converting the elements of the f... |
python_sharegpt_23k | 10,451 | Write a Python function to reverse the content of an array without using any built-in array reverse functions. The input array will contain at least 10 elements and at most 1000 elements. The array elements will be integers between -100 and 100. The function should also handle the reversal of arrays containing duplicat... | 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 content of an array without using any built-in array reverse functions.
Args:
arr (List[int]): The input array containing int... |
python_sharegpt_23k | 10,456 | Write a Python function to determine if a given integer is a prime number. If the number is not prime, the function should return the smallest prime factor of the number. The function should efficiently handle inputs up to \(10^9\) and provide the result within 2 seconds.
The code should start with:
```python
import m... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime_or_smallest_factor(n: int) -> int:
"""
Determine if a given integer is a prime number. If not, return the smallest prime factor.
Args:
n (int): The integer to check for primality.
Retu... |
python_sharegpt_23k | 10,461 | Write a Python function to determine if two given strings are equal, considering the following constraints:
1. The comparison should be case-sensitive.
2. Whitespace should be considered in the comparison.
3. Punctuation marks should be ignored.
4. You are not allowed to use any built-in string comparison functions or ... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_strings_equal(string1: str, string2: str) -> bool:
"""
Determine if two strings are equal considering case sensitivity, whitespace, and ignoring punctuation.
Args:
string1 (str): The first string to compare.... |
python_sharegpt_23k | 10,463 | Write a Python function to check if an array contains all unique values and, if not, remove the duplicates while maintaining the order of the elements. The function should have a time complexity of O(n), where n is the length of the array, and should not use any additional data structures other than a list to store the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def check_and_remove_duplicates(arr: List[int]) -> List[int]:
"""
Check if an array contains all unique values and remove duplicates if any.
The function maintains the order of elements and has a tim... |
python_sharegpt_23k | 10,464 | Write a Python function to convert all words in a given list to uppercase and append the string "Welcome to the magnificent celebration" at the end. The function should return the modified list.
The code should start with:
```python
from typing import List
def convert_and_append(words: List[str]) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_and_append(words: List[str]) -> List[str]:
"""
Converts all words in the given list to uppercase and appends the string "Welcome to the magnificent celebration" at the end.
Args:
... |
python_sharegpt_23k | 10,469 | 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 given array in ascending order without any duplicates.
The sorting algorithm has a time complexity of O(n log n) and a space complexity ... |
python_sharegpt_23k | 10,475 | Write a Python function to find the minimum prime number in an unordered array of integers. The array may contain duplicates. If no prime numbers are found, the function should return `None`.
The code should start with:
```python
from typing import List, Optional
def find_min_prime(arr: List[int]) -> Optional[int]:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
import math
def is_prime(n: int) -> bool:
"""
Check if a given number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is ... |
python_sharegpt_23k | 10,477 | Write a Python function to merge two given lists and sort the merged list in descending order without using any built-in sorting functions or libraries. The sorting should be done in-place using a custom sorting algorithm.
The code should start with:
```python
from typing import List
def merge_and_sort_descending(lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_and_sort_descending(list_a: List[int], list_b: List[int]) -> None:
"""
Merges two lists and sorts the merged list in descending order in-place.
Args:
list_a (List[int]): The first ... |
python_sharegpt_23k | 10,478 | Write a Python function to create an empty set and add the items "C", "B", "A" in reverse alphabetical order. Ensure that the set does not contain any duplicate elements. Additionally, implement a function that takes this set as input and returns the set in ascending alphabetical order. The code should start with:
```p... | Here is the Python code to solve the given problem in the markdown block:
```python
def create_and_sort_set() -> set:
"""
Creates an empty set and adds the items "C", "B", "A" in reverse alphabetical order.
Then, sorts the set in ascending alphabetical order and returns the sorted set.
Returns:
... |
python_sharegpt_23k | 10,480 | Write a Python function to convert a given string in snake_case to camelCase. In camelCase, the first letter of each word (except the first word) is capitalized and all the words are concatenated together without any spaces. The function should handle multiple consecutive underscores, leading and trailing underscores, ... | Here is the Python code to solve the given problem in the markdown block:
```python
def snake_to_camel(snake_case_string: str) -> str:
"""
Converts a given string in snake_case to camelCase.
Args:
snake_case_string (str): The input string in snake_case format.
Returns:
str: The convert... |
python_sharegpt_23k | 10,481 | Write a Python function to calculate the sum of even elements in a given list of positive integers, excluding any element greater than 10. The function should have a time complexity of O(n) and should not use any built-in sum or filter functions.
The code should start with:
```python
from typing import List
def sum_e... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_even_elements(nums: List[int]) -> int:
"""
Calculate the sum of even elements in a given list of positive integers, excluding any element greater than 10.
Args:
nums (List[int]): A l... |
python_sharegpt_23k | 10,486 | Write a Python function to insert a new element at the beginning of a given list without using any built-in list manipulation methods or functions such as `insert()` or `append()`. Additionally, you are not allowed to use any loops or recursion. You can only use built-in Python functions and operators.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insert_at_beginning(lst: List[int], new_element: int) -> List[int]:
"""
Inserts a new element at the beginning of the given list without using any built-in list manipulation methods or loops.
Ar... |
python_sharegpt_23k | 10,488 | 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 and whitespace. The function should start with:
```python
def are_anagrams(str1: str, str2: ... | 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 of each other.
Args:
str1 (str): The first string to check.
str2 (str): The second string to check.
Returns:
... |
python_sharegpt_23k | 10,495 | Write a Python function to extract and return a list of all unique alphabets from a given string, sorted in alphabetical order, while ignoring case and non-alphabet characters. The function should handle strings with spaces, punctuation, and mixed cases.
The code should start with:
```python
from typing import List
d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_unique_alphabets(input_string: str) -> List[str]:
"""
Extracts and returns a list of all unique alphabets from the given string, sorted in alphabetical order.
Ignores case and non-alphabet ch... |
python_sharegpt_23k | 10,496 | Write a Python function to sort a given array in decreasing order using only recursion and without using any built-in sorting functions or loops. Additionally, the function should remove any duplicate elements from the sorted array. The function should have a time complexity of O(n log n) and a space complexity of O(lo... | 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 array in decreasing order and removes duplicate elements using only recursion.
Args:
arr (List[int]): The arr... |
python_sharegpt_23k | 10,497 | 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_and_index(array: List[int]) -> Tuple[int, int]:
"""
Find the largest number in the given array and its corresponding index.
If multiple numbers have the same maximum va... |
python_sharegpt_23k | 10,500 | Write a Python function to filter a given list of integers, returning only the prime numbers, and then sort the resulting list in descending order. The function should be named `filter_and_sort_primes`.
The code should start with:
```python
from typing import List
def filter_and_sort_primes(numbers: List[int]) -> Lis... | 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.
Returns:
bool: True if the number is prime, False otherwise.
"""
if n <... |
python_sharegpt_23k | 10,505 | Write a Python function to sort a list of integers in descending order using a stable O(n^2) time complexity algorithm. The function should not use any built-in sorting functions or libraries. The relative order of equal elements should be preserved in the sorted list.
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 stable_descending_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using a stable O(n^2) time complexity algorithm.
Args:
arr (List[int]): The list of ... |
python_sharegpt_23k | 10,507 | Write a Python function to sort a given list in ascending order and remove any duplicate elements. Additionally, the function should remove any negative numbers from the list before performing the sorting and duplicate removal. The code should start with:
```python
from typing import List
def clean_and_sort_list(lst: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def clean_and_sort_list(lst: List[int]) -> List[int]:
"""
Cleans and sorts a list by removing negative numbers, duplicates, and sorting in ascending order.
Args:
lst (List[int]): The input l... |
python_sharegpt_23k | 10,509 | Write a Python function to calculate the average of a list of integers, handling both empty lists and lists containing negative integers and floating-point numbers. The function should return 0 if the list is empty and should have a time complexity of O(n), where n is the length of the input list.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_average(numbers: List[int]) -> float:
"""
Calculate the average of a list of integers, handling empty lists and lists containing negative integers and floating-point numbers.
Args:
... |
python_sharegpt_23k | 10,511 | Write a Python function that removes the last item from a given list and returns the modified list. The function should also check if the list is empty before attempting to remove the item. If the list is empty, the function should raise a `ValueError` with the message "List is empty, cannot remove last item". The func... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_last_item(lst: List[int]) -> List[int]:
"""
Removes the last item from the given list and returns the modified list.
If the list is empty, raises a ValueError with the message "List is emp... |
python_sharegpt_23k | 10,513 | Write a Python function to calculate the average of all positive numbers in a given array of integers. The function should exclude any negative numbers and should handle empty arrays gracefully by returning 0. The code should start with:
```python
from typing import List
def average_of_positive_numbers(nums: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def average_of_positive_numbers(nums: List[int]) -> float:
"""
Calculate the average of all positive numbers in a given array of integers.
Args:
nums (List[int]): The array of integers from ... |
python_sharegpt_23k | 10,516 | Write a Python function to count the frequency of each word in a given string, considering both uppercase and lowercase letters as the same, and excluding any punctuation marks. The function should return a dictionary where the keys are the words and the values are their respective frequencies.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
import re
def count_word_frequency(string: str) -> Dict[str, int]:
"""
Counts the frequency of each word in a given string, considering both uppercase and lowercase letters as the same, and excluding any... |
python_sharegpt_23k | 10,517 | 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 between words into a single space. The code should start with:
```python
de... | 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 | 10,518 | 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 contain at least 5 numbers and may include duplicates. The function should handle lists with both positive and negative numbers. The function should return ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_min_max_without_builtins(numbers: List[int]) -> Tuple[int, int]:
"""
Find the minimum and maximum values in a list of numbers without using built-in functions.
Args:
numbers ... |
python_sharegpt_23k | 10,520 | 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 code should start with:
```python
def convert_to_uppercase(st... | 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 | 10,521 | Write a Python function to find the highest common factor (HCF) of two given numbers using only bitwise operators, without using any arithmetic operators, and ensuring that the solution has a time complexity of O(log(min(a, b))). The function should be efficient and leverage bitwise operations to achieve the desired ti... | Here is the Python code to solve the given problem in the markdown block:
```python
def highest_common_factor(a: int, b: int) -> int:
"""
Calculate the highest common factor (HCF) of two numbers using only bitwise operators.
Args:
a (int): The first number.
b (int): The second number.
... |
python_sharegpt_23k | 10,522 | Write a Python function to iterate over a list of strings and stop when it encounters a string that contains both uppercase and lowercase letters, but only if the string also contains at least one special character from a given set of special characters. The function should return the first such string found.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_special_string(strings: List[str], special_chars: List[str]) -> Optional[str]:
"""
Finds and returns the first string in the list that contains both uppercase and lowercase letters,
... |
python_sharegpt_23k | 10,524 | Write a Python function to compute the factorial of a non-negative integer using recursion. The function should handle negative integers by raising a `ValueError` and return `None` for any other non-integer inputs. The function should also have a time complexity of O(n).
The code should start with:
```python
def facto... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Compute the factorial of a non-negative integer using recursion.
Args:
n (int): The non-negative integer for which to compute the factorial.
Returns:
int: The factorial... |
python_sharegpt_23k | 10,529 | Write a Python function to determine if a given list of strings is a palindrome list. A palindrome list is defined as a list where each string in the list is a palindrome. A string is considered a palindrome if it reads the same backward as forward.
The code should start with:
```python
from typing import List
def is... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_palindrome_list(lst: List[str]) -> bool:
"""
Determine if a given list of strings is a palindrome list.
A palindrome list is defined as a list where each string in the list is a palindrome.
... |
python_sharegpt_23k | 10,530 | Write a Python function to find the maximum number in a given array of integers. If the maximum number appears multiple times in the array, return the indices of all occurrences of the maximum number in ascending order. If the maximum number does not appear in the array, return an empty list. The function should be eff... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_indices(arr: List[int]) -> List[int]:
"""
Find the maximum number in the given array and return the indices of all its occurrences in ascending order.
Args:
arr (List[int]): The... |
python_sharegpt_23k | 10,532 | Write a Python function that takes two parameters, one of which must be an integer and the other must be a string. The function should return a list containing the integer and the string. Additionally, the function should validate the types of the parameters and raise a `TypeError` if the types are incorrect.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_list_with_validation(num: int, text: str) -> List[int | str]:
"""
Creates a list containing an integer and a string after validating their types.
Args:
num (int): The integer to i... |
python_sharegpt_23k | 10,538 | Write a Python function to implement the bubble sort algorithm. The function should sort an array of integers in ascending order by iterating through the array only once during the sorting process. The function should handle arrays of any length, including arrays with duplicate numbers and negative numbers. The impleme... | 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 | 10,548 | Write a Python function to convert a string representing a time in the format HH:MM to the number of minutes since midnight. The function should validate the input format and ensure that the hours and minutes are within the valid range (0-23 for hours and 0-59 for minutes). If the input is invalid, the function should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def time_to_minutes(time_str: str) -> int:
"""
Converts a string representing a time in the format HH:MM to the number of minutes since midnight.
Args:
time_str (str): A string representing the time in the format HH... |
python_sharegpt_23k | 10,549 | Write a Python function to output all the elements from a given list in reverse order. The list must contain at least 5 elements and at most 10 elements. Each element in the list should be a string of exactly 3 characters. The output should be formatted in uppercase. The function should return a list of these formatted... | 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]) -> List[str]:
"""
Returns a list of strings in reverse order and formatted in uppercase.
Args:
lst (List[str]): A list of strings, each of exactly 3... |
python_sharegpt_23k | 10,551 | Write a Python function to find the closest decimal approximation of a given floating-point number using a recursive algorithm. The function should stop when a specified precision is reached, where precision is defined as the number of decimal places to consider. The function should handle the base case where the preci... | Here is the Python code to solve the given problem in the markdown block:
```python
def closest_decimal_approximation(n: float, precision: int) -> float:
"""
Find the closest decimal approximation of a given floating-point number using a recursive algorithm.
Args:
n (float): The initial approximati... |
python_sharegpt_23k | 10,552 | Write a Python function to determine if a given string is a palindrome without using any built-in string manipulation functions or data structures. The function should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the string.
The code should start with:
```python
def is_palind... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(string: str) -> bool:
"""
Determine if a given string is a palindrome without using any built-in string manipulation functions or data structures.
Args:
string (str): The string to check for palind... |
python_sharegpt_23k | 10,554 | Write a Python function to swap the contents of two variables without using a temporary variable. The function should work for any data type, including integers, floating-point numbers, strings, and lists. The function should return the swapped values.
The code should start with:
```python
def swap_variables(a, b):
``... | Here is the Python code to solve the given problem in the markdown block:
```python
def swap_variables(a, b):
"""
Swaps the contents of two variables without using a temporary variable.
Args:
a: The first variable to be swapped.
b: The second variable to be swapped.
Returns:
tu... |
python_sharegpt_23k | 10,556 | Write a Python function to find the minimum of three given numbers. The function should take three integers as input and return the smallest one. The code should start with:
```python
def find_minimum_of_three(num1: int, num2: int, num3: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_minimum_of_three(num1: int, num2: int, num3: int) -> int:
"""
Find the minimum of three given integers.
Args:
num1 (int): The first integer.
num2 (int): The second integer.
num3 (int): The t... |
python_sharegpt_23k | 10,560 | Write a Python function to implement the insertion sort algorithm to sort an 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 algori... | 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 the insertion sort algorithm.
Args:
arr (List[int]): The array of ... |
python_sharegpt_23k | 10,562 | Write a Python function to remove duplicates from a given array while maintaining the order of the first occurrence of each element. The algorithm should have a time complexity of O(n) and use only constant space. The input array will contain integers ranging from -10^6 to 10^6 and can have a maximum length of 10^5.
T... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_order(arr: List[int]) -> List[int]:
"""
Removes duplicates from the given array while maintaining the order of the first occurrence of each element.
Args:
arr (List[... |
python_sharegpt_23k | 10,567 | 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, nor any iterative methods to approximate the square root. The func... | Here is the Python code to solve the given problem in the markdown block:
```python
def compute_square_root(number: int) -> int:
"""
Computes the square root of a given non-negative integer using only basic arithmetic operations.
Args:
number (int): The non-negative integer for which to compute the... |
python_sharegpt_23k | 10,571 | Write a Python function to create a new list from an existing list with no duplication, while also maintaining the original order of elements in the list. The original list must be sorted in descending order, and the resulting list must be sorted in ascending order.
The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(list_int: List[int]) -> List[int]:
"""
Create a new list from an existing list with no duplication, while also maintaining the original order of elements.
The original ... |
python_sharegpt_23k | 10,572 | Write a Python function to calculate the average value of a given list of numbers, where the average is rounded to the nearest integer. The list must contain at least 5 numbers, and the numbers must be between 1 and 100 inclusive. The function should validate the input list before calculating the average.
The code sho... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_average(numbers: List[int]) -> int:
"""
Calculate the average value of a given list of numbers, rounded to the nearest integer.
Args:
numbers (List[int]): A list of integers wh... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.