source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
6,695
Write a Python function to filter out elements from a list that are multiples of a given integer `k`, and return a new list containing the squares of the remaining elements. The function should start with: ```python from typing import List def filter_and_square(numbers: List[int], k: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_and_square(numbers: List[int], k: int) -> List[int]: """ Filters out elements from the list that are multiples of k and returns a new list containing the squares of the remaining elements. ...
python_sharegpt_23k
6,698
Write a Python function to compare two strings and determine if they are identical without using any built-in string comparison functions or libraries. The function should return `True` if the strings are identical and `False` otherwise. The code should start with: ```python def are_strings_identical(string1: str, stri...
Here is the Python code to solve the given problem in the markdown block: ```python def are_strings_identical(string1: str, string2: str) -> bool: """ Compare two strings to determine if they are identical without using built-in string comparison functions. Args: string1 (str): The first string to ...
python_sharegpt_23k
6,699
Write a Python function to convert a decimal number to its binary representation using bitwise operations. The function should take an integer as input and return its binary representation as a string. You are not allowed to use any built-in or library functions to convert the number. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_binary(num: int) -> str: """ Convert a decimal number to its binary representation using bitwise operations. Args: num (int): The decimal number to convert. Returns: str: The binary repre...
python_sharegpt_23k
6,703
Write a Python function to calculate the sum of each row in a two-dimensional array, but ignore any rows that contain negative numbers. The function should return a list of sums for the rows that do not contain any negative numbers. The code should start with: ```python from typing import List def sum_non_negative_ro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_non_negative_rows(matrix: List[List[int]]) -> List[int]: """ Calculate the sum of each row in a two-dimensional array, ignoring any rows that contain negative numbers. Args: matrix (...
python_sharegpt_23k
6,707
Write a Python function to efficiently select the last element of a given list with a time complexity of O(1). The function should handle cases where the list is empty by returning `None`. The code should start with: ```python from typing import List, Optional def get_last_element(lst: List[int]) -> Optional[int]: ``...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def get_last_element(lst: List[int]) -> Optional[int]: """ Efficiently selects the last element of a given list with a time complexity of O(1). If the list is empty, returns None. Args...
python_sharegpt_23k
6,709
Write a Python function to sort a given list of numbers from smallest to largest without using any built-in sorting functions or libraries. Implement the sorting algorithm manually. The code should start with: ```python from typing import List def custom_sort(lst: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def custom_sort(lst: List[int]) -> List[int]: """ Sorts a given list of numbers from smallest to largest using the Insertion Sort algorithm. Args: lst (List[int]): The list of integers to be...
python_sharegpt_23k
6,711
Write a Python function to transform a given list of integers into a dictionary. The dictionary should contain the numbers as keys and their squares as values. The list should not contain any duplicates and should be sorted in ascending order. The dictionary should only include numbers that are divisible by 2 and their...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def transform_list_to_dict(numbers: List[int]) -> Dict[int, int]: """ Transforms a given list of integers into a dictionary. The dictionary contains the numbers as keys and their squares as values....
python_sharegpt_23k
6,719
Write a Python function to convert a positive decimal number to its binary representation using only bitwise operators. The function should return the binary string in the correct order (most significant bit to least significant bit). The code should start with: ```python def decimal_to_binary(decimal_number: int) -> ...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_binary(decimal_number: int) -> str: """ Converts a positive decimal number to its binary representation using only bitwise operators. Args: decimal_number (int): The positive decimal number to convert...
python_sharegpt_23k
6,721
Write a Python function to calculate the average of all even numbers in a given list of integers. The function should handle lists of any size and return the average as a float. If the list contains no even numbers, the function should return 0. The code should start with: ```python from typing import List def averag...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def average_of_even_numbers(numbers: List[int]) -> float: """ Calculate the average of all even numbers in the given list of integers. Args: numbers (List[int]): A list of integers from whic...
python_sharegpt_23k
6,723
Write a Python function to calculate the factorial of a given non-negative integer using an iterative approach. The factorial of a non-negative integer `n`, denoted as `n!`, is the product of all positive integers less than or equal to `n`. The function should handle edge cases such as `n = 0` and `n = 1` correctly. T...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial(n: int) -> int: """ Calculate the factorial of a given non-negative integer. The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. Args...
python_sharegpt_23k
6,727
Write a Python function to generate a square matrix filled with zeros, where the size of the matrix is a prime number. The function should validate the input size to ensure it is a prime number before generating the matrix. If the input size is not a prime number, the function should return `None`. The code should sta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional import math def is_prime(n: int) -> bool: """ Check if a number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime,...
python_sharegpt_23k
6,733
Write a Python function to find and return all prime numbers between 1 and a given upper limit `n`. The function should use an efficient algorithm to determine if a number is prime. The code should start with: ```python from typing import List def find_primes_up_to(n: 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_up_to(n: int) -> List[int]: """ Find all prime numbers up to a given upper limit n using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit up to...
python_sharegpt_23k
6,740
Write a Python function to generate a dictionary where the keys are prime numbers between 1 and 100 (both included) and the values are the sum of the digits of the corresponding keys. The function should return the dictionary. The code should start with: ```python from typing import Dict def generate_prime_digit_sum_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def is_prime(n: int) -> bool: """ Helper function to check if a number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, Fa...
python_sharegpt_23k
6,744
Write a Python function to find and return a list of all odd numbers between a given range `[start, end]` (inclusive) that are not divisible by 3. The function should take two integers, `start` and `end`, as input and return a list of integers. The code should start with: ```python from typing import List def find_od...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_odd_numbers_not_divisible_by_3(start: int, end: int) -> List[int]: """ Find and return a list of all odd numbers between the given range [start, end] (inclusive) that are not divisible by 3....
python_sharegpt_23k
6,751
Write a Python function to find the lowest and highest numbers in a given list of integers, and calculate the sum of all numbers in the list. The function should return a tuple containing the lowest number, the highest number, and the sum of all numbers. The code should start with: ```python from typing import List, T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def find_lowest_highest_sum(numbers: List[int]) -> Tuple[int, int, int]: """ Find the lowest and highest numbers in a list of integers and calculate the sum of all numbers. Args: numb...
python_sharegpt_23k
6,752
Write a Python function to find the median of an array of n numbers, where n is an odd number. The function should ensure that the input array has an odd number of elements and raise an error if the input array has an even number of elements. The code should start with: ```python from typing import List def find_media...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_median_odd(nums: List[int]) -> int: """ Find the median of an array of n numbers, where n is an odd number. Args: nums (List[int]): The input array of integers with an odd number of...
python_sharegpt_23k
6,758
Write a Python function to find the number of unique values in a given array of integers and return the modified array with duplicates removed, sorted in ascending order. The function should also return the count of unique values. The code should start with: ```python from typing import List, Tuple def remove_duplica...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def remove_duplicates_and_count_unique(arr: List[int]) -> Tuple[List[int], int]: """ Removes duplicates from the given array and returns the modified array sorted in ascending order, along wit...
python_sharegpt_23k
6,760
Write a Python function to concatenate two lists without using the `+` operator or the `extend` method. The function should take two lists as input and return a new list that contains all elements from both input lists in the order they appear. The code should start with: ```python from typing import List def concate...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def concatenate_lists(list1: List[int], list2: List[int]) -> List[int]: """ Concatenate two lists without using the '+' operator or the 'extend' method. Args: list1 (List[int]): The first li...
python_sharegpt_23k
6,764
Write a Python function to calculate the sum of the elements in each row of a given 2-dimensional array. The function should return a list of these sums, where each element in the list corresponds to the sum of the elements in the respective row of the input array. The code should start with: ```python from typing imp...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def row_sums(arr: List[List[int]]) -> List[int]: """ Calculate the sum of the elements in each row of a given 2-dimensional array. Args: arr (List[List[int]]): A 2-dimensional array of integ...
python_sharegpt_23k
6,765
Write a Python function to compute the nth Fibonacci number using an iterative approach. The function should have a time complexity of O(n) and a space complexity of O(1). The code should start with: ```python def fibonacci(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def fibonacci(n: int) -> int: """ Compute the nth Fibonacci number using an iterative approach. Args: n (int): The position in the Fibonacci sequence to compute. Returns: int: The nth Fibonacci number...
python_sharegpt_23k
6,766
Write a Python function to group an array of numbers based on given conditions. Each condition is a tuple of two boolean expressions that define the range of numbers for each group. The function should return a dictionary where the keys are group names (e.g., 'Group 1', 'Group 2') and the values are lists of numbers th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def group_numbers(array: List[int], conditions: List[Tuple[int, int]]) -> Dict[str, List[int]]: """ Groups numbers in the array based on the given conditions. Args: array (List[...
python_sharegpt_23k
6,767
Write a Python function to find the prime factors of a given positive integer. The function should return a list of prime factors in ascending order. The code should start with: ```python from typing import List def find_prime_factors(num: 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_factors(num: int) -> List[int]: """ Find the prime factors of a given positive integer. Args: num (int): The positive integer for which to find the prime factors. Returns...
python_sharegpt_23k
6,768
Write a Python function to perform a deep copy of a nested object. The function should handle objects, arrays (lists and tuples), strings, numbers, booleans, and null values. You are not allowed to use any built-in methods or libraries for copying the object. The function should be recursive and should start with: ```p...
Here is the Python code to solve the given problem in the markdown block: ```python def deep_copy_object(obj): """ Perform a deep copy of a nested object. The function handles objects, arrays (lists and tuples), strings, numbers, booleans, and null values. Args: obj: The object to be deep copied. ...
python_sharegpt_23k
6,769
Write a Python function to sort an array of dictionaries by the value of 'popularity' in descending order. Additionally, ensure that any dictionaries with a 'popularity' value of less than 3 are placed at the end of the sorted array. The function should start with: ```python from typing import List, Dict def sort_by_p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_by_popularity(array: List[Dict[str, int]]) -> List[Dict[str, int]]: """ Sorts an array of dictionaries by the 'popularity' value in descending order. Dictionaries with a 'popularity' v...
python_sharegpt_23k
6,771
Write a Python function to generate a Fibonacci sequence up to a given number N, where N is a positive integer less than or equal to 1000. The function should return a list of Fibonacci numbers that do not exceed N. The code should start with: ```python from typing import List def generate_fibonacci_sequence(N: int) ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_fibonacci_sequence(N: int) -> List[int]: """ Generate a Fibonacci sequence up to a given number N. Args: N (int): The upper limit for the Fibonacci sequence. Must be a positive ...
python_sharegpt_23k
6,774
Write a Python function that takes in two sets, `A` and `B`, and removes all elements from `A` that are also present in `B`. The function should return the modified set `A`. The function should have a time complexity of O(n), where n is the number of elements in set `A`. The code should start with: ```python def remov...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_common_elements(A: set, B: set) -> set: """ Removes all elements from set A that are also present in set B and returns the modified set A. Args: A (set): The set from which elements are to be removed. ...
python_sharegpt_23k
6,776
Write a Python function to generate a random hexadecimal string of a specified length and return the string in reverse order. The function should accept an integer `length` as input and return the reversed hexadecimal string. The code should start with: ```python import random def generate_and_reverse_hex_string(leng...
Here is the Python code to solve the given problem in the markdown block: ```python import random def generate_and_reverse_hex_string(length: int) -> str: """ Generates a random hexadecimal string of the specified length and returns it in reverse order. Args: length (int): The length of the hexade...
python_sharegpt_23k
6,777
Write a Python function to find the minimum value in an unsorted array of integers without using any built-in sorting functions or data structures. The function should have a time complexity of O(n) and a space complexity of O(1). The code should start with: ```python from typing import List, Optional def find_minimu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_minimum(arr: List[int]) -> Optional[int]: """ Find the minimum value in an unsorted array of integers without using any built-in sorting functions or data structures. Args: ...
python_sharegpt_23k
6,786
Write a Python function to check if there are any two consecutive elements in a given array that are equal to a specified value. The function should return `True` if such a pair exists, otherwise `False`. The code should start with: ```python from typing import List def has_consecutive_value(arr: List[int], value: in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_consecutive_value(arr: List[int], value: int) -> bool: """ Check if there are any two consecutive elements in the array that are equal to the specified value. Args: arr (List[int]): ...
python_sharegpt_23k
6,788
Write a Python function to sort a list of integers in non-increasing order without using any built-in sorting functions or libraries. The function should have a time complexity of O(n^2) and should not use any additional data structures. The code should start with: ```python from typing import List def non_increasing_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def non_increasing_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers in non-increasing order using a selection sort algorithm. Args: arr (List[int]): The list of integers to be...
python_sharegpt_23k
6,790
Write a Python function to convert a given timestamp in a specified time zone to ISO 8601 format, ensuring that the resulting string includes the time zone offset. The function should handle the conversion from the specified time zone to UTC and then format the UTC datetime object in ISO 8601 format. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from datetime import datetime, timezone, timedelta def convert_to_iso8601(timestamp: str, time_zone_offset: int) -> str: """ Convert a given timestamp in a specified time zone to ISO 8601 format. Args: timestamp (s...
python_sharegpt_23k
6,791
Write a Python function to check if an array contains any duplicate elements, while also ensuring that the array elements are sorted in ascending order and that the length of the array is less than or equal to 100. The function should return a boolean value indicating the presence of duplicate elements. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_duplicates_and_is_sorted(arr: List[int]) -> bool: """ Check if an array contains any duplicate elements, is sorted in ascending order, and has a length less than or equal to 100. Args: ...
python_sharegpt_23k
6,797
Write a Python function to generate a matrix of random numbers between a specified range and of a specified size. Each number in the matrix should be rounded to the nearest integer. The function should take three parameters: the number of rows, the number of columns, and the range of the random numbers (a tuple contain...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import numpy as np def generate_random_matrix(rows: int, cols: int, range_values: Tuple[int, int]) -> List[List[int]]: """ Generate a matrix of random numbers between a specified range and of a sp...
python_sharegpt_23k
6,798
Write a Python function to generate all possible combinations of three different numbers from 1 to 10. Each combination should be a tuple of three distinct integers. The function should return a list of these tuples. The code should start with: ```python from typing import List, Tuple def generate_combinations() -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def generate_combinations() -> List[Tuple[int, int, int]]: """ Generate all possible combinations of three different numbers from 1 to 10. Returns: List[Tuple[int, int, int]]: A list ...
python_sharegpt_23k
6,801
Write a Python function to recognize the type of a credit card from a given string of numbers. The function should return the type of the credit card ("Visa", "Mastercard", "American Express", "Discover", or "Unknown") based on the first few digits of the card number. Additionally, if the input string contains any non-...
Here is the Python code to solve the given problem in the markdown block: ```python def recognize_credit_card_type(card_number: str) -> str: """ Recognize the type of a credit card from a given string of numbers. Args: card_number (str): The string representation of the credit card number. Ret...
python_sharegpt_23k
6,808
Write a Python function to find the common elements between two given arrays while maintaining the relative order of the elements in the first array. The function should have a time complexity of O(n), where n is the length of the longer array, and a space complexity of O(m), where m is the number of common elements be...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements_in_order(arr1: List[int], arr2: List[int]) -> List[int]: """ Find the common elements between two arrays while maintaining the relative order of the elements in the first array. ...
python_sharegpt_23k
6,809
Write a Python function to generate a list of random numbers within a specified range and calculate the average of these numbers, rounded to the nearest integer. The function should also return a summary of the key concepts used in the code. The code should start with: ```python from typing import List, Tuple import r...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import random def generate_random_numbers_and_average(n: int, lower_bound: int, upper_bound: int) -> Tuple[List[int], int, str]: """ Generates a list of random numbers within a specified range and...
python_sharegpt_23k
6,810
Write a Python function to combine two given lists into a single list containing each element of both lists in ascending order. The input lists may contain duplicate elements. The function should start with: ```python from typing import List def combine_and_sort_lists(list1: List[int], list2: List[int]) -> List[int]: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def combine_and_sort_lists(list1: List[int], list2: List[int]) -> List[int]: """ Combines two lists and returns a single sorted list containing all elements from both lists in ascending order. Args:...
python_sharegpt_23k
6,811
Write a Python function to convert an integer into its string representation using a recursive approach. The function should handle both positive and negative integers. The code should start with: ```python def int_to_string(n: int) -> str: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def int_to_string(n: int) -> str: """ Convert an integer to its string representation using a recursive approach. Args: n (int): The integer to be converted. Returns: str: The string representation of...
python_sharegpt_23k
6,813
Write a Python function that takes three integers as input: `num1`, `num2`, and `multiplier`. The function should first add `num1` and `num2`, and then multiply the result by `multiplier`. The function should return the final result. The code should start with: ```python def add_and_multiply(num1: int, num2: int, multi...
Here is the Python code to solve the given problem in the markdown block: ```python def add_and_multiply(num1: int, num2: int, multiplier: int) -> int: """ Adds two integers and then multiplies the sum by a third integer. Args: num1 (int): The first integer to be added. num2 (int): The seco...
python_sharegpt_23k
6,814
Write a Python function to print a given string 10 times, with each repetition removing a random character from the string. The removed character should be different each time. The final output should be the original string printed 10 times with a progressively shorter string each time. The function should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python import random def progressively_shorten_string(s: str) -> None: """ Prints the given string 10 times, with each repetition removing a random character from the string. The removed character should be different each time. ...
python_sharegpt_23k
6,816
Write a Python function to insert an element at a specified position in a given list. The function should take three parameters: the list, the element to be inserted, and the position at which the element should be inserted. The position should be zero-based, meaning the first position is 0. If the position is greater ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_element(arr: List[int], element: int, position: int) -> List[int]: """ Inserts an element at a specified position in a list. Args: arr (List[int]): The list in which the element i...
python_sharegpt_23k
6,817
Write a Python function to print the numbers from a given start number to a given end number, incrementing by the smallest prime number greater than a specified increment base with each iteration. The function should handle cases where the increment base is less than or equal to 4 by setting the increment to the smalle...
Here is the Python code to solve the given problem in the markdown block: ```python def print_numbers_with_increment(start: int, end: int, increment_base: int) -> None: """ Prints the numbers from the start number to the end number, incrementing by the smallest prime number greater than the increment base. ...
python_sharegpt_23k
6,818
Write a Python function to check if a given string only contains alphabets and if it contains at least one uppercase and one lowercase letter. The function should return `True` if both conditions are met, otherwise `False`. The code should start with: ```python def check_string_properties(string: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def check_string_properties(string: str) -> bool: """ Checks if the given string only contains alphabets and if it contains at least one uppercase and one lowercase letter. Args: string (str): The string to be check...
python_sharegpt_23k
6,821
Write a Python function to generate an n x n identity matrix using nested loops. The identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. The function should take an integer `n` as input and return a 2D list representing the identity matrix. The code should start with: ```python from ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_identity_matrix(n: int) -> List[List[int]]: """ Generate an n x n identity matrix. Args: n (int): The size of the identity matrix. Returns: List[List[int]]: A 2D li...
python_sharegpt_23k
6,822
Write a Python function to reverse a list without using the `reverse()` method and without using any built-in Python functions or methods. The function should take a list as input and return a new list that is the reverse of the input list. The code should start with: ```python from typing import List def reverse_list...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(lst: List[int]) -> List[int]: """ Reverses the given list without using the reverse() method or any built-in Python functions or methods. Args: lst (List[int]): The list to ...
python_sharegpt_23k
6,833
Write a Python function to determine whether a given list of integers contains an odd number of odd integers. The function should return `True` if the list contains an odd number of odd integers, and `False` otherwise. The code should start with: ```python from typing import List def has_odd_number_of_odds(numbers: Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_odd_number_of_odds(numbers: List[int]) -> bool: """ Determines whether a given list of integers contains an odd number of odd integers. Args: numbers (List[int]): The list of integer...
python_sharegpt_23k
6,835
Write a Python function to sort a list of integers by their absolute values without using any built-in sorting functions or methods. The function should implement a sorting algorithm of your choice (e.g., Bubble Sort, Selection Sort, Insertion Sort). The code should start with: ```python from typing import List def s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_by_absolute_value(lst: List[int]) -> List[int]: """ Sorts a list of integers by their absolute values without using built-in sorting functions or methods. Args: lst (List[int]): The...
python_sharegpt_23k
6,837
Write a Python function to sort an array of numbers from low to high using the Bubble Sort algorithm, which has a time complexity of O(n^2). The array may contain duplicate numbers and have a maximum length of 1000. The code should start with: ```python from typing import List def bubble_sort(arr: List[int]) -> List[i...
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
6,838
Write a Python function to sort a given array of integers in ascending order using the Merge Sort algorithm. The function should have a time complexity of O(n log n) and should not use any built-in sorting functions or libraries. The sorted array should be stored in a new array, and the original array should remain unc...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts a given array of integers in ascending order using the Merge Sort algorithm. Args: arr (List[int]): The array of integers to be sorted....
python_sharegpt_23k
6,841
Write a Python function to calculate the square root of a given non-negative integer `x` using a binary search approach. The function should have a time complexity of O(log n). The code should start with: ```python def square_root_binary_search(x: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def square_root_binary_search(x: int) -> int: """ Calculate the integer part of the square root of a given non-negative integer x using binary search. Args: x (int): The non-negative integer for which to find the sq...
python_sharegpt_23k
6,848
Write a Python function to create a reverse lookup dictionary from a given list of words. The dictionary should map each word (in lowercase) to a list of indices where the word appears in the original list. However, the dictionary should only include words that contain at least one vowel (ignoring case sensitivity). T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def create_reverse_lookup(words: List[str]) -> Dict[str, List[int]]: """ Creates a reverse lookup dictionary from a given list of words. The dictionary maps each word (in lowercase) to a list o...
python_sharegpt_23k
6,852
Write a Python function to filter and return a list of elements from a given array that are greater than 5 and less than 10. The function should handle any size of input array and return an empty list if no elements meet the criteria. The code should start with: ```python from typing import List def filter_elements(a...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_elements(arr: List[int]) -> List[int]: """ Filters and returns a list of elements from the given array that are greater than 5 and less than 10. Args: arr (List[int]): The input a...
python_sharegpt_23k
6,853
Write a Python function that takes in two lists of numbers and returns a new list where each element is the product of the corresponding elements in the two input lists. If the input lists are not of the same length, the function should return a string indicating that the lists must have the same length. The code shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def multiply_lists(list1: List[int], list2: List[int]) -> List[int] or str: """ Multiplies corresponding elements of two lists and returns a new list with the products. Args: list1 (List[int...
python_sharegpt_23k
6,854
Write a Python function to sort a given array of numbers in ascending order using only a single loop and without using any built-in sorting functions or methods. The function should start with: ```python from typing import List def sort_array_with_single_loop(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 sort_array_with_single_loop(arr: List[int]) -> List[int]: """ Sorts a given array of numbers in ascending order using only a single loop and without using any built-in sorting functions or methods. ...
python_sharegpt_23k
6,856
Write a Python function to find the common elements between two sorted lists without using any built-in functions or methods for comparing or iterating through the lists. You can only use basic operations like indexing and arithmetic operations. The lists may contain duplicates and can have different lengths. The code ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_common_elements(list1: List[int], list2: List[int]) -> List[int]: """ Find the common elements between two sorted lists without using built-in functions or methods for comparison or iteration. ...
python_sharegpt_23k
6,857
Write a Python function to remove all duplicates from a given input list and return a new list without modifying the original list. The function should maintain the order of elements as they appear in the input list. The code should start with: ```python from typing import List def remove_duplicates(input_list: List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates(input_list: List[int]) -> List[int]: """ Removes all duplicates from the input list and returns a new list with unique elements in the order they appear. Args: input_li...
python_sharegpt_23k
6,860
Write a Python function to remove all duplicates from a given list of numbers and sort the remaining elements in descending order. The function should have a time complexity of O(n log n). The code should start with: ```python from typing import List def remove_duplicates_and_sort(numbers: List[int]) -> List[int]: ```...
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def remove_duplicates_and_sort(numbers: List[int]) -> List[int]: """ Removes all duplicates from the given list of numbers and sorts the remaining elements in descending order. Args: numbe...
python_sharegpt_23k
6,862
Write a Python function to create a dictionary where the keys are the letters of the alphabet (both uppercase and lowercase) and the values are the sum of the ASCII values of the corresponding letters. The function should handle both uppercase and lowercase letters. The code should start with: ```python def create_alp...
Here is the Python code to solve the given problem in the markdown block: ```python def create_alphabet_ascii_sum_dict() -> dict: """ Create a dictionary where the keys are the letters of the alphabet (both uppercase and lowercase) and the values are the sum of the ASCII values of the corresponding letters....
python_sharegpt_23k
6,864
Write a Python function to generate all permutations of a given list of letters without using any built-in library functions or external modules. The function should handle lists of varying lengths and return a list of all possible permutations. The code should start with: ```python from typing import List def generat...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_permutations(letters: List[str]) -> List[List[str]]: """ Generate all permutations of a given list of letters. Args: letters (List[str]): The list of letters to permute. Re...
python_sharegpt_23k
6,865
Write a Python function to search for a given number in a given sorted array. The function should return the index of the first occurrence of the number if it exists in the array, or -1 if it does not. The algorithm should have a time complexity of O(log n), where n is the length of the array. The array may contain dup...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def search_first_occurrence(arr: List[int], target: int) -> int: """ Searches for the first occurrence of a given number in a sorted array. Args: arr (List[int]): The sorted array to sea...
python_sharegpt_23k
6,868
Write a Python function to print each item in a given list using a while loop. The function should take a list of items as input and print each item in the list one by one. The code should start with: ```python from typing import List def print_items(items: List[str]) -> None: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_items(items: List[str]) -> None: """ Prints each item in the given list of strings using a while loop. Args: items (List[str]): The list of strings to be printed. Returns: ...
python_sharegpt_23k
6,869
Write a Python function to reverse a given list in place without using built-in list functions and without using additional memory space. The function should start with: ```python from typing import List def reverse_list_in_place(lst: List[int]) -> None: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def reverse_list_in_place(lst: List[int]) -> None: """ Reverses the given list in place without using built-in list functions and without using additional memory space. Args: lst (List[int...
python_sharegpt_23k
6,872
Write a Python function to remove duplicate elements from a given list while preserving the order of the first occurrence of each element. The function should return the modified list. The code should start with: ```python from typing import List def remove_duplicates(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 remove_duplicates(arr: List[int]) -> List[int]: """ Removes duplicate elements from the given list while preserving the order of the first occurrence of each element. Args: arr (List[int...
python_sharegpt_23k
6,874
Write a Python function to find the minimum value in an array without using any built-in functions or libraries like `min()` or `sorted()`. The function should iterate through the array and keep track of the smallest value found. The code should start with: ```python from typing import List def find_minimum_value(arr...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_minimum_value(arr: List[int]) -> int: """ Find the minimum value in an array without using built-in functions or libraries. Args: arr (List[int]): The array of integers from which t...
python_sharegpt_23k
6,875
Write a Python function to perform a linear search in a given list of integers and return the index of the first occurrence of a target number. If the target number is not found in the list, the function should return -1. The function should handle lists with up to 1,000,000 elements and target numbers within the range...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def linear_search(arr: List[int], target: int) -> int: """ Perform a linear search in a given list of integers and return the index of the first occurrence of a target number. If the target number is...
python_sharegpt_23k
6,876
Write a Python function to find all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. The function should return a list of all prime numbers in sorted order, optimized for efficiency and minimizing memory usage. The code should start with: ```python from typing import List def find_primes_u...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_primes_up_to(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 prime...
python_sharegpt_23k
6,878
Write a Python function to find the nth to last node in a singly linked list. The function should have a time complexity of O(n) and a space complexity of O(1), meaning it should not use any extra data structures. The linked list is represented by a `Node` class, where each node has a value and a reference to the next ...
Here is the Python code to solve the given problem in the markdown block: ```python class Node: def __init__(self, value, next=None): self.value = value self.next = next def find_nth_to_last_node(head: Node, n: int) -> Node: """ Finds the nth to last node in a singly linked list. Args:...
python_sharegpt_23k
6,882
Write a Python function to process a list of strings, converting each string to uppercase if it contains only alphabetic characters, and then sort the resulting list in descending order based on the length of the strings. Strings containing numbers or special characters should be ignored. 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 process_strings(strings: List[str]) -> List[str]: """ Processes a list of strings by converting each string to uppercase if it contains only alphabetic characters, and then sorts the resulting li...
python_sharegpt_23k
6,883
Write a Python function that takes a string as an argument and returns an updated string where each character is repeated twice. If the original string contains any digits, they should be repeated three times instead of two. The function should handle any input string, including empty strings and strings with special c...
Here is the Python code to solve the given problem in the markdown block: ```python def repeat_characters(string: str) -> str: """ Returns a new string where each character from the input string is repeated twice. If the character is a digit, it is repeated three times instead of two. Args: str...
python_sharegpt_23k
6,888
Write a Python function to find the Greatest Common Divisor (GCD) of all elements in a given array of integers. The function should handle various edge cases, such as an empty array, an array with a single element, an array with only zeros, and an array with both positive and negative integers. The function should not ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_gcd_of_array(arr: List[int]) -> int: """ Find the Greatest Common Divisor (GCD) of all elements in the given array. Args: arr (List[int]): The array of integers for which the GCD is...
python_sharegpt_23k
6,890
Write a Python function to calculate the size of a dictionary without using the built-in `len()` function. The function should iterate through the dictionary's keys to count the number of key-value pairs. The code should start with: ```python def get_dict_size(dictionary: dict) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def get_dict_size(dictionary: dict) -> int: """ Calculate the size of a dictionary by counting its keys without using the built-in `len()` function. Args: dictionary (dict): The dictionary whose size is to be calcul...
python_sharegpt_23k
6,895
Write a Python function to create and manipulate a dictionary (hash table) with specified key-value pairs. The function should be able to handle any type of key and value, including numbers and objects. The function should start with: ```python from typing import Any, Dict def create_and_manipulate_hash_table(initial_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Any, Dict def create_and_manipulate_hash_table(initial_pairs: Dict[Any, Any], new_key: Any, new_value: Any) -> Dict[Any, Any]: """ Create a dictionary with initial key-value pairs and add a new key-value pair...
python_sharegpt_23k
6,901
Write a Python function to repeat a given string `s` a specified number of times `n`. The function should ensure that `n` is a positive integer and raise an error if it is not. The code should start with: ```python def repeat_string(s: str, n: int) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def repeat_string(s: str, n: int) -> str: """ Repeats the given string `s` exactly `n` times. Args: s (str): The string to be repeated. n (int): The number of times the string should be repeated. Must be a p...
python_sharegpt_23k
6,902
Write a Python function to determine the number of days in a given month and year, considering leap years. The function should take two parameters: the month (as an integer) and the year (as an integer) and return the number of days in that month. The function should handle leap years correctly. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python def get_days_in_month(month: int, year: int) -> int: """ Determine the number of days in a given month and year, considering leap years. Args: month (int): The month (1-12) for which to determine the number of days....
python_sharegpt_23k
6,909
Write a Python function to create a new list from a given list that contains the square of each element, but excluding any elements that are divisible by 3. The function should take a list of integers as input and return a new list of integers. The code should start with: ```python from typing import List def square_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def square_and_exclude_divisible_by_3(input_list: List[int]) -> List[int]: """ Creates a new list from the given list that contains the square of each element, but excludes any elements that are divi...
python_sharegpt_23k
6,912
Write a Python function to calculate the sum of all values in a given two-dimensional array (matrix) of integers. The function should take a 2D list of integers as input and return the sum of all elements in the matrix. The code should start with: ```python from typing import List def sum_of_matrix(matrix: List[List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_matrix(matrix: List[List[int]]) -> int: """ Calculate the sum of all values in a given two-dimensional array (matrix) of integers. Args: matrix (List[List[int]]): A 2D list of int...
python_sharegpt_23k
6,913
Write a Python function to search for a specified item in a list and return the index of the first occurrence. If the item is not found in the list, the function should return -1. The function should be able to handle lists containing duplicate elements. The code should start with: ```python from typing import List, An...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def find_first_occurrence(lst: List[Any], item: Any) -> int: """ Searches for the first occurrence of a specified item in a list and returns its index. If the item is not found, returns -1. ...
python_sharegpt_23k
6,914
Write a Python function to calculate the sum of all non-negative elements in a given matrix. The matrix is a 2D list of integers. The function should exclude any negative numbers from the sum. The code should start with: ```python from typing import List def sum_non_negative_elements(matrix: List[List[int]]) -> int: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_non_negative_elements(matrix: List[List[int]]) -> int: """ Calculate the sum of all non-negative elements in a given matrix. Args: matrix (List[List[int]]): A 2D list of integers rep...
python_sharegpt_23k
6,918
Write a Python function to sort a list of numbers in increasing order without using any built-in sorting functions or libraries. The solution should have a time complexity of O(n^2), where n is the length of the list, and a space complexity of O(1). The function should start with: ```python from typing import List def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def custom_sort(nums: List[int]) -> List[int]: """ Sorts a list of integers in increasing order using the Bubble Sort algorithm. Args: nums (List[int]): The list of integers to be sorted. ...
python_sharegpt_23k
6,920
Write a Python function to calculate the difference between two lists, defined as the elements that are present in the first list but not in the second list. The function should return the result as a new list. The code should start with: ```python from typing import List def list_difference(a: List[int], b: List[int]...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def list_difference(a: List[int], b: List[int]) -> List[int]: """ Calculate the difference between two lists, defined as the elements that are present in the first list but not in the second list. A...
python_sharegpt_23k
6,923
Write a Python function to extract a sublist from a given list that satisfies two conditions: 1. The elements in the sublist should be greater than a specified threshold value. 2. The elements in the sublist should be divisible by a specified divisor value. The function should not use any built-in Python functions suc...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_sublist(lst: List[int], threshold: int, divisor: int) -> List[int]: """ Extracts a sublist from the given list where each element is greater than a specified threshold and divisible by a spec...
python_sharegpt_23k
6,926
Write a Python function to compare two given lists and return `True` if their contents are the same and the order of elements is also the same. If the contents or the order of elements differ, return `False`. The code should start with: ```python from typing import List def compare_lists(list1: List[int], list2: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def compare_lists(list1: List[int], list2: List[int]) -> bool: """ Compare two lists to check if their contents are the same and the order of elements is also the same. Args: list1 (List[int...
python_sharegpt_23k
6,930
Write a Python function to remove duplicate elements from a given list while maintaining the original order of the elements. The function should not use any built-in functions or data structures that directly support set operations or sorting. The code should start with: ```python from typing import List def remove_du...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_preserve_order(original_list: List[int]) -> List[int]: """ Removes duplicate elements from the given list while preserving the original order of the elements. Args: ori...
python_sharegpt_23k
6,931
Write a Python function to generate a list of all prime numbers within a given range `[start, end]`. The function should use a helper function to check if a number is prime and then use list comprehension to generate the list of prime numbers. 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 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
6,932
Write a Python function to generate a list of all prime numbers within a given range `[start, end]`. The function should return the list of prime numbers. The code should start with: ```python from typing import List def generate_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 def is_prime(num: int) -> bool: """ Helper function to check if a number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is pri...
python_sharegpt_23k
6,934
Write a Python function to generate the FizzBuzz series up to a given number `n`. The function should return a list of strings where each string is either: - "FizzBuzz" if the number is divisible by both 3 and 5, - "Fizz" if the number is divisible by 3, - "Buzz" if the number is divisible by 5, - The number itself (as...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def fizz_buzz(n: int) -> List[str]: """ Generate the FizzBuzz series up to a given number `n`. Args: n (int): The number up to which the FizzBuzz series should be generated. Returns: ...
python_sharegpt_23k
6,942
Write a Python function to generate a list of squared numbers from a given list of integers, but the squared numbers should be in reverse order of the original list. The function should return the list of squared numbers. The code should start with: ```python from typing import List def reverse_squared_numbers(number...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_squared_numbers(numbers: List[int]) -> List[int]: """ Generate a list of squared numbers from a given list of integers, in reverse order. Args: numbers (List[int]): The list of i...
python_sharegpt_23k
6,943
Write a Python function to count the number of integers in a given 2D array that are divisible by 2. The function should iterate through each element in the 2D array, check if the element is divisible by 2, and increment a counter if it is. The function should return the total count of such integers. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_even_numbers(arr: List[List[int]]) -> int: """ Count the number of integers in a given 2D array that are divisible by 2. Args: arr (List[List[int]]): A 2D array of integers. R...
python_sharegpt_23k
6,946
Write a Python function to format a given floating-point number to contain exactly two decimal points and round it to the nearest hundredth. The function should handle any floating-point number and return the formatted and rounded number as a string. The code should start with: ```python def format_and_round_number(nu...
Here is the Python code to solve the given problem in the markdown block: : ```python def format_and_round_number(number: float) -> str: """ Formats a given floating-point number to contain exactly two decimal points and rounds it to the nearest hundredth. Args: number (float): The floating-point n...
python_sharegpt_23k
6,950
Write a Python function to perform a linear search in an array starting from the end of the array and iterating backwards. The function should return the index of the first occurrence of the target number in the array. If the target number is not found, the function should return -1. The code should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def linear_search_from_end(arr: List[int], target: int) -> int: """ Perform a linear search starting from the end of the array and return the index of the first occurrence of the target number. If th...
python_sharegpt_23k
6,951
Write a Python function to find the most frequent name in a given array of names. The array may contain up to 10^6 names. The function should have a time complexity of O(n), where n is the number of names in the array. The code should start with: ```python from typing import List def find_most_frequent_name(names: Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_most_frequent_name(names: List[str]) -> str: """ Find the most frequent name in a given array of names. Args: names (List[str]): A list of names. Returns: str: The most...
python_sharegpt_23k
6,952
Write a Python function that takes a dictionary as input and returns a list containing all the keys in the dictionary, but with each key repeated twice. For example, given the dictionary `{"apple": 3, "banana": 2, "orange": 1}`, the function should return `["apple", "apple", "banana", "banana", "orange", "orange"]`. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def duplicate_keys(dictionary: Dict[str, int]) -> List[str]: """ Returns a list containing each key from the input dictionary repeated twice. Args: dictionary (Dict[str, int]): The inp...
python_sharegpt_23k
6,954
Write a Python function to generate a 10x10 multiplication table using only prime numbers. The function should return the table as a list of lists, where each inner list represents a row in the table. The table should be formatted such that each cell contains the product of the corresponding prime numbers. The function...
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
6,955
Write a Python function to apply a given function to each element of a list and return a new list containing the results. The function should be flexible enough to handle any function and any list of elements. The code should start with: ```python from typing import List, Callable def apply_function_to_list(func: Call...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Callable def apply_function_to_list(func: Callable, lst: List) -> List: """ Applies a given function to each element of a list and returns a new list with the results. Args: func (Callable)...
python_sharegpt_23k
6,956
Write a Python function to calculate the Hamming distance between two binary strings of equal length efficiently. The length of the strings can be up to \(10^6\). The function should handle large inputs efficiently and avoid unnecessary computations. The code should start with: ```python def hamming_distance(s1: str, ...
Here is the Python code to solve the given problem in the markdown block: ```python def hamming_distance(s1: str, s2: str) -> int: """ Calculate the Hamming distance between two binary strings of equal length. The Hamming distance is the number of positions at which the corresponding bits are different...
python_sharegpt_23k
6,958
Write a Python function to find the first n prime numbers and return them as a list. The function should use a helper function to check if a number is prime. The code should start with: ```python from typing import List def find_first_n_primes(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def is_prime(n: int) -> bool: """ Check if a given number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwi...
python_sharegpt_23k
6,959
Write a Python function to determine the maximum value among three given integers using the ternary operator. The function should return the largest integer among the three inputs. If two or more integers are equal and are the largest, the function should return one of those integers. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python def get_max_value(a: int, b: int, c: int) -> int: """ Determine the maximum value among three given integers using the ternary operator. Args: a (int): The first integer. b (int): The second integer. ...
python_sharegpt_23k
6,960
Write a Python function to generate a list of prime numbers between a given range `[start, end]`. The function should return the list of prime numbers. The code should start with: ```python from typing import List def generate_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 def is_prime(num: int) -> bool: """ Check if a given number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False oth...
python_sharegpt_23k
6,962
Write a Python function to reverse the order of the values in a given list without using any built-in Python functions or methods. The solution should have a time complexity of O(n) and a space complexity of O(1). The input list will always contain at least one element. You are not allowed to use any additional data st...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(lst: List[int]) -> List[int]: """ Reverses the order of the values in the input list in-place without using any built-in Python functions or methods. Args: lst (List[int]): ...