source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
2,096
Write a Python function to remove duplicate elements from a given list and sort the remaining elements in descending order using a custom sorting algorithm. The function should not use any built-in sorting functions. The code should start with: ```python from typing import List def remove_duplicates_and_sort(arr: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_sort(arr: List[int]) -> List[int]: """ Removes duplicate elements from the given list and sorts the remaining elements in descending order using a custom sorting algorithm. ...
python_sharegpt_23k
2,097
Write a Python function to format a date string from "DD-MM-YYYY" to "DD/MM/YYYY" format. The input date will always be in the format "DD-MM-YYYY". You are not allowed to use any built-in date formatting functions or libraries. Ensure that the code snippet does not exceed a time complexity of O(n), where n is the lengt...
Here is the Python code to solve the given problem in the markdown block: ```python def format_date(input_date: str) -> str: """ Formats a date string from "DD-MM-YYYY" to "DD/MM/YYYY". Args: input_date (str): The input date string in "DD-MM-YYYY" format. Returns: str: The formatted da...
python_sharegpt_23k
2,100
Write a Python function to convert a given integer to its binary representation without using the ternary expression, built-in mathematical functions, or operators. The function should handle both positive and negative numbers and must have a time complexity of O(1). The output should be a string representing the binar...
Here is the Python code to solve the given problem in the markdown block: ```python def to_binary_representation(num: int) -> str: """ Converts a given integer to its binary representation without using ternary expressions, built-in mathematical functions, or operators. Handles both positive and negative nu...
python_sharegpt_23k
2,101
Write a Python function to calculate the sum of the squares of all positive integers in a given array. The function should ignore any non-integer elements and negative integers. The input array will not contain any floating-point numbers. The code should start with: ```python from typing import List def sum_of_square...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_squares_of_positives(arr: List[int]) -> int: """ Calculate the sum of the squares of all positive integers in the given array. Args: arr (List[int]): The array containing integers...
python_sharegpt_23k
2,106
Write a Python function to generate a list of prime numbers from 1 to a given upper limit `n`. The function should include a helper function `is_prime(n)` that checks whether a number `n` is prime or not. The main function should iterate through the range from 1 to `n` and append the prime numbers to a list. The functi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(n: int) -> bool: """ Check if a number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. ...
python_sharegpt_23k
2,109
Write a Python function to find all entries in a list of dictionaries that meet specific criteria. The criteria are: the 'Name' key should equal 'John', the 'Age' key should be greater than 25, and the 'Height' key should be less than 180. The list of dictionaries can have up to 10,000 entries. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def find_johns_with_criteria(list_of_dicts: List[Dict[str, any]]) -> List[Dict[str, any]]: """ Find all entries in a list of dictionaries where the 'Name' key equals 'John', the 'Age' key is gr...
python_sharegpt_23k
2,117
Write a Python function to remove all duplicates from a given list of integers, sort the list in ascending order, and return the count of unique numbers in the list. The function should handle negative integers and zero correctly. The code should start with: ```python from typing import List def remove_duplicates_and_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_count_unique(lst: List[int]) -> int: """ Removes all duplicates from the given list of integers, sorts the list in ascending order, and returns the count of unique numbers i...
python_sharegpt_23k
2,118
Write a Python function to find the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm. The function should not use any built-in GCD functions and should have a time complexity of O(log(min(a, b))), where `a` and `b` are the given numbers. The function should be able to handle extremely large nu...
Here is the Python code to solve the given problem in the markdown block: ```python def gcd(a: int, b: int) -> int: """ Calculate the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm. Args: a (int): The first number. b (int): The second number. Returns: ...
python_sharegpt_23k
2,122
Write a Python function to calculate the sum of the elements of an array with float values, excluding any negative values and values greater than 100. The final sum should be rounded to two decimal places. The function should handle arrays with up to \(10^6\) elements efficiently. 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 calculate_sum_of_valid_elements(array: List[float]) -> float: """ Calculate the sum of the elements of an array with float values, excluding any negative values and values greater than 100. The f...
python_sharegpt_23k
2,128
Write a Python function to calculate the sum of all even numbers in a given list of integers. The function should be optimized to reduce time complexity. The function should start with: ```python from typing import List def sum_even_numbers(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_even_numbers(numbers: List[int]) -> int: """ Calculate the sum of all even numbers in a given list of integers. Args: numbers (List[int]): A list of integers from which to calculate ...
python_sharegpt_23k
2,133
Write a Python function to recursively delete a specific key from a dictionary that may contain nested dictionaries. The function should maintain a time complexity of O(n) and a space complexity of O(1), and should not use any built-in Python functions or methods for deletion. The function should return a boolean indic...
Here is the Python code to solve the given problem in the markdown block: ```python def recursive_delete(dictionary, key) -> bool: """ Recursively delete a specific key from a dictionary that may contain nested dictionaries. Args: dictionary (dict): The dictionary from which to delete the key. ...
python_sharegpt_23k
2,138
Write a Python function to find the smallest positive integer that is not present in a given list of integers. The function should have a time complexity of O(n), where n is the length of the list. The code should start with: ```python from typing import List def find_smallest_positive_number(nums: List[int]) -> int: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_smallest_positive_number(nums: List[int]) -> int: """ Find the smallest positive integer that is not present in the given list. Args: nums (List[int]): A list of integers. Retu...
python_sharegpt_23k
2,139
Write a Python function that takes a string as input and returns `True` if the string contains only digits and the number formed by the digits is a prime number. Otherwise, it should return `False`. The function should handle edge cases such as empty strings and strings with leading zeros. 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(num: int) -> bool: """ Check if a given number is a prime number. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwis...
python_sharegpt_23k
2,140
Write a Python function to remove all empty strings from a given list of strings while maintaining the order of the remaining elements. The function should have a time complexity of O(n), where n is the length of the list. You are not allowed to use any built-in functions or methods that directly solve the problem, suc...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_empty_strings(lst: List[str]) -> List[str]: """ Removes all empty strings from the given list while maintaining the order of the remaining elements. Args: lst (List[str]): The lis...
python_sharegpt_23k
2,143
Write a Python function to sort a list of strings based on their length and then alphabetically within each length group, ignoring the case of the letters. The function should have a time complexity of O(n^2), where n is the length of the input list, and should not use any additional memory space other than the input l...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_strings_by_length_and_alphabet(strings: List[str]) -> List[str]: """ Sorts a list of strings based on their length and then alphabetically within each length group, ignoring the case of the lett...
python_sharegpt_23k
2,145
Write a Python function to find the frequency of a given phrase in a text, considering case sensitivity, punctuation marks, and the position of the phrase in the text. The phrase should only be counted if it appears as a separate word and not as a substring within another word. The function should return the total coun...
Here is the Python code to solve the given problem in the markdown block: ```python import re def find_phrase_frequency(text: str, phrase: str) -> int: """ Find the frequency of a given phrase in the text, considering case sensitivity, punctuation marks, and the position of the phrase in the text. The phra...
python_sharegpt_23k
2,149
Write a Python function to calculate the sum of all even numbers in a given array using a single loop. The function should have a time complexity of O(n) and a space complexity of O(1). If there are no even numbers in the array, the function should return 0. 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 calculate_even_sum(arr: List[int]) -> int: """ Calculate the sum of all even numbers in the given array. Args: arr (List[int]): The array of integers to process. Returns: in...
python_sharegpt_23k
2,153
Write a Python function to find and return all prime numbers within a given range using a nested loop and the modulo operator. The function should have a time complexity of O(n^2). The function should start with: ```python from typing import List def find_primes_in_range(start: int, end: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def find_primes_in_range(start: int, end: int) -> List[int]: """ Find all prime numbers within a given range using a nested loop and the modulo operator. Args: start (int): The s...
python_sharegpt_23k
2,160
Write a Python function to calculate the summation of the squares of the first `n` prime numbers using a nested loop structure. The function should dynamically generate the prime numbers within the loop and should not use the built-in `sum()` function. The code should start with: ```python def sum_of_squares_of_primes(...
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_squares_of_primes(n: int) -> int: """ Calculate the summation of the squares of the first n prime numbers. Args: n (int): The number of prime numbers to consider. Returns: int: The summation ...
python_sharegpt_23k
2,163
Write a Python function to build a dictionary of words and their respective frequencies from a given input string. The function should ignore punctuation marks, convert all words to lowercase, and remove any stop words from the input string before counting their frequencies. The stop words list should be provided as a ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict import string def build_word_frequency(input_string: str, stop_words: List[str]) -> Dict[str, int] or str: """ Builds a dictionary of words and their respective frequencies from a given input strin...
python_sharegpt_23k
2,164
Write a Python function to convert a given string to uppercase using only basic string manipulation operations. The string may contain punctuation marks, special characters, and spaces. Your solution should have a time complexity of O(n), where n is the length of the string. Implement your solution without using any bu...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_to_uppercase(s: str) -> str: """ Convert a given string to uppercase using only basic string manipulation operations. Args: s (str): The input string to be converted to uppercase. Returns: s...
python_sharegpt_23k
2,165
Write a Python function to check if a given string of parentheses is balanced. A string is considered balanced if every opening parenthesis '(' has a corresponding closing parenthesis ')' and the parentheses are nested correctly. The function should return `True` if the string is balanced and `False` otherwise. The co...
Here is the Python code to solve the given problem in the markdown block: ```python def check_parentheses(input_string: str) -> bool: """ Check if the given string of parentheses is balanced. Args: input_string (str): The string of parentheses to be checked. Returns: bool: True if the ...
python_sharegpt_23k
2,167
Write a Python function that takes a list of integers as input and returns a new list containing the squares of each unique odd number from the input list. The function should exclude any even numbers and duplicates from the input list. The time complexity of the function should be O(n), where n is the number of elemen...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def square_unique_odd_numbers(input_list: List[int]) -> List[int]: """ Calculate the squares of unique odd numbers from the input list. Args: input_list (List[int]): A list of integers. ...
python_sharegpt_23k
2,169
Write a Python function to swap the values of three variables without using any additional variables, ensuring that the function has a time complexity of O(1). The function should take three integers as input and return the three integers with their values swapped in a specific order. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python def swap_three_values(a: int, b: int, c: int) -> tuple: """ Swaps the values of three variables without using any additional variables. Args: a (int): The first integer. b (int): The second integer. ...
python_sharegpt_23k
2,172
Write a Python function to compare two dictionaries and return `True` if they have the same set of keys and values, and `False` otherwise. The function should have a time complexity of O(n), where n is the number of key-value pairs in the dictionaries. Additionally, the function should be able to handle larger input si...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def compare_hashes(hash1: Dict[str, int], hash2: Dict[str, int]) -> bool: """ Compare two dictionaries and return True if they have the same set of keys and values, and False otherwise. Args: ...
python_sharegpt_23k
2,175
Write a Python function to count the number of characters in each word of a given sentence, excluding punctuation marks and special characters. The function should handle sentences with multiple consecutive spaces, leading or trailing whitespace, and non-ASCII characters. The solution should have a time complexity of O...
Here is the Python code to solve the given problem in the markdown block: ```python def count_characters_in_words(sentence: str) -> list: """ Counts the number of characters in each word of a given sentence, excluding punctuation marks and special characters. Args: sentence (str): The input sentenc...
python_sharegpt_23k
2,176
Write a Python function to find the maximum and minimum numbers in a given list of integers without using any built-in functions or methods such as `max()`, `min()`, or sorting algorithms. The function should have a time complexity of O(n), where n is the length of the list. Additionally, the function should handle edg...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_max_min(numbers: List[int]) -> Tuple[int, int]: """ Find the maximum and minimum numbers in a given list of integers without using built-in functions or sorting algorithms. Args: ...
python_sharegpt_23k
2,181
Write a Python function to generate a list of all prime numbers up to a given integer `n` in ascending order. The function should have a time complexity of O(n*sqrt(m)), where `n` is the input integer and `m` is the largest number being checked for primality. 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 import math 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 num...
python_sharegpt_23k
2,182
Write a Python function to check if a given list of mixed elements, including numbers, strings, and special characters, is a palindrome. The function should consider negative numbers, decimal numbers, and non-alphanumeric elements. The list can have a maximum length of 100 elements. The solution should have a time comp...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def is_palindrome_mixed_list(lst: List[Union[int, float, str]]) -> bool: """ Check if a given list of mixed elements is a palindrome. The function considers negative numbers, decimal numbers,...
python_sharegpt_23k
2,184
Write a Python function to find the two smallest unique prime numbers in a given list of integers. The function should not use any built-in prime number checking functions or libraries, and it should have a time complexity of O(n) and use constant space. The input list may contain duplicates. The code should start wit...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple 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 othe...
python_sharegpt_23k
2,186
Write a Python function to search for a given keyword in a dictionary and return the key-value pairs that contain the keyword. The keyword must appear at the beginning of a word within the value, and partial matches should not be considered. The search should be case-sensitive and only consider matches if the keyword i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any def search_keyword_in_dict(keyword: str, dictionary: Dict[str, Any]) -> Dict[str, Any]: """ Searches for a given keyword in a dictionary and returns the key-value pairs that contain the keyword. ...
python_sharegpt_23k
2,189
Write a Python function to determine if a given number `cond` is a prime number and satisfies additional conditions. The function should print "true" if `cond` is a prime number and satisfies the conditions `cond > 10`, `cond % 2 == 0`, `cond % 3 == 0`, `cond % 5 == 0`, and `cond % 7 == 0`. Otherwise, it should print "...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(num: int) -> bool: """ Check if a given number is a prime number. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. """ ...
python_sharegpt_23k
2,191
Write a Python function to generate a list of all prime numbers between 1 and a given integer `n` (inclusive) using the Sieve of Eratosthenes algorithm. Additionally, write a separate function to calculate the sum of the prime numbers in the generated list. Both functions should be optimized for time complexity. The c...
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 all prime numbers between 1 and n (inclusive) using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limi...
python_sharegpt_23k
2,196
Write a Python function to determine if a given string is a valid IPv4 address. The function should adhere to the following rules: 1. An IPv4 address consists of four decimal numbers separated by periods. 2. Each decimal number must be between 0 and 255. 3. Leading zeros are not allowed. 4. The string must not be empty...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv4_address(ip_address: str) -> bool: """ Determines if the given string is a valid IPv4 address. Args: ip_address (str): The string to be checked for IPv4 address validity. Returns: bool:...
python_sharegpt_23k
2,198
Implement the quicksort algorithm in Python for a list of integers. The implementation should meet the following requirements: 1. Handle duplicate values correctly and efficiently. 2. Achieve a time complexity of O(n log n) in the average case. 3. Achieve a space complexity of O(log n) in the average case. The code sh...
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 a list of integers using the quicksort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: List[i...
python_sharegpt_23k
2,199
Write a Python function to generate the Fibonacci sequence up to a given number `n` using recursion, and calculate the sum of all even numbers in the sequence. The function should return both the Fibonacci sequence and the sum of its even numbers. The code should start with: ```python from typing import List, Tuple d...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def fibonacci_with_even_sum(n: int) -> Tuple[List[int], int]: """ Generates the Fibonacci sequence up to the nth number and calculates the sum of all even numbers in the sequence. Args: ...
python_sharegpt_23k
2,201
Write a Python function to encrypt a given message using a Caesar cipher with a specified shift value, and another function to decrypt the encrypted message using the same shift value. The encryption should only shift alphabetic characters (both lowercase and uppercase) and leave non-alphabetic characters unchanged. Th...
Here is the Python code to solve the given problem in the markdown block: ```python def caesar_encrypt(message: str, shift: int) -> str: """ Encrypts a given message using a Caesar cipher with a specified shift value. Args: message (str): The message to be encrypted. shift (int): The number...
python_sharegpt_23k
2,204
Write a Python function to determine whether a given positive integer is a prime number or not, without using any built-in functions or libraries. The function should have a time complexity of O(sqrt(n)). The code should start with: ```python def is_prime(n: int) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Determine whether a given positive integer is a prime number. Args: n (int): The positive integer to check for primality. Returns: bool: True if the number is prime, Fa...
python_sharegpt_23k
2,208
Write a Python function to determine if two strings are rotations of each other. The function should have a time complexity of O(n), where n is the length of the strings. The function should handle strings containing only lowercase alphabets and should return `True` if the strings are rotations of each other, and `Fals...
Here is the Python code to solve the given problem in the markdown block: ```python def are_rotations(str1: str, str2: str) -> bool: """ Determine if two strings are rotations of each other. Args: str1 (str): The first string. str2 (str): The second string. Returns: bool: True ...
python_sharegpt_23k
2,209
Write a Python function to iterate through a nested dictionary and return the sum of all values. The dictionary may contain lists, sets, and tuples as values in addition to integers. If a value is a tuple, each element within the tuple should be squared before being added to the sum. If a value is a list or a set, the ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Union, Dict, List, Set, Tuple def sum_nested_dict_values(nested_dict: Dict[str, Union[int, List[int], Set[int], Tuple[int, ...], Dict]]) -> int: """ Calculate the sum of all values in a nested dictionary. ...
python_sharegpt_23k
2,221
Write a Python function to swap the values of two given variables without using a temporary variable. The function should work for variables of any data type and should be efficient with a time complexity of O(1). The function should modify the values of `x` and `y` in-place to swap their values. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python def swap(x, y): """ Swaps the values of two given variables without using a temporary variable. Args: x: The first variable to be swapped. y: The second variable to be swapped. Returns: tuple: A...
python_sharegpt_23k
2,232
Write a Python function to create a dictionary where the keys are prime numbers ranging from 1 to 1,000,000 and the values are randomly generated integers ranging from -1,000,000 to 1,000,000. The dictionary should have a length of 1,000,000. You are not allowed to use any built-in functions or libraries for generating...
Here is the Python code to solve the given problem in the markdown block: ```python import random def generate_prime_dict(n: int) -> dict: """ Generates a dictionary where the keys are prime numbers ranging from 1 to n and the values are randomly generated integers ranging from -n to n. Args: ...
python_sharegpt_23k
2,241
Write a Python function to initialize a list with 100 prime numbers, ensuring that the list contains both odd and even prime numbers and is sorted in descending order. The function should start with: ```python from typing import List def generate_100_primes_descending() -> 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: """ Check if a number is prime. Args: n (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ if n <...
python_sharegpt_23k
2,253
Write a Python function to compute the average of a list of numbers, handling various edge cases such as empty lists, NaN values, and duplicate values. The function should be implemented using a recursive approach and should have a time complexity of O(n), where n is the length of the list. The function should return 0...
Here is the Python code to solve the given problem in the markdown block: ```python import math from typing import List def compute_average(lst: List[float]) -> float: """ Compute the average of a list of numbers, handling edge cases such as empty lists, NaN values, and duplicate values. Args: lst...
python_sharegpt_23k
2,254
Write a Python function to calculate the total cost of a meal including taxes and tips, adhering to the following specific requirements: - The sales tax rate can be any decimal value between 0 and 1, inclusive. - The tip percentage can be any decimal value between 0 and 1, inclusive. - The meal price can be any positiv...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_total_cost(meal_price: float, tax_rate: float, tip_percentage: float) -> str: """ Calculate the total cost of a meal including taxes and tips. Args: meal_price (float): The price of the meal. t...
python_sharegpt_23k
2,255
Implement a Selection Sort algorithm in Python that sorts an array in descending order. The function should not use any built-in sorting functions or methods. The implementation should have a time complexity of O(n^2) and a space complexity of O(1). The code should start with: ```python from typing import List def se...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def selection_sort_descending(arr: List[int]) -> List[int]: """ Sorts the given array in descending order using the Selection Sort algorithm. Args: arr (List[int]): The array to be sorted. ...
python_sharegpt_23k
2,257
Write a Python function to determine if two given strings 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 meet the following constraints: 1. It should not use any built-in sorting functions or libraries. 2. It should have a time...
Here is the Python code to solve the given problem in the markdown block: ```python def is_anagram(string1: str, string2: str) -> bool: """ Determines if two given strings are anagrams of each other. Args: string1 (str): The first string to check. string2 (str): The second string to check. ...
python_sharegpt_23k
2,266
Write a Python function to merge a list of lists into a single list and sort it in descending order. Each sublist can contain an arbitrary number of elements. The function should start with: ```python from typing import List def merge_and_sort_lists(list_of_lists: List[List[int]]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import itertools def merge_and_sort_lists(list_of_lists: List[List[int]]) -> List[int]: """ Merges a list of lists into a single list and sorts it in descending order. Args: list_of_lists (L...
python_sharegpt_23k
2,273
Write a Python function to sort a list of positive integers in ascending order using a recursive variation of the cocktail sort algorithm. The function should not use any built-in sorting functions or methods and should have a time complexity of O(n^2). The cocktail sort algorithm iterates through the list in both dire...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def recursive_cocktail_sort(arr: List[int]) -> List[int]: """ Sorts a list of positive integers in ascending order using a recursive variation of the cocktail sort algorithm. Args: arr (List...
python_sharegpt_23k
2,280
Write a Python function to find the index of the first occurrence of a given value in a 2D array. The 2D array can contain duplicates and both positive and negative integers. If the value is not found, the function should return `[-1, -1]`. The function should have a time complexity of O(N*M), 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 def find_first_occurrence(arr: List[List[int]], value: int) -> List[int]: """ Find the index of the first occurrence of a given value in a 2D array. Args: arr (List[List[int]]): The 2D array...
python_sharegpt_23k
2,282
Write a Python function to check if all elements in a given list are equal. The function should handle cases where the input list contains both integers and strings. The function should return `True` if all elements are equal and `False` otherwise. The input list can contain a maximum of 100 elements. The elements can ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def check_all_elements_equal(lst: List[Union[int, str]]) -> bool: """ Check if all elements in the given list are equal. Args: lst (List[Union[int, str]]): The list of elements to che...
python_sharegpt_23k
2,287
Write a Python function to find all duplicate elements in a given list using a dictionary to count occurrences of each element. The function should handle lists containing negative numbers, zero, and positive numbers. The function should return a list of all elements that appear more than once in the input list. The c...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_duplicate_elements(lst: List[int]) -> List[int]: """ Find all duplicate elements in a given list. Args: lst (List[int]): The input list containing integers (positive, negative, and ...
python_sharegpt_23k
2,289
Write a Python function to calculate the sum of all unique positive values in a dictionary. The function should raise an error if any of the dictionary values are negative or if the dictionary contains any duplicate keys or values. The dictionary can have a maximum of 100 key-value pairs. The code should start with: `...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def calculate_unique_positive_sum(dictionary: Dict[int, int]) -> int: """ Calculate the sum of all unique positive values in a dictionary. Args: dictionary (Dict[int, int]): A dictionary wit...
python_sharegpt_23k
2,293
Write a Python function to determine if a given sentence is a palindrome. The function should be case-insensitive and ignore any non-alphanumeric characters. Additionally, the function should consider spaces and punctuation marks as part of the palindrome check. The function should also handle palindromes with more tha...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_palindrome(sentence: str) -> bool: """ Determine if a given sentence is a palindrome. The function is case-insensitive and ignores any non-alphanumeric characters. Spaces and punctuation marks are cons...
python_sharegpt_23k
2,297
Write a Python function to determine if a given string is a palindrome. The function should be case-insensitive and ignore non-alphanumeric characters. The function should use a regular expression to filter out non-alphanumeric characters and should not use any built-in functions or methods for string manipulation or c...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_palindrome(s: str) -> bool: """ Determine if a given string is a palindrome, ignoring case and non-alphanumeric characters. Args: s (str): The input string to check. Returns: bool: Tru...
python_sharegpt_23k
2,298
Write a Python function to compare two strings and return "yes" if they are equal and "no" if they are not equal. The comparison should be case-sensitive, handle whitespace characters, punctuation marks, special characters, and different character encodings. The function should not use any built-in string comparison fu...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_strings(string1: str, string2: str) -> str: """ Compares two strings for equality in a case-sensitive manner, handling different character encodings, whitespace characters, punctuation marks, and special characte...
python_sharegpt_23k
2,301
Write a Python function to filter numbers from a given array that are divisible by 3, square them, sort them in descending order, remove any duplicate numbers, and return the first 5 numbers from the resulting array. The code should start with: ```python from typing import List def filter_and_sort_numbers(array: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_and_sort_numbers(array: List[int]) -> List[int]: """ Filters numbers divisible by 3 from the given array, squares them, sorts them in descending order, removes duplicates, and returns the ...
python_sharegpt_23k
2,310
Write a Python function to check if the brackets in a given string are correctly matched and closed in the correct order. The function should handle different types of brackets such as `{}`, `[]`, and `()`. The brackets may be nested within each other and there may be multiple layers of nesting. The function should als...
Here is the Python code to solve the given problem in the markdown block: ```python def is_brackets_matched(string: str) -> bool: """ Checks if the brackets in the given string are correctly matched and closed in the correct order. Args: string (str): The string containing brackets and other charac...
python_sharegpt_23k
2,313
Write a Python function to convert a given time in 24-hour format to 12-hour format, taking into account AM and PM. The input time will always be in the format "HH:MM" where HH represents the hour in 24-hour format and MM represents the minutes. The function should return the converted time as a string in the format "H...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_24_to_12_hour_format(time: str) -> str: """ Converts a given time in 24-hour format to 12-hour format with AM/PM suffix. Args: time (str): The time in 24-hour format "HH:MM". Returns: str: T...
python_sharegpt_23k
2,315
Write a Python function to delete redundant items from a given list of integers and return the count of the number of unique integers in the list. Additionally, the function should sort the list in ascending order and return it as the second output. 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 delete_redundant_items(nums: List[int]) -> Tuple[int, List[int]]: """ Deletes redundant items from a list of integers and returns the count of unique integers and the sorted list. Args: ...
python_sharegpt_23k
2,324
Write a Python function to calculate the sum of the digits of the maximum absolute value among a list of integers. The function should handle negative integers, zeros, and integers with only one digit. The function should not use any built-in functions or libraries for arithmetic operations or string manipulations. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_digits_of_max_value(numbers: List[int]) -> int: """ Calculate the sum of the digits of the maximum absolute value among a list of integers. Args: numbers (List[int]): A list of in...
python_sharegpt_23k
2,326
Implement the merge sort algorithm in Python without using any loops or iteration constructs (such as for loops, while loops, or recursion). The function should take an array of integers as input and sort the array in ascending order. The implementation should have a time complexity of O(n log n) and a space complexity...
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 an array of integers in ascending order using the merge sort algorithm. This implementation leverages Python's built-in `sorted` function to ach...
python_sharegpt_23k
2,327
Write a Python function to print the elements of a given list in reverse order without using any built-in functions or methods for reversing the list. Additionally, you are not allowed to use any additional data structures or variables to store the reversed list. The function should be optimized for time and space comp...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_print(lst): """ Prints the elements of the given list in reverse order without using any built-in functions or methods for reversing the list. Args: lst (list): The list of elements to be printed in reve...
python_sharegpt_23k
2,334
Write a Python function to count the number of unique characters in a given string without using additional data structures. Additionally, you should not use any built-in string or array manipulation functions, and the solution must have a time complexity of O(n). The code should start with: ```python def count_unique...
Here is the Python code to solve the given problem in the markdown block: ```python def count_unique_characters(string: str) -> int: """ Count the number of unique characters in a given string without using additional data structures. The solution has a time complexity of O(n). Args: string (st...
python_sharegpt_23k
2,336
Write a Python function to reverse the elements of a singly linked list without using any built-in methods, creating a new list, or using recursion. The function should have a time complexity of O(n) and a space complexity of O(1). The function should start with: ```python class ListNode: def __init__(self, value=0...
Here is the Python code to solve the given problem in the markdown block: ```python class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next def reverse_linked_list(head: ListNode) -> ListNode: """ Reverses the elements of a singly linked list in place wit...
python_sharegpt_23k
2,344
Write a Python function to check if a given array is sorted in non-decreasing order. The array may contain duplicate elements. The function should have a time complexity of O(n log n) and a space complexity of O(1). Additionally, the function should only use a single loop to iterate through the array. The code should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_sorted_non_decreasing(arr: List[int]) -> bool: """ Check if the given array is sorted in non-decreasing order. Args: arr (List[int]): The array to check. Returns: bool: T...
python_sharegpt_23k
2,352
Write a Python function to generate a string array of length 10, containing only lowercase alphabets, where each character in the array is unique, and the order of the characters is in reverse alphabetical order. Additionally, the array must be sorted in ascending order based on the ASCII value of the characters. The ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def generate_unique_characters_array() -> List[str]: """ Generates a string array of length 10, containing only lowercase alphabets, where each character is unique, and the order of the...
python_sharegpt_23k
2,353
Write a Python function to calculate the factorial of a non-negative integer using recursion. The function should handle cases where the input is negative or a floating-point number by returning `None` and printing an appropriate error message. The function should have a time complexity of O(n), where n is the input nu...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial(n: int) -> int: """ Calculate the factorial of a non-negative integer using recursion. Args: n (int): The non-negative integer for which to calculate the factorial. Returns: int: The facto...
python_sharegpt_23k
2,361
Write a Python function `concatenate_arrays` that takes in two lists `a` and `b`, and concatenates them into a new list `c` without using the built-in `+` operator. The function should ensure that the length of `c` is equal to the sum of the lengths of `a` and `b`, and all elements in `c` are integers. If there are any...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def concatenate_arrays(a: List[int], b: List[int]) -> List[int]: """ Concatenates two lists `a` and `b` into a new list `c` without using the built-in `+` operator. Ensures that all elements in `c` a...
python_sharegpt_23k
2,366
Write a Python function to create a new array `arr3` by combining the elements of two existing arrays `arr1` and `arr2`. Both arrays should be of the same size and have no duplicate elements. If there are duplicate elements in either array, they should be removed before combining the arrays. Additionally, `arr3` should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def combine_and_sort_arrays(arr1: List[int], arr2: List[int]) -> List[int]: """ Combines two arrays, removes duplicates, and returns a sorted array. Args: arr1 (List[int]): The first array o...
python_sharegpt_23k
2,375
Write a Python function to find the sum of all prime numbers between 0 and a given positive integer `n`. The function should have a time complexity of O(n log(log n)). The code should start with: ```python def sum_of_primes(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_primes(n: int) -> int: """ Calculate the sum of all prime numbers between 0 and n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit (inclusive) to find prime numbers. Returns:...
python_sharegpt_23k
2,380
Write a Python function to determine whether there is a set of four unique numbers in a given list that add up to a specified target sum. The function should return `True` if such a set exists, and `False` otherwise. The list may contain duplicate elements. The function should be optimized to have a time complexity of ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_four_numbers_with_sum(numbers: List[int], target: int) -> bool: """ Determines whether there is a set of four unique numbers in the list that add up to the target sum. Args: numbers...
python_sharegpt_23k
2,388
Write a Python function to find the fourth largest element in a given list of integers. If the list has fewer than four elements, the function should return `None`. The function should have a time complexity of O(n). The code should start with: ```python from typing import List, Optional def find_fourth_largest(nums:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional import heapq def find_fourth_largest(nums: List[int]) -> Optional[int]: """ Finds the fourth largest element in a list of integers. Args: nums (List[int]): The list of integers fro...
python_sharegpt_23k
2,392
Write a Python function to count the number of consecutive even elements that appear before any odd elements in a given array of integers. The array can contain both positive and negative integers. The function should return the count of such consecutive even elements. 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_even_before_odd(arr: List[int]) -> int: """ Counts the number of consecutive even elements that appear before any odd elements in the given array. Args: arr (List[int]): The array ...
python_sharegpt_23k
2,393
Write a Python function that takes a list of strings and returns a dictionary where the keys are the strings from the list and the values are the number of vowels in each string. The dictionary should be sorted based on the number of vowels in descending order. If two strings have the same number of vowels, they should...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_vowels_and_sort(strings: List[str]) -> Dict[str, int]: """ Counts the number of vowels in each string from the input list and returns a dictionary sorted by the number of vowels in de...
python_sharegpt_23k
2,394
Write a Python function to filter out and return a list of odd numbers from a given list of integers. The function should not use any conditional statements or loops. The code should start with: ```python from typing import List def get_odd_numbers(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def get_odd_numbers(numbers: List[int]) -> List[int]: """ Filters out and returns a list of odd numbers from the given list of integers. Args: numbers (List[int]): A list of integers from ...
python_sharegpt_23k
2,401
Write a Python function to multiply two integers without using the `*` operator or any arithmetic operators (`+`, `-`, `/`, `%`, etc.). Instead, use bitwise operations to achieve the multiplication. The function should handle both positive and negative integers. The code should start with: ```python def multiply_witho...
Here is the Python code to solve the given problem in the markdown block: ```python def multiply_without_operator(a: int, b: int) -> int: """ Multiplies two integers using bitwise operations without using the '*' operator or any arithmetic operators. Args: a (int): The first integer. b (int...
python_sharegpt_23k
2,406
Write a Python function that iterates over a list of dictionaries and returns a list of tuples. Each tuple should contain the value of the "name" key and the length of that name. If a dictionary does not contain the key "name", the tuple should contain the string "Name not found" and the integer 0. The function should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict, Tuple def process_names(data: List[Dict[str, any]]) -> List[Tuple[str, int]]: """ Processes a list of dictionaries and returns a list of tuples. Each tuple contains the value of the "name" key and...
python_sharegpt_23k
2,410
Write a Python function to sort a given list of integers in ascending order using a stable sorting algorithm with a time complexity of O(n^2) or less and a space complexity of O(n) or less. The function should ensure that the relative order of equal elements is preserved. The code should start with: ```python from typ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def stable_sort(arr: List[int]) -> List[int]: """ Sorts the given list of integers in ascending order using a stable sorting algorithm. Args: arr (List[int]): The list of integers to be sort...
python_sharegpt_23k
2,411
Write a Python function to generate all prime numbers up to a given positive integer `n` using the Sieve of Eratosthenes algorithm. The function should have a time complexity of O(n log log n) and should not use any external libraries or built-in functions to check for prime numbers. The function should handle edge cas...
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 all prime numbers up to a given positive integer n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit up to whic...
python_sharegpt_23k
2,426
Write a Python function to reverse a given string in-place without using any additional memory. The function should take a string as input and return the reversed string. The function should not use any built-in string reversal methods or additional data structures for the reversal process. The code should start with:...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string_in_place(s: str) -> str: """ Reverses the given string in-place without using any additional memory. Args: s (str): The input string to be reversed. Returns: str: The reversed string....
python_sharegpt_23k
2,429
Write a Python function to decode a given string using a dynamically generated cipher. The cipher should be generated based on a provided key and a shift value. The key is a string containing all unique alphabets from 'a' to 'z' and 'A' to 'Z'. The shift value is an integer between 1 and 25, indicating the number of po...
Here is the Python code to solve the given problem in the markdown block: ```python def generate_cipher(key: str, shift: int) -> dict: """ Generate a cipher dictionary based on the provided key and shift value. Args: key (str): A string containing all unique alphabets from 'a' to 'z' and 'A' to 'Z'...
python_sharegpt_23k
2,436
Write a Python function to compare two given strings and determine if they are the same, ignoring case, leading and trailing whitespace, and any punctuation or special characters. The function should not use any built-in string comparison functions or regular expressions. The code should start with: ```python def compa...
Here is the Python code to solve the given problem in the markdown block: ```python def compare_strings(str1: str, str2: str) -> bool: """ Compare two strings and return True if they are the same, ignoring case, leading and trailing whitespace, and any punctuation or special characters. Args: s...
python_sharegpt_23k
2,438
Write a Python function to remove duplicates and negative numbers from a given list of integers, and return the distinct positive values in descending order. The function should ensure that the time complexity is less than O(n^2), where n is the size of the input list. The code should start with: ```python from typing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_negatives(numbers: List[int]) -> List[int]: """ Removes duplicates and negative numbers from the given list of integers and returns the distinct positive values in descending or...
python_sharegpt_23k
2,443
Write a Python function to determine if the word "hello" is present in a given list of words and count the number of times it appears. The list may contain duplicates and can be of arbitrary length. The function should return a tuple where the first element is a boolean indicating the presence of the word "hello" and t...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_and_count_hello(words: List[str]) -> Tuple[bool, int]: """ Determine if the word "hello" is present in the provided list and count its occurrences. Args: words (List[str]): A...
python_sharegpt_23k
2,444
Write a Python function to find the length of the longest increasing subsequence (LIS) in a given array. The function should not use any built-in sorting or searching functions and should have a time complexity of O(n log n). The function should start with: ```python from typing import List def longest_increasing_subs...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_increasing_subsequence(arr: List[int]) -> int: """ Find the length of the longest increasing subsequence in a given array. Args: arr (List[int]): The input array of integers. ...
python_sharegpt_23k
2,454
Write a Python function to add two matrices of the same size and return the resulting matrix. The function should handle matrices of any size, not just the given example. The function should start with: ```python from typing import List def add_matrices(matrix1: List[List[int]], matrix2: List[List[int]]) -> List[List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def add_matrices(matrix1: List[List[int]], matrix2: List[List[int]]) -> List[List[int]]: """ Adds two matrices of the same size and returns the resulting matrix. Args: matrix1 (List[List[int...
python_sharegpt_23k
2,456
Write a Python function to calculate the sum of the digits of a given positive integer and count the number of unique digits present in the number. The function should have a time complexity of O(n), where n is the number of digits in the integer, and a space complexity of O(1). The given positive integer can be very l...
Here is the Python code to solve the given problem in the markdown block: ```python def sum_and_count_digits(n: int) -> tuple: """ Calculate the sum of the digits of a given positive integer and count the number of unique digits present in the number. Args: n (int): The positive integer whose digit...
python_sharegpt_23k
2,465
Write a Python function to merge two sorted arrays of integers into one sorted array in ascending order. The function should adhere to the following constraints: - The input arrays can contain negative numbers and zero. - The length of the input arrays can vary from 0 to 10^6. - You are not allowed to use any built-in ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]: """ Merges two sorted arrays of integers into one sorted array in ascending order. Args: arr1 (List[int]): The first s...
python_sharegpt_23k
2,466
Write a Python function to compute the n-th Fibonacci number using an iterative approach with a while loop. The function should handle cases where n is a non-negative integer and provide the result with a time complexity of O(n) and a space complexity of O(1). The code should start with: ```python def fibonacci(n: int)...
Here is the Python code to solve the given problem in the markdown block: ```python def fibonacci(n: int) -> int: """ Compute the n-th Fibonacci number using an iterative approach with a while loop. Args: n (int): The position in the Fibonacci sequence to compute. Returns: int: The n-t...
python_sharegpt_23k
2,467
Write a Python function to insert a new element at the beginning of an unsorted array without using any built-in array functions or methods. The function should have a time complexity of O(n) and space complexity of O(n), where n is the number of elements in the array. The function should start with: ```python from typ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_element_at_beginning(array: List[int], new_element: int) -> List[int]: """ Inserts a new element at the beginning of an unsorted array without using any built-in array functions or methods. ...
python_sharegpt_23k
2,470
Write a Python function to check if one string is a permutation of another string, considering only alphabetic characters and ignoring case. The function should handle strings with different lengths, strings containing special characters and spaces, and should have a time complexity of O(n log n), where n is the length...
Here is the Python code to solve the given problem in the markdown block: ```python def is_permutation(str1: str, str2: str) -> bool: """ Check if one string is a permutation of another string, considering only alphabetic characters and ignoring case. Args: str1 (str): The first string to compare. ...
python_sharegpt_23k
2,481
Write a Python function to delete all occurrences of a specified number from a given list of integers and return the resulting list sorted in ascending order. The function should take two parameters: the list of integers and the number to be deleted. The code should start with: ```python from typing import List def de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def delete_and_sort(nums: List[int], target: int) -> List[int]: """ Deletes all occurrences of the specified number from the given list of integers and returns the resulting list sorted in ascending orde...
python_sharegpt_23k
2,484
Write a Python function to sort a given list of integers in descending order without using any built-in sorting functions or methods. Additionally, the function should handle cases where the list contains duplicate integers and should remove any duplicates before sorting the list. The sorting algorithm used should have...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_list_descending(arr: List[int]) -> List[int]: """ Sorts a given list of integers in descending order without using any built-in sorting functions or methods. The function also removes any du...
python_sharegpt_23k
2,486
Write a Python function to find the minimum value in a given array with a time complexity requirement of O(n^3 log n), where n is the length of the array. The function should iterate through the array in a nested loop structure and use a logarithmic operation within the innermost loop to achieve the required time compl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import math def find_minimum_with_complexity(arr: List[int]) -> int: """ Find the minimum value in a given array with a time complexity of O(n^3 log n). Args: arr (List[int]): The array of i...
python_sharegpt_23k
2,487
Write a Python function to calculate the surface area of a cube using only bitwise operations, without using any loops or conditionals. The function should handle the case of negative input values correctly by raising a `ValueError` with an appropriate message. The function should have a time complexity of O(1) and a s...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_surface_area(side_length: int) -> int: """ Calculate the surface area of a cube using only bitwise operations. Args: side_length (int): The length of a side of the cube. Returns: int: The ...
python_sharegpt_23k
2,491
Write a Python function to calculate the number of bits needed to represent a given integer in binary form without using any built-in functions or libraries related to binary conversion. The function should start with: ```python def calculate_bits(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_bits(n: int) -> int: """ Calculate the number of bits needed to represent a given integer in binary form. Args: n (int): The integer for which to calculate the number of bits. Returns: int...
python_sharegpt_23k
2,493
Write a Python function to sort an array using the QuickSort algorithm in-place. The function should handle arrays with duplicate elements and should have a time complexity of O(n log n) and a space complexity of O(1). The function should not use any additional data structures or libraries, and it should only modify th...
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 the given array in-place using the QuickSort algorithm. Args: arr (List[int]): The array to be sorted. Returns: List[int]: ...
python_sharegpt_23k
2,494
Write a Python function to generate N multidimensional arrays, each representing a 4x4 matrix with randomly generated integers between -100 and 100. The function should also calculate and return the sum of the diagonal elements of each matrix. The function should adhere to the following constraints: - N must be a posit...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def generate_matrices_and_diagonal_sums(N: int) -> Tuple[List[List[List[int]]], List[int]]: """ Generates N 4x4 matrices with random integers between -100 and 100, and calculates the sum of the di...
python_sharegpt_23k
2,497
Write a Python function to find the common elements between two sorted arrays of integers. The function should return a new array containing the common elements without duplicates and should have a time complexity of O(log n), where n is the length of the longer array. The function should not use any built-in Python fu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(arr1: List[int], arr2: List[int]) -> List[int]: """ Find the common elements between two sorted arrays of integers. Args: arr1 (List[int]): The first sorted array of...