source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
4,222
Write a Python function to count the number of prime numbers from 1 to n (inclusive) without using any built-in prime number libraries, functions, or mathematical operations related to prime numbers. The function should use a brute-force approach to determine if a number is prime by checking divisibility. The code sho...
Here is the Python code to solve the given problem in the markdown block: ```python def count_primes_brute_force(n: int) -> int: """ Count the number of prime numbers from 1 to n (inclusive) using a brute-force approach. Args: n (int): The upper limit of the range to count prime numbers. Retur...
python_sharegpt_23k
4,224
Write a Python function to create a Numpy array with a specified number of columns and rows, where each element is a random integer between 0 and 100. Additionally, ensure that all the elements in the first column of the array are unique. The function should return the created array. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_random_array(rows: int, cols: int) -> np.ndarray: """ Create a Numpy array with the specified number of rows and columns, where each element is a random integer between 0 and 100. Additionally,...
python_sharegpt_23k
4,226
Write a Python function to determine if a given integer is a prime number. Additionally, create a list of prime numbers from 1 to 1000, excluding numbers that are divisible by 2. The code should start with: ```python def is_prime(n: int) -> bool: ``` and ```python def generate_prime_list() -> list: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Determines if a given integer is a prime number. Args: n (int): The integer to check for primality. Returns: bool: True if the number is prime, False otherwise. """...
python_sharegpt_23k
4,238
Write a Python function to find the second largest value in a list of positive integers with a time complexity of O(n), where n is the length of the list. The list will always have at least two elements. The code should start with: ```python from typing import List def find_second_largest(numbers: List[int]) -> int: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_second_largest(numbers: List[int]) -> int: """ Find the second largest value in a list of positive integers with a time complexity of O(n). Args: numbers (List[int]): A list of posi...
python_sharegpt_23k
4,241
Write a Python function to convert a given hexadecimal color code to RGB format without using any built-in functions or libraries. The input will always be a valid hexadecimal color code and will be a 6-digit code without any leading "#" symbol. The output should be a string in the format "RGB(x, y, z)" where x, y, and...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_hex_to_rgb(hex_code: str) -> str: """ Convert a given hexadecimal color code to RGB format. Args: hex_code (str): A 6-digit hexadecimal color code without any leading "#". Returns: str: A st...
python_sharegpt_23k
4,246
Write a Python function to find the most frequent character in a given string, excluding punctuation, numbers, and whitespace characters. Both uppercase and lowercase characters should be considered the same. If there are multiple characters with the same highest frequency, return the character that appears first in th...
Here is the Python code to solve the given problem in the markdown block: ```python def most_frequent_character(input_string: str) -> str: """ Find the most frequent character in a given string, excluding punctuation, numbers, and whitespace characters. Both uppercase and lowercase characters are considered...
python_sharegpt_23k
4,255
Write a Python function to calculate the sum of the first n prime numbers. The function should take an integer `n` as input and return the sum of the first `n` prime numbers. The code should start with: ```python def sum_of_first_n_primes(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Check if a number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. """ if n < 2: ...
python_sharegpt_23k
4,264
Write a Python function to find the length of the longest word that can be formed by rearranging the characters of a given string. The characters in the string can be rearranged in any order to form a word. A word is defined as a sequence of one or more characters, where each character is an uppercase or lowercase lett...
Here is the Python code to solve the given problem in the markdown block: ```python import re def longest_word_length(string: str) -> int: """ Find the length of the longest word that can be formed by rearranging the characters of a given string. Args: string (str): The input string from which to ...
python_sharegpt_23k
4,267
Write a Python function to find the largest sum of any two distinct numbers in a given list of integers. The function should be efficient and handle large input sizes within the given constraints. The code should start with: ```python from typing import List def get_largest_sum(num_list: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_largest_sum(num_list: List[int]) -> int: """ Find the largest sum of any two distinct numbers in the given list. Args: num_list (List[int]): A list of integers. Returns: ...
python_sharegpt_23k
4,269
Write a Python function to count the number of occurrences of a given character in a string, considering case sensitivity. The function should also count occurrences of the character as part of a larger substring, and handle strings with special characters and spaces. The code should start with: ```python def count_occ...
Here is the Python code to solve the given problem in the markdown block: ```python def count_occurrences(string: str, character: str) -> int: """ Counts the number of occurrences of a given character in a string, considering case sensitivity. Args: string (str): The string in which to count the oc...
python_sharegpt_23k
4,287
Write a Python function to calculate the Fibonacci sequence up to the n-th number where n is provided as an argument. The solution must have a time complexity of O(n) and a space complexity of O(n). Additionally, you are not allowed to use recursion or iteration to solve this problem. The function should start with: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def fibonacci(n: int) -> int: """ Calculate the n-th Fibonacci number using a dynamic programming approach. Args: n (int): The position in the Fibonacci sequence to calculate. Returns: int: The n-th Fib...
python_sharegpt_23k
4,301
Write a Python function to sort a list of numbers in ascending order using the Merge Sort algorithm. You are not allowed to use any built-in sorting functions or libraries. The function should have a time complexity of O(n log n) or better. The code should start with: ```python from typing import List def merge_sort(...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(numbers: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using the Merge Sort algorithm. Args: numbers (List[int]): The list of integers to be sorted....
python_sharegpt_23k
4,306
Write a Python function to reverse the characters in a given string without using any built-in reverse functions or extra memory. The function should handle strings that contain special characters such as punctuation marks and whitespace. 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 characters in a given string without using any built-in reverse functions or extra memory. Args: s (str): The input string to be reversed. Returns: st...
python_sharegpt_23k
4,311
Write a Python function to sort the positive elements in a given array in descending order, ensuring that the sorted elements are unique and do not contain any duplicates. The input array will contain at most \(10^6\) elements, and the elements will be positive integers between 1 and \(10^9\) (inclusive). Your solution...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_positive_descending_unique(arr: List[int]) -> List[int]: """ Sorts the positive elements in the given array in descending order, ensuring the sorted elements are unique. Args: arr (...
python_sharegpt_23k
4,315
Implement the Sieve of Eratosthenes algorithm to efficiently find all prime numbers up to a given limit. The function should take an integer `limit` as input and return a list of all prime numbers up to the given `limit`. The code should start with: ```python from typing import List def sieve_of_eratosthenes(limit: i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sieve_of_eratosthenes(limit: int) -> List[int]: """ Find all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. Args: limit (int): The upper limit up to which t...
python_sharegpt_23k
4,318
Write a Python function to return a new string containing the characters present only in the first string `s1`, while ignoring any duplicates and case sensitivity. The returned string should be sorted in ascending order and should only contain alphabetic characters. The function should handle strings with special chara...
Here is the Python code to solve the given problem in the markdown block: ```python def unique_alphabetic_chars(s1: str, s2: str) -> str: """ Returns a new string containing the alphabetic characters present only in s1, ignoring any duplicates and case sensitivity. The returned string is sorted in ascen...
python_sharegpt_23k
4,321
Write a Python function to calculate the Damerau-Levenshtein Distance between two strings, `str1` and `str2`, with a time complexity of O(m*n), where `m` is the length of `str1` and `n` is the length of `str2`. The function should handle cases where either `str1` or `str2` is an empty string and should also handle stri...
Here is the Python code to solve the given problem in the markdown block: ```python def damerau_levenshtein_distance(str1: str, str2: str) -> int: """ Calculate the Damerau-Levenshtein Distance between two strings. The Damerau-Levenshtein Distance allows for four types of operations: insertion, deletio...
python_sharegpt_23k
4,323
Write a Python function to check if all numbers in a given list are prime numbers. The function should have a time complexity less than O(n^(1.5)), where n is the length of the input list. The function should start with: ```python from typing import List def are_all_numbers_prime(numbers: List[int]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def is_prime(n: int) -> bool: """ Check if a given number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, Fal...
python_sharegpt_23k
4,326
Write a Python function to reverse the order of words in a given string while also ensuring that each word is reversed as well, without using any built-in string reversal functions or methods. The solution should handle edge cases such as leading/trailing spaces, multiple spaces between words, and punctuation marks. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_and_order(string: str) -> str: """ Reverses the order of words in a given string while also reversing each word. Args: string (str): The input string to be processed. Returns: str: The...
python_sharegpt_23k
4,332
Write a Python function to find the maximum value in each row of a given matrix using a recursive approach. The function should return a list where each element is the maximum value of the corresponding row in the matrix. The function should handle various edge cases, such as an empty matrix, empty rows, and matrices w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_in_each_row(matrix: List[List[int]]) -> List[int]: """ Find the maximum value in each row of a given matrix using a recursive approach. Args: matrix (List[List[int]]): A 2D list...
python_sharegpt_23k
4,337
Write a Python function to convert a binary string to a list of integers and return the maximum value from the list. The binary string should not exceed a length of 10,000 characters. If the binary string contains any invalid characters (characters other than '0' and '1'), raise a `ValueError` with the error message "I...
Here is the Python code to solve the given problem in the markdown block: ```python def max_binary_value(binary_string: str) -> int: """ Convert a binary string to a list of integers and return the maximum value. Args: binary_string (str): The binary string to be converted. Returns: in...
python_sharegpt_23k
4,340
Write a Python function to check if a specific item exists in a list of lists, where each inner list is sorted in ascending order. The function should minimize memory usage and have a time complexity of O(log(n)), where n is the total number of inner lists. The code should start with: ```python from typing import List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def binary_search(sorted_list: List[Any], target: Any) -> bool: """ Perform binary search on a sorted list to check if a target element exists. Args: sorted_list (List[Any]): A sorted l...
python_sharegpt_23k
4,351
Write a Python function to insert a new item into a given list at a specified index, ensuring that the index is within the valid range of the list's length. If the index is out of bounds, return an appropriate error message. Additionally, the function should check if the item to be inserted is already present in the li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def insert_item_at_index(lst: List[int], item: int, index: int) -> Union[List[int], str]: """ Inserts a new item into a given list at the specified index, ensuring that the index is within the val...
python_sharegpt_23k
4,356
Write a Python function to find all prime numbers from 1 to a given integer `n` using the Sieve of Eratosthenes algorithm. The function should use a single for loop and maintain a time complexity of O(n). The code should start with: ```python from typing import List def find_prime_numbers(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_prime_numbers(n: int) -> List[int]: """ Find all prime numbers from 1 to n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit to find prime numbers up to. ...
python_sharegpt_23k
4,369
Write a Python function to check if the parentheses, brackets, and curly braces in a given string are balanced. The string may contain other characters apart from these symbols. The function should return `True` if the parentheses, brackets, and curly braces are balanced and `False` otherwise. Additionally, the functio...
Here is the Python code to solve the given problem in the markdown block: ```python def is_balanced_parentheses(string: str) -> bool: """ Check if the parentheses, brackets, and curly braces in the given string are balanced. Args: string (str): The string to check for balanced parentheses, brackets...
python_sharegpt_23k
4,371
Write a Python function to reverse the characters in each word of a given string while maintaining the original order of the words. The function should reverse each word in-place without using any additional space. The input string will only contain lowercase English letters and spaces, with no leading or trailing spac...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_in_string(s: str) -> str: """ Reverses the characters in each word of the given string while maintaining the original order of the words. Args: s (str): The input string containing lowercase Englis...
python_sharegpt_23k
4,373
Write a Python function to sort an array of integers in ascending order using the Bubble Sort algorithm. The function should have a time complexity of O(n^2) and should only use array manipulation and comparison operators. The code should start with: ```python from typing import List def bubble_sort(arr: 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 an array of integers in ascending order using the Bubble Sort algorithm. Args: arr (List[int]): The array of integers to be sorted. ...
python_sharegpt_23k
4,377
Write a Python function to generate an array containing all prime numbers between 1 and a given integer `n` using the Sieve of Eratosthenes algorithm. The function should have a time complexity of O(n log log n) and a space complexity of O(n). The code should start with: ```python from typing import List def generate...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_primes_up_to_n(n: int) -> List[int]: """ Generate a list of all prime numbers up to a given integer n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit u...
python_sharegpt_23k
4,383
Write a Python function to filter an array of mixed data types, removing all integers and duplicate elements while preserving the order of the remaining elements. The resulting array should only contain strings and floating-point numbers. Additionally, the filter should be case-sensitive and exclude any strings that st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def filter_array(arr: List[Union[int, float, str]]) -> List[Union[float, str]]: """ Filters an array of mixed data types, removing all integers and duplicate elements while preserving the order of...
python_sharegpt_23k
4,385
Write a Python function to create a new array by multiplying each element of a given array by a given number. The resulting array should only contain unique elements, without changing the order of the elements. The given array contains positive integers and has a length between 1 and 100. The given number is a positive...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def multiply_array_unique(arr: List[int], num: int) -> List[int]: """ Create a new array by multiplying each element of the given array by the given number. The resulting array will contain only uniq...
python_sharegpt_23k
4,390
Write a Python function to reverse the elements of an array in groups of size `k`, where `k` is a positive integer. The function should reverse the elements of each group in-place without using any additional data structures and should have a time complexity of O(n) and a space complexity of O(1). The code should star...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_in_groups(arr: List[int], k: int) -> List[int]: """ Reverses the elements of the array in groups of size k in-place. Args: arr (List[int]): The array of integers to be reversed i...
python_sharegpt_23k
4,395
Write a Python function to check whether each property in a given object includes a specified value. The object can contain strings, numbers, arrays, and nested objects. The function should return `True` if all properties include the specified value and are of valid types (strings, numbers, arrays, or nested objects), ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Union def check_properties_contain_value(obj: Union[dict, list, str, int], value: Any) -> bool: """ Check whether each property in a given object includes a specified value. The object can contain s...
python_sharegpt_23k
4,399
Write a Python function to convert a binary string to its corresponding decimal value. The function should handle the following scenarios: 1. If the binary string is empty or contains characters other than '0' and '1', the function should return an error message. 2. If the binary string is valid, the function should co...
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_decimal(binary_string: str) -> int or str: """ Converts a binary string to its corresponding decimal value. Args: binary_string (str): The binary string to be converted. Returns: int or st...
python_sharegpt_23k
4,401
Write a Python function to capitalize each word in a given sentence without using any built-in string functions or methods that directly capitalize words or convert strings to title case. The sentence can contain multiple words separated by spaces. The function should start with: ```python def capitalize_sentence(sente...
Here is the Python code to solve the given problem in the markdown block: ```python def capitalize_sentence(sentence: str) -> str: """ Capitalizes each word in the given sentence without using any built-in string functions or methods. Args: sentence (str): The input sentence containing multiple wor...
python_sharegpt_23k
4,405
Write a Python function to calculate the product of two positive integers `x` and `y`, where both `x` and `y` are less than or equal to 1000, without using the multiplication operator `(*)` or any built-in functions or methods that involve multiplication. The function should use an iterative approach to achieve the mul...
Here is the Python code to solve the given problem in the markdown block: ```python def multiply_without_operator(x: int, y: int) -> int: """ Calculate the product of two positive integers x and y without using the multiplication operator (*). Args: x (int): The first positive integer, less than or...
python_sharegpt_23k
4,407
Write a Python function to determine if a given year is a leap year according to the Gregorian calendar, considering the exceptions introduced by the Julian calendar reform in 1582, and the variations in leap year calculations used in different countries, such as the proleptic Gregorian calendar used by the Internation...
Here is the Python code to solve the given problem in the markdown block: ```python def check_leap_year(year: int) -> tuple: """ Determine if a given year is a leap year according to the Gregorian calendar, considering the exceptions introduced by the Julian calendar reform in 1582, and the variations i...
python_sharegpt_23k
4,413
Write a Python function to print the keys and values of a given dictionary in reverse order, but only for the keys that have a length greater than 5 and do not contain the letter 'e'. The function should use a while loop to iterate through the dictionary. The code should start with: ```python def print_filtered_dict_r...
Here is the Python code to solve the given problem in the markdown block: ```python def print_filtered_dict_reverse(my_dict: dict): """ Prints the keys and values of a given dictionary in reverse order, but only for the keys that have a length greater than 5 and do not contain the letter 'e'. Args: ...
python_sharegpt_23k
4,415
Write a Python function to convert a given number from degrees to radians. The function should take an integer representing the number of degrees and return a float representing the corresponding value in radians. The conversion should be done using the formula: \[ \text{radians} = \frac{\text{degrees} \times \pi}{180...
Here is the Python code to solve the given problem in the markdown block: ```python def degrees_to_radians(degrees: int) -> float: """ Convert a given number from degrees to radians. Args: degrees (int): The number of degrees to convert, must be an integer between -360 and 360. Returns: ...
python_sharegpt_23k
4,419
Write a Python function to calculate the sum of positive numbers in each row of a given two-dimensional array. The function should handle rows that contain negative or zero values by ignoring them in the sum calculation. The code should start with: ```python from typing import List def sum_positive_row_elements(arr: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_positive_row_elements(arr: List[List[int]]) -> List[int]: """ Calculate the sum of positive numbers in each row of a given two-dimensional array. Args: arr (List[List[int]]): A two-d...
python_sharegpt_23k
4,420
Write a Python function to calculate the sum of all elements in a given array of numbers. However, if any element in the array is a negative number, raise a `ValueError` with a custom error message. Additionally, if the sum of all elements in the array exceeds a certain threshold value, raise an `OverflowError` with a ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_sum(numbers: List[int], threshold: int) -> int: """ Calculate the sum of all elements in the given array of numbers. If any element in the array is a negative number, raise a ValueError...
python_sharegpt_23k
4,424
Write a Python function to generate and return a list of prime numbers from 0 to a given upper limit `n` using an optimized algorithm. The function should minimize the use of built-in functions or libraries that directly solve the problem and ensure efficient execution with a time complexity of O(n log(log n)) or bette...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_primes(n: int) -> List[int]: """ Generate a list of prime numbers from 0 to the given upper limit n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit up ...
python_sharegpt_23k
4,426
Write a Python function to create a 4x4 NumPy array of random numbers from -1 to 1, where each element in the array is unique. The array should be sorted in ascending order along each row and column. The function should return the sorted array. The code should start with: ```python import numpy as np def create_sorte...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_sorted_unique_array() -> np.ndarray: """ Create a 4x4 NumPy array of unique random numbers from -1 to 1, sorted in ascending order along each row and column. Returns: np.ndarray: A 4x4...
python_sharegpt_23k
4,428
Write a Python function to filter out prime numbers from a given list of integers and return a tuple containing the list of prime numbers and the sum of these prime numbers. The function should use a helper function to determine if a number is prime. The code should start with: ```python from typing import List, Tuple...
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: """ 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 pr...
python_sharegpt_23k
4,431
Write a Python function to calculate the sum of all numbers in a given list without using the built-in `sum()` function or any other function that directly calculates the sum of a list. Implement your own algorithm to achieve this, ensuring that the function has a time complexity of O(n) and a space complexity of O(1),...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_sum(numbers: List[int]) -> int: """ Calculate the sum of all numbers in the given list without using the built-in sum() function. Args: numbers (List[int]): A list of integers ...
python_sharegpt_23k
4,432
Write a Python function to check if all strings in a given array contain at least three digits, three lowercase letters, and three special characters. The function should return `True` if all strings meet the criteria, otherwise it should return `False`. The code should start with: ```python from typing import List d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def check_strings(arr: List[str]) -> bool: """ Check if all strings in the given array contain at least three digits, three lowercase letters, and three special characters. Args: arr (List[s...
python_sharegpt_23k
4,440
Write a Python function to shuffle a list of integers such that all even numbers appear before all odd numbers, while preserving the relative order of the even and odd numbers. The original list should remain unchanged. The function should have a time complexity of O(n) or better, where n is the length of the input lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def shuffle_even_odd(lst: List[int]) -> List[int]: """ Shuffles a list of integers such that all even numbers appear before all odd numbers, while preserving the relative order of the even and odd nu...
python_sharegpt_23k
4,443
Write a Python function to determine if a given integer is a prime number. The function should be optimized to handle large prime numbers efficiently. Additionally, provide a piece of erroneous code that falsely claims to correctly detect prime numbers. Explain why it is incorrect and provide a fix for the code. The c...
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 integer is a prime number. Args: n (int): The integer to check for primality. Returns: bool: True if the integer is prime, False other...
python_sharegpt_23k
4,446
Write a Python function to extract and return a list of odd numbers from a given input list while preserving their original order. If the input list is empty, return a specific message indicating that the list is empty. The function should have a time complexity of O(n), where n is the length of the input list. The co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_odd_numbers(input_list: List[int]) -> List[int] or str: """ Extracts and returns a list of odd numbers from the given input list while preserving their original order. If the input list is em...
python_sharegpt_23k
4,447
Write a Python function to swap the values of two variables using only bitwise operators and without using any arithmetic or assignment operators. Additionally, you cannot use any conditional statements or loops. The function should take two integers as input and return a tuple containing the swapped values. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_values_bitwise(a: int, b: int) -> tuple: """ Swaps the values of two integers using only bitwise operators. Args: a (int): The first integer. b (int): The second integer. Returns: tuple...
python_sharegpt_23k
4,448
Write a Python function to find the maximum and minimum values from a given array of integers, ignoring any negative integers. The function should perform this task using a single loop and should not use any built-in functions or libraries to determine the maximum and minimum values. The function should return both the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_min_non_negative(arr: List[int]) -> Tuple[int, int]: """ Finds the maximum and minimum non-negative values in the given array using a single loop. Args: arr (List[int]): ...
python_sharegpt_23k
4,452
Write a Python function that performs a loop which terminates after exactly 10 iterations. The loop condition must not use any comparison operators (such as `<`, `>`, `<=`, `>=`), arithmetic operators (such as `+`, `-`, `*`, `/`), or bitwise operators (such as `&`, `|`, `^`). Instead, you must use only logical operator...
Here is the Python code to solve the given problem in the markdown block: ```python def perform_loop_with_logical_operators() -> None: """ Perform a loop that terminates after exactly 10 iterations using only logical operators in the loop condition. This function initializes a counter and uses a logical co...
python_sharegpt_23k
4,455
Write a Python function to transpose a given 2D matrix and calculate the product of each row and each column in the transposed matrix. The matrix should only contain integers in the range of -1000 to 1000, and it should handle matrices with dimensions up to 1000x1000. The function should return the transposed matrix, t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def transpose_and_calculate_product(matrix: List[List[int]]) -> (List[List[int]], List[int], List[int]): """ Transposes the given matrix and calculates the product of each row in the transposed matrix ...
python_sharegpt_23k
4,456
Write a Python function to reverse the order of elements in a given array using a recursive approach. The function should not use any built-in reverse functions or temporary variables. The code should start with: ```python from typing import List def reverse_array_recursive(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_recursive(arr: List[int]) -> List[int]: """ Reverses the order of elements in a given array using a recursive approach. Args: arr (List[int]): The array to be reversed. ...
python_sharegpt_23k
4,461
Write a Python function to detect if a given URL contains a query string parameter named "food" with the value "burger" case-insensitively. If found, extract and return the values of all other query string parameters present in the URL, excluding any query string parameters that have the same name as the "food" paramet...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def extract_query_params(url: str) -> List[str]: """ Detects if the given URL contains a query string parameter named "food" with the value "burger" case-insensitively. If found, extracts and returns...
python_sharegpt_23k
4,476
Write a Python function to count the number of males in a given JSON dataset, while ignoring individuals whose age is less than 20 or greater than 40. The function should handle cases where the "age" key is missing in the JSON data and should be implemented with a time complexity of O(log(n)), where n is the number of ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_males_in_age_range(data: List[Dict[str, any]]) -> int: """ Counts the number of males in the given dataset whose age is between 20 and 40 (inclusive). Args: data (List[Dict[s...
python_sharegpt_23k
4,479
Write a Python function to find the first unique odd number greater than 5 and less than 20 in a given list of positive integers. The function should consider both the value and its position in the list. The solution should have a time complexity of O(n), where n is the length of the 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 find_first_unique_odd_number(numbers: List[int]) -> int: """ Find the first unique odd number greater than 5 and less than 20 in the list of positive integers. Args: numbers (List[int]):...
python_sharegpt_23k
4,482
Write a Python function to compare two lists and determine whether they are equal or not. The lists can contain integers, strings, nested lists, dictionaries, tuples, and sets. The function should consider the order of elements in the lists and handle nested structures recursively. The function should have a time compl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Tuple, Set def are_lists_equal(list1: List, list2: List) -> bool: """ Compare two lists and determine whether they are equal. The lists can contain integers, strings, nested lists, dictionaries, t...
python_sharegpt_23k
4,489
Write a Python function to sort a list of integers in descending order using a modified version of the quicksort algorithm. The function should meet the following requirements: 1. The sorting algorithm must be a modified version of quicksort. 2. The modified quicksort algorithm must use a pivot element that is randomly...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def modified_quicksort(arr: List[int], low: int = 0, high: int = None) -> None: """ Sorts the input list of integers in descending order using a modified quicksort algorithm. Args: ...
python_sharegpt_23k
4,492
Write a Python function to find the product of corresponding elements from two lists of numbers. The function should handle any possible errors or edge cases, such as non-list inputs, empty lists, non-numeric values, and lists of different lengths. The code should start with: ```python from typing import List def find...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_product(list1: List[float], list2: List[float]) -> List[float]: """ Calculate the product of corresponding elements from two lists of numbers. Args: list1 (List[float]): The first l...
python_sharegpt_23k
4,495
Write a Python function to identify which strings in a given list are palindromes. The strings may contain punctuation marks, spaces, and numbers, and should be treated as case-insensitive. Additionally, you are not allowed to use any built-in string manipulation or regular expression functions in your solution. The fu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_palindromes(strings: List[str]) -> List[str]: """ Identify which strings in a given list are palindromes. Args: strings (List[str]): A list of strings to check for palindromes. ...
python_sharegpt_23k
4,498
Write a Python function to find the k-th largest element in a list of integers efficiently, with a time complexity of O(n log k), where n is the length of the list. The function should handle lists with duplicate values, negative integers, empty lists, and lists with only one element correctly. If there is no k-th larg...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional import heapq def find_kth_largest_element(arr: List[int], k: int) -> Optional[int]: """ Find the k-th largest element in a list of integers efficiently. Args: arr (List[int]): The ...
python_sharegpt_23k
4,503
Write a Python function to determine the maximum and minimum values in an array of integers without using any built-in sorting functions or data structures. The function should run in O(n) time complexity, where n is the length of the array. The array can have duplicate values and can be of any length between 1 and 10^...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_min(arr: List[int]) -> Tuple[int, int]: """ Determine the maximum and minimum values in an array of integers without using any built-in sorting functions or data structures. Args...
python_sharegpt_23k
4,509
Write a Python function that takes in a string and returns a list of all unique words in the string in alphabetical order, without using any built-in sorting functions or data structures. The function should be case-insensitive, meaning that uppercase and lowercase letters should be treated as equal. Additionally, the ...
Here is the Python code to solve the given problem in the markdown block: ```python def unique_words_in_order(string: str) -> list: """ Extracts all unique words from a given string and returns them in alphabetical order. Args: string (str): The input string from which to extract unique words. ...
python_sharegpt_23k
4,513
Write a Python function to check if two lists have the same elements regardless of their order. The elements in the lists will be strings consisting of lowercase letters only, and each element will have a length between 1 and 20 characters inclusive. The lists can have a maximum of 1000 elements. If the lists have the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List from collections import Counter def have_same_elements(list1: List[str], list2: List[str]) -> bool: """ Check if two lists have the same elements regardless of their order. Args: list1 (List...
python_sharegpt_23k
4,517
Write a Python function to scale a given function for n values, where n is a positive integer, using recursion. The function should ensure that the scaling process has a time complexity of O(n) or less and should handle cases where the input n is larger than the available memory capacity without causing a memory overfl...
Here is the Python code to solve the given problem in the markdown block: ```python import sys from typing import List def scale_function(n: int, result: List[int] = None) -> List[int]: """ Scales a function for n values using recursion. Ensures the time complexity is O(n) or less and handles memory constraint...
python_sharegpt_23k
4,531
Write a Python function to generate all possible permutations of a given string. The input string will contain only lowercase alphabets and may contain duplicate characters. The output list should be sorted in lexicographically increasing order. The solution should have a time complexity of O(n!) or better, where n is ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_permutations(s: str) -> List[str]: """ Generate all possible permutations of a given string. Args: s (str): The input string containing only lowercase alphabets. Returns: ...
python_sharegpt_23k
4,538
Write a Python function to reverse a given string without using any built-in reverse functions, methods, or string manipulation functions. The input string will have at most 1000 characters and will only consist of lowercase letters. You must implement your own algorithm to reverse the string character by character. T...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(input_str: str) -> str: """ Reverses the given string without using any built-in reverse functions, methods, or string manipulation functions. Args: input_str (str): The input string to be reverse...
python_sharegpt_23k
4,539
Write a Python function to find and return the top three words with the most occurrences in a given string, excluding common English stop words. The function should handle cases where the input string contains special characters, multiple whitespaces, and punctuation marks. The function should also be case-insensitive,...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import re from collections import Counter # Common English stop words STOP_WORDS = { 'the', 'and', 'but', 'or', 'if', 'is', 'are', 'it', 'of', 'in', 'on', 'to', 'was', 'were', 'for', 'with', 'as', 'at...
python_sharegpt_23k
4,544
Write a Python function to generate a spiral array from a given 2D array. The function should handle cases where the input array contains negative numbers and should only use a single loop to iterate through the elements of the input array. The function should also handle cases where the input array has an odd number o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def spiral_array(input_array: List[List[int]]) -> List[int]: """ Generates a spiral array from a given 2D array. Args: input_array (List[List[int]]): The 2D array from which to generate the ...
python_sharegpt_23k
4,546
Write a Python function to compute the maximum absolute difference between two lists of numbers, considering only unique numbers in each list. The function should take two lists as input and return the maximum absolute difference. The absolute difference between two numbers is the absolute value of their difference. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def compute_max_absolute_difference(list1: List[int], list2: List[int]) -> int: """ Computes the maximum absolute difference between two lists of numbers, considering only unique numbers in each list. ...
python_sharegpt_23k
4,557
Write a Python function to find the maximum value in each column of a 2D array and return a new 2D array where each element is replaced by its corresponding column's maximum value. The input array can have any number of rows and columns. The function should handle edge cases such as an empty array, non-numeric values, ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_column_max(array: List[List[int]]) -> List[List[int]]: """ Find the maximum value in each column of a 2D array and return a new 2D array where each element is replaced by its corresponding colum...
python_sharegpt_23k
4,559
Write a Python function to sort an array of integers in descending order using a variation of the quicksort algorithm. The function should not use any built-in sorting functions or libraries and should have a time complexity of O(n^2). The function should be able to handle arrays with up to 10,000,000 elements and shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def quicksort(arr: List[int]) -> List[int]: """ Sorts an array of integers in descending order using a variation of the quicksort algorithm. Args: arr (List[int]): The array of integers to b...
python_sharegpt_23k
4,562
Write a Python function to extract unique elements from a given list of integers while maintaining their original order. The function should not use any built-in functions or libraries to solve this problem. The function should start with: ```python from typing import List def extract_unique_elements(lst: List[int]) -...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def extract_unique_elements(lst: List[int]) -> List[int]: """ Extracts unique elements from a given list of integers while maintaining their original order. Args: lst (List[int]): The input ...
python_sharegpt_23k
4,567
Write a Python function to check if a given input string is a palindrome, ignoring any punctuation marks, spaces, and special characters. The function should be case-insensitive and should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the input string. The function should also ...
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(input_string: str) -> bool: """ Check if the given input string is a palindrome, ignoring punctuation, spaces, and special characters. The function is case-insensitive and operates in O(n) time complexity a...
python_sharegpt_23k
4,568
Write a Python function to remove duplicates from a list of strings while ignoring case sensitivity and removing any leading or trailing whitespace. Additionally, the function should remove any strings that are palindromes. The function should start with: ```python from typing import List def remove_duplicates_and_pal...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_palindromes(strings: List[str]) -> List[str]: """ Removes duplicates and palindromes from a list of strings while ignoring case sensitivity and removing leading/trailing whitesp...
python_sharegpt_23k
4,574
Write a Python function to extract all possible subsequences of a specified length `n` from a given string `s`. The function should return a list of all the subsequences. The code should start with: ```python from typing import List def extract_subsequences(s: str, n: int) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def extract_subsequences(s: str, n: int) -> List[str]: """ Extract all possible subsequences of length n from the given string s. Args: s (str): The input string from which to extract subseq...
python_sharegpt_23k
4,577
Write a Python function to calculate the `n`th number in the Fibonacci sequence using an iterative approach. The function should have a time complexity of O(n) and a space complexity of O(1). Additionally, the function should handle input validation. If the input `n` is not a positive integer, the function should raise...
Here is the Python code to solve the given problem in the markdown block: ```python def fibonacci(n: int) -> int: """ Calculate the nth number in the Fibonacci sequence using an iterative approach. The Fibonacci sequence is defined as follows: - The 0th number is 0. - The 1st number is 1. - Eac...
python_sharegpt_23k
4,578
Write a Python function to count the frequency of occurrence of each vowel (a, e, i, o, u) in a given string. The input string may contain uppercase and lowercase letters, digits, special characters, and spaces, and can have a maximum length of 10^6 characters. The function should have a time complexity of O(n) and a s...
Here is the Python code to solve the given problem in the markdown block: ```python def count_vowel_frequency(input_str: str) -> list: """ Counts the frequency of occurrence of each vowel (a, e, i, o, u) in the given string. Args: input_str (str): The input string to analyze. Returns: ...
python_sharegpt_23k
4,579
Write a Python function to remove duplicate elements from a given array of integers and sort the remaining elements in descending order. If the input array is empty or contains only one element, return the original array. The code should start with: ```python from typing import List def remove_duplicates_and_sort_des...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_sort_descending(arr: List[int]) -> List[int]: """ Removes duplicate elements from the given array and sorts the remaining elements in descending order. Args: arr (L...
python_sharegpt_23k
4,599
Write a Python function to perform data analysis on a list of integers and return the average, median, mode, range, and number of distinct elements. The function should handle lists of any length, including empty lists. The code should start with: ```python from typing import List, Union from collections import Counter...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union, Tuple from collections import Counter def analyze_data(data: List[int]) -> Union[Tuple[float, float, List[int], int, int], Tuple[None, None, None, None, None]]: """ Perform data analysis on a lis...
python_sharegpt_23k
4,610
Write a Python function to calculate the result when a power of 2 is raised to the power of n, where n is a positive integer less than or equal to 1,000. The function should not use any mathematical operations or built-in functions for exponentiation. Your solution should have a time complexity of O(log n) and a space ...
Here is the Python code to solve the given problem in the markdown block: ```python def power_of_two(n: int) -> int: """ Calculate the result of 2 raised to the power of n using an iterative approach with O(log n) time complexity. Args: n (int): The exponent, a positive integer less than or equal t...
python_sharegpt_23k
4,615
Write a Python function to perform an interpolation search on a sorted list of integers to find the position of a target number. The function should be efficient and handle lists of up to 1 million integers. The code should start with: ```python from typing import List def interpolation_search(arr: List[int], target: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def interpolation_search(arr: List[int], target: int) -> int: """ Perform an interpolation search to find the position of a target number in a sorted list of integers. Args: arr (List[int]):...
python_sharegpt_23k
4,617
Write a Python function to find the maximum sum of a contiguous subarray within a given array of integers. The function should use list comprehensions and ensure that it runs in O(n) time complexity, where n is the length of the input array. The code should start with: ```python from typing import List def max_subarra...
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 sum of a contiguous subarray within the given array. Args: arr (List[int]): The input array of integers. Returns: ...
python_sharegpt_23k
4,618
Write a Python function called `sum_of_lists` that takes in a list of lists as a parameter and returns the sum of all the elements in the input lists. The input list of lists will always contain integers or nested lists of integers. The function should have the following specifications: - Input: a list of lists `lists...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_lists(lists: List[List[int]]) -> float: """ Calculate the sum of all elements in a list of lists containing integers or nested lists of integers. Args: lists (List[List[int]]): A ...
python_sharegpt_23k
4,626
Implement a recursive merge sort algorithm in Python that sorts an array of integers in ascending order. The algorithm should have a time complexity of O(n log n) and use only a constant amount of extra space. The function should handle arrays of size up to 10^6. 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 recursive_merge_sort(arr: List[int]) -> List[int]: """ Sorts an array of integers in ascending order using a recursive merge sort algorithm. Args: arr (List[int]): The array of integers ...
python_sharegpt_23k
4,640
Write a Python function to retrieve the value of a specific key from a given dictionary, while ignoring the built-in dictionary methods. The function should handle nested dictionaries as well. Additionally, the function should only return the value if the key is present in the dictionary and if the value is a string, o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict def retrieve_value(dictionary: Dict[str, Any], key: str) -> str: """ Retrieves the value of a specific key from a given dictionary, handling nested dictionaries and converting string values to lower...
python_sharegpt_23k
4,641
Write a Python function to calculate the nth term of the Fibonacci sequence using a recursive approach. However, you are not allowed to use any loops or the 'if' statement in your code. The function should be able to handle any non-negative integer input for n. The code should start with: ```python def fibonacci(n: in...
Here is the Python code to solve the given problem in the markdown block: ```python def fibonacci(n: int, memo: dict = None) -> int: """ Calculate the nth term of the Fibonacci sequence using a recursive approach. Args: n (int): The position in the Fibonacci sequence to calculate. memo (dic...
python_sharegpt_23k
4,642
Write a Python function that determines if a given number is a prime number and, if not, returns the list of all prime factors of the given number along with their multiplicities. The function should start with: ```python from typing import List, Tuple def is_prime_with_factors(number: int) -> Tuple[bool, List[Tuple[i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def is_prime_with_factors(number: int) -> Tuple[bool, List[Tuple[int, int]]]: """ Determines if a given number is a prime number and, if not, returns the list of all prime factors of the given num...
python_sharegpt_23k
4,646
Write a Python function to find the k-th smallest integer element in a given list. The function should handle cases where the list contains duplicate elements, negative numbers, and floating point numbers. Additionally, the function should include error handling to display an appropriate message if the list is empty or...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_kth_smallest_element(lst: List[float], k: int) -> str: """ Find the k-th smallest integer element in a given list. Args: lst (List[float]): The list of numbers from which to find th...
python_sharegpt_23k
4,648
Write a Python function to extract the first 3 elements from a given array that are prime numbers. If there are no prime numbers among the first 3 elements, return an empty array. The function should start with: ```python from typing import List def extract_first_3_primes(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 extract_first_3_primes(arr: List[int]) -> List[int]: """ Extracts the first 3 elements from a given array that are prime numbers. Args: arr (List[int]): The input array of integers. ...
python_sharegpt_23k
4,653
Write a Python function to calculate the mean of a given array `arr` from scratch without using any built-in numpy functions or methods. The function must ensure that it has a time complexity of O(n) and a space complexity of O(1). Additionally, handle cases where the input array `arr` is empty by returning `None`. Th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def calculate_mean(arr: List[int]) -> Optional[float]: """ Calculate the mean of a given array without using any built-in numpy functions or methods. The function ensures a time complexity ...
python_sharegpt_23k
4,658
Write a Python function to find and return the 3 highest distinct numbers in descending order from a given list of integers. The algorithm should have a time complexity of O(n) and use constant space. Additionally, the algorithm should not modify the original array. The input array will contain at most 10^5 integers, r...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_3_highest_distinct_numbers(nums: List[int]) -> List[int]: """ Find and return the 3 highest distinct numbers in descending order from a given list of integers. The algorithm runs in O(n) ti...
python_sharegpt_23k
4,660
Write a Python function `play_game` that simulates an extended version of the game Rock Paper Scissors Lizard Spock, which includes additional elements: Fire, Water, Air, and Earth. The function should take two parameters, `player1_choice` and `player2_choice`, both of which are strings representing the choices of the ...
Here is the Python code to solve the given problem in the markdown block: ```python def play_game(player1_choice: str, player2_choice: str) -> str: """ Simulates an extended version of Rock Paper Scissors Lizard Spock, including Fire, Water, Air, and Earth. Args: player1_choice (str): The choice of...
python_sharegpt_23k
4,661
Write a Python function to generate the Fibonacci sequence up to the nth term using a recursive approach. The function should return both the sequence and the sum of all the terms in the sequence up to the nth term. The function should have a time complexity of O(n) and should not use any loops or temporary variables. ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def fibonacci_sequence(n: int) -> Tuple[List[int], int]: """ Generate the Fibonacci sequence up to the nth term using a recursive approach. Returns both the sequence and the sum of all the ter...
python_sharegpt_23k
4,662
Write a Python function to retrieve the values of all the leaf nodes from a deeply nested JSON structure. The function should handle any level of nesting and return a list of all leaf node values. The function should also ensure that it can handle and recover from any errors that may occur during the retrieval process,...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def retrieve_leaf_nodes(json_structure: Any) -> List[Any]: """ Retrieve the values of all the leaf nodes from a deeply nested JSON structure. Args: json_structure (Any): The JSON struct...
python_sharegpt_23k
4,679
Write a Python function to calculate the square of a given integer without using the multiplication operator (`*`). Instead, implement your own algorithm to calculate the square. The function should handle both positive and negative integers. The code should start with: ```python def calculate_square(n: int) -> int: `...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_square(n: int) -> int: """ Calculate the square of a given integer without using the multiplication operator (*). Args: n (int): The integer for which to calculate the square. Returns: int...
python_sharegpt_23k
4,681
Write a Python function to calculate the sum, average, maximum, and minimum of elements in a given array of integers. The function should use a single loop to achieve this and should not use any built-in array methods or functions. The time complexity of the solution must be O(n), where n is the length of the input arr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def calculate_stats(arr: List[int]) -> Tuple[int, float, int, int]: """ Calculate the sum, average, maximum, and minimum of elements in a given array of integers. Args: arr (List[int]...
python_sharegpt_23k
4,684
Write a Python function to count the total number of elements in a given list, and separately count the number of prime and composite numbers in the list. The function should return a tuple containing the total number of elements, the number of prime numbers, and the number of composite numbers. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import math def count_elements_and_primes(mylist: List[int]) -> Tuple[int, int, int]: """ Counts the total number of elements in the list, and separately counts the number of prime and composite n...
python_sharegpt_23k
4,694
Write a Python function to process a given string based on a flag. The flag can either be `True` or `False`. If the flag is `True`, the function should print the string in reverse order after removing any duplicate characters. If the flag is `False`, the function should print the string in reverse order without removin...
Here is the Python code to solve the given problem in the markdown block: ```python def process_string(flag: bool, input_string: str) -> None: """ Process a given string based on a flag. Args: flag (bool): A boolean flag that controls the output. input_string (str): The string to be process...
python_sharegpt_23k
4,695
Write a Python function to merge two sorted arrays into one sorted array without using any built-in sorting functions. The two input arrays are `arr1` and `arr2`. Your task is to create a new array that contains all the elements from `arr1` and `arr2`, in sorted order. The new array should have a time complexity of O(n...
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 one sorted array without duplicates. Args: arr1 (List[int]): The first sorted array. ...