source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
8,938
Write a Python function that extracts all integer values from a given format string, considering any placeholders indicated by `%i`. The function should return a list of integers found in the string. The code should start with: ```python from typing import List def extract_integers_from_string(format_string: str) -> L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import re def extract_integers_from_string(format_string: str) -> List[int]: """ Extracts all integer values from a given format string, considering any placeholders indicated by '%i'. Args: ...
python_sharegpt_23k
8,940
Write a Python function to compare two strings and return the index of the first character that is unequal in the two strings. If the strings are identical, return -1. If the strings are of different lengths and all characters up to the length of the shorter string are equal, return the length of the shorter string. T...
Here is the Python code to solve the given problem in the markdown block: ```python def find_first_unequal_index(string1: str, string2: str) -> int: """ Compare two strings and return the index of the first character that is unequal. If the strings are identical, return -1. If the strings are of different l...
python_sharegpt_23k
8,941
Write a Python function to convert an array of tuples of 2 numbers into an array of complex numbers, and then sort the resulting array based on the magnitude of the complex numbers in descending order. The function should return the sorted array of complex numbers. 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, Tuple import math def convert_and_sort_complex_numbers(tuple_array: List[Tuple[int, int]]) -> List[complex]: """ Converts an array of tuples of 2 numbers into an array of complex numbers, and sorts ...
python_sharegpt_23k
8,942
Write a Python function to create a list of unique elements from a given list of integers. The function should not use any built-in functions or libraries to achieve this. Instead, implement your own algorithm to find the unique elements. The input list will always be non-empty and will only contain integer elements. ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_unique_elements(nums: List[int]) -> List[int]: """ Returns a list of unique elements from the given list of integers. Args: nums (List[int]): The input list of integers. Return...
python_sharegpt_23k
8,946
Write a Python function to construct a dictionary from a provided list of tuples such that the keys and values are switched. The tuples will contain strings and integers. The keys in the original tuples will have unique values. The dictionary should be sorted in ascending order based on the original values in the tuple...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict def switch_keys_and_values(tuples: List[Tuple[str, int]]) -> Dict[int, str]: """ Constructs a dictionary from a list of tuples such that the keys and values are switched. The resulting d...
python_sharegpt_23k
8,947
Write a Python function to calculate the perimeter of a triangle given its three side lengths. The side lengths must be positive integers. If any side length is not a positive integer, the function should return an error message. The code should start with: ```python def calculate_triangle_perimeter(side1: int, side2:...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_triangle_perimeter(side1: int, side2: int, side3: int) -> int or str: """ Calculate the perimeter of a triangle given its three side lengths. Args: side1 (int): The length of the first side of the tria...
python_sharegpt_23k
8,948
Write a Python function to filter prime numbers from a given list and return a new list containing only the prime numbers. The function should exclude any negative numbers and raise a `ValueError` if the list is empty or contains any non-integer values. The output list should be sorted in descending order. The code sh...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_prime_numbers(lst: List[int]) -> List[int]: """ Filters prime numbers from a given list and returns a new list containing only the prime numbers. The output list is sorted in descending or...
python_sharegpt_23k
8,950
Write a Python function to replace each character of a given string with the next character in the alphabetic order, while maintaining the case of the original character. Non-alphabetic characters should remain unchanged. The function should start with: ```python def replace_with_next_char(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def replace_with_next_char(string: str) -> str: """ Replace each alphabetic character in the input string with the next character in the alphabetic order, while maintaining the case of the original character. Non-alphabetic ...
python_sharegpt_23k
8,957
Write a Python function to efficiently check if a given number is an Armstrong number. An Armstrong number (also known as a narcissistic number) is a number that is the sum of its own digits each raised to the power of the number of digits. The function should have a time complexity of O(log n), where n is the number t...
Here is the Python code to solve the given problem in the markdown block: ```python def is_armstrong_number(num: int) -> bool: """ Check if a given number is an Armstrong number. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. Args: ...
python_sharegpt_23k
8,958
Write a Python function to reverse a given string without using any loops or recursion. The function should use slicing to achieve the reversal. The code should start with: ```python def reverse_string(input_str: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def reverse_string(input_str: str) -> str: """ Reverses the given string using slicing. Args: input_str (str): The string to be reversed. Returns: str: The reversed string. """ # Use slicing t...
python_sharegpt_23k
8,961
Write a Python function to find the first occurrence of a target element in a sorted array using Binary Search. The function should return the index of the first occurrence of the target element if it exists in the array, otherwise it should return -1. 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 find_first_occurrence(arr: List[int], target: int) -> int: """ Find the first occurrence of a target element in a sorted array using Binary Search. Args: arr (List[int]): The sorted arra...
python_sharegpt_23k
8,962
Write a Python function to reverse a given string without using any built-in string reversal functions or methods. The function should use a two-pointer technique to swap characters from both ends of the string until the pointers meet or cross each other. The code should start with: ```python def reverse_string(input_...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(input_str: str) -> str: """ Reverses the given string using a two-pointer technique without using any built-in string reversal functions or methods. Args: input_str (str): The string to be reverse...
python_sharegpt_23k
8,963
Modify the given function `set_parameter` to set the third parameter `c` to 0 without using any conditional statements (e.g., `if`, `else`, `switch`) or arithmetic operators (e.g., `+`, `-`, `*`, `/`, `%`). The function should still take three parameters `a`, `b`, and `c`, and return the modified value of `c`. The cod...
Here is the Python code to solve the given problem in the markdown block: ```python def set_parameter(a, b, c): """ Set the third parameter `c` to 0 without using conditional statements or arithmetic operators. Args: a (int): The first parameter. b (int): The second parameter. c (in...
python_sharegpt_23k
8,964
Write a Python function to reverse a list of numbers without using any built-in functions or methods. The function should only use a single loop and a constant amount of extra space. The time complexity should be O(n), where n is the length of the input list. The code should start with: ```python from typing import Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_list(nums: List[int]) -> List[int]: """ Reverses a list of numbers in place without using any built-in functions or methods. Args: nums (List[int]): The list of numbers to be rev...
python_sharegpt_23k
8,965
Write a Python function to remove all prime numbers from a given list of integers. The function should return a new list containing only the non-prime numbers from the original list. The code should start with: ```python from typing import List def remove_prime_numbers(arr: List[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 is_prime(num: int) -> bool: """ Check if a number is prime. Args: num (int): The number to check. Returns: bool: True if the number is prime, False otherwise. ...
python_sharegpt_23k
8,972
Write a Python function to filter an array of strings and return only those strings that start with a specified character. The function should be efficient and handle various edge cases, such as an empty array or a character that does not match any string in the array. The code should start with: ```python from typing...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_strings_by_char(strings: List[str], char: str) -> List[str]: """ Filters an array of strings and returns only those strings that start with the specified character. Args: strings ...
python_sharegpt_23k
8,973
Write a Python function to create an array of size `n` filled with unique random integer numbers between `0` and `m-1`, without using any loops. The function should use the `numpy` library to achieve this. The code should start with: ```python import numpy as np def create_unique_random_array(n: int, m: int) -> np.nd...
Here is the Python code to solve the given problem in the markdown block: ```python import numpy as np def create_unique_random_array(n: int, m: int) -> np.ndarray: """ Creates an array of size `n` filled with unique random integer numbers between `0` and `m-1`. Args: n (int): The size of the arra...
python_sharegpt_23k
8,976
Implement a stable sorting algorithm that sorts an array of integers in ascending order with a time complexity of O(n log n). The algorithm must not use any built-in sorting functions or libraries. The input array may contain duplicate values, and the algorithm must maintain the relative order of elements with equal va...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def stable_sort(arr: List[int]) -> List[int]: """ Sorts an array of integers in ascending order using a stable merge sort algorithm. Args: arr (List[int]): The array of integers to be sorted...
python_sharegpt_23k
8,979
Write a Python function to count the occurrences of each character in a given string and return a dictionary where the keys are the unique characters in the string and the values are the counts of each character. The function should handle any string input, including strings with special characters, spaces, and numbers...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def count_character_occurrences(string: str) -> Dict[str, int]: """ Count the occurrences of each character in the given string and return a dictionary. Args: string (str): The input string ...
python_sharegpt_23k
8,982
Write a Python function to calculate the total number of unique elements in a given array of sub-arrays, where each sub-array contains exactly two elements. The elements in each sub-array are restricted to be positive integers greater than zero. The code should start with: ```python from typing import List def count_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_unique_elements(array: List[List[int]]) -> int: """ Calculate the total number of unique elements in a given array of sub-arrays. Each sub-array contains exactly two elements, and the elem...
python_sharegpt_23k
8,983
Write a Python function to group elements of a given array into pairs and return a list of these pairs. If the array has an odd number of elements, the last element should be ignored. The code should start with: ```python from typing import List, Tuple def group_elements_into_pairs(arr: List[int]) -> List[Tuple[int, ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def group_elements_into_pairs(arr: List[int]) -> List[Tuple[int, int]]: """ Groups elements of a given array into pairs and returns a list of these pairs. If the array has an odd number of ele...
python_sharegpt_23k
8,984
Write a Python function to flatten a deeply nested list into a single list. The function should handle lists of arbitrary depth and structure, including lists within lists, and return a single list containing all the elements in a flat structure. The code should start with: ```python from typing import List, Any def ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def flatten_nested_list(nested_list: List[Any]) -> List[Any]: """ Flattens a deeply nested list into a single list. Args: nested_list (List[Any]): The nested list to be flattened. ...
python_sharegpt_23k
8,988
Write a Python function to remove all occurrences of specified characters from a given string. The function should take two parameters: the first parameter is the string from which characters need to be removed, and the second parameter is a list of characters to be removed. The function should return the modified stri...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_characters(name: str, characters: list) -> str: """ Removes all occurrences of specified characters from a given string. Args: name (str): The string from which characters need to be removed. char...
python_sharegpt_23k
8,990
Write a Python function to generate a one-dimensional array of a specified length, filled with unique random numbers between a given range. The function should ensure that no numbers are repeated within the array. The code should start with: ```python from typing import List import random def generate_unique_random_nu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def generate_unique_random_numbers(length: int, lower_bound: int, upper_bound: int) -> List[int]: """ Generates a one-dimensional array of a specified length filled with unique random numbe...
python_sharegpt_23k
8,991
Write a Python function to merge two given arrays into a new array without using any built-in array concatenation methods (e.g., `array1.concat(array2)`) or the spread operator. The function should iterate through each element of the input arrays and append them to the new array manually. 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 merge_arrays(arr1: List[int], arr2: List[int]) -> List[int]: """ Merges two given arrays into a new array without using built-in concatenation methods. Args: arr1 (List[int]): The first ...
python_sharegpt_23k
9,000
Write a Python function to determine if a given number is a prime number with a time complexity of O(sqrt(n)). The function should return `True` if the number is prime and `False` otherwise. The code should start with: ```python import math def is_prime(n: int) -> bool: ```
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 number is a prime number. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwi...
python_sharegpt_23k
9,003
Write a Python function to calculate the sum of all elements in a given list of integers. The function should handle lists of any length, including empty lists. The code should start with: ```python from typing import List def sum_list_elements(lst: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_list_elements(lst: List[int]) -> int: """ Calculate the sum of all elements in a given list of integers. Args: lst (List[int]): The list of integers to sum. Returns: int...
python_sharegpt_23k
9,004
Write a Python function to sort a given list in descending order using a modified insertion sort algorithm. The function should be implemented recursively and should not use any built-in sorting functions or methods. The function should have a time complexity of O(n^2). 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 recursive_modified_insertion_sort(arr: List[int]) -> List[int]: """ Sorts a given list in descending order using a modified insertion sort algorithm implemented recursively. Args: arr (L...
python_sharegpt_23k
9,005
Write a Python function to reverse a given string in place with a time complexity of O(n), where n is the length of the string. The function should not use any additional data structures. The code 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 given string in place with a time complexity of O(n), where n is the length of the string. Args: s (str): The string to be reversed. Returns: ...
python_sharegpt_23k
9,010
Write a Python function to reverse a given string without using any built-in string reversal functions or methods. 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 any built-in string reversal functions or methods. Args: s (str): The input string to be reversed. Returns: str: The revers...
python_sharegpt_23k
9,012
Write a Python function to find the k-th largest element in an array of integers efficiently. The function should handle edge cases such as an empty array or k being larger than the array length. The code should start with: ```python from typing import List def find_kth_largest(arr: List[int], k: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List import heapq def find_kth_largest(arr: List[int], k: int) -> int: """ Find the k-th largest element in an array of integers. Args: arr (List[int]): The array of integers. k (int): ...
python_sharegpt_23k
9,016
Write a Python function to find the minimum value from a given array without using any built-in functions or methods for finding the minimum value (e.g., `min()`, `sort()`, etc.). The function must implement an algorithm with a time complexity of O(n), where n is the number of elements in the array. The array may conta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_minimum(arr: List[int]) -> int: """ Find the minimum value in a given array without using built-in functions or methods. Args: arr (List[int]): The array of integers from which to f...
python_sharegpt_23k
9,018
Write a Python function to find the first `n` prime numbers and return them in 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 find_first_n_primes(n: int) -> List[int]: """ Finds the first n prime numbers and returns them in a list. Args: n (int): The number of prime numbers to find. Returns: List[i...
python_sharegpt_23k
9,020
Write a Python function to calculate the factorial of a given non-negative integer using a recursive approach. The function should handle the base case where the input is 0 and return 1. For other cases, it should recursively call itself to compute the factorial. The code should start with: ```python def factorial_rec...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial_recursive(n: int) -> int: """ Calculate the factorial of a given non-negative integer using a recursive approach. Args: n (int): The non-negative integer for which to calculate the factorial. Retu...
python_sharegpt_23k
9,021
Write a Python function to filter out numbers divisible by 2 from a given list and return the filtered list in a readable format. The function should handle lists of integers and return a string where each number is separated by a comma and a space. The code should start with: ```python from typing import List def fi...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def filter_odd_numbers(numbers: List[int]) -> str: """ Filters out numbers divisible by 2 from the given list and returns the filtered list as a readable string. Args: numbers (List[int]): T...
python_sharegpt_23k
9,023
Write a Python function to encrypt a given text using a Caesar cipher with a specified shift value. The Caesar cipher shifts each letter in the text by a fixed number of positions down the alphabet. Non-alphabetic characters should remain unchanged. The function should handle both uppercase and lowercase letters correc...
Here is the Python code to solve the given problem in the markdown block: ```python def caesar_cipher(text: str, shift: int) -> str: """ Encrypts the given text using a Caesar cipher with the specified shift value. Args: text (str): The text to be encrypted. shift (int): The number of posit...
python_sharegpt_23k
9,024
Write a Python function to extract all strings from a given text block that match a specific regex pattern and sort them in ascending order based on a 4-digit integer found in each string. The pattern is "name #number", where the name can be any alphanumeric characters and the number is a 4-digit integer. The function ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import re def extract_and_sort_strings(text_block: str) -> List[Tuple[str, str]]: """ Extracts all strings from a given text block that match the pattern "name #number" and sorts them in ascen...
python_sharegpt_23k
9,025
Write a Python function to generate an HTML button tag that redirects the user to a specified URL when clicked. The function should take a URL as input and return the HTML code for the button. The code should start with: ```python def generate_button_html(url: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def generate_button_html(url: str) -> str: """ Generates an HTML button tag that redirects the user to the specified URL when clicked. Args: url (str): The URL to which the button should redirect when clicked. ...
python_sharegpt_23k
9,027
Write a Python function to generate a multi-dimensional array where each element is a randomly generated integer between 1 and 100. The array should have dimensions of at least 5x5 and should be populated in a way that no two elements in the same row or column have the same value. The function should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import numpy as np def generate_unique_array(rows: int, cols: int) -> np.ndarray: """ Generates a multi-dimensional array where each element is a randomly generated integer between 1 and 100. No two ...
python_sharegpt_23k
9,033
Write a Python function to implement the bottom-up cut rod algorithm, which calculates the maximum revenue obtainable by cutting a rod of length `n` into pieces, given a price list for each possible piece length. The algorithm should have a time complexity of O(n^2) and a space complexity of O(n). The code should star...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def max_rod_revenue(price: List[int], n: int) -> int: """ Calculate the maximum revenue obtainable by cutting a rod of length n into pieces, given a price list for each possible piece length. Ar...
python_sharegpt_23k
9,040
Write a Python function to determine the eligibility of a person to vote based on their age. The function should return a string indicating whether the person is eligible to vote or not. The function should handle both cases where the age is exactly 18 and where it is less than 18. The code should start with: ```pytho...
Here is the Python code to solve the given problem in the markdown block: ```python def check_voting_eligibility(age: int) -> str: """ Determine the eligibility of a person to vote based on their age. Args: age (int): The age of the person. Returns: str: A message indicating whether th...
python_sharegpt_23k
9,042
Write a Python function to traverse through a given list and return a list of even elements, while also counting the number of even elements encountered. The function should start with: ```python from typing import List def get_even_elements(arr: List[int]) -> (List[int], int): ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_even_elements(arr: List[int]) -> (List[int], int): """ Traverse through the given list and return a list of even elements, along with the count of even elements encountered. Args: ...
python_sharegpt_23k
9,045
Write a Python function to calculate the factorial of a given non-negative integer using a recursive approach. The function should handle edge cases such as input being 0 or 1 correctly. The code should start with: ```python def factorial_recursive(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def factorial_recursive(n: int) -> int: """ Calculate the factorial of a given non-negative integer using a recursive approach. Args: n (int): The non-negative integer for which to calculate the factorial. Re...
python_sharegpt_23k
9,046
Write a Python function to find the two largest numbers in a given list of integers without using any comparison operators. The list can contain both positive and negative integers, and the numbers can be repeated. The solution should have a time complexity of O(n) and a space complexity of O(1). The code should start...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_two_numbers(arr: List[int]) -> List[int]: """ Find the two largest numbers in a list without using any comparison operators. Args: arr (List[int]): The list of integers from whi...
python_sharegpt_23k
9,047
Write a Python function to replace all occurrences of a specified element in a given list with another specified element. The function should have a time complexity of O(n), where n is the length of the list. The function should not modify the original list but return a new list with the replacements. The code should ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def replace_elements(lst: List[int], old_element: int, new_element: int) -> List[int]: """ Replaces all occurrences of a specified element in a given list with another specified element. Returns a ne...
python_sharegpt_23k
9,052
Write a Python function to sort a given list of integers in ascending order. The function should return the sorted list. The code should start with: ```python from typing import List def sort_list(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_list(arr: List[int]) -> List[int]: """ Sorts a given list of integers in ascending order. Args: arr (List[int]): The list of integers to be sorted. Returns: List[int]: ...
python_sharegpt_23k
9,054
Write a Python function to classify a list of animals into two categories: "mammal" and "reptile". The function should return a dictionary with these two keys, each containing a list of animals that belong to that category. The function should handle cases where the input list is empty by raising an exception. The inpu...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def classify_animals(animals: List[str]) -> Dict[str, List[str]]: """ Classify a list of animals into "mammal" and "reptile" categories. Args: animals (List[str]): A list of animal nam...
python_sharegpt_23k
9,055
Write a Python function to find the index of the first occurrence of a given word in a text, while handling cases where the word is followed by punctuation marks or is part of a larger word. The function should be case-insensitive and should return the index of the first valid occurrence or -1 if the word is not found....
Here is the Python code to solve the given problem in the markdown block: ```python def find_word_index(text: str, word: str) -> int: """ Find the index of the first occurrence of a given word in a text, handling cases where the word is followed by punctuation marks or is part of a larger word. Args: ...
python_sharegpt_23k
9,059
Write a Python function to validate if a given string is a valid email address according to the specified format. The email must follow the format "username@domain.com", where: - The username can only contain lowercase letters, numbers, periods (`.`), underscores (`_`), and hyphens (`-`), and must start and end with a ...
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 a given string is a valid email address according to the specified format. Args: email (str): The email address to validate. Returns: ...
python_sharegpt_23k
9,060
Write a Python function to count the number of characters that are different between two given strings. The strings may contain any printable ASCII characters and can have a length of up to \(10^6\) characters. The function should handle strings of different lengths by considering the extra characters in the longer str...
Here is the Python code to solve the given problem in the markdown block: ```python def count_different_characters(str1: str, str2: str) -> int: """ Counts the number of characters that are different between two given strings. Args: str1 (str): The first string to compare. str2 (str): The s...
python_sharegpt_23k
9,061
Write a Python function to determine if a given string is a palindrome, ignoring punctuation and white spaces. The function should return `True` if the string is a palindrome and `False` otherwise. The code should start with: ```python import re def is_palindrome(word: str) -> bool: ```
Here is the Python code to solve the given problem in the markdown block: ```python import re def is_palindrome(word: str) -> bool: """ Determine if a given string is a palindrome, ignoring punctuation and white spaces. Args: word (str): The string to check for palindrome property. Returns: ...
python_sharegpt_23k
9,064
Write a Python function to extract the city name from a given URL, which follows the format "https://www.example.com/[city_name]". The function should be able to handle URLs with multiple segments before the city name and with or without the "www" prefix. The city name can consist of multiple words separated by undersc...
Here is the Python code to solve the given problem in the markdown block: ```python import re def extract_city_name(url: str) -> str: """ Extracts the city name from a given URL that follows the format "https://www.example.com/[city_name]". Args: url (str): The URL from which to extract the city n...
python_sharegpt_23k
9,066
Write a Python function to create an array named `students` which contains the elements `"John"`, `"Jane"`, `"Emily"`, and `"Michael"`. Each element in the array should be a dictionary with the following keys: `name`, `age`, and `grade`. The values for `name` should be the same as the element value (e.g., `"John"`), th...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict import random def create_students_array() -> List[Dict[str, object]]: """ Creates an array of student dictionaries with randomly generated ages and grades. Returns: List[Dict[str, obje...
python_sharegpt_23k
9,071
Write a Python function to generate all unique 5-digit combinations where each digit can be between 0 and 9. The function should return a list of strings, where each string represents a unique 5-digit combination. The code should start with: ```python from typing import List def generate_unique_5_digit_combos() -> Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import itertools def generate_unique_5_digit_combos() -> List[str]: """ Generate all unique 5-digit combinations where each digit can be between 0 and 9. Returns: List[str]: A list of string...
python_sharegpt_23k
9,077
Write a Python function to generate the first `n` prime numbers. The function should return a list of these prime numbers. The code should start with: ```python from typing import List def generate_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 otherwise...
python_sharegpt_23k
9,081
Write a Python function to find all the factors of a given number, excluding 1 and the number itself. The function should return a list of these factors. The code should start with: ```python from typing import List def get_factors_excluding_extremes(num: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: : ```python from typing import List def get_factors_excluding_extremes(num: int) -> List[int]: """ Returns a list of all factors of the given number, excluding 1 and the number itself. Args: num (int): The number for which t...
python_sharegpt_23k
9,082
Write a Python function to perform a linear search algorithm that returns the index of the first occurrence of the target element in the given array. The target element can be either a string or an integer. If the target element is not present in the array, the function should return -1. The function should handle arra...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Union def linear_search(arr: List[Union[int, str]], target: Union[int, str]) -> int: """ Perform a linear search to find the index of the first occurrence of the target element in the array. Args: ...
python_sharegpt_23k
9,096
Write a Python function to calculate the sum of all elements in a given integer array without using any built-in functions or libraries. The function should implement the summing logic manually. The code should start with: ```python from typing import List def sum_array(arr: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_array(arr: List[int]) -> int: """ Calculate the sum of all elements in a given integer array without using built-in functions or libraries. Args: arr (List[int]): The integer array w...
python_sharegpt_23k
9,097
Write a Python function to calculate the least common multiple (LCM) of three positive integers. The function should use the Euclidean algorithm to compute the greatest common divisor (GCD) and then use the GCD to compute the LCM. The code should start with: ```python def lcm_of_three(a: int, b: int, c: int) -> int: ``...
Here is the Python code to solve the given problem in the markdown block: ```python def lcm_of_three(a: int, b: int, c: int) -> int: """ Calculate the least common multiple (LCM) of three positive integers. Args: a (int): The first positive integer. b (int): The second positive integer. ...
python_sharegpt_23k
9,099
Write a Python function to implement the selection sort algorithm correctly. The function should sort a list of integers in ascending order. The code should start with: ```python from typing import List def selection_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 selection_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers in ascending order using the selection sort algorithm. Args: arr (List[int]): The list of integers to be sorted....
python_sharegpt_23k
9,100
Write a Python function to calculate the largest common divisor (GCD) of two integers, A and B, without using any built-in functions or libraries for calculating the GCD. The function should use the Euclidean algorithm to find the GCD. 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 integers A and B using the Euclidean algorithm. Args: A (int): The first integer. B (int): The second integer. Retu...
python_sharegpt_23k
9,101
Write a Python function to count the number of elements in a given list that are both even and greater than 10. The function should take a list of integers as input and return an integer representing the count of such elements. The code should start with: ```python from typing import List def count_even_greater_than_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def count_even_greater_than_10(arr: List[int]) -> int: """ Counts the number of elements in the given list that are both even and greater than 10. Args: arr (List[int]): The list of integers...
python_sharegpt_23k
9,102
Write a Python function to check if a given number is a prime number within a specified range. The function should return `True` if the number is prime within the range, and `False` otherwise. The range is inclusive of both the start and end values. The code should start with: ```python def is_prime_in_range(n: int, s...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime_in_range(n: int, start: int, end: int) -> bool: """ Check if a given number is a prime number within a specified range. Args: n (int): The number to check for primality. start (int): The start o...
python_sharegpt_23k
9,104
Write a Python function to evaluate a mathematical expression of the form `a * (b + c)`, where `a`, `b`, and `c` are positive integers ranging from 1 to 100. The function should validate the input values and return an error message if any of the values are outside the specified range. If the inputs are valid, the funct...
Here is the Python code to solve the given problem in the markdown block: ```python def evaluate_expression(a: int, b: int, c: int) -> int or str: """ Evaluate the mathematical expression a * (b + c) where a, b, and c are positive integers between 1 and 100. Args: a (int): The first integer in the ...
python_sharegpt_23k
9,105
Write a Python function to print the elements of a given list in reverse order without using any built-in functions or methods such as `reverse()`, `[::-1]`, or `reversed()`. The function should iterate through the list manually and print each element from the last to the first. 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 print_reverse_list(lst: List[int]) -> None: """ Prints the elements of the given list in reverse order without using any built-in functions or methods such as `reverse()`, `[::-1]`, or `reversed()`. ...
python_sharegpt_23k
9,107
Write a Python function to remove all duplicate elements from a given list of integers while maintaining the order of the first occurrence of each element. 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_dup...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_in_order(arr: List[int]) -> List[int]: """ Removes duplicate elements from the given list while maintaining the order of the first occurrence of each element. Args: arr...
python_sharegpt_23k
9,112
Write a Python function to find the maximum value in a given list of integers using a recursive approach. The function should run in O(N) time complexity and use constant space complexity. The code should start with: ```python from typing import List def find_max_recursive(arr: List[int], start: int, end: int) -> int:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_recursive(arr: List[int], start: int, end: int) -> int: """ Find the maximum value in a given list of integers using a recursive approach. Args: arr (List[int]): The list of int...
python_sharegpt_23k
9,113
Write a Python function to generate an array of elements that are the cubes of the integers between 1 and a given positive integer `n`. The function should handle cases where `n` is not a positive integer by returning an empty list. The code should start with: ```python from typing import List def generate_cubes(n: in...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_cubes(n: int) -> List[int]: """ Generates an array of elements that are the cubes of the integers between 1 and n. Args: n (int): The upper limit (inclusive) for generating cube...
python_sharegpt_23k
9,114
Write a Python function to convert an angle from degrees to radians. The function should have a time complexity of O(1) and a space complexity of O(1). The code should start with: ```python import math def degrees_to_radians(degrees: float) -> float: ```
Here is the Python code to solve the given problem in the markdown block: ```python import math def degrees_to_radians(degrees: float) -> float: """ Converts an angle from degrees to radians. Args: degrees (float): The angle in degrees to be converted. Returns: float: The angle in rad...
python_sharegpt_23k
9,115
Write a Python function to calculate the median of an array of integers without using any built-in sorting functions. The median is the middle value of a sorted array. If the array has an odd number of elements, the median is the middle element. If the array has an even number of elements, the median is the average of ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def partition(arr: List[int], low: int, high: int) -> int: """ Partition the array around the pivot element. Args: arr (List[int]): The array to be partitioned. low (int): The starti...
python_sharegpt_23k
9,116
Write a Python function to remove the last `n` characters from a given string. The function should handle cases where `n` is greater than the length of the string by returning an empty string. The function should also handle cases where `n` is less than or equal to zero by returning the original string. The code shoul...
Here is the Python code to solve the given problem in the markdown block: ```python def remove_last_n_characters(s: str, n: int) -> str: """ Removes the last `n` characters from the given string `s`. Args: s (str): The input string from which characters are to be removed. n (int): The numbe...
python_sharegpt_23k
9,117
Write a Python function to convert a binary string to an integer without using any built-in functions or libraries. The binary string consists of '0's and '1's. The function should start with: ```python def binary_to_integer(binary_str: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def binary_to_integer(binary_str: str) -> int: """ Converts a binary string to an integer without using any built-in functions or libraries. Args: binary_str (str): The binary string to be converted. Returns: ...
python_sharegpt_23k
9,118
Write a Python function to decode a Base64 string to its corresponding string representation without using the `base64` library. The function should handle padding characters correctly and return the decoded string. The code should start with: ```python def decode_base64(base64_string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def decode_base64(base64_string: str) -> str: """ Decodes a Base64 string to its corresponding string representation without using the base64 library. Args: base64_string (str): The Base64 encoded string to be decod...
python_sharegpt_23k
9,127
Write a Python function to determine if a given number is a prime number. The function should return `True` if the number is prime and `False` otherwise. The function should handle edge cases such as negative numbers, 0, and 1, which are not prime numbers. The code should start with: ```python def is_prime(n: int) -> b...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(n: int) -> bool: """ Determine if a given number is a prime number. Args: n (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. """ ...
python_sharegpt_23k
9,130
Write a Python function to filter and return a list of prime numbers from a given array of integers. The function should use a helper function to determine if a number is prime. The code should start with: ```python from typing import List def is_prime(n: int) -> bool: # Helper function to determine if a number i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(n: int) -> bool: """ Helper function to determine 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
9,135
Write a Python function to compute the sum of squares of all the even numbers from 0 up to a given integer `n`. The function should efficiently identify the even numbers within the range and calculate their sum of squares. The code should start with: ```python def sum_of_squares_even(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_squares_even(n: int) -> int: """ Compute the sum of squares of all even numbers from 0 up to a given integer n. Args: n (int): The upper limit (inclusive) for the range of numbers to consider. Return...
python_sharegpt_23k
9,136
Write a Python function to find the character with the highest frequency in a given string, considering both uppercase and lowercase letters as the same. The function should ignore non-alphabetic characters. The code should start with: ```python def find_highest_frequency_char(string: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def find_highest_frequency_char(string: str) -> str: """ Find the character with the highest frequency in a given string, considering both uppercase and lowercase letters as the same. Non-alphabetic characters are ignored. ...
python_sharegpt_23k
9,137
Write a Python function to calculate the sum of boolean values (`True` and `False`) in a list that can contain both boolean and integer elements. The function should return a tuple where the first element is the sum of all `True` values and the second element is the sum of all `False` values. The sum of a boolean value...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Union def true_false_sum(x: List[Union[bool, int]]) -> Tuple[int, int]: """ Calculate the sum of boolean values (True and False) in a list that can contain both boolean and integer elements. ...
python_sharegpt_23k
9,139
Implement a Python function to perform the quicksort algorithm on a list of integers. The function should sort the list in ascending order. The code should start with: ```python from typing import List def quicksort(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 quicksort(arr: List[int]) -> List[int]: """ Sorts a list of integers using the quicksort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: List[i...
python_sharegpt_23k
9,143
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 import math def generate_primes_in_range(start: int, end: int) -> List[int]: """ Generate a list of all prime numbers within the given range [start, end]. Args: start (int): The starting n...
python_sharegpt_23k
9,144
Write a Python function to generate a list of random integers of a specified length. Each integer in the list should be between 1 and 100, inclusive. The function should handle edge cases such as when the input length is zero or negative. The code should start with: ```python from typing import List import random def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List import random def generate_random_list(length: int) -> List[int]: """ Generates a list of random integers of the specified length. Args: length (int): The length of the list to generate. ...
python_sharegpt_23k
9,145
Write a Python function to find the index of the first occurrence of a given target element in a sorted array using binary search. The array may contain duplicate elements, and the function should return the index of the first occurrence of the target element. The code should start with: ```python from typing import L...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_first_occurrence(arr: List[int], target: int) -> int: """ Find the index of the first occurrence of a target element in a sorted array using binary search. Args: arr (List[int]): Th...
python_sharegpt_23k
9,147
Write a Python function to count the occurrences of each word in a given string using regular expressions. The function should ignore case and punctuation. The code should start with: ```python from typing import Dict def count_word_occurrences(text: str) -> Dict[str, int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict import re def count_word_occurrences(text: str) -> Dict[str, int]: """ Counts the occurrences of each word in a given string, ignoring case and punctuation. Args: text (str): The input strin...
python_sharegpt_23k
9,150
Write a Python function to transform a given multidimensional array into a string of comma-separated values. The multidimensional array is a list of lists, where each sublist contains integers. The function should flatten the array and join all elements into a single string with each element separated by a comma. The ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def flatten_and_join(array: List[List[int]]) -> str: """ Transforms a given multidimensional array into a string of comma-separated values. Args: array (List[List[int]]): A list of lists, wh...
python_sharegpt_23k
9,151
Write a Python function to sort an array of numbers in ascending order using the bubble sort algorithm. The function should take a list of integers as input and return the sorted list. The code should start with: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def bubble_sort(arr: List[int]) -> List[int]: """ Sorts an array of integers in ascending order using the bubble sort algorithm. Args: arr (List[int]): The list of integers to be sorted. ...
python_sharegpt_23k
9,154
Write a Python function to process a list of tuples, where each tuple contains exactly two integers. The function should return a list where each element is determined based on the following rules: - If the first element of the tuple is an odd number, append the first element to the result list. - If the first element ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def process_tuples(lst: List[Tuple[int, int]]) -> List[int]: """ Processes a list of tuples where each tuple contains two integers. Returns a list where each element is determined based on the...
python_sharegpt_23k
9,159
Write a Python function to find the highest number in a given array without using any built-in functions or sorting algorithms, and ensuring that the array is only looped through once. The function should start with: ```python from typing import List def find_highest_number(arr: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_highest_number(arr: List[int]) -> int: """ Find the highest number in a given array without using any built-in functions or sorting algorithms, and ensuring that the array is only looped thr...
python_sharegpt_23k
9,161
Write a Python function to print an upside-down triangle pattern of numbers. The function should take an integer `n` as input, which represents the number of rows in the triangle. The pattern should start from the maximum number of rows and decrease down to 1. Each row should print numbers from 1 up to the row number. ...
Here is the Python code to solve the given problem in the markdown block: ```python def print_upside_down_triangle(n: int) -> None: """ Prints an upside-down triangle pattern of numbers with `n` rows. Args: n (int): The number of rows in the triangle. Returns: None: This function does ...
python_sharegpt_23k
9,163
Write a Python function to count the number of unique characters in a given string. The function should handle any string input, including empty strings and strings with special characters. The code should start with: ```python def count_unique_characters(input_string: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_unique_characters(input_string: str) -> int: """ Counts the number of unique characters in the given input string. Args: input_string (str): The string from which to count unique characters. Returns: ...
python_sharegpt_23k
9,164
Write a Python function to perform a binary search on a sorted 1D array of integers and return the index of the target if it is present. If the target is not found, return -1. The function should use a binary search algorithm to achieve this. The code should start with: ```python from typing import List def binary_se...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def binary_search(arr: List[int], target: int) -> int: """ Perform a binary search on a sorted 1D array to find the index of the target. Args: arr (List[int]): A sorted list of integers. ...
python_sharegpt_23k
9,165
Write a Python function to count the number of prime numbers from 1 to n (inclusive) where n is a positive integer greater than or equal to 2. The function should use the Sieve of Eratosthenes algorithm to efficiently determine the prime numbers. The code should start with: ```python def count_primes(n: int) -> int: `...
Here is the Python code to solve the given problem in the markdown block: ```python def count_primes(n: int) -> int: """ Count the number of prime numbers from 1 to n (inclusive) using the Sieve of Eratosthenes algorithm. Args: n (int): The upper limit to count prime numbers up to (inclusive). ...
python_sharegpt_23k
9,166
Implement the Shell sort algorithm in Python without using any built-in sorting functions or libraries. The function should take a list of integers as input and return the sorted list. The code should start with: ```python from typing import List def shell_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 shell_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers using the Shell sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: List...
python_sharegpt_23k
9,168
Write a Python function to convert a given integer number to Roman numerals without using any built-in functions or libraries. The function should handle integers in the range from 1 to 3999. The code should start with: ```python def int_to_roman(num: int) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def int_to_roman(num: int) -> str: """ Convert a given integer number to Roman numerals. Args: num (int): The integer number to convert, must be in the range 1 to 3999. Returns: str: The Roman numeral r...
python_sharegpt_23k
9,169
Write a Python function to limit the number of characters printed from a given string to a specified maximum length without using any built-in string manipulation functions. The function should handle cases where the specified maximum length is greater than or equal to the length of the string, and it should return the...
Here is the Python code to solve the given problem in the markdown block: ```python def limit_string_length(statement: str, max_length: int) -> str: """ Limits the number of characters printed from a given string to a specified maximum length. Args: statement (str): The string to be truncated. ...
python_sharegpt_23k
9,171
Write a Python function that prints the square of each number from 1 to a given limit `n`, followed by an asterisk. The function should skip any number that is divisible by 3 and only print the squares and asterisks for the remaining numbers. The function should start with: ```python def print_squares_with_asterisks(n:...
Here is the Python code to solve the given problem in the markdown block: ```python def print_squares_with_asterisks(n: int) -> None: """ Prints the square of each number from 1 to n, followed by an asterisk, skipping any number that is divisible by 3. Args: n (int): The upper limit of the rang...
python_sharegpt_23k
9,175
Write a Python function to efficiently calculate the result of raising a given positive integer `base` to the power of a given positive integer `exponent`. The function should handle large values of `exponent` efficiently using the method of exponentiation by squaring. The code should start with: ```python def efficie...
Here is the Python code to solve the given problem in the markdown block: ```python def efficient_power(base: int, exponent: int) -> int: """ Efficiently calculates the result of raising a given base to the power of a given exponent using exponentiation by squaring. Args: base (int): The base numbe...
python_sharegpt_23k
9,176
Write a Python function to reverse a given string without using any built-in string reversal functions or methods. The algorithm should have a time complexity of O(n), where n is the length of the string, and a space complexity of O(1). The function should start with: ```python def reverse_string(string: str) -> str: `...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(string: str) -> str: """ Reverses a given string without using any built-in string reversal functions or methods. Args: string (str): The input string to be reversed. Returns: str: Th...
python_sharegpt_23k
9,183
Write a Python function to segregate a given array of integers into even and odd numbers. The even numbers should be placed at the beginning of the array, and the odd numbers should be placed at the end of the array. The order of the numbers within the even and odd sections should be maintained. The function should sta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def segregate_even_odd(arr: List[int]) -> List[int]: """ Segregates the given array of integers into even and odd numbers. Even numbers are placed at the beginning of the array, and odd numbers are p...
python_sharegpt_23k
9,184
Write a Python function to convert a given hexadecimal color code to its RGB format without using any built-in functions or libraries. The input will always be a valid hexadecimal color code in the format `RRGGBB`. The output should be a string in the format `"RGB(x, y, z)"` where `x`, `y`, and `z` are integers represe...
Here is the Python code to solve the given problem in the markdown block: ```python def hex_to_rgb(hex_code: str) -> str: """ Converts a given hexadecimal color code to its RGB format. Args: hex_code (str): A valid hexadecimal color code in the format "RRGGBB". Returns: str: A string i...
python_sharegpt_23k
9,202
Write a Python function to generate the first n numbers of the Fibonacci sequence and return them as a list. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, starting with 0 and 1. The function should start with: ```python from typing import List def generate_fib...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def generate_fibonacci(n: int) -> List[int]: """ Generate the first n numbers of the Fibonacci sequence. The Fibonacci sequence is a series of numbers in which each number is the sum of the two prec...