source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
9,488
Write a Python function to select specific columns from a given data table and optionally filter rows based on a specified condition. The function should handle cases where the specified column does not exist in the data table and where the specified condition does not match any rows. The function should have a time co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def select_columns_with_condition(table: List[List[str]], columns: List[str], condition_column: Optional[str] = None, condition_value: Optional[str] = None) -> Optional[List[List[str]]]: """ Se...
python_sharegpt_23k
9,489
Write a Python function to determine if a given string is a valid IPv6 address. The function should adhere to the following rules for IPv6 addresses: 1. An IPv6 address consists of eight groups of four hexadecimal digits, separated by colons. 2. Each group can be abbreviated by using double colons (`::`), but there can...
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_ipv6(ip_str: str) -> bool: """ Determines if a given string is a valid IPv6 address. Args: ip_str (str): The string to be checked for IPv6 validity. Returns: bool: True if the string is a v...
python_sharegpt_23k
9,490
Write a Python function to generate a Fibonacci sequence up to `n` elements using an iterative approach. The function should not use recursion or the golden ratio formula. The code should start with: ```python from typing import List def generate_fibonacci_sequence(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_fibonacci_sequence(n: int) -> List[int]: """ Generate a Fibonacci sequence up to n elements using an iterative approach. Args: n (int): The number of elements in the Fibonacci s...
python_sharegpt_23k
9,491
Write a Python function to check if a given string is a palindrome with O(1) space complexity and O(n) time complexity. A palindrome is a string that reads the same backward as forward. The function should return `True` if the string is a palindrome and `False` otherwise. The code should start with: ```python def is_p...
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome(s: str) -> bool: """ Check if the given string is a palindrome with O(1) space complexity and O(n) time complexity. Args: s (str): The string to check for palindrome. Returns: bool: Tr...
python_sharegpt_23k
9,495
Write a Python function to check if a given string contains only alphabetical characters and has a length of at least 5 characters. The function should return `True` if both conditions are met, otherwise `False`. The code should start with: ```python def is_valid_string(s: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def is_valid_string(s: str) -> bool: """ Check if the given string contains only alphabetical characters and has a length of at least 5 characters. Args: s (str): The string to be checked. Returns: bool...
python_sharegpt_23k
9,496
Write a Python function to create an array filled with alternating 0s and 1s, where the number of 0s and 1s is equal. The array should start with 0. The function should take the size of the array as an input parameter and return the array. If the size is odd, the function should return an empty list. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def create_alternating_array(size: int) -> List[int]: """ Creates an array filled with alternating 0s and 1s, where the number of 0s and 1s is equal. The array starts with 0. If the size is odd, retu...
python_sharegpt_23k
9,498
Write a Python function to reverse a string in place without using any additional data structures. The function should have a time complexity of O(n) and a space complexity of O(1). The function should start with: ```python def reverse_string_in_place(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string_in_place(s: str) -> str: """ Reverses a string in place without using any additional data structures. Args: s (str): The input string to be reversed. Returns: str: The reversed string...
python_sharegpt_23k
9,499
Write a Python function to perform a binary search on a sorted list of integers and return both the element and its index if found. If the element is not found, return `None` for both the element and the index. The code should start with: ```python from typing import List, Tuple, Optional def binary_search_with_index...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Optional def binary_search_with_index(arr: List[int], target: int) -> Tuple[Optional[int], Optional[int]]: """ Perform a binary search on a sorted list of integers and return both the element and...
python_sharegpt_23k
9,500
Write a Python function to extract all prime numbers from a given string using regular expressions. The function should return a list of integers representing the prime numbers found in the string. The code should start with: ```python import re from typing import List def extract_prime_numbers(string: str) -> List[i...
Here is the Python code to solve the given problem in the markdown block: ```python import re 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...
python_sharegpt_23k
9,508
Write a Python function to generate a multiplication table up to a given number N efficiently. The function should return a list of lists, where each inner list represents a row in the multiplication table. The code should be optimized to handle large values of N without unnecessary computations. The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_multiplication_table(N: int) -> List[List[int]]: """ Generate a multiplication table up to a given number N. Args: N (int): The number up to which the multiplication table is to...
python_sharegpt_23k
9,510
Write a Python function to process a list of user dictionaries, filter out users who are under 18 years old, sort the remaining users by their age in ascending order, and return a CSV string with the format "user_id, name, age". The function should start with: ```python from typing import List, Dict def generate_csv_s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def generate_csv_string(users: List[Dict[str, any]]) -> str: """ Generates a CSV string from a list of user dictionaries, filtering out users under 18 and sorting by age. Args: users (...
python_sharegpt_23k
9,511
Write a Python function to calculate the average of all integer values in a given array. The function should handle the following steps: 1. Check if the input array is empty. If it is, return 0 as the average. 2. Ensure the input array contains only integer values. If any non-integer value is found, raise a `ValueError...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import numpy as np def calculate_average(arr: List[int]) -> float: """ Calculate the average of all integer values in a given array. Args: arr (List[int]): The input array of integers. ...
python_sharegpt_23k
9,512
Write a Python function to calculate the parity of a given binary number using only bitwise operations. The function should take in an integer and return 1 if the number of set bits (bits that are 1) in the binary representation of the number is odd, and 0 if it is even. You are not allowed to use any other operations ...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_parity(n: int) -> int: """ Calculate the parity of a given binary number using only bitwise operations. Args: n (int): The integer whose parity is to be calculated. Returns: int: The parit...
python_sharegpt_23k
9,513
Write a Python function to store and retrieve multiple values of different data types in a single variable. The function should accept a list of tuples, where each tuple contains a value and its corresponding data type. The function should return a dictionary where the keys are the data types and the values are lists o...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def store_and_retrieve_values(values: List[Tuple[any, str]]) -> Dict[str, List[any]]: """ Stores and retrieves multiple values of different data types in a single variable. Args: ...
python_sharegpt_23k
9,517
Write a Python function called `sum_of_lists` that takes in a list of lists as a parameter and returns the sum of all the elements in the input lists. The input list of lists will always contain integers. The function should have the following specifications: - Input: a list of lists `lists` containing integers. - Out...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_of_lists(lists: List[List[int]]) -> int: """ Calculate the sum of all elements in a list of lists containing integers. Args: lists (List[List[int]]): A list of lists containing integ...
python_sharegpt_23k
9,518
Write a Python function to calculate the greatest common divisor (GCD) of two numbers without using any built-in library or function for finding the GCD. The function should handle large numbers efficiently. The code should start with: ```python def gcd(a: int, b: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def gcd(a: int, b: int) -> int: """ Calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. Args: a (int): The first number. b (int): The second number. Returns: ...
python_sharegpt_23k
9,519
Write a Python function to check if a given list contains any duplicate elements. The function should be implemented using list comprehensions for improved readability and efficiency. The code should start with: ```python from typing import List def has_duplicates(arr: List[int]) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def has_duplicates(arr: List[int]) -> bool: """ Check if the given list contains any duplicate elements. Args: arr (List[int]): The list of integers to check for duplicates. Returns: ...
python_sharegpt_23k
9,520
Write a Python function to convert a given number expressed in hexadecimal representation to its corresponding decimal representation. The function should handle both uppercase and lowercase hexadecimal digits and should return the decimal equivalent. The code should start with: ```python def hex_to_decimal(hex_num: st...
Here is the Python code to solve the given problem in the markdown block: ```python def hex_to_decimal(hex_num: str) -> int: """ Converts a given hexadecimal number to its corresponding decimal representation. Args: hex_num (str): The hexadecimal number to be converted. It can include digits 0-9 an...
python_sharegpt_23k
9,522
Write a Python function to search for a specific element in a given list and return its position. If the element is not found in the list, the function should return -1. The list can contain up to 1000 elements, and each element can be a string of maximum length 50 characters. The function should handle cases where the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def search_element(list_a: List[str], element: str) -> int: """ Searches for a specific element in a given list and returns its position. If the element is not found, returns -1. Args: l...
python_sharegpt_23k
9,523
Write a Python function to create a new list that contains the square of every element in the original list. Additionally, for each square, if it is divisible by 3, append the string "divisible by 3" to the new list; if it is divisible by 5, append the string "divisible by 5" to the new list; if it is divisible by both...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def process_squares(lst: List[int]) -> List[int]: """ Create a new list that contains the square of every element in the original list. Additionally, for each square, append the appropriate string ba...
python_sharegpt_23k
9,525
Write a Python function to calculate the surface area of a cube with a given side length using a recursive function. The function should only return the surface area of the cube with the specified side length, not the sum of surface areas of smaller cubes. The code should start with: ```python def calculate_surface_ar...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_surface_area_of_cube(side_length: int) -> int: """ Calculate the surface area of a cube with the given side length using a recursive function. Args: side_length (int): The side length of the cube. ...
python_sharegpt_23k
9,526
Write a Python function to count the number of similar characters in two given strings, considering the following constraints: 1. The function should be case-sensitive. 2. Spaces and special characters should be considered as regular characters. 3. The function should have a time complexity of O(n), where n is the leng...
Here is the Python code to solve the given problem in the markdown block: ```python def count_similar_characters(string1: str, string2: str) -> int: """ Counts the number of similar characters in two given strings. Args: string1 (str): The first string to compare. string2 (str): The second ...
python_sharegpt_23k
9,528
Write a Python function to generate a unique list from a given list of duplicate integers while maintaining the order of the first occurrence of each integer in the input list. The function should start with: ```python from typing import List def generate_unique_list(input_list: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_unique_list(input_list: List[int]) -> List[int]: """ Generates a unique list from a given list of duplicate integers, maintaining the order of the first occurrence of each integer. Args...
python_sharegpt_23k
9,530
Write a Python function to calculate the sum of each row and each column in a given matrix of integers. The matrix can have variable dimensions and can contain both positive and negative numbers. The function should return two lists: one containing the sum of each row and the other containing the sum of each column. Th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def calculate_row_and_column_sums(matrix: List[List[int]]) -> Tuple[List[int], List[int]]: """ Calculate the sum of each row and each column in a given matrix of integers. Args: matri...
python_sharegpt_23k
9,537
Write a Python function to calculate the sum of the squares of the elements in a given list. The function should take a list of integers as input and return the sum of the squares of those integers. The code should start with: ```python from typing import List def sum_of_squares(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_of_squares(numbers: List[int]) -> int: """ Calculate the sum of the squares of the elements in a given list of integers. Args: numbers (List[int]): A list of integers for which to ca...
python_sharegpt_23k
9,541
Write a Python function to find the index of the first occurrence of a target value in a given array of integers. If the target value is not found in the array, return -1. The function should start with: ```python from typing import List def find_first_occurrence(nums: List[int], target: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_first_occurrence(nums: List[int], target: int) -> int: """ Find the index of the first occurrence of a target value in a given array of integers. Args: nums (List[int]): The array o...
python_sharegpt_23k
9,548
Write a Python function to create a new array `b` from an input array `a` containing only the even numbers, sorted in ascending order. The input array `a` should contain at least 10 elements. The function should start with: ```python from typing import List def filter_and_sort_even_numbers(a: List[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_sort_even_numbers(a: List[int]) -> List[int]: """ Create a new array containing only the even numbers from the input array, sorted in ascending order. Args: a (List[int]): The...
python_sharegpt_23k
9,553
Write a Python function to find and return the k highest numbers in descending order from a given dataset. The algorithm should have a time complexity of O(n log k) and use constant space. The input array will contain at most 10^5 integers, ranging from -10^9 to 10^9. 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 import heapq def find_k_highest_numbers(arr: List[int], k: int) -> List[int]: """ Returns the k highest numbers from the given dataset in descending order. Args: arr (List[int]): The dataset...
python_sharegpt_23k
9,556
Write a Python function to generate a list of all prime numbers between a given range `[start, end]`, excluding the numbers `2` and `3`. 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 prime...
python_sharegpt_23k
9,557
Write a Python function to create a nested dictionary with URLs and titles of 5 different webpages. Each webpage should have additional attributes such as the number of images, the number of hyperlinks, and the number of paragraphs present on the webpage. The function should return the nested dictionary. The code shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def create_webpage_dictionary() -> Dict[str, Dict[str, str | int]]: """ Creates a nested dictionary with URLs and titles of 5 different webpages. Each webpage has additional attributes: number of ima...
python_sharegpt_23k
9,558
Write a Python function to convert a given decimal number to its hexadecimal representation without using any built-in functions or libraries for conversion. The function should implement the conversion algorithm from scratch. The code should start with: ```python def decimal_to_hexadecimal(decimal_num: int) -> str: `...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_hexadecimal(decimal_num: int) -> str: """ Convert a given decimal number to its hexadecimal representation. Args: decimal_num (int): The decimal number to be converted. Returns: str: The ...
python_sharegpt_23k
9,563
Write a Python function to compute the transpose of a given matrix. The matrix is represented as a list of lists, where each inner list represents a row of the matrix. The transpose of a matrix is obtained by swapping its rows with its columns. The function should return the transposed matrix as a new list of lists. T...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def transpose_matrix(matrix: List[List[int]]) -> List[List[int]]: """ Compute the transpose of a given matrix. The transpose of a matrix is obtained by swapping its rows with its columns. Args:...
python_sharegpt_23k
9,568
Write a Python function to calculate the area of a triangle given the lengths of all three sides. The function should handle cases where the triangle is not valid (i.e., the sum of any two sides is less than or equal to the third side). The function should return the area rounded to the nearest whole number. If the tri...
Here is the Python code to solve the given problem in the markdown block: ```python import math from typing import Union def calculate_triangle_area(a: float, b: float, c: float) -> Union[int, str]: """ Calculate the area of a triangle given the lengths of its three sides. Args: a (float): Length ...
python_sharegpt_23k
9,570
Write a Python function to merge two sorted lists of numbers and remove any duplicates. The function should handle cases where one or both of the lists contain negative numbers. The code should start with: ```python from typing import List def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]: """ Merges two sorted lists of integers and removes any duplicates. Args: list1 (List[int]): The first sorted list of...
python_sharegpt_23k
9,576
Write a Python function to find the most frequent number in an array of integers and its frequency. The array should be of length 10 and each element should be a random integer between 0 and 9. The function should return a tuple containing the most frequent number and its frequency. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import random def most_frequent_number(array: List[int]) -> Tuple[int, int]: """ Finds the most frequent number in a given array of integers and returns it along with its frequency. Args: ...
python_sharegpt_23k
9,577
Write a Python function to calculate the sum of all elements in a given list of integers and return the average of the sum. The function should handle lists of any length, including empty lists. If the list is empty, the function should return 0 for both the sum and the average. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_and_average(arr: List[int]) -> (int, float): """ Calculate the sum and average of all elements in a given list of integers. Args: arr (List[int]): The list of integers for which to c...
python_sharegpt_23k
9,579
Write a Python function to calculate the product of all prime numbers in a given list of positive integers. The function should return the product as an integer. The code should start with: ```python from typing import List def product_of_primes(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def product_of_primes(numbers: List[int]) -> int: """ Calculate the product of all prime numbers in the given list of positive integers. Args: numbers (List[int]): A list of positive integer...
python_sharegpt_23k
9,580
Write a Python function to find the length of the longest increasing subsequence (LIS) in a given list of integers. The function should ensure that the time complexity of the solution is O(n log n), where n is the length of the list. The code should start with: ```python from typing import List def longest_increasing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_increasing_subsequence(nums: List[int]) -> int: """ Calculate the length of the longest increasing subsequence in a given list of integers. Args: nums (List[int]): The list of in...
python_sharegpt_23k
9,584
Write a Python function to separate the numbers from the non-numeric characters in a given string and return the sum of all the separated numbers. The function should not use any built-in functions or libraries that directly solve the problem. The code should start with: ```python def separate_and_sum_numbers(s: str) ...
Here is the Python code to solve the given problem in the markdown block: ```python def separate_and_sum_numbers(s: str) -> int: """ Separates the numbers from the non-numeric characters in the given string and returns the sum of all the separated numbers. Args: s (str): The input string containing...
python_sharegpt_23k
9,585
Write a Python function to merge two sorted arrays into one sorted array. The function should take two sorted arrays as input and return a new sorted array that contains all the elements from both input arrays. The function should have a time complexity of O(n), where n is the total number of elements in both arrays. Y...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]: """ Merge two sorted arrays into one sorted array. Args: arr1 (List[int]): The first sorted array. arr2 (List[...
python_sharegpt_23k
9,587
Write a Python function that takes an object as input and returns a dictionary containing the names and values of the top-level attributes of the object, excluding any attributes within nested objects or lists. The function should handle both dictionaries and lists as input objects. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Any def get_top_level_attributes(obj: Any) -> Dict[str, Any]: """ Extracts and returns the names and values of the top-level attributes of the given object, excluding any attributes within nested ob...
python_sharegpt_23k
9,588
Write a Python function to iterate through a given list of integers in reverse order and return a new list containing the elements in reverse order. The list can contain at most \(10^6\) elements, and each element is a positive integer between 1 and \(10^9\) (inclusive). The code should start with: ```python from typi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(lst: List[int]) -> List[int]: """ Returns a new list containing the elements of the input list in reverse order. Args: lst (List[int]): The list of integers to be reversed. ...
python_sharegpt_23k
9,590
Write a Python function to calculate the sum of each row in a given two-dimensional array and return the sums in reverse order. The function should handle any rectangular two-dimensional array of integers. The code should start with: ```python from typing import List def sum_rows_reverse(arr: List[List[int]]) -> List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_rows_reverse(arr: List[List[int]]) -> List[int]: """ Calculate the sum of each row in a given two-dimensional array and return the sums in reverse order. Args: arr (List[List[int]]):...
python_sharegpt_23k
9,591
Write a Python function to find the minimum element in a given array. The function should iterate through the array and keep track of the smallest element found. The code should start with: ```python from typing import List def findMin(arr: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def findMin(arr: List[int]) -> int: """ Find the minimum element in a given array. Args: arr (List[int]): The array from which to find the minimum element. Returns: int: The min...
python_sharegpt_23k
9,592
Write a Python function to check if a given substring is present in a set of strings, but the substring must be a palindrome. The function should return `True` if the palindromic substring is found in any of the strings, otherwise `False`. The code should start with: ```python from typing import List def is_palindrom...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_palindrome(string: str) -> bool: """ Helper function to check if a string is a palindrome. Args: string (str): The string to check. Returns: bool: True if the string is a...
python_sharegpt_23k
9,594
Write a Python function to filter out all the prime numbers from a given list of integers. The function should return a list containing only the prime numbers from the input list. The code should start with: ```python from typing import List def filter_primes(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 is_prime(n: int) -> bool: """ Check if a given number is prime. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise...
python_sharegpt_23k
9,598
Write a Python function to validate if a given string can be used as a valid email address based on the following criteria: 1. The string must contain exactly one '@' symbol, splitting it into a username and a domain. 2. The username must contain only alphanumeric characters, periods ('.'), underscores ('_'), and hyphe...
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_valid_email(email: str) -> bool: """ Validates if the given string can be used as a valid email address. Args: email (str): The email address to validate. Returns: bool: True if the em...
python_sharegpt_23k
9,603
Write a Python function to count from 0 to a given number `n`, but only return the odd numbers in a list. The function should handle cases where `n` is negative by returning an empty list. The code should start with: ```python from typing import List def get_odd_numbers(n: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_odd_numbers(n: int) -> List[int]: """ Returns a list of odd numbers between 0 and n (inclusive). Args: n (int): The upper limit of the range (inclusive). Returns: List[i...
python_sharegpt_23k
9,605
Write a Python function to print the length of words that contain an even number of characters from a given list of words. The function should stop processing the list when it encounters the word 'foo'. The code should start with: ```python from typing import List def print_even_length_words(words: List[str]) -> None...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_even_length_words(words: List[str]) -> None: """ Prints the length of words that contain an even number of characters from the given list. Stops processing the list when it encounters the w...
python_sharegpt_23k
9,607
Write a Python function to remove all occurrences of the element "Apple" from a given list of fruits. The function should not use any built-in functions or methods that directly delete elements from a list. Instead, it should manually iterate through the list and reconstruct the list without the element "Apple". The fu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_apples(fruits: List[str]) -> List[str]: """ Removes all occurrences of "Apple" from the given list of fruits. Args: fruits (List[str]): The list of fruits from which to remove "Ap...
python_sharegpt_23k
9,612
Write a Python function to decode a given string that contains special character sequences into its original form. The function should have a time complexity of O(n), where n is the length of the string. Additionally, the function should not use any built-in string manipulation methods (such as `replace` or `split`) or...
Here is the Python code to solve the given problem in the markdown block: ```python def decode_string(s: str) -> str: """ Decodes a string that contains special character sequences into its original form. Args: s (str): The input string containing special character sequences. Returns: ...
python_sharegpt_23k
9,613
Write a Python function to generate a list of squares of the first n natural numbers. The function should take an integer `n` as input and return a list of integers where each integer is the square of the corresponding natural number from 1 to `n`. The code should start with: ```python from typing import List def gen...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_squares(n: int) -> List[int]: """ Generates a list of squares of the first n natural numbers. Args: n (int): The number of natural numbers for which to generate squares. Re...
python_sharegpt_23k
9,615
Write a Python function to count the total number of items in a given dictionary, including all levels of nested dictionaries. The function should recursively traverse the dictionary and count all key-value pairs, including those in nested dictionaries. The code should start with: ```python from typing import Dict de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_total_items(dictionary: Dict) -> int: """ Count the total number of items in a dictionary, including all levels of nested dictionaries. Args: dictionary (Dict): The dictionary to c...
python_sharegpt_23k
9,617
Write a Python function to extract an integer value from a given string. The integer value will always be followed by the word "value" and will be enclosed within parentheses. The function should return the extracted integer value. If no integer value is found, the function should return `None`. The code should start ...
Here is the Python code to solve the given problem in the markdown block: ```python import re def extract_integer_value(value_string: str) -> int: """ Extracts an integer value from a given string. The integer value is expected to be enclosed within parentheses and followed by the word "value". Args: ...
python_sharegpt_23k
9,626
Write a Python function to convert two given lists into a dictionary. The first list contains keys and the second list contains values. The function should handle duplicate keys by keeping the last value encountered and should return an empty dictionary if the input lists are empty. The code should start with: ```pyth...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def to_dict(list1: List[str], list2: List[int]) -> Dict[str, int]: """ Converts two lists into a dictionary where the elements of the first list are the keys and the elements of the second list...
python_sharegpt_23k
9,629
Write a Python function to process a list by removing all duplicate numbers, filtering out non-numeric elements, and sorting the remaining numbers in descending order. The function should handle large input lists efficiently, up to 100,000 elements. The code should start with: ```python from typing import List def pro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def process_list(input_list: List[int]) -> List[int]: """ Processes a list by removing duplicate numbers, filtering out non-numeric elements, and sorting the remaining numbers in descending order. ...
python_sharegpt_23k
9,630
Write a Python function to generate a list of strings representing the FizzBuzz game from 1 to a given number `n`. The function should replace numbers divisible by 3 with the word "Fizz", numbers divisible by 5 with the word "Buzz", and numbers divisible by both 3 and 5 with the word "FizzBuzz". For all other numbers, ...
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 a list of strings representing the FizzBuzz game from 1 to n. Args: n (int): The upper limit of the range (inclusive). Returns: ...
python_sharegpt_23k
9,632
Write a Python function to output an array that contains all the unique prime elements from two input arrays. The function should check each element to determine if it is a prime number and ensure that the resulting array contains only unique prime numbers. The code should start with: ```python from typing import List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List 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
9,633
Write a Python function to create a new list where each element from the given list is repeated twice. The function should take a list of integers as input and return a new list with each element repeated twice. The code should start with: ```python from typing import List def repeat_elements_twice(arr: List[int]) ->...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def repeat_elements_twice(arr: List[int]) -> List[int]: """ Creates a new list where each element from the given list is repeated twice. Args: arr (List[int]): The input list of integers. ...
python_sharegpt_23k
9,634
Write a Python function to rearrange a given list of integers such that all elements divisible by 3 appear first in the list, while maintaining the original relative order of the elements. Elements not divisible by 3 should follow the elements divisible by 3, also maintaining their original relative order. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def rearrange_divisible_by_3(arr: List[int]) -> List[int]: """ Rearranges the given list of integers such that all elements divisible by 3 appear first, while maintaining the original relative order ...
python_sharegpt_23k
9,637
Write a Python function to generate a list of all odd numbers between a given start and end integer (inclusive), excluding a specified number. The function should ensure that the code runs within a time complexity of O(n), where n is the number of integers in the range. The code should start with: ```python from typin...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_odd_numbers(start: int, end: int, exclude: int) -> List[int]: """ Generate a list of all odd numbers between start and end (inclusive), excluding the specified number. Args: sta...
python_sharegpt_23k
9,638
Write a Python function to determine if there exists any pair of integers in a given array whose sum is divisible by 10. The function should handle arrays with duplicates and should be computationally efficient. The code should start with: ```python from typing import List def has_pair_sum_divisible_by_10(nums: List[...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def has_pair_sum_divisible_by_10(nums: List[int]) -> bool: """ Determines if there exists any pair of integers in the given array whose sum is divisible by 10. Args: nums (List[int]): The ar...
python_sharegpt_23k
9,642
Write a Python function to convert a list of numbers into a dictionary where the numeric values are the keys and the corresponding characters are the values. However, only include the numbers that are prime numbers as keys in the dictionary. The function should take two lists as input: one containing the numbers and th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict 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...
python_sharegpt_23k
9,647
Write a Python function to create a new list where each element is doubled from a given list, and the resulting list is sorted in ascending order. The original list should not be modified. The function should use list comprehension for the manipulation. The code should start with: ```python from typing import List de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def double_and_sort(input_list: List[int]) -> List[int]: """ Creates a new list where each element is doubled from the given list, and the resulting list is sorted in ascending order. Args: ...
python_sharegpt_23k
9,649
Write a Python function to find the index of a given string in a list of strings. If the string is not present in the list, return -1. The function should be case-sensitive, meaning that "apple" and "Apple" should be treated as different strings. The code should start with: ```python from typing import List def find_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_string_index(list_of_strings: List[str], target_string: str) -> int: """ Find the index of a given string in a list of strings. If the string is not present, return -1. Args: list_o...
python_sharegpt_23k
9,652
Write a Python function that prints out numbers from 0 to a given limit `n`, but skips specific numbers and every even number after the skipped numbers. The function should skip the numbers 6, 7, and 12, and skip every even number after these skipped numbers. The function should start with: ```python def custom_loop(n:...
Here is the Python code to solve the given problem in the markdown block: ```python def custom_loop(n: int) -> None: """ Prints numbers from 0 to n, skipping numbers 6, 7, and 12, and skipping every even number after these skipped numbers. Args: n (int): The upper limit of the range (inclusive). ...
python_sharegpt_23k
9,653
Write a Python function to find the longest common substring of a given list of integers, where the substring consists of consecutive integers in strictly increasing order. If there are multiple substrings with the same maximum length, return the substring that occurs first in the list. The code should start with: ```...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def longest_increasing_substring(nums: List[int]) -> List[int]: """ Find the longest common substring of a list of integers where the substring consists of consecutive integers in strictly increasing ord...
python_sharegpt_23k
9,654
Write a Python function to reverse a given string without using the built-in `reverse()` function or slicing. The function should iterate through the string and construct the reversed string manually. The code should start with: ```python def reverse_string(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(s: str) -> str: """ Reverses a given string without using the built-in `reverse()` function or slicing. Args: s (str): The input string to be reversed. Returns: str: The reversed stri...
python_sharegpt_23k
9,656
Write a Python function to create a nested dictionary structure representing a person's information, including their name, age, and address. The address should be a nested dictionary containing the street, city, and country. The function should return the constructed dictionary. The code should start with: ```python f...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def create_person_info(name: str, age: int, street: str, city: str, country: str) -> Dict: """ Creates a nested dictionary structure representing a person's information. Args: name (str): Th...
python_sharegpt_23k
9,661
Write a Python function to create and initialize a two-dimensional array (matrix) of size `m x n` with randomly generated integers between a specified range `[min_value, max_value]`. The function should return the initialized matrix. The code should start with: ```python from typing import List import random def crea...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def create_random_matrix(m: int, n: int, min_value: int, max_value: int) -> List[List[int]]: """ Create and initialize a two-dimensional array of size m x n with randomly generated integers...
python_sharegpt_23k
9,668
Write a Python function to check if a given key exists in a dictionary. The function should return a boolean value indicating whether the key exists in the dictionary or not. The code should start with: ```python def key_exists(dictionary: dict, key_to_check: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python def key_exists(dictionary: dict, key_to_check: str) -> bool: """ Check if a given key exists in the dictionary. Args: dictionary (dict): The dictionary in which to check for the key. key_to_check (str): The ...
python_sharegpt_23k
9,669
Write a Python function that takes a NumPy array as input and converts it to a list. The function should sort the elements in descending order before converting the array to a list. The code should start with: ```python import numpy as np from typing import List def convert_and_sort_array(array: np.ndarray) -> List[in...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np from typing import List def convert_and_sort_array(array: np.ndarray) -> List[int]: """ Converts a NumPy array to a list and sorts the elements in descending order. Args: array (np.ndarray): The ...
python_sharegpt_23k
9,670
Write a Python function to capitalize the first letter of each word in a list of strings and convert all remaining letters to lowercase. The function should handle cases where words may contain special characters or numbers. The code should start with: ```python from typing import List def capitalize_words(words: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def capitalize_words(words: List[str]) -> List[str]: """ Capitalizes the first letter of each word in a list of strings and converts all remaining letters to lowercase. Args: words (List[str...
python_sharegpt_23k
9,672
Write a Python function to transpose a given 4x4 2D array. The function should correctly swap the elements across the main diagonal to produce the transposed matrix. The code should start with: ```python from typing import List def transpose_4x4_matrix(matrix: List[List[int]]) -> List[List[int]]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def transpose_4x4_matrix(matrix: List[List[int]]) -> List[List[int]]: """ Transposes a given 4x4 2D array. Args: matrix (List[List[int]]): A 4x4 2D array to be transposed. Returns: ...
python_sharegpt_23k
9,675
Write a Python function that uses nested ternary operators to determine the classification of a given number based on its properties. The function should classify the number into one of four categories: "Even and Positive", "Even", "Positive", or "Neither Even nor Positive". The function should use two user-defined hel...
Here is the Python code to solve the given problem in the markdown block: ```python def is_even(number: int) -> bool: """ Check if a number is even. Args: number (int): The number to check. Returns: bool: True if the number is even, False otherwise. """ return number % 2 == 0 ...
python_sharegpt_23k
9,676
Write a Python function to create a `rows x cols` matrix initialized with random integer values between 1 and 10 (inclusive). The function should ensure that the matrix is represented as a nested list, where each sublist represents a row in the matrix. The function should also handle the constraints where the number of...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def create_random_matrix(rows: int, cols: int) -> List[List[int]]: """ Creates a `rows x cols` matrix initialized with random integer values between 1 and 10 (inclusive). Args: ...
python_sharegpt_23k
9,678
Write a Python function that takes a string as an argument and replaces all occurrences of the lowercase letter 'e' with an asterisk ('*'). If the input string is empty or `None`, the function should return `None`. Additionally, if the string contains any uppercase 'E', the function should raise a `ValueError` with the...
Here is the Python code to solve the given problem in the markdown block: ```python def replace_e(string: str) -> str: """ Replaces all occurrences of the lowercase letter 'e' with an asterisk ('*') in the given string. If the string is empty or None, returns None. Raises a ValueError if the string cont...
python_sharegpt_23k
9,679
Write a Python function to find the common elements between two lists, ensuring that the result contains no duplicates and is sorted in ascending order. The function should have a time complexity of O(n log n), where n is the total number of elements in both lists. 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 find_common_elements(list1: List[int], list2: List[int]) -> List[int]: """ Find the common elements between two lists, ensuring no duplicates and sorted in ascending order. Args: list1 (...
python_sharegpt_23k
9,681
Write a Python function to reverse a given array without using any built-in array reversing functions or methods. The function should take a list of integers as input and return the reversed list. The code should start with: ```python from typing import List def reverse_array(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_array(arr: List[int]) -> List[int]: """ Reverses the given array in place without using any built-in array reversing functions or methods. Args: arr (List[int]): The array to be ...
python_sharegpt_23k
9,683
Write a Python function to insert `n` new elements at the beginning of a given list, where `n` is the length of the list. Each new element should be in the format `"x" + str(i)`, where `i` is the index of the new element starting from 1. The function should maintain the original elements of the list and ensure that the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_n_elements_at_beginning(lst: List[str]) -> List[str]: """ Inserts n new elements at the beginning of the given list, where n is the length of the list. Each new element is in the format "x...
python_sharegpt_23k
9,690
Write a Python function to calculate the largest possible number from a given list of integers, considering that the numbers can be negative as well. The function should handle if the list contains duplicates. The code should start with: ```python from typing import List def calculate_largest_number(numbers: List[int]...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_largest_number(numbers: List[int]) -> int: """ Calculate the largest possible number from a given list of integers, considering that the numbers can be negative as well. The function ha...
python_sharegpt_23k
9,692
Write a Python function to convert a given decimal value to its binary equivalent. The function should have a time complexity of O(log n), where n is the decimal value. The function should handle both positive and non-positive decimal values. The code should start with: ```python def decimal_to_binary(n: int) -> str: ...
Here is the Python code to solve the given problem in the markdown block: ```python def decimal_to_binary(n: int) -> str: """ Converts a given decimal value to its binary equivalent. Args: n (int): The decimal value to be converted. Returns: str: The binary equivalent of the decimal va...
python_sharegpt_23k
9,693
Write a Python function to merge two sorted linked lists into a single sorted linked list. The function should return the head node of the merged list. The linked list nodes are defined as follows: ```python class ListNode: def __init__(self, data=0, next=None): self.data = data self.next = next ```...
Here is the Python code to solve the given problem in the markdown block: ```python class ListNode: def __init__(self, data=0, next=None): self.data = data self.next = next def merge_sorted_lists(list1: ListNode, list2: ListNode) -> ListNode: """ Merge two sorted linked lists into a single ...
python_sharegpt_23k
9,699
Write a Python function to determine if a given positive integer is a prime number. A prime number is defined as a number greater than 1 that has no positive divisors other than 1 and itself. The function should return `True` if the number is prime and `False` otherwise. The code should start with: ```python import ma...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_prime(n: int) -> bool: """ Determines if 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 ...
python_sharegpt_23k
9,708
Write a Python function that takes a list of characters from the Lord of the Rings and returns an array of their corresponding nicknames. The function should have a time complexity of O(n), where n is the number of characters in the input list. The nicknames should be returned in alphabetical order. The input list may ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_character_nicknames(characters: List[str]) -> List[str]: """ Returns an array of unique nicknames corresponding to the given characters from the Lord of the Rings, sorted alphabetically. Arg...
python_sharegpt_23k
9,711
Write a Python function to reverse each word in a given string and return the modified string. The function should handle any string input, including strings with multiple spaces, leading/trailing spaces, and empty strings. The code should start with: ```python def reverse_words_in_string(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_words_in_string(s: str) -> str: """ Reverses each word in the given string and returns the modified string. Args: s (str): The input string containing words to be reversed. Returns: str: The...
python_sharegpt_23k
9,712
Write a Python function to reverse each character of each element in a given array without using any built-in functions or methods to reverse the characters. The function should start with: ```python from typing import List def reverse_each_element(array: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_each_element(array: List[str]) -> List[str]: """ Reverses each character of each element in the given array. Args: array (List[str]): The array of strings to be processed. R...
python_sharegpt_23k
9,714
Write a Python function to safely add two large integers, `a` and `b`, where each integer can have a maximum value of \(10^{18}\). The function should handle potential overflow issues by using Python's built-in capabilities for handling large integers. The code should start with: ```python def safe_large_integer_addit...
Here is the Python code to solve the given problem in the markdown block: ```python def safe_large_integer_addition(a: int, b: int) -> int: """ Safely adds two large integers, a and b, where each integer can have a maximum value of 10^18. Args: a (int): The first large integer. b (int): The...
python_sharegpt_23k
9,719
Write a Python function to find the index of a target number in a list. If the target number is not found, the function should return the string "Index not found". The function should handle the case where the target number is not in the list without using exception handling. 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, Union def find_index(nums: List[int], target: int) -> Union[int, str]: """ Find the index of a target number in a list. If the target number is not found, return "Index not found". Args: nu...
python_sharegpt_23k
9,722
Write a Python function to calculate the median of a given list of numbers without using any built-in functions or libraries that directly calculate the median. The input list will always have an odd number of elements. The function should sort the list using a custom implementation of the bubble sort algorithm and the...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_median(nums: List[int]) -> int: """ Calculate the median of a given list of numbers without using built-in functions or libraries. Args: nums (List[int]): The list of numbers with a...
python_sharegpt_23k
9,723
Write a Python function to remove elements that occur only once from a given array without modifying the original array. Instead, create a new array that contains only the elements that occur more than once. The original array should remain unchanged. The code should start with: ```python from typing import List def ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_non_repeating_elements(arr: List[int]) -> List[int]: """ Removes elements that occur only once from the given array and returns a new array with the remaining elements. The original array ...
python_sharegpt_23k
9,725
Write a Python function to find all duplicate elements in a given list of integers with a worst-case time complexity of O(n^2), where n is the size of the input. The function should return a list of all unique duplicate elements found in the input list. The code should start with: ```python from typing import List de...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_duplicates_quadratic(arr: List[int]) -> List[int]: """ Find all unique duplicate elements in the given list with a worst-case time complexity of O(n^2). Args: arr (List[int]): The l...
python_sharegpt_23k
9,727
Write a Python function to determine if a given number occurs more than twice in a list. The function should have a time complexity of O(n) and should not use any built-in functions or libraries that provide direct solutions to the problem. The code should start with: ```python from typing import List def occurs_more_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def occurs_more_than_twice(lst: List[int], number: int) -> bool: """ Determines if the given number occurs more than twice in the list. Args: lst (List[int]): The list of integers to check. ...
python_sharegpt_23k
9,729
Write a Python function to remove all occurrences of a specified element from a given list while maintaining the original order of the remaining elements. 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 remove_all...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_all_occurrences(element: int, lst: List[int]) -> List[int]: """ Removes all occurrences of the specified element from the given list while maintaining the original order of the remaining eleme...
python_sharegpt_23k
9,730
Write a Python function to sort a given list of integers in ascending order using the Merge Sort algorithm. The function should not use any built-in sorting functions or libraries. The code should start with: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using the Merge Sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Re...
python_sharegpt_23k
9,732
Write a Python function to transform a given list such that all elements are converted to their absolute values and any duplicate elements are removed. The function should return the modified list in ascending order. The code should start with: ```python from typing import List def transform_list(lst: List[int]) -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def transform_list(lst: List[int]) -> List[int]: """ Transforms the given list by converting all elements to their absolute values, removing any duplicate elements, and returning the modified list in...
python_sharegpt_23k
9,736
Write a Python function to generate a histogram from a given list of integers. The function should return a dictionary where the keys are the unique integers from the input list and the values are the frequencies of those integers in the input list. The function should handle invalid inputs gracefully by raising a `Val...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def generate_histogram(data: List[int]) -> Dict[int, int]: """ Generates a histogram from a given list of integers. Args: data (List[int]): A list of integers for which the histogram i...
python_sharegpt_23k
9,739
Write a Python function to combine two lists of strings without duplicates, and the resulting list should only contain strings that are palindromes. The function should start with: ```python from typing import List def combine_palindromes(list1: List[str], list2: List[str]) -> List[str]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def combine_palindromes(list1: List[str], list2: List[str]) -> List[str]: """ Combines two lists of strings and returns a list containing only unique palindromes. Args: list1 (List[str]): Th...
python_sharegpt_23k
9,740
Write a Python function to sort a list containing numbers and a list containing strings together, in ascending order. The sorting should be based on the sum of the ASCII values of the characters in the strings, rather than the alphabetical order of the strings. Additionally, the numbers in the number list should be sor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def sort_mixed_list(num_list: List[int], string_list: List[str]) -> List[Union[int, str]]: """ Sorts a list containing numbers and a list containing strings together, in ascending order. The s...
python_sharegpt_23k
9,741
Write a Python function to flatten a given two-dimensional array and exclude any negative numbers from the result. The function should return a one-dimensional list containing only the non-negative elements from the input array. The code should start with: ```python from typing import List def flatten_and_exclude_neg...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def flatten_and_exclude_negatives(arr: List[List[int]]) -> List[int]: """ Flattens a given two-dimensional array and excludes any negative numbers. Args: arr (List[List[int]]): A two-dimensi...