source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 5,486 | Write a Python function to multiply two integers without using the multiplication operator, loops, or built-in functions that directly perform multiplication. The function should handle both positive and negative integers. The code should start with:
```python
def multiply_without_operator(x: int, y: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def multiply_without_operator(x: int, y: int) -> int:
"""
Multiplies two integers without using the multiplication operator, loops, or built-in functions.
Args:
x (int): The first integer.
y (int): The secon... |
python_sharegpt_23k | 5,490 | Write a Python function to find the first non-repeating character in a given string without using any built-in Python functions or libraries. The function should return the character if found, otherwise return `None`.
The code should start with:
```python
def first_non_repeating_char(input_str: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def 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.
Retu... |
python_sharegpt_23k | 5,492 | Write a Python function to count the number of unique words in a given sentence. The function should handle punctuation marks and case sensitivity by converting the sentence to lowercase and removing punctuation marks. The code should start with:
```python
from typing import List
def count_unique_words(sentence: str) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_unique_words(sentence: str) -> int:
"""
Count the number of unique words in a given sentence, ignoring case and punctuation.
Args:
sentence (str): The sentence from which to count ... |
python_sharegpt_23k | 5,494 | Write a Python function to generate the Fibonacci sequence up to the nth term. The function should return a list containing the Fibonacci sequence. The code should start with:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the nth term.
Args:
n (int): The number of terms in the Fibonacci sequence to generate.
Returns:
... |
python_sharegpt_23k | 5,496 | Write a Python function to merge two sorted arrays in O(n) time, where n is the total number of elements in both arrays. The function should handle the case when one of the arrays is empty without raising an IndexError. The code should start with:
```python
from typing import List
def merge_sorted_arrays(arr1: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Merge two sorted arrays into a single sorted array in O(n) time, where n is the total number of elements in both arrays.
A... |
python_sharegpt_23k | 5,497 | Write a Python function to convert a given binary string to its corresponding decimal number without using any built-in functions or libraries for converting binary to decimal. Implement your own algorithm to solve the problem.
The code should start with:
```python
def binary_to_decimal(binary: str) -> int:
```
| 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 | 5,499 | Write a Python function to reverse a given string without using any built-in string reversal functions or slicing. The function should iterate through the string and construct the reversed string manually.
The code should start with:
```python
def reverse_string(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverses a given string without using any built-in string reversal functions or slicing.
Args:
string (str): The input string to be reversed.
Returns:
str: Th... |
python_sharegpt_23k | 5,500 | 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 function should handle an empty list by returning `None` for both the minimum and maximum values.
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, Tuple, Optional
def find_min_max_without_builtins(numbers: List[int]) -> Optional[Tuple[int, int]]:
"""
Find the minimum and maximum elements in a list of numbers without using built-in functions or met... |
python_sharegpt_23k | 5,502 | Write a Python function to create a two-dimensional array of specified dimensions, ensuring that all elements in the array are unique. The function should return the array filled with unique integers in a random order.
The code should start with:
```python
from typing import List
import random
def create_unique_array... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def create_unique_array(rows: int, cols: int) -> List[List[int]]:
"""
Creates a two-dimensional array of specified dimensions with all elements being unique integers.
Args:
row... |
python_sharegpt_23k | 5,504 | Write a Python function to replace all occurrences of a given substring with another substring in a case-insensitive manner. The function should not use any built-in string manipulation methods like `replace()` or regular expressions. Additionally, the function should return the number of replacements made, in addition... | Here is the Python code to solve the given problem in the markdown block:
```python
def replace_substring(string: str, substring: str, replacement: str) -> (str, int):
"""
Replace all occurrences of a given substring with another substring in a case-insensitive manner.
Args:
string (str): The origi... |
python_sharegpt_23k | 5,508 | Write a Python function to sort a given list of integers in descending order efficiently. The list may contain duplicates and have a length of up to \(10^6\). Implement an algorithm that uses \(O(1)\) extra space and runs in \(O(n \log n)\) time complexity, where \(n\) is the length of the list.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_descending(my_list: List[int]) -> List[int]:
"""
Sorts the given list of integers in descending order efficiently.
Args:
my_list (List[int]): The list of integers to be sorted.
... |
python_sharegpt_23k | 5,509 | Write a Python function to find the greatest common divisor (GCD) of two non-negative integers using the Euclidean algorithm. The function should not use any built-in functions or modules that directly calculate the GCD. The code should start with:
```python
def gcd(a: int, b: int) -> int:
```
| 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 non-negative integers using the Euclidean algorithm.
Args:
a (int): The first non-negative integer.
b (int): The secon... |
python_sharegpt_23k | 5,511 | Write a Python function to sort a given list of numbers in ascending order using the Bubble Sort algorithm. The function should correctly swap elements to ensure the list is sorted. 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 | 5,512 | Write a Python function to generate the first n prime numbers. 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. The code should start with:
```python
def generate_prime_numbers(n: int) -> list:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def generate_prime_numbers(n: int) -> list:
"""
Generate the first n prime numbers.
Args:
n (int): The number of prime numbers to generate.
Returns:
list: A list containing the first n prime numbers.
... |
python_sharegpt_23k | 5,517 | Write a Python function to convert a given number from decimal to binary representation without using any built-in functions or libraries for converting decimal to binary. The function should handle both positive and zero values. The code should start with:
```python
def decimal_to_binary(n: int) -> str:
```
| 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 decimal number to its binary representation without using built-in functions or libraries.
Args:
n (int): The decimal number to be converted.
Returns... |
python_sharegpt_23k | 5,522 | Write a Python function to calculate the length of a list without using the built-in `len()` function or the `len()` method of the list object. The function should iterate through the list and count the number of elements manually.
The code should start with:
```python
from typing import List
def get_list_length(lst:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_list_length(lst: List[any]) -> int:
"""
Calculate the length of a list without using the built-in len() function or the len() method of the list object.
Args:
lst (List[any]): The li... |
python_sharegpt_23k | 5,525 | 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) or better.
The code should start with:
```python
from typing import List
def remove_duplicates(my_list: List[int]) -> List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates(my_list: List[int]) -> List[int]:
"""
Removes duplicates from a list of integers while maintaining the order of the first occurrence of each element.
Args:
my_list (Lis... |
python_sharegpt_23k | 5,528 | Write a Python function to find the first repeating element in a given array of integers. If no repeating element is found, return -1. The function should efficiently identify the first repeating element by iterating through the array only once.
The code should start with:
```python
from typing import List
def find_f... | 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 a given array of integers.
Args:
arr (List[int]): The array of integers to search for the fir... |
python_sharegpt_23k | 5,529 | Write a Python function to convert a string containing dates from the format `MM/DD/YYYY` to `DD-MM-YYYY`, ensuring that leap years are handled correctly. The function should validate the input date and raise a ValueError if the date is invalid.
The code should start with:
```python
from datetime import datetime
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from datetime import datetime
def convert_date_format(date_string: str) -> str:
"""
Converts a date string from the format MM/DD/YYYY to DD-MM-YYYY, ensuring leap years are handled correctly.
Args:
date_string (str... |
python_sharegpt_23k | 5,531 | Write a Python function to find the maximum subarray sum in a given array using dynamic programming. The function must have a time complexity of O(n), where n is the length of the array. The function should handle both positive and negative numbers in the array.
The code should start with:
```python
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def 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 | 5,533 | Write a Python function to remove duplicates from an unsorted array of integers in-place. The function should have a time complexity of O(n), where n is the number of elements in the array. The function should also remove all occurrences of duplicates, not just one occurrence. 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 remove_duplicates_in_place(nums: List[int]) -> int:
"""
Removes all duplicates from an unsorted array of integers in-place and returns the length of the array after removal.
Args:
nums (... |
python_sharegpt_23k | 5,536 | Write a Python function to sort a list of numbers in ascending order using the merge sort algorithm, ensuring that the algorithm has a time complexity of O(n log n). Additionally, the algorithm should handle duplicate numbers within the list and ensure their relative order is preserved. The function should start with:
... | 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 | 5,538 | Write a Python function to calculate the sum of all the "price" values in a list of dictionaries. Each dictionary contains a key "price" with a positive integer value. The function should return the total sum as an integer.
The code should start with:
```python
from typing import List, Dict
def calculate_total_price(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def calculate_total_price(data: List[Dict[str, int]]) -> int:
"""
Calculate the sum of all "price" values in a list of dictionaries.
Args:
data (List[Dict[str, int]]): A list of dictio... |
python_sharegpt_23k | 5,541 | Write a Python function to sort elements in an array in descending order using a custom sorting algorithm. The function should not use Python's built-in `sort` method or any other built-in sorting functions. Instead, implement a sorting algorithm such as Bubble Sort, Selection Sort, or Insertion Sort to achieve the tas... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_array_descending(arr: List[int]) -> List[int]:
"""
Sorts the elements in the given array in descending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The array of int... |
python_sharegpt_23k | 5,542 | Write a Python function to determine whether a given number is prime or not, optimizing the code to reduce the time complexity to O(sqrt(n)). The function should handle edge cases such as negative numbers, 0, and 1, and should be efficient for larger numbers.
The code should start with:
```python
import math
def is_p... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime(n: int) -> bool:
"""
Determine whether a given number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 5,546 | 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 meets the following criteria:
1. It is greater than 1.
2. It is not a perfect square.
3. It is not a multiple of the first three prime numbers (2, 3, and 5).
The function should... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_strong_prime(n: int) -> bool:
"""
Determine whether a given number is a strong prime number.
A strong prime number is defined as a prime number that:
1. Is greater than 1.
2. Is not a perfect squ... |
python_sharegpt_23k | 5,549 | Write a Python function to sort a given list of integers in descending order using the bubble sort algorithm. The function should modify the input list in place and return the sorted list. The code should start with:
```python
from typing import List
def bubble_sort_descending(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_descending(arr: List[int]) -> List[int]:
"""
Sorts the given list of integers in descending order using the bubble sort algorithm.
Args:
arr (List[int]): The list of intege... |
python_sharegpt_23k | 5,550 | Write a Python function that removes all special characters from a given string. Special characters are defined as any non-alphanumeric characters (i.e., characters that are not letters or digits). The function should have a time complexity of O(n), where n is the length of the input string. The function should not use... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_special_characters(string: str) -> str:
"""
Removes all special characters from the given string. Special characters are defined as any non-alphanumeric characters.
Args:
string (str): The input string fr... |
python_sharegpt_23k | 5,551 | Write a Python function to preprocess a given string by removing all whitespace and punctuation, and converting all characters to lowercase. The function should return the preprocessed string. The code should start with:
```python
import string
def preprocess_string(input_string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import string
def preprocess_string(input_string: str) -> str:
"""
Preprocesses the input string by removing all whitespace and punctuation, and converting all characters to lowercase.
Args:
input_string (str): The... |
python_sharegpt_23k | 5,552 | Write a Python function to find the second smallest element in a given list of integers. If the list contains duplicates of the smallest element, the second smallest should be the next unique smallest element. If the list has fewer than two unique elements, the function should return `None`.
The code should start with... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_second_smallest(numbers: List[int]) -> Optional[int]:
"""
Find the second smallest element in a list of integers.
Args:
numbers (List[int]): The list of integers from whic... |
python_sharegpt_23k | 5,553 | Write a Python function to reverse a given string and return the reversed string. The function should handle any string input, including empty strings. The code should start with:
```python
def reverse_string(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(s: str) -> str:
"""
Reverses the given string and returns the reversed string.
Args:
s (str): The input string to be reversed.
Returns:
str: The reversed string.
"""
# Use sli... |
python_sharegpt_23k | 5,554 | Write a Python function to find the maximum sum of any contiguous subarray within each row of a given two-dimensional array of integers. The function should ensure that the maximum sum for each row is always a non-negative integer. The code should start with:
```python
from typing import List
def max_subarray_sum_per_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum_per_row(arr: List[List[int]]) -> List[int]:
"""
Find the maximum sum of any contiguous subarray within each row of a given two-dimensional array.
Args:
arr (List[List[in... |
python_sharegpt_23k | 5,555 | Write a Python function to convert a nested list into a nested dictionary. The nested list can contain other lists and dictionaries, and the function should recursively convert all nested structures. The function should handle lists and dictionaries at any depth.
The code should start with:
```python
from typing impor... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Any, List, Dict
def convert_nested_list_to_dict(nested_list: List[Any]) -> Dict[Any, Any]:
"""
Converts a nested list into a nested dictionary.
Args:
nested_list (List[Any]): The nested list to b... |
python_sharegpt_23k | 5,558 | Write a Python function to find the common items between two given arrays. The function should handle the case where one of the arrays is provided as a string instead of a list. The function should not use any built-in functions or libraries to find the common items, except for basic type checking and conversion. The f... | 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 the case where one of the arrays is provid... |
python_sharegpt_23k | 5,559 | 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 recursively flatten the list by removing any nested lists and merging their elements into a single flat list. The final list should maintain the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def flatten_nested_list(lst: List[Union[int, float, str, dict, List]]) -> List[Union[int, float, str, dict]]:
"""
Flattens a nested list of lists, where each inner list may contain any combination... |
python_sharegpt_23k | 5,561 | 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 | 5,562 | Write a Python function to find the nth Fibonacci number that is also a prime number. The function should efficiently compute the Fibonacci sequence and check for prime numbers until the nth prime Fibonacci number is found.
The code should start with:
```python
def find_nth_prime_fibonacci(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_nth_prime_fibonacci(n: int) -> int:
"""
Find the nth Fibonacci number that is also a prime number.
Args:
n (int): The position of the prime Fibonacci number to find.
Returns:
int: The nth Fibon... |
python_sharegpt_23k | 5,566 | Write a Python function to calculate the sum of the squares of the first n natural numbers. The function should take an integer `n` as input and return the sum of the squares of all numbers from 1 to `n`. The code should start with:
```python
def sum_of_squares(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_squares(n: int) -> int:
"""
Calculate the sum of the squares of the first n natural numbers.
Args:
n (int): The number of natural numbers to consider.
Returns:
int: The sum of the squares of ... |
python_sharegpt_23k | 5,567 | Write a Python function to find the minimum element in a given array without using any looping constructs or recursion. The function should leverage Python's built-in functions and capabilities to achieve this efficiently.
The code should start with:
```python
from typing import List
def find_min_element(arr: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_min_element(arr: List[int]) -> int:
"""
Find the minimum element in a given array without using loops or recursion.
Args:
arr (List[int]): The array of integers from which to find t... |
python_sharegpt_23k | 5,568 | Write a Python function to shuffle an array of elements without using the `random.shuffle()` function or any built-in shuffling function. Implement a custom shuffling algorithm that ensures each permutation of the array is equally likely.
The code should start with:
```python
from typing import List
import random
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def custom_shuffle(arr: List[int]) -> List[int]:
"""
Shuffles the elements of the given array using the Fisher-Yates shuffle algorithm.
Args:
arr (List[int]): The array to be s... |
python_sharegpt_23k | 5,573 | 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 5 elements and at most 100 elements. The function should start with:
```python
from typing import List
def reverse_array(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 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 at ... |
python_sharegpt_23k | 5,581 | Write a Python function to compare two strings for equality, considering case sensitivity and whitespace. The function should not use any built-in string comparison functions or methods. The code should start with:
```python
def compare_strings(string1: str, string2: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(string1: str, string2: str) -> bool:
"""
Compare two strings for equality considering case sensitivity and whitespace.
Args:
string1 (str): The first string to compare.
string2 (str): The... |
python_sharegpt_23k | 5,582 | Write a Python function to filter a given list of integers, returning only the even numbers, and then sort the resulting list in ascending order. The function should handle lists of any length, including empty lists.
The code should start with:
```python
from typing import List
def filter_and_sort_even_numbers(number... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_and_sort_even_numbers(numbers: List[int]) -> List[int]:
"""
Filters the given list of integers to include only even numbers and sorts the resulting list in ascending order.
Args:
... |
python_sharegpt_23k | 5,583 | Write a Python function to determine if an array contains all unique values. 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. The function should start with:
```python
from typing import List
def is_unique_array(arr: List[int]) -... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_unique_array(arr: List[int]) -> bool:
"""
Determines if an array contains all unique values.
This function sorts the array in place and checks for duplicates in a single pass,
ensuring a ... |
python_sharegpt_23k | 5,584 | Write a Python function to perform a binary search on a sorted array to find the index of the smallest element that is greater than a given target value. If no such element exists, return -1. The function should start with:
```python
from typing import List
def find_smallest_greater_index(array: List[int], target: int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_greater_index(array: List[int], target: int) -> int:
"""
Find the index of the smallest element in a sorted array that is greater than a given target value.
Args:
array (Li... |
python_sharegpt_23k | 5,585 | Write a Python function to implement a modified version of the insertion sort algorithm that sorts an array of integers in descending order. The function should take a list of integers as input and return the sorted list in descending order. The code should start with:
```python
from typing import List
def descending_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def descending_insertion_sort(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in descending order using a modified insertion sort algorithm.
Args:
arr (List[int]): The array of ... |
python_sharegpt_23k | 5,586 | Write a Python function to sort an array in ascending order while ensuring that the array does not contain any duplicate elements. The sorting algorithm should be optimized to have a time complexity of O(n log n) and a space complexity of O(1). The function should start with:
```python
from typing import List
def sort... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_unique_array(arr: List[int]) -> List[int]:
"""
Sorts an array in ascending order while removing duplicate elements.
The sorting algorithm is optimized to have a time complexity of O(n log n)... |
python_sharegpt_23k | 5,590 | Write a Python function to generate a random number within a specified range, ensuring that the generated number has exactly `n` digits. The function should take three parameters: `n` (the number of digits), `min_value` (the minimum value of the range), and `max_value` (the maximum value of the range). The generated nu... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
def generate_random_number(n: int, min_value: int, max_value: int) -> int:
"""
Generates a random number with exactly `n` digits within the specified range [min_value, max_value].
Args:
n (int): The n... |
python_sharegpt_23k | 5,594 | Write a Python function to add a new item to the online grocery store's inventory. The function should take the name of the item, its quantity, price per unit, and nutritional information (calories, carbohydrates, and vitamins) as inputs and update the existing inventory dictionary. If the item already exists in the in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Union
def add_item_to_inventory(inventory: Dict[str, Dict[str, Union[int, float, Dict[str, Union[int, List[str]]]]]],
item_name: str,
quantity: int,
... |
python_sharegpt_23k | 5,596 | Write a Python function to find the minimum prime number in an unordered array of integers. The function should return `None` if no prime numbers are present in the array.
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
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 prime, False... |
python_sharegpt_23k | 5,598 | Write a Python function to sort a given array in descending order using a recursive merge sort algorithm. The function should have a time complexity of O(nlogn) and must be implemented using recursion. Additionally, the sorted array should be stored in a new array and the original array should remain unchanged. The fun... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def 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 | 5,599 | Write a Python function to create an empty set and add a list of items into it in alphabetical order. Ensure that the set does not contain any duplicate elements. The function should return the final set.
The code should start with:
```python
from typing import List
def create_alphabetical_set(items: List[str]) -> se... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_alphabetical_set(items: List[str]) -> set:
"""
Creates an empty set and adds the given list of items into it in alphabetical order.
Ensures that the set does not contain any duplicate elem... |
python_sharegpt_23k | 5,600 | Write a Python function to generate a 3x3 matrix of random real numbers in the range of -10.0 to 10.0, sort the matrix in ascending order, and calculate the sum of the numbers in the first row. The function should return the sorted matrix and the sum of the first row.
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 Tuple
import numpy as np
def generate_and_process_matrix() -> Tuple[np.ndarray, float]:
"""
Generate a 3x3 matrix of random real numbers in the range of -10.0 to 10.0,
sort the matrix in ascending order, ... |
python_sharegpt_23k | 5,603 | Write a Python function to generate an array of prime numbers up to and including a given number `n`. The function should efficiently determine if a number is prime and then collect all prime numbers in the range from 2 to `n`.
The code should start with:
```python
from typing import List
def generate_primes_up_to_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 given number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise... |
python_sharegpt_23k | 5,604 | Write a Python function to count the occurrences of each letter in a given sentence, excluding any numbers, special characters, and spaces. The function should be case-insensitive and should return a dictionary where the keys are letters and the values are their respective counts. The sentence will not exceed 1000 char... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_letters(sentence: str) -> Dict[str, int]:
"""
Counts the occurrences of each letter in a given sentence, excluding numbers, special characters, and spaces.
Args:
sentence (str): Th... |
python_sharegpt_23k | 5,605 | 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()`. The function should take two parameters: the original list and the new element to be inserted. The function should return the modified list... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insert_at_beginning(original_list: List[int], new_element: int) -> List[int]:
"""
Inserts a new element at the beginning of the given list without using built-in list manipulation methods.
Args:... |
python_sharegpt_23k | 5,606 | Write a Python function to check if two strings are anagrams of each other without using any built-in string manipulation methods or libraries. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. The function should handl... | 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 without using built-in string manipulation methods or libraries.
Args:
str1 (str): The first string to compare.
... |
python_sharegpt_23k | 5,614 | Write a Python function to sort a list of integers in descending order using the Bubble Sort algorithm. The function should have a time complexity of O(n^2) and should not use any built-in sorting functions or libraries. The code should start with:
```python
from typing import List
def sort_descending(arr: List[int]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_descending(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
python_sharegpt_23k | 5,616 | Write a Python function to print all the numbers from 0 to a given limit `n`, but skip printing the number `skip_num` and only print even numbers. The function should start with:
```python
def print_even_numbers_with_skip(n: int, skip_num: int) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_even_numbers_with_skip(n: int, skip_num: int) -> None:
"""
Prints all even numbers from 0 to n, skipping the specified number skip_num.
Args:
n (int): The upper limit of the range (inclusive).
skip... |
python_sharegpt_23k | 5,617 | Write a Python function to calculate the average of a list of integers, handling the case when the list is empty by returning 0. The function should also handle the case when the list contains only one element by returning that element as the average. The code should start with:
```python
from typing import List
def c... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def calculate_average(numbers: List[int]) -> float:
"""
Calculate the average of a list of integers. If the list is empty, return 0.
If the list contains only one element, return that element as th... |
python_sharegpt_23k | 5,620 | Write a Python function to merge two sorted lists into a single sorted list, ensuring that all elements from the first list come before any elements from the second list. The function should start with:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merges two sorted lists into a single sorted list, ensuring all elements from the first list come before any elements from the... |
python_sharegpt_23k | 5,622 | Write a Python function to find the subset of size K from a given array where the sum of all elements in the subset is the maximum possible value. The function should return the subset with the maximum sum.
The code should start with:
```python
from typing import List
def find_max_subset(arr: List[int], K: int) -> Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_subset(arr: List[int], K: int) -> List[int]:
"""
Find the subset of size K from the given array where the sum of all elements in the subset is the maximum possible value.
Args:
... |
python_sharegpt_23k | 5,623 | Write a Python function to transform a given input string into a dictionary. The input string will always be in the format "Name, Age, Hobby, Name, Age, Hobby, Name, Age, Hobby, ...". The names can contain any alphanumeric characters, spaces, and special characters. The ages will always be positive integers. The hobbie... | Here is the Python code to solve the given problem in the markdown block:
```python
def transform_string_to_dict(input_string: str) -> dict:
"""
Transforms a given input string into a dictionary.
The input string should be in the format "Name, Age, Hobby, Name, Age, Hobby, ...".
The output dictionary w... |
python_sharegpt_23k | 5,624 | Write a Python function to count the frequency of each word in a given string, considering both uppercase and lowercase letters as the same. The function should ignore punctuation and special characters. The code should start with:
```python
from typing import Dict
def count_word_frequency(string: str) -> Dict[str, in... | 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.
Punctuation a... |
python_sharegpt_23k | 5,626 | 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 handle strings with multiple spaces between words and leading/trailing spaces. The code should start with:
```python
def reverse_words_order(string: str) -> st... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_order(string: str) -> str:
"""
Reverses the order of words in a given string while maintaining the original order of characters within each word.
Args:
string (str): The input string containing wor... |
python_sharegpt_23k | 5,627 | 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 return a tuple containing the minimum and maximum values.
The code should start ... | 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 | 5,630 | Write a Python function to convert a given string to uppercase without using any built-in string manipulation functions or methods. The function should iterate through each character of the string, check if it is a lowercase letter, and convert it to its uppercase equivalent using ASCII values. If the character is not ... | 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 | 5,632 | Write a Python function to count the frequency of each character in a given string. The function should return a dictionary where the keys are characters from the string and the values are the counts of those characters.
The code should start with:
```python
from typing import Dict
def count_character_frequencies(str... | 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 count character freque... |
python_sharegpt_23k | 5,635 | Write a Python function to concatenate all elements of a given list of strings into a single string. The function should handle lists of varying lengths, including empty lists. The code should start with:
```python
from typing import List
def concatenate_strings(strings: List[str]) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def concatenate_strings(strings: List[str]) -> str:
"""
Concatenates all elements of a given list of strings into a single string.
Args:
strings (List[str]): A list of strings to be concaten... |
python_sharegpt_23k | 5,636 | Write a Python function to calculate and print the time difference from each person's birthday to the current time. The input is a list of tuples where each tuple contains a person's name and their corresponding birth date. The output should be formatted as follows:
```
John's birthday is in X days, X hours, X minutes,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import datetime
def print_birthday_time_difference(people: List[Tuple[str, datetime.datetime]]) -> None:
"""
Calculate and print the time difference from each person's birthday to the current time... |
python_sharegpt_23k | 5,637 | Write a Python function to determine if two given strings are anagrams of each other, considering only alphabetic characters and ignoring case, whitespace, and any special characters or numbers.
The code should start with:
```python
def are_anagrams(str1: str, str2: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Determines if two given strings are anagrams of each other, considering only alphabetic characters and ignoring case, whitespace, and special characters.
Args:
str... |
python_sharegpt_23k | 5,638 | Write a Python function to find the maximum number in a given array of integers and return the index of its first occurrence. If the array is empty, return -1. If the maximum number does not appear in the array, return -1.
The code should start with:
```python
from typing import List
def find_max_index(arr: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_index(arr: List[int]) -> int:
"""
Find the maximum number in the given array and return the index of its first occurrence.
If the array is empty, return -1.
Args:
arr (List[... |
python_sharegpt_23k | 5,639 | 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(string_list: 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 palin... |
python_sharegpt_23k | 5,641 | Write a Python function to generate an array of `n` unique random numbers between `min_value` and `max_value` (both inclusive). The function should ensure that no two numbers in the array are the same. The code should start with:
```python
from typing import List
import random
def generate_unique_random_numbers(n: int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_random_numbers(n: int, min_value: int, max_value: int) -> List[int]:
"""
Generate an array of n unique random numbers between min_value and max_value (both inclusive).
... |
python_sharegpt_23k | 5,644 | Write a Python function to flatten a nested list that may contain other nested lists, and return a single list containing only the integer values from the original nested list. The function should handle any level of nesting and should not include any non-integer values in the output list.
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 flatten_nested_list(nested_list: List) -> List[int]:
"""
Flattens a nested list and returns a single list containing only integer values.
Args:
nested_list (List): The nested list to be ... |
python_sharegpt_23k | 5,646 | Write a Python function to sort a list of integers using the bubble sort algorithm. The function should ensure that it only iterates through the list once during the sorting process, even if the list contains duplicate numbers. The code should start with:
```python
from typing import List
def bubble_sort_single_pass(n... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort_single_pass(nums: List[int]) -> List[int]:
"""
Sorts a list of integers using the bubble sort algorithm with a single pass.
Args:
nums (List[int]): The list of integers to be... |
python_sharegpt_23k | 5,649 | 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 return the longest palindromic substring found in the input string.
The code should start with:
```python
def longest_palindromic_substring(s:... | 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 string in which to find the longest palindromic substring.
... |
python_sharegpt_23k | 5,650 | Write a Python function to calculate the sum of the squares of the digits of a given positive integer. The function should handle any positive integer input and return the sum of the squares of its digits.
The code should start with:
```python
def sum_of_squares_of_digits(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_squares_of_digits(n: int) -> int:
"""
Calculate the sum of the squares of the digits of a given positive integer.
Args:
n (int): The positive integer whose digits' squares are to be summed.
Returns:
... |
python_sharegpt_23k | 5,651 | Write a Python function to create an array of size `n` filled with the string "hello". Additionally, convert all vowels in the string to uppercase and all consonants to lowercase. The function should start with:
```python
from typing import List
def create_and_convert_array(n: int) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_and_convert_array(n: int) -> List[str]:
"""
Create an array of size n filled with the string "hello". Convert all vowels in the string to uppercase and all consonants to lowercase.
Args:
... |
python_sharegpt_23k | 5,655 | Write a Python function to calculate the Levenshtein distance between two strings. The Levenshtein distance is defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into another. The function should return the minimum number of operations requ... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_levenshtein_distance(str1: str, str2: str) -> int:
"""
Calculate the Levenshtein distance between two strings.
The Levenshtein distance is the minimum number of single-character edits
(insertions, deletion... |
python_sharegpt_23k | 5,658 | Write a Python function to convert a given time string in the format "HH:MM" (24-hour format) into the total number of minutes since midnight. The input string will always include both hours and minutes, separated by a colon. The function should handle any valid 24-hour time format.
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
def time_to_minutes(current_time: str) -> int:
"""
Converts a given time string in "HH:MM" format (24-hour format) to the total number of minutes since midnight.
Args:
current_time (str): The time string in "HH:MM" ... |
python_sharegpt_23k | 5,660 | Write a Python function to generate and return a multiplication table for numbers from 1 to 10 as a 2D list. Each element in the 2D list should represent the product of the corresponding row and column indices. The function should start with:
```python
from typing import List
def generate_multiplication_table() -> Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_multiplication_table() -> List[List[int]]:
"""
Generates a multiplication table for numbers from 1 to 10 as a 2D list.
Returns:
List[List[int]]: A 2D list where each element at ... |
python_sharegpt_23k | 5,662 | Write a Python function to reverse a list and return the reversed list. The function should handle lists of any length and type of elements. The code should start with:
```python
from typing import List, TypeVar
T = TypeVar('T')
def reverse_list(lst: List[T]) -> List[T]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, TypeVar
T = TypeVar('T')
def reverse_list(lst: List[T]) -> List[T]:
"""
Reverses the given list and returns the reversed list.
Args:
lst (List[T]): The list to be reversed.
Returns:
... |
python_sharegpt_23k | 5,664 | Write a Python function to validate a phone number using a regular expression and check if it is not in a list of restricted phone numbers. The phone number must be in the format of `XXX-XXX-XXXX`, where each `X` represents a digit from 0-9. The function should return `True` if the phone number is valid and not restric... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def validate_phone_number(phone_number: str, restricted_numbers: List[str] = []) -> bool:
"""
Validate a phone number using a regular expression and check if it is not in a list of restricted p... |
python_sharegpt_23k | 5,665 | Write a Python function to reverse a given string without using any built-in string reversal functions or methods. The function should iterate through the string and construct the reversed string manually.
The code should start with:
```python
def reverse_string(input_string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(input_string: str) -> str:
"""
Reverses the given input string without using any built-in string reversal functions or methods.
Args:
input_string (str): The string to be reversed.
Returns:
... |
python_sharegpt_23k | 5,670 | Write a Python function to calculate the value of Pi rounded to the nth decimal point, where n is an input parameter. The function should handle cases where n is negative or exceeds the precision limits of the Python `round` function.
The code should start with:
```python
import math
def calculate_pi_to_nth_decimal(n... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def calculate_pi_to_nth_decimal(n: int) -> float:
"""
Calculate the value of Pi rounded to the nth decimal point.
Args:
n (int): The number of decimal places to round Pi to.
Returns:
float:... |
python_sharegpt_23k | 5,671 | Write a Python function to find the maximum non-negative number in a list of length 5. The function should ignore any negative numbers in the list and return the maximum non-negative number. If all numbers in the list are negative, the function should return `None`.
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, Optional
def find_max_non_negative(nums: List[int]) -> Optional[int]:
"""
Find the maximum non-negative number in a list of length 5.
Args:
nums (List[int]): A list of integers with exactly... |
python_sharegpt_23k | 5,674 | 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 function should take a list of integers as input and return a new list with unique elements in the same order as they first appeared in the input list.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_keep_order(input_list: List[int]) -> List[int]:
"""
Creates a new list from an existing list with no duplication, while maintaining the original order of elements.
Args:
... |
python_sharegpt_23k | 5,677 | Write a Python function to return the third character of a given string using a single loop iteration and without using any built-in functions or libraries. The function should handle strings of any length, including those with fewer than three characters. If the string has fewer than three characters, the function sho... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_third_character(word: str) -> str:
"""
Returns the third character of the given string using a single loop iteration.
If the string has fewer than three characters, returns an empty string.
Args:
word (s... |
python_sharegpt_23k | 5,678 | Write a Python function to merge two sorted lists into a single sorted list, considering that the lists can contain duplicate elements. The function should efficiently merge the lists in O(n + m) time complexity, where n and m are the lengths of the two lists.
The code should start with:
```python
from typing import L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merge two sorted lists into a single sorted list, considering duplicates.
Args:
list1 (List[int]): The first sort... |
python_sharegpt_23k | 5,679 | Write a Python function to create a 2-D array of size (n, n) and fill it with a pattern of alternating 1's and 0's, starting with 1 in the top-left corner. The function should return the filled 2-D array.
The code should start with:
```python
from typing import List
def create_alternating_pattern(n: int) -> List[List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_alternating_pattern(n: int) -> List[List[int]]:
"""
Create a 2-D array of size (n, n) filled with an alternating pattern of 1's and 0's, starting with 1 in the top-left corner.
Args:
... |
python_sharegpt_23k | 5,681 | Write a Python function to sort a list of strings by their length in descending order. If two strings have the same length, they should be sorted lexicographically (alphabetically). The function should have a time complexity of O(n log n) and should not use any built-in sorting functions. The function should start with... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_strings_by_length(strings: List[str]) -> List[str]:
"""
Sorts a list of strings by their length in descending order. If two strings have the same length,
they are sorted lexicographically. T... |
python_sharegpt_23k | 5,683 | Write a Python function to implement a binary search algorithm that searches for a target element `x` in a sorted list `arr`. The function should return the index of the target element if it is found, otherwise it should return -1. The code should start with:
```python
from typing import List
def binary_search(arr: Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search(arr: List[int], x: int) -> int:
"""
Perform a binary search to find the index of the target element `x` in a sorted list `arr`.
Args:
arr (List[int]): A sorted list of inte... |
python_sharegpt_23k | 5,689 | Write a Python function to find and return the elements that are present in all given arrays, along with the count of arrays in which each common element appears. The function should accept a list of lists (arrays) as input and return a dictionary where the keys are the common elements and the values are the counts of ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def find_common_elements(arrays: List[List[int]]) -> Dict[int, int]:
"""
Find and return the elements that are present in all given arrays, along with the count of arrays in which each common eleme... |
python_sharegpt_23k | 5,691 | Write a Python function to calculate the sum of the squares of all even numbers in a given list of integers. The function should return 0 if the list is empty or if there are no even numbers in the list.
The code should start with:
```python
from typing import List
def sum_of_squares_of_evens(numbers: List[int]) -> i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_squares_of_evens(numbers: List[int]) -> int:
"""
Calculate the sum of the squares of all even numbers in the given list.
Args:
numbers (List[int]): A list of integers.
Return... |
python_sharegpt_23k | 5,692 | Write a Python function to calculate the factorial of a given non-negative integer `n` using recursion. The function should ensure that the time complexity is less than or equal to O(n) and the space complexity is less than or equal to O(n). The code should start with:
```python
def factorial(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial(n: int) -> int:
"""
Calculate the factorial of a given non-negative integer n using recursion.
Args:
n (int): The non-negative integer for which to calculate the factorial.
Returns:
int: T... |
python_sharegpt_23k | 5,693 | Write a Python function to sort a given list of numbers in ascending order and remove any duplicate numbers from the list before sorting it. The function should not use any built-in sorting functions or libraries to sort the list. The code should start with:
```python
from typing import List
def custom_sort_and_remove... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_sort_and_remove_duplicates(nums: List[int]) -> List[int]:
"""
Sorts a given list of numbers in ascending order and removes any duplicate numbers.
The function does not use any built-in sor... |
python_sharegpt_23k | 5,694 | Write a Python function to find all elements in a given array that appear more than once, excluding the element 3 from the result. The function should return a list of these elements.
The code should start with:
```python
from typing import List
def find_duplicates_excluding_three(nums: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_duplicates_excluding_three(nums: List[int]) -> List[int]:
"""
Find all elements in the given array that appear more than once, excluding the element 3.
Args:
nums (List[int]): The i... |
python_sharegpt_23k | 5,695 | Write a Python function to calculate the average of a given array of numbers, ensuring the following requirements:
1. The function should handle arrays containing both integers and floating-point numbers.
2. The function should return the average as a floating-point number with precision up to two decimal places.
3. Th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_average(arr: List[float]) -> float:
"""
Calculate the average of a given array of numbers, returning the result with precision up to two decimal places.
Args:
arr (List[float])... |
python_sharegpt_23k | 5,697 | Write a Python function to merge two given arrays and create a new array that contains unique elements from both arrays, sorted in ascending order. The function should handle arrays of integers.
The code should start with:
```python
from typing import List
def merge_unique_sorted_arrays(array1: List[int], array2: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_unique_sorted_arrays(array1: List[int], array2: List[int]) -> List[int]:
"""
Merges two arrays and returns a new array containing unique elements from both arrays, sorted in ascending order.
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.