source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
11,918
Write a Python function to find all prime numbers between two given numbers, inclusive. The function should return a list of these prime numbers, the total count of prime numbers, and the sum of these prime numbers. The function should start with: ```python from typing import List, Tuple def find_prime_numbers(start: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def is_prime(n: int) -> bool: """ Check if a number is prime. Args: n (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ ...
python_sharegpt_23k
11,921
Write a Python function to compare two given lists and return `True` if their contents are the same and the order of elements is also the same. The function should have a time complexity of O(n), where n is the length of the lists, and should use only constant space, i.e., it should not use any additional data structur...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def compare_lists(list1: List[int], list2: List[int]) -> bool: """ Compares two lists to check if they have the same contents and order. Args: list1 (List[int]): The first list to compare. ...
python_sharegpt_23k
11,922
Write a Python function to remove duplicate elements from a given list while maintaining the original order of the elements. The function should not use any built-in functions for this purpose and should be implemented with a time complexity of O(n), where n is the length of the list. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates(lst: List[int]) -> List[int]: """ Removes duplicate elements from a list while maintaining the original order of the elements. Args: lst (List[int]): The input list fro...
python_sharegpt_23k
11,927
Write a Python function to count from 1 to a given number `n`, but skip the numbers that are divisible by 3. Additionally, ensure that the loop does not execute if the number is a prime number. The function should return a list of numbers that satisfy these conditions. 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 count_and_skip(n: int) -> List[int]: """ Count from 1 to n, skipping numbers divisible by 3 and not executing for prime numbers. Args: n (int): The upper limit of the counting range. ...
python_sharegpt_23k
11,929
Write a Python function to generate the FizzBuzz series up to the 1000th term, where each term must be a prime number. The function should return a list of strings where each string is either "Fizz", "Buzz", "FizzBuzz", or the term number itself if it is not divisible by 3 or 5. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(num: int) -> bool: """ Check if a number is prime. Args: num (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ if...
python_sharegpt_23k
11,931
Write a Python function to implement the Merge Sort algorithm, which has a time complexity of O(n log n). The function should take a list of integers as input and return the sorted list. The code should start with: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers using the Merge Sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: Li...
python_sharegpt_23k
11,933
Write a Python function to generate an array containing 100 unique random numbers between 0 and 1000, such that no two numbers are the same and all numbers are sorted in ascending order. The function should return the sorted array. The code should start with: ```python from typing import List import random def genera...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def generate_unique_sorted_random_numbers() -> List[int]: """ Generates an array containing 100 unique random numbers between 0 and 1000, sorted in ascending order. Returns: Li...
python_sharegpt_23k
11,941
Write a Python function to generate a 100x100 random matrix of integers up to a given maximum value, where each element in the matrix represents the sum of its row and column indices. The function should use a random seed of 0 for reproducibility. The code should start with: ```python import numpy as np def generate_...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def generate_random_matrix(max_value: int) -> np.ndarray: """ Generate a 100x100 random matrix of integers up to a given maximum value, where each element in the matrix represents the sum of its row and c...
python_sharegpt_23k
11,944
Write a Python function to perform a linear search in an array, starting from the end of the array and iterating backwards. The function should return the index of the last occurrence of the target number in the array. If the target number is not found, the function should return -1. If there are multiple occurrences o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def linear_search_last_occurrence(arr: List[int], target: int) -> int: """ Perform a linear search in an array starting from the end and return the index of the last occurrence of the target number. ...
python_sharegpt_23k
11,945
Write a Python function to find the most frequent name in a given array of names, considering the names as case-insensitive. The array may contain up to 10^6 names. The function should have a time complexity of O(n), where n is the number of names 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 find_most_frequent_name(names: List[str]) -> str: """ Find the most frequent name in a given array of names, considering the names as case-insensitive. Args: names (List[str]): A list of...
python_sharegpt_23k
11,946
Write a Python function to implement the bubble sort algorithm with error handling for the following scenarios: - Input is not a list or array - Input list contains non-integer elements The code should start with: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers using the bubble sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: Li...
python_sharegpt_23k
11,950
Write a Python function to generate an HTML table from a given dictionary containing student information. The table must adhere to the following constraints: - The table must have a border of 1 pixel. - The table headers (Field and Value) must have a background color of light gray. - Each row in the table must have a b...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def generate_html_table(student_info: Dict[str, str]) -> str: """ Generates an HTML table from a dictionary containing student information. Args: student_info (Dict[str, str]): A dictionary ...
python_sharegpt_23k
11,953
Write a Python function to create a new list where each element is the square of the corresponding element in the original list. The function should handle empty lists and non-integer elements in the input list by ignoring the non-integer elements. The code should start with: ```python from typing import List def squa...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def square_elements(lst: List[int]) -> List[int]: """ Returns a new list where each element is the square of the corresponding integer element in the original list. Non-integer elements in the input ...
python_sharegpt_23k
11,954
Write a Python function to calculate the Hamming distance between two binary strings, where the length of the strings can be up to \(10^6\). The input strings may contain additional characters other than '0' and '1'. The function should handle these additional characters by ignoring them and only considering valid bina...
Here is the Python code to solve the given problem in the markdown block: ```python def hamming_distance_with_filter(str1: str, str2: str) -> int: """ Calculate the Hamming distance between two binary strings, ignoring any non-binary characters. Args: str1 (str): The first binary string. st...
python_sharegpt_23k
11,956
Write a Python function to determine if there are two distinct indices `i` and `j` in a given array such that `nums[i] == nums[j]` and the absolute difference between `i` and `j` is at most `k`. The array will have at most `10^6` elements and all elements will be between `-10^6` and `10^6`. The function should have a t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def contains_nearby_duplicate(nums: List[int], k: int) -> bool: """ Determines if there are two distinct indices i and j in the array such that nums[i] == nums[j] and the absolute difference between ...
python_sharegpt_23k
11,957
Write a Python function to analyze a list of strings and find the most frequent character that is not a vowel. If there are multiple characters that are equally the most frequent, return the one that appears first in the list. The function should ignore case sensitivity when checking for vowels and ignore spaces when c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_most_frequent_character(strings: List[str]) -> Optional[str]: """ Analyzes a list of strings and finds the most frequent character that is not a vowel. If there are multiple charac...
python_sharegpt_23k
11,959
Write a Python function to generate the first `n` prime numbers and return them as a list. The function should use a while loop and a helper function to check if a number is prime. The code should start with: ```python from typing import List def generate_primes(n: 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, Fa...
python_sharegpt_23k
11,960
Write a Python function to find and return a list of all prime numbers between a given range `[start, end]`. The function should efficiently determine if a number is prime by checking divisibility up to the square root of the number. The code should start with: ```python from typing import List def find_primes_in_ran...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_primes_in_range(start: int, end: int) -> List[int]: """ Find and return a list of all prime numbers between the given range [start, end]. Args: start (int): The starting number of t...
python_sharegpt_23k
11,962
Write a Python function to generate a list of consecutive prime numbers starting from 2 up to a given number `n`. The function should ensure that `n` is greater than 2. The function should have a time complexity of O(n^2) or less and use a memory-efficient approach to store the prime numbers. 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 generate_prime_numbers(n: int) -> List[int]: """ Generate a list of consecutive prime numbers starting from 2 up to a given number n. Args: n (int): The upper limit for generating prime ...
python_sharegpt_23k
11,963
Write a Python function to reverse the order of the values in a given list without using any built-in Python functions or methods. The solution should have a time complexity of O(n) and a space complexity of O(1). The input list will always contain at least one element. You are not allowed to use any additional data st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(input_list: List[int]) -> List[int]: """ Reverses the order of the values in the input list without using any built-in Python functions or methods. Args: input_list (List[in...
python_sharegpt_23k
11,966
Write a Python function to process a list of names with the following functionalities: 1. **Sort the names**: Return a sorted list of names. 2. **Count names starting with a specific letter**: Implement a function to count the number of names in the list that start with a given letter. 3. **Check if a name is present**...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_names(names: List[str]) -> List[str]: """ Sorts the list of names in alphabetical order. Args: names: A list of names to be sorted. Returns: A sorted list of names. ...
python_sharegpt_23k
11,968
Write a Python function to generate a sorted array of size `n` (where `n` is a positive integer) filled with unique random integers ranging from 0 to 100. The function should ensure that the array is sorted in ascending order and has a time complexity of O(n log n). The code should start with: ```python from typing imp...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def generate_unique_sorted_array(n: int) -> List[int]: """ Generates a sorted array of size `n` filled with unique random integers from 0 to 100. Args: n (int): The size of the...
python_sharegpt_23k
11,972
Write a Python function that reverses the order of words in a given sentence while keeping the order of characters within each word intact. The function should handle punctuation marks and multiple whitespaces between words appropriately. The function should have a time complexity of O(n), where n is the length of the ...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_in_sentence(sentence: str) -> str: """ Reverses the order of words in a given sentence while keeping the order of characters within each word intact. The function handles punctuation marks and multiple whit...
python_sharegpt_23k
11,974
Write a Python function `has_even_sum` that determines if the sum of all numbers in a given list of positive integers is even. The function should not use any built-in functions or methods for checking if a number is even. Instead, it should use a recursive approach to determine the parity (evenness or oddness) of a nu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_even_sum(numbers: List[int]) -> bool: """ Determines if the sum of all numbers in the given list is even. Args: numbers (List[int]): A list of positive integers. Returns: ...
python_sharegpt_23k
11,975
Write a Python function to filter and print the odd numbers from a given array that are divisible by 3, in reverse order. The function should take a list of integers as input and print the filtered numbers. The code should start with: ```python from typing import List def print_odd_divisible_by_3_reverse(arr: List[in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_odd_divisible_by_3_reverse(arr: List[int]) -> None: """ Prints the odd numbers from the given array that are divisible by 3, in reverse order. Args: arr (List[int]): The list of in...
python_sharegpt_23k
11,977
Write a Python function to calculate the sum of all prime numbers within a given range [1, n]. The function should efficiently determine if a number is prime and then sum all prime numbers up to n. The code should start with: ```python def sum_of_primes_up_to(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_primes_up_to(n: int) -> int: """ Calculate the sum of all prime numbers up to a given number n. Args: n (int): The upper limit of the range [1, n] to find the sum of prime numbers. Returns: i...
python_sharegpt_23k
11,983
Write a Python function to sort all strings in a given list alphabetically and remove any duplicate strings. The function should return a new list containing the sorted unique strings. The code should start with: ```python from typing import List def sort_and_remove_duplicates(strings: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_and_remove_duplicates(strings: List[str]) -> List[str]: """ Sorts all strings in a given list alphabetically and removes any duplicate strings. Args: strings (List[str]): A list of ...
python_sharegpt_23k
11,985
Write a Python function to flatten a 2-dimensional array of integers, remove duplicate elements, and return the sorted list of unique elements. The function should handle an empty 2-d array by returning an empty list. The solution should have a time complexity of O(n log n), where n is the total number of elements in t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def flatten_and_sort_unique(arr: List[List[int]]) -> List[int]: """ Flattens a 2-dimensional array of integers, removes duplicate elements, and returns a sorted list of unique elements. Args: ...
python_sharegpt_23k
11,986
Write a Python function to find the smallest number in a given list of numbers without using any built-in functions or methods such as `min()`, `sorted()`, or `sort()`. The list may contain negative numbers and decimals. The function should iterate through the list and determine the smallest number using a custom algor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_smallest_number(numbers: List[int]) -> int: """ Find the smallest number in a given list of numbers without using built-in functions or methods. Args: numbers (List[int]): A list of...
python_sharegpt_23k
11,993
Write a Python function to find the maximum element and its index in a given list after removing any duplicate elements. The function should handle the case of an empty list by returning an error message. The code should start with: ```python from typing import List, Tuple def find_max_unique(arr: List[int]) -> Tuple[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_unique(arr: List[int]) -> Tuple[str, int, int]: """ Find the maximum element and its index in a given list after removing any duplicate elements. Args: arr (List[int]): T...
python_sharegpt_23k
11,996
Write a Python function to sort an array in descending order using a custom merge sort algorithm. The function should not use any built-in sorting functions or methods. Only basic operations such as loops, conditional statements, and variable assignments are allowed. The input array will contain integers ranging from -...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def custom_merge_sort(arr: List[int]) -> List[int]: """ Sorts the input array in descending order using a custom merge sort algorithm. Args: arr (List[int]): The input array of integers to b...
python_sharegpt_23k
11,997
Write a Python function to print all the elements of a given 2D array in row-wise order, without using any loops or recursion, and ensuring that each element is printed exactly once. Additionally, the solution should have a time complexity of O(1) and use constant space. The function 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 import itertools def print_2d_array_elements(arr: List[List[int]]) -> None: """ Prints all elements of a given 2D array in row-wise order without using loops or recursion, ensuring each element is pr...
python_sharegpt_23k
12,000
Write a Python function to sort an array of integers in O(n log n) time using a recursive merge sort algorithm, ensuring that the sorted array contains no duplicate elements. The function should start with: ```python from typing import List def merge_sort_unique(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort_unique(arr: List[int]) -> List[int]: """ Sorts an array of integers in O(n log n) time using a recursive merge sort algorithm, ensuring that the sorted array contains no duplicate elem...
python_sharegpt_23k
12,001
Write a Python function to count the occurrences of each character in a given input string, including special characters, whitespace, and punctuation marks. The function should return a dictionary where the keys are the characters from the input string and the values are the counts of their occurrences. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_character_occurrences(input_string: str) -> Dict[str, int]: """ Counts the occurrences of each character in the input string, including special characters, whitespace, and punctuation marks. ...
python_sharegpt_23k
12,005
Write a Python function to calculate the sum of all elements in each row of a given matrix and return the sum of all the row sums. The matrix is represented as a list of lists, where each inner list represents a row in the matrix. The matrix may have varying number of elements in each row. 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 sum_of_all_elements(matrix: List[List[int]]) -> int: """ Calculate the sum of all elements in each row of a given matrix and return the sum of all the row sums. Args: matrix (List[List[i...
python_sharegpt_23k
12,010
Write a Python function to generate the Fibonacci sequence up to the nth number using an iterative approach without using recursion. The function should return a list containing the Fibonacci sequence up to the nth number. The code should start with: ```python from typing import List def generate_fibonacci(n: 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 number using an iterative approach. Args: n (int): The number up to which the Fibonacci sequenc...
python_sharegpt_23k
12,012
Write a Python function to remove all instances of a specified element from a given list while preserving the order of the remaining elements. The function should also return the number of elements removed. Additionally, the function should handle edge cases such as an empty input list and the case where the element to...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Union def remove_element(lst: List[int], element: int) -> Union[Tuple[List[int], int], str, int]: """ Removes all instances of a specified element from a given list and returns the modified list ...
python_sharegpt_23k
12,017
Write a Python function to demonstrate the difference between mutable and immutable data types in Python. The function should take a data type as input and return a string indicating whether the data type is mutable or immutable. Additionally, the function should include an example of a common mistake made when dealing...
Here is the Python code to solve the given problem in the markdown block: ```python def check_mutability(data_type: str) -> str: """ Determines whether the given data type is mutable or immutable and provides an example of a common mistake with mutable data types and its correction. Args: data_...
python_sharegpt_23k
12,022
Write a Python function to calculate the sum of positive elements for each row in a given 2D array. The function should ignore any negative numbers and only consider positive numbers for the row sums. The input array will always have at least 1 row and each row will have at least 1 element. All elements in the array wi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def row_sums_of_positives(arr: List[List[int]]) -> List[int]: """ Calculate the sum of positive elements for each row in a given 2D array. Args: arr (List[List[int]]): A 2D array where each ...
python_sharegpt_23k
12,030
Write a Python function to join two dictionaries by adding the values of the same keys. The result should be a new dictionary where the keys are the union of the keys from both dictionaries, and the values are the sum of the corresponding values from both dictionaries. If a key is present in only one dictionary, it sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def join_dictionaries(d1: Dict[str, int], d2: Dict[str, int]) -> Dict[str, int]: """ Joins two dictionaries by adding the values of the same keys. Args: d1 (Dict[str, int]): The first dictio...
python_sharegpt_23k
12,032
Write a Python function to generate and return a multiplication table for even numbers from 1 to 10 as a list of strings. Each string in the list should represent a row in the multiplication table for a specific even number. The function should start with: ```python from typing import List def generate_even_multiplica...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_even_multiplication_table() -> List[str]: """ Generates a multiplication table for even numbers from 1 to 10. Returns: List[str]: A list of strings where each string represents ...
python_sharegpt_23k
12,035
Write a Python function to determine if a given string is a palindrome. The function should ignore case, special characters, whitespace, and punctuation. A palindrome is a sequence of characters that reads the same forward and backward. The code should start with: ```python import re def is_palindrome(input_string: s...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_palindrome(input_string: str) -> bool: """ Determine if the given string is a palindrome, ignoring case, special characters, whitespace, and punctuation. Args: input_string (str): The string to che...
python_sharegpt_23k
12,037
Write a Python function to find the common elements between two lists of integers efficiently. The function should have a time complexity of O(n), where n is the length of the longer list. The order of elements in the resulting list does not matter, and the lists can contain duplicates. 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 find_common_elements(list1: List[int], list2: List[int]) -> List[int]: """ Find the common elements between two lists of integers efficiently. Args: list1 (List[int]): The first list of ...
python_sharegpt_23k
12,038
Write a Python function to check whether a given array contains at least two odd numbers that are also prime numbers. The function should return `True` if there are at least two such numbers, otherwise `False`. The code should start with: ```python from typing import List def contains_two_odd_primes(arr: List[int]) -...
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: """ Helper function to check if a number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime...
python_sharegpt_23k
12,040
Write a Python function to determine if a given number is a prime number or not. The function should have a time complexity of O(sqrt(n)) and a space complexity of O(1). 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: ```...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_prime(n: int) -> bool: """ Determine if a given number is a prime number. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwis...
python_sharegpt_23k
12,044
Write a Python function to calculate the Euclidean distance between two points in a 2D plane using tuples to represent the coordinates of the points. The function should take two tuples as input, where each tuple contains two integers representing the x and y coordinates of a point. The function should return the Eucli...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple import math def euclidean_distance(point1: Tuple[int, int], point2: Tuple[int, int]) -> float: """ Calculate the Euclidean distance between two points in a 2D plane. Args: point1 (Tuple[int...
python_sharegpt_23k
12,045
Write a Python function to determine the size of a given byte array that contains only positive integers and is sorted in ascending order. The byte array must also have a length of at least 5 and cannot exceed 100 elements. If the byte array does not meet these criteria, the function should return -1. The code should ...
Here is the Python code to solve the given problem in the markdown block: ```python def find_byte_array_size(byte_array: bytearray) -> int: """ Determine the size of a given byte array that contains only positive integers and is sorted in ascending order. The byte array must have a length of at least 5 and ...
python_sharegpt_23k
12,048
Write a Python function to count the occurrences of each alphanumeric character in a given string and return the result as a dictionary. The function should exclude any special characters (non-alphanumeric characters) from the dictionary. The function should have a time complexity of O(n), where n is the length of the ...
Here is the Python code to solve the given problem in the markdown block: ```python def count_alphanumeric_characters(string: str) -> dict: """ Counts the occurrences of each alphanumeric character in the given string and returns the result as a dictionary. Args: string (str): The input string to c...
python_sharegpt_23k
12,050
Write a Python function to compute the most frequent character from a given string using a recursive approach. You are not allowed to use built-in functions or libraries to solve this problem. The function should start with: ```python def most_frequent_char(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def most_frequent_char(s: str) -> str: """ Compute the most frequent character from a given string using a recursive approach. Args: s (str): The input string from which to find the most frequent character. Ret...
python_sharegpt_23k
12,051
Write a Python function to sort a given array in descending order using only recursion. The input array will contain unique integers between -1000 and 1000 inclusive, and the array will have a maximum length of 1000 elements. The function should use a merge sort algorithm implemented recursively. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge(arr1: List[int], arr2: List[int]) -> List[int]: """ Merges two sorted arrays (in descending order) into one sorted array (in descending order). Args: arr1 (List[int]): The first so...
python_sharegpt_23k
12,053
Write a Python function to identify the type of each element in a given list of mixed data types. The types should be categorized as "alphabet", "number", or "special character". The function should return a list of these types in the same order as the original data. The code should start with: ```python from typing i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def identify_element_types(data: List[Union[str, int]]) -> List[str]: """ Identifies the type of each element in a given list of mixed data types. Args: data (List[Union[str, int]]): ...
python_sharegpt_23k
12,054
Write a Python function to generate the first `n` Fibonacci numbers iteratively with a time complexity of O(n) and a space complexity of O(1). The function should not use recursion and should return a list of the first `n` Fibonacci numbers. The code should start with: ```python from typing import List def generate_f...
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 first n Fibonacci numbers iteratively with a time complexity of O(n) and a space complexity of O(1). Args: n (int): The number o...
python_sharegpt_23k
12,055
Write a Python function to print "hi" for each pair of integers (i, j) within a given range, where the sum of i and j is a prime number. The function should use more efficient loops and avoid redundant calculations. The code should start with: ```python def print_hi_for_prime_sums(range_limit: int): ```
Here is the Python code to solve the given problem in the markdown block: ```python def print_hi_for_prime_sums(range_limit: int): """ Prints "hi" for each pair of integers (i, j) within the given range_limit, where the sum of i and j is a prime number. Args: range_limit (int): The upper limit ...
python_sharegpt_23k
12,058
Write a Python function to reverse the order of characters in a given string without using any built-in string manipulation functions or libraries. The function must use a loop or iteration and should not create any new variables or data structures. The code should start with: ```python def reverse_string(string: str) ...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses the order of characters in a given string without using any built-in string manipulation functions or libraries. Args: string (str): The input string to be revers...
python_sharegpt_23k
12,060
Write a Python function to generate a list containing the cubes of numbers between 1 and a given upper limit `n`, excluding any numbers that are divisible by 3. The function should start with: ```python from typing import List def generate_cubes_excluding_divisible_by_3(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_cubes_excluding_divisible_by_3(n: int) -> List[int]: """ Generate a list containing the cubes of numbers between 1 and n, excluding any numbers that are divisible by 3. Args: n ...
python_sharegpt_23k
12,062
Write a Python function to find the second highest prime number in a given array of positive integers. The function should not use the built-in `max()` or `sort()` functions and should have a time complexity of O(n), where n is the length of the array. The code should start with: ```python from typing import List def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def 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
12,063
Write a Python function to sort a given array in descending order using a more efficient approach than the provided erroneous code. The function should use a stable sorting algorithm to ensure that the relative order of equal elements is preserved. The code 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_array_descending(arr: List[int]) -> List[int]: """ Sorts the given array in descending order using a stable sorting algorithm. Args: arr (List[int]): The array to be sorted. Re...
python_sharegpt_23k
12,066
Write a Python function to compute the exponential of a given number `x` using only bitwise operations and loops. You are not allowed to use any built-in functions, modules, or arithmetic operations related to exponentiation or mathematical calculations. The function should start with: ```python def bitwise_exponential...
Here is the Python code to solve the given problem in the markdown block: ```python def bitwise_exponential(x: int) -> int: """ Compute the exponential of a given number x using only bitwise operations and loops. Args: x (int): The exponent to which 2 is raised. Returns: int: The resul...
python_sharegpt_23k
12,070
Write a Python function to construct a dictionary with countries and their total population from a list of countries with potential duplicates. The population values should be provided in descending order. Implement the solution in a recursive function with a time complexity of O(n log n) and a space complexity of O(n)...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def construct_population_dict(countries: List[str], population: Dict[str, int]) -> Dict[str, int]: """ Constructs a dictionary with countries and their total population from a list of countries wit...
python_sharegpt_23k
12,074
Write a Python function to recursively multiply two non-negative integers without using loops or the multiplication operator. The function should handle the base case where either of the integers is zero and should use recursion to compute the product by repeatedly adding one of the integers to the result of the recurs...
Here is the Python code to solve the given problem in the markdown block: ```python def multiply_recursive(x: int, y: int) -> int: """ Recursively multiplies two non-negative integers without using loops or the multiplication operator. Args: x (int): The first non-negative integer. y (int):...
python_sharegpt_23k
12,077
Write a Python function to remove all occurrences of a specified element from a given tuple without using built-in functions or libraries. The function should maintain the order of the remaining elements and should not use any additional data structures. The time complexity should be O(n), where n is the size of the tu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Tuple def remove_element_from_tuple(tup: Tuple, element) -> Tuple: """ Removes all occurrences of a specified element from a given tuple. Args: tup (Tuple): The input tuple from which to remove t...
python_sharegpt_23k
12,078
Write a Python function to remove duplicates from a given array while preserving the order of the elements. The array may contain integers, strings, or a combination of both. The function should handle large input arrays efficiently and maintain a time complexity of O(n) or less. The code should start with: ```python ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def remove_duplicates_preserve_order(arr: List[Union[int, str]]) -> List[Union[int, str]]: """ Removes duplicates from the given array while preserving the order of the elements. Args: ...
python_sharegpt_23k
12,079
Write a Python function to calculate the sum of the values in a dictionary where the keys are strings and the values are numbers. The function should ignore keys that contain any uppercase letters, special characters, or numbers. The code should start with: ```python from typing import Dict def sum_valid_values(dictio...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def sum_valid_values(dictionary: Dict[str, int]) -> int: """ Calculate the sum of the values in a dictionary where the keys are strings and the values are numbers. Ignore keys that contain any upperc...
python_sharegpt_23k
12,080
Write a Python function to generate an array of length n that contains the square of the odd numbers from 1 to n, but excluding any number that is divisible by 3. The function should start with: ```python from typing import List def generate_odd_squares_excluding_divisible_by_3(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_odd_squares_excluding_divisible_by_3(n: int) -> List[int]: """ Generate an array of length n that contains the square of the odd numbers from 1 to n, but excluding any number that is div...
python_sharegpt_23k
12,083
Write a Python function to transform an input array by adding 5 to each element, removing any duplicate elements, and then sorting the array in ascending order. The function should start with: ```python from typing import List def transform_and_sort_array(input_array: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def transform_and_sort_array(input_array: List[int]) -> List[int]: """ Transforms the input array by adding 5 to each element, removes duplicates, and sorts the array in ascending order. Args: ...
python_sharegpt_23k
12,085
Write a Python function to determine whether a given array is sorted in either ascending or descending order. The function should have a time complexity of O(n), where n is the number of elements in the array. The function should return `True` if the array is sorted in either order, and `False` otherwise. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_sorted_array(array: List[int]) -> bool: """ Determine whether the given array is sorted in either ascending or descending order. Args: array (List[int]): The array to check for sorted...
python_sharegpt_23k
12,086
Write a Python function to concatenate two given lists, square each element in the concatenated list, remove any duplicates, and return the result in descending order. The function should start with: ```python from typing import List def concatenate_and_square_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 concatenate_and_square_lists(list1: List[int], list2: List[int]) -> List[int]: """ Concatenates two lists, squares each element, removes duplicates, and returns the result in descending order. A...
python_sharegpt_23k
12,088
Write a Python function to return the kth element in the Fibonacci sequence with a time complexity of O(log n) and without using recursion. The function should handle edge cases such as negative input values and return appropriate messages. The code should start with: ```python def kth_fibonacci(k: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def kth_fibonacci(k: int) -> int: """ Returns the kth element in the Fibonacci sequence using matrix exponentiation with O(log n) time complexity. Args: k (int): The position in the Fibonacci sequence to return. ...
python_sharegpt_23k
12,093
Write a Python function to sort a list of integers in descending order using a custom merge sort algorithm. The function should not use any built-in sorting functions or libraries and should have a time complexity of O(n log n) and a space complexity of O(1). 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 merge_sort_descending(arr: List[int]) -> List[int]: """ Sorts a list of integers in descending order using a custom merge sort algorithm. Args: arr (List[int]): The list of integers to b...
python_sharegpt_23k
12,094
Write a Python function to determine if a given integer is a prime number greater than 100. The function should return `True` if the number is a prime number and greater than 100, otherwise it should return `False`. The code should start with: ```python import math def is_prime_greater_than_100(value: int) -> bool: `...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_prime_greater_than_100(value: int) -> bool: """ Determines if a given integer is a prime number greater than 100. Args: value (int): The integer to check. Returns: bool: True if the ...
python_sharegpt_23k
12,097
Write a Python function to compute the sum of all elements in a given array. The function should correctly handle arrays of any length, including empty arrays. The code should start with: ```python from typing import List def sum_array_elements(arr: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def sum_array_elements(arr: List[int]) -> int: """ Compute the sum of all elements in a given array. Args: arr (List[int]): The array of integers to sum. Returns: int: The sum...
python_sharegpt_23k
12,099
Write a Python function to remove duplicate elements from a given list of integers without using any built-in functions or data structures like sets, dictionaries, or lists for storing unique elements. The function should return a new list containing only the unique elements in the order they first appear. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates(arr: List[int]) -> List[int]: """ Removes duplicate elements from the given list of integers and returns a new list containing only the unique elements in the order they first a...
python_sharegpt_23k
12,100
Write a Python function to count the number of integers in a given list that are greater than a specified value. The function should efficiently iterate through the list and count the qualifying integers. The code should start with: ```python from typing import List def count_integers_greater_than(lst: List[int], val...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_integers_greater_than(lst: List[int], value: int) -> int: """ Counts the number of integers in the given list that are greater than the specified value. Args: lst (List[int]): The ...
python_sharegpt_23k
12,104
Write a Python function that takes in two lists as input and returns a dictionary where the elements of the first list are the keys and the elements of the second list are the values. Ensure that all keys in the resulting dictionary are unique and of string data type. If the lengths of the two lists are not equal, rais...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def create_dictionary(keys: List[str], values: List[str]) -> Dict[str, str]: """ Creates a dictionary from two lists where the elements of the first list are the keys and the elements of the se...
python_sharegpt_23k
12,105
Write a Python function to remove even numbers and duplicates from a given array using recursion. The function should return a new array containing only the odd numbers, with all duplicates removed. If the input array is empty or contains only even numbers, the function should return an empty array. The function should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_even_duplicates(arr: List[int]) -> List[int]: """ Remove even numbers and duplicates from the given array using recursion. Args: arr (List[int]): The input array of integers. ...
python_sharegpt_23k
12,106
Write a Python function to sort a list of integers in descending order using a more efficient algorithm than the provided one. The function should use a stable sorting algorithm and should not modify the original list. The code should start with: ```python from typing import List def sort_descending(lst: List[int]) ->...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_descending(lst: List[int]) -> List[int]: """ Sorts the given list of integers in descending order and returns a new sorted list. Args: lst (List[int]): The list of integers to be so...
python_sharegpt_23k
12,107
Write a Python function to create a dictionary that contains countries and their total populations from a given list of country-population pairs. Additionally, calculate the average population of all the countries in the dictionary and store it in a separate variable. The function should start with: ```python from typi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def create_population_dict(country_population_pairs: List[Tuple[str, int]]) -> Tuple[dict, float]: """ Create a dictionary from a list of country-population pairs and calculate the average populat...
python_sharegpt_23k
12,109
Write a Python function to check whether a given array contains a specified element. The array can contain integers or floating-point numbers. The function should return `True` if the element is found in the array and `False` otherwise. The function must adhere to the following constraints: - The array can have a maxim...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def contains_element(arr: List[Union[int, float]], element: Union[int, float]) -> bool: """ Checks whether the given array contains the specified element. Args: arr (List[Union[int, f...
python_sharegpt_23k
12,110
Write a Python function to calculate the nth Fibonacci number using an iterative approach without using any built-in functions or libraries. The function should handle error cases for non-positive integer inputs and should be able to compute Fibonacci numbers for values up to 10^5 efficiently. The code should start wi...
Here is the Python code to solve the given problem in the markdown block: ```python def fibonacci(n: int) -> int: """ Calculate the nth Fibonacci number using an iterative approach. Args: n (int): The position of the Fibonacci number to calculate. Returns: int: The nth Fibonacci number...
python_sharegpt_23k
12,111
Write a Python function to determine if a given year is a leap year without using any built-in mathematical functions or operators. You can only use logical operators and loops. The function should return `True` if the year is a leap year and `False` otherwise. The code should start with: ```python def is_leap_year(ye...
Here is the Python code to solve the given problem in the markdown block: ```python def is_leap_year(year: int) -> bool: """ Determine if a given year is a leap year without using any built-in mathematical functions or operators. Args: year (int): The year to check. Returns: bool: True...
python_sharegpt_23k
12,113
Write a Python function to remove all whitespaces from a given string while maintaining the original order of the characters. The solution should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the string. Implement the solution without using any built-in string manipulation func...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_whitespaces(string: str) -> str: """ Removes all whitespaces from the given string while maintaining the original order of characters. Args: string (str): The input string from which whitespaces need to b...
python_sharegpt_23k
12,114
Write a Python function to print the elements of a given list in reverse order without using the `reverse()` method, any additional lists or data structures, or built-in functions or methods that directly manipulate strings. The solution should have a time complexity of O(n), where n is the length of the list. The func...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_reverse_list(list_of_words: List[str]) -> None: """ Prints the elements of the given list in reverse order without using the reverse() method, any additional lists or data structures, or bu...
python_sharegpt_23k
12,121
Write a Python function to determine if two given sentences are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The function should ignore case, spaces, and punctuation marks. The function should return `True` if the sentences are anagrams, and `False`...
Here is the Python code to solve the given problem in the markdown block: ```python import string def are_anagrams(sentence1: str, sentence2: str) -> bool: """ Determine if two sentences are anagrams of each other. Args: sentence1 (str): The first sentence to compare. sentence2 (str): The ...
python_sharegpt_23k
12,123
Write a Python function to convert a binary string to a decimal integer without using any built-in conversion functions or libraries. You can only use basic arithmetic operations (such as addition, subtraction, multiplication, and division) and loops. The binary string can have up to 1 billion digits. The time complexi...
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_decimal(binary: str) -> int: """ Converts a binary string to a decimal integer without using built-in conversion functions or libraries. Args: binary (str): The binary string to be converted. Retu...
python_sharegpt_23k
12,125
Write a Python function to compute the factorial of a positive integer using recursion. The function should adhere to the following constraints: - The input integer must be between 1 and 10. - The function should have a time complexity of O(n), where n is the input integer. - The function should handle negative integer...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial_recursive(n: int) -> int: """ Compute the factorial of a positive integer using recursion. Args: n (int): The positive integer for which to compute the factorial. Returns: int: The factori...
python_sharegpt_23k
12,129
Write a Python function to reverse a given array of numbers in-place without using any built-in methods or additional data structures. The time complexity should be less than O(n^2). The function should start with: ```python from typing import List def reverse_array_in_place(arr: List[int]) -> None: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_array_in_place(arr: List[int]) -> None: """ Reverses the given array of numbers in-place without using any built-in methods or additional data structures. Args: arr (List[int]): ...
python_sharegpt_23k
12,131
Write a Python function to extract all the unique words from a given string while preserving their order of occurrence. You are not allowed to use any built-in functions or libraries for this task, except for the basic operations like string manipulation and list/set operations. The code should start with: ```python d...
Here is the Python code to solve the given problem in the markdown block: ```python def extract_unique_words(string: str) -> list: """ Extracts all unique words from a given string while preserving their order of occurrence. Args: string (str): The input string from which to extract unique words. ...
python_sharegpt_23k
12,133
Write a Python function to find the word with the maximum length in a given array of strings without using any built-in functions or methods that directly solve the problem. Additionally, calculate the total length of all words in the array. The code should start with: ```python from typing import List def find_longe...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_longest_word_and_total_length(arr: List[str]) -> (str, int): """ Finds the word with the maximum length and calculates the total length of all words in the given array of strings. Args: ...
python_sharegpt_23k
12,139
Write a Python function to divide a given list of numbers by a specified divisor and return a list of tuples, where each tuple contains the quotient and the remainder for each number in the list. The quotient should be rounded towards negative infinity for negative numbers. The function should handle both positive and ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def divide_list(numbers: List[int], divisor: int) -> List[Tuple[int, int]]: """ Divide each number in the given list by the specified divisor and return a list of tuples, where each tuple cont...
python_sharegpt_23k
12,140
Write a Python function to find the first occurrence of a prime number in a given array of numbers. The array can contain up to 10^6 elements, and the numbers in the array can range from 1 to 10^9. The function should return the first prime number found in the array or `None` if no prime number is present. The code sh...
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
12,146
Write a Python function to create a copy of an existing array, but with the elements sorted in descending order using a recursive sorting algorithm. The function should have a time complexity of O(n log n). The code should start with: ```python from typing import List def recursive_sort_descending(arr: List[int]) -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def recursive_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 sorte...
python_sharegpt_23k
12,149
Write a Python function to reverse the order of elements in a given array without using any built-in functions or creating a new array. The solution should have a time complexity of O(n) and should not use any extra space beyond a few integer variables. The array will contain integers ranging from -10^9 to 10^9. The c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_array_in_place(arr: List[int]) -> List[int]: """ Reverses the order of elements in a given array in place. Args: arr (List[int]): The array of integers to be reversed. Retur...
python_sharegpt_23k
12,150
Write a Python function to print out a countdown from a given number `n` to 1, but only display odd numbers that are also prime. The function should start with: ```python def countdown_odd_primes(n: int) -> None: ```
Here is the Python code to solve the given problem in the markdown block: ```python 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. """ if num < 2: ...
python_sharegpt_23k
12,152
Write a Python function that takes two strings as input, removes any leading or trailing white spaces from both strings, and concatenates the two strings. If either string is empty after removing the white spaces, return an error message stating that the input strings cannot be empty. Additionally, the function should ...
Here is the Python code to solve the given problem in the markdown block: ```python def concatenate_strings(string1: str, string2: str) -> str: """ Concatenates two strings after removing leading and trailing white spaces. Returns an error message if either string is empty after stripping or if the concaten...
python_sharegpt_23k
12,157
Write a Python function to generate a random string of length `n` containing only lowercase letters and special characters. The string must have at least one vowel, one consonant, and one special character from the set `!@#$%^&*-=+`. The function should ensure that the generated string meets these criteria. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python import random import string def generate_random_string(n: int) -> str: """ Generate a random string of length n containing only lowercase letters and special characters. The string must have at least one vowel, one consonan...
python_sharegpt_23k
12,164
Write a Python function to convert a given list of integers to a NumPy array, square each element of the array, sum all the squared elements, and return the result. The function should handle lists of any length and ensure that the result is accurate. The code should start with: ```python from typing import List impor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import numpy as np def sum_of_squared_elements(arr: List[int]) -> int: """ Convert a list of integers to a NumPy array, square each element, sum all the squared elements, and return the result. Args...
python_sharegpt_23k
12,165
Write a Python function to calculate the weighted average of a given list of tuples, where each tuple contains a number and its corresponding weight. The function should handle cases where the list is empty or contains invalid data (e.g., non-numeric values or negative weights). The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def calculate_weighted_average(data: List[Tuple[float, float]]) -> float: """ Calculate the weighted average of a list of tuples, where each tuple contains a number and its corresponding weight. ...
python_sharegpt_23k
12,166
Write a Python function to convert a given string to an all lowercase string without using any built-in string manipulation functions or methods such as `toLowerCase()`, `lower()`, or any similar ones. Implement your own algorithm to achieve the conversion with a time complexity of O(n), where n is the length of the in...
Here is the Python code to solve the given problem in the markdown block: ```python def to_lowercase(input_string: str) -> str: """ Converts the given string to an all lowercase string without using any built-in string manipulation functions or methods. Args: input_string (str): The input string to...
python_sharegpt_23k
12,168
Write a Python function to reverse a given array in-place without using any built-in functions or libraries that directly reverse the array. The function should have a time complexity of O(n) and should handle arrays with duplicates efficiently, without using any additional space. The code should start with: ```python...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_array(arr: List[int]) -> List[int]: """ Reverses the given array in-place without using any built-in functions or libraries that directly reverse the array. Args: arr (List[int])...
python_sharegpt_23k
12,169
Write a Python function to generate an array of a specified length, initialized to zero, and then fill it with a sequence of numbers starting from 1 and incrementing by 2. The function should return the filled array. The code should start with: ```python from typing import List def generate_sequence_array(length: int...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_sequence_array(length: int) -> List[int]: """ Generates an array of the specified length, filled with a sequence of numbers starting from 1 and incrementing by 2. Args: length (...