source
stringclasses
6 values
id
int64
0
111k
instruction
stringlengths
167
4.39k
response
stringlengths
244
6.1k
python_sharegpt_23k
12,557
Write a Python function to sort a given list in ascending order using the Bubble Sort algorithm. The algorithm should have a time complexity of O(n^2) or less and must be stable, meaning that the relative order of equal elements should be preserved. The function should start with: ```python from typing import List def...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def stable_bubble_sort(arr: List[int]) -> List[int]: """ Sorts the given list in ascending order using the Bubble Sort algorithm. The algorithm is stable, meaning that the relative order of equal ele...
python_sharegpt_23k
12,561
Write a Python function to check if a certain element is present in a given array without using any built-in array functions or methods (e.g., `in` operator, `index()` method). The function should return `True` if the element is found, and `False` otherwise. Additionally, the function should only use a single loop to i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def is_element_present(arr: List[Any], element: Any) -> bool: """ Checks if a certain element is present in a given array without using any built-in array functions or methods. Args: ar...
python_sharegpt_23k
12,564
Write a Python function to find all the prime numbers in a given range of numbers and return the product of the digits of each prime number found. The function should start with: ```python from typing import List def find_prime_numbers_and_digit_products(start: int, end: int) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(num: int) -> bool: """ Check if a number is prime. Args: num (int): The number to check. Returns: bool: True if the number is prime, False otherwise. """ if...
python_sharegpt_23k
12,566
Write a Python function to print a pattern of numbers using nested while loops. The pattern should start from 1 and go up to a given number `n`, where each row contains numbers from 1 to the current row number. The function should handle the case where `n` is a positive integer. Additionally, provide a corrected versio...
Here is the Python code to solve the given problem in the markdown block: ```python def print_number_pattern(n: int): """ Prints a pattern of numbers from 1 to n using nested while loops. Each row contains numbers from 1 to the current row number. Args: n (int): The number of rows in the patter...
python_sharegpt_23k
12,567
Write a Python function that generates a list of all even numbers from 0 to 10 (inclusive) in descending order, excluding the number 6. The function should achieve this in a single iteration without using any conditional statements or the modulus operator. 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 generate_even_numbers_descending() -> List[int]: """ Generates a list of all even numbers from 0 to 10 (inclusive) in descending order, excluding the number 6. Returns: List[int]: A list...
python_sharegpt_23k
12,574
Write a Python function to merge two given lists into a single list by alternating elements from each list. If one list is longer than the other, the remaining elements from the longer list should be appended to the end of the merged list. The function should start with: ```python from typing import List, Any def merg...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def merge_lists(list1: List[Any], list2: List[Any]) -> List[Any]: """ Merge two lists by alternating elements from each list. If one list is longer, append the remaining elements from the longer lis...
python_sharegpt_23k
12,582
Write a Python function to remove duplicates and negative numbers from a given list of integers, and return the distinct positive values in descending order. The function should start with: ```python from typing import List def remove_duplicates_and_negatives(numbers: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_duplicates_and_negatives(numbers: List[int]) -> List[int]: """ Removes duplicates and negative numbers from a list of integers and returns the distinct positive values in descending order. ...
python_sharegpt_23k
12,589
Write a Python function to determine if the word "hello" is present in a given list of strings. The list may contain duplicates and can be of arbitrary length. The function should return `True` if the word "hello" is present in the list, and `False` otherwise. 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 is_hello_present(words: List[str]) -> bool: """ Determines if the word "hello" is present in the provided list of strings. Args: words (List[str]): A list of strings to search for the wo...
python_sharegpt_23k
12,596
Write a Python function to calculate the sum of the digits of a given positive integer and count the number of unique digits present in the number. The function should have a time complexity of O(n), where n is the number of digits in the integer, and a space complexity of O(1). The function should return a tuple conta...
Here is the Python code to solve the given problem in the markdown block: ```python def sum_and_count_unique_digits(n: int) -> tuple: """ Calculate the sum of the digits of a given positive integer and count the number of unique digits. Args: n (int): The positive integer whose digits are to be pro...
python_sharegpt_23k
12,600
Write a Python function to remove the second to last element from a given list of integers. If the list has fewer than two elements, the function should return the list unchanged. The function should start with: ```python from typing import List def remove_second_to_last_element(lst: List[int]) -> List[int]: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_second_to_last_element(lst: List[int]) -> List[int]: """ Removes the second to last element from the given list of integers. Args: lst (List[int]): The list of integers from w...
python_sharegpt_23k
12,604
Write a Python function to merge two sorted arrays of integers into one sorted array in ascending order. The input arrays can contain negative numbers and zero, and their lengths can vary from 0 to 10^6. The solution should have a time complexity of O(n), where n is the total number of elements in both input arrays, an...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sorted_arrays(arr1: List[int], arr2: List[int]) -> List[int]: """ Merges two sorted arrays of integers into one sorted array in ascending order. Args: arr1 (List[int]): The first s...
python_sharegpt_23k
12,606
Write a Python function to calculate the factorial of a given non-negative integer using a while loop. The function should demonstrate how a while loop can be used to implement a recursive-like algorithm. Discuss the time and space complexity of your solution. The code should start with: ```python def factorial_with_w...
Here is the Python code to solve the given problem in the markdown block: ```python def factorial_with_while(n: int) -> int: """ Calculate the factorial of a given non-negative integer using a while loop. Args: n (int): The non-negative integer for which to calculate the factorial. Returns: ...
python_sharegpt_23k
12,607
Write a Python function to insert a new element into an existing unsorted array efficiently, ensuring that the inserted element is always placed at the beginning of the array. The function should not use any built-in array functions or methods. The code should start with: ```python from typing import List def insert_e...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def insert_element_at_beginning(arr: List[int], element: int) -> List[int]: """ Inserts a new element at the beginning of an existing unsorted array. Args: arr (List[int]): The existing unso...
python_sharegpt_23k
12,611
Write a Python function to reverse a doubly linked list in place. The function should not create a new list or use any built-in functions or methods. The time complexity of the algorithm should be O(n), where n is the number of nodes in the linked list. The function should start with: ```python class Node: def __in...
Here is the Python code to solve the given problem in the markdown block: ```python class Node: def __init__(self, data): self.data = data self.prev = None self.next = None def reverse_doubly_linked_list(head: Node) -> Node: """ Reverses a doubly linked list in place. Args: ...
python_sharegpt_23k
12,612
Write a Python function to determine if one string is a permutation of another, considering only alphabetic characters and ignoring case. The function should handle strings with different lengths, special characters, and spaces. The function should have a time complexity of O(n), where n is the length of the longer str...
Here is the Python code to solve the given problem in the markdown block: ```python def is_permutation(str1: str, str2: str) -> bool: """ Determines if one string is a permutation of another, considering only alphabetic characters and ignoring case. Args: str1 (str): The first string to compare. ...
python_sharegpt_23k
12,624
Write a Python function to iterate through a given list, print each element, and calculate the sum of all elements in the list. The function should return the sum of the elements. The code should start with: ```python from typing import List def iterate_and_sum(my_list: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def iterate_and_sum(my_list: List[int]) -> int: """ Iterates through a given list, prints each element, and calculates the sum of all elements. Args: my_list (List[int]): The list of integer...
python_sharegpt_23k
12,625
Write a Python function to delete all occurrences of a specified number from a given list and return the resulting list sorted in ascending order. The function should handle lists of integers and the number to be deleted can be any integer. The code should start with: ```python from typing import List def delete_and_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def delete_and_sort(arr: List[int], num: int) -> List[int]: """ Deletes all occurrences of the specified number from the given list and returns the resulting list sorted in ascending order. Args: ...
python_sharegpt_23k
12,628
Write a Python function to sort a given list of integers in descending order without using any built-in sorting functions or methods. Additionally, the function should handle cases where the list contains duplicate integers and should remove any duplicates before sorting the list. The function should start with: ```pyt...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_list_descending(nums: List[int]) -> List[int]: """ Sorts a given list of integers in descending order without using any built-in sorting functions or methods. Additionally, removes any dupli...
python_sharegpt_23k
12,630
Write a Python function to create an NxN matrix filled with 0s, where N is a prime number, and fill the diagonal of the matrix with 1s. The function should return `None` if N is not a prime number. The code should start with: ```python from typing import List, Optional def create_prime_matrix(n: int) -> Optional[List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def create_prime_matrix(n: int) -> Optional[List[List[int]]]: """ Create an NxN matrix filled with 0s, where N is a prime number, and fill the diagonal with 1s. Args: n (int): The ...
python_sharegpt_23k
12,631
Write a Python function to calculate the surface area of a cube using only bitwise operations, without using any loops or conditionals. The function should have a time complexity of O(1) and a space complexity of O(1). The function should start with: ```python def cube_surface_area(side_length: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: : ```python def cube_surface_area(side_length: int) -> int: """ Calculate the surface area of a cube using only bitwise operations. Args: side_length (int): The length of a side of the cube. Returns: int: The sur...
python_sharegpt_23k
12,633
Write a Python function to calculate the greatest common divisor (GCD) of two given integers using the Euclidean algorithm. The function must handle negative numbers and return the positive GCD. The implementation should not use any built-in functions or libraries to calculate the GCD, and it must have a time complexit...
Here is the Python code to solve the given problem in the markdown block: ```python def calculate_gcd(a: int, b: int) -> int: """ Calculate the greatest common divisor (GCD) of two integers using the Euclidean algorithm. Args: a (int): The first integer. b (int): The second integer. Re...
python_sharegpt_23k
12,636
Write a Python function to calculate the number of bits needed to represent a given integer in binary form without using any built-in functions or libraries related to binary conversion. The function should start with: ```python def count_bits_needed(num: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def count_bits_needed(num: int) -> int: """ Calculate the number of bits needed to represent a given integer in binary form. Args: num (int): The integer for which to calculate the number of bits. Returns: ...
python_sharegpt_23k
12,640
Write a Python function to find the common elements between two sorted arrays of integers. The function should return a new array containing the common elements without duplicates. The function should have a time complexity of O(log n), where n is the length of the longer array. 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 find_common_elements(arr1: List[int], arr2: List[int]) -> List[int]: """ Find the common elements between two sorted arrays of integers without duplicates. Args: arr1 (List[int]): The fi...
python_sharegpt_23k
12,645
Write a Python function to calculate the slope of the line passing through the first and last points in a given list of points. Each point is represented as a tuple of two integers (x, y). The function should handle cases where the x-coordinates of the first and last points are the same (vertical line) by returning `No...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Optional def calculate_slope(points: List[Tuple[int, int]]) -> Optional[float]: """ Calculate the slope of the line passing through the first and last points in a list of points. Args: ...
python_sharegpt_23k
12,648
Write a Python function to calculate the sum of all prime numbers between 1 and a given upper limit `n`. The function should correctly identify prime numbers using an efficient algorithm and compute the sum of these primes. The code should start with: ```python def sum_of_primes(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_primes(n: int) -> int: """ Calculate the sum of all prime numbers between 1 and the given upper limit n. Args: n (int): The upper limit up to which prime numbers are to be summed. Returns: in...
python_sharegpt_23k
12,649
Write a Python function that performs the following operations on an array of integers: 1. Initialize an array of integers with values from 1 to 100. 2. Return a list of all the odd numbers in the array. 3. Sort the array in ascending order. 4. Return the sum of all the even numbers in the array. The function should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def array_operations() -> (List[int], List[int], int): """ Performs a series of operations on an array of integers from 1 to 100. Returns: tuple: A tuple containing: - A list of ...
python_sharegpt_23k
12,651
Write a Python function to convert a given number of minutes into hours, minutes, and seconds. The input should be a positive integer between 1 and 10000. The output should be in the format "X hours, Y minutes, and Z seconds". The function should validate the input to ensure that it is a positive integer within the spe...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_minutes_to_hms(minutes: int) -> str: """ Convert a given number of minutes into hours, minutes, and seconds. Args: minutes (int): The number of minutes to convert. Must be a positive integer between 1 an...
python_sharegpt_23k
12,652
Write a Python function to extract all digits from a given string and return them as a list of integers. The input string may contain both letters and digits, and may have leading or trailing whitespace. The output list should contain the digits in the same order as they appear in the string. Additionally, the input st...
Here is the Python code to solve the given problem in the markdown block: ```python def extract_digits(s: str) -> list: """ Extracts all digits from a given string and returns them as a list of integers. Args: s (str): The input string containing digits and letters, with possible leading or trailin...
python_sharegpt_23k
12,657
Write a Python function to determine the sum of all even elements in a given list of integers that are divisible by 3. If the list is empty or contains only odd elements, return -1. The code should start with: ```python from typing import List def sum_even_divisible_by_three(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_even_divisible_by_three(numbers: List[int]) -> int: """ Determine the sum of all even elements in the given list that are divisible by 3. If the list is empty or contains only odd elements, r...
python_sharegpt_23k
12,660
Write a Python function to find the maximum number in a given list of integers without using any built-in functions or methods to find the maximum value. Additionally, handle the case where the list is empty by returning an appropriate error message. The function should be efficient and capable of handling a large list...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_max_number(numbers: List[int]) -> int or str: """ Find the maximum number in a given list of integers without using built-in functions or methods. If the list is empty, return an appropriate...
python_sharegpt_23k
12,664
Write a Python function to implement a correct version of the Bubble Sort algorithm. The function should sort a list of integers in ascending order. 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 a list of integers in ascending order using the Bubble Sort algorithm. Args: arr (List[int]): The list of integers to be sorted. ...
python_sharegpt_23k
12,666
Write a Python function to calculate the total sum of all even numbers that are divisible by 4 in a given list. The function should use an accumulator pattern to achieve this. The code should start with: ```python from typing import List def sum_even_divisible_by_4(numbers: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_even_divisible_by_4(numbers: List[int]) -> int: """ Calculate the total sum of all even numbers that are divisible by 4 in a given list. Args: numbers (List[int]): The list of intege...
python_sharegpt_23k
12,668
Write a Python function to sort a given dictionary of strings in reverse alphabetical order based on the keys. The function should return a new dictionary with the sorted key-value pairs. The code should start with: ```python from typing import Dict def sort_dict_reverse_alphabetical(input_dict: Dict[str, str]) -> Di...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def sort_dict_reverse_alphabetical(input_dict: Dict[str, str]) -> Dict[str, str]: """ Sorts a given dictionary of strings in reverse alphabetical order based on the keys. Args: input_dict (D...
python_sharegpt_23k
12,669
Create a Python function that calculates the total cost of items in a dictionary where each item's price is stored as a tuple with four decimal places precision. The function should round the total cost to the nearest whole number. The dictionary should be predefined with items and their corresponding prices. The code...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, Tuple def calculate_total_cost(items: Dict[str, Tuple[float]]) -> int: """ Calculate the total cost of items in a dictionary where each item's price is stored as a tuple with four decimal places precisi...
python_sharegpt_23k
12,672
Write a Python function to generate a list of all prime numbers within a given range `[start, end]` and return them sorted in ascending order. The function should use list comprehension and a helper function to check if a number is prime. The code should start with: ```python from typing import List def generate_sort...
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
12,673
Write a Python function to extract prime numbers from a given list of integers, ensuring that duplicates are removed before checking for prime numbers. The function should return a new list containing only the prime numbers. The code should start with: ```python from typing import List def get_prime_numbers(numbers: ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(num: int) -> bool: """ Check if a given number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False other...
python_sharegpt_23k
12,674
Write a Python function to compare two arrays and determine the number of elements that are equal at the same position in both arrays, and the number of elements that are equal but not at the same position in both arrays. The function should return these two counts as a tuple. The code should start with: ```python fro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple def compare_arrays(arr1: List[int], arr2: List[int]) -> Tuple[int, int]: """ Compare two arrays and determine the number of elements that are equal at the same position and the number of eleme...
python_sharegpt_23k
12,676
Write a Python function to sort an array of positive integers in ascending order without using any built-in sorting methods or functions. The array should contain at least 10 elements and each element should be between 1 and 100 (inclusive). The code should start with: ```python from typing import List def sort_array(...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_array(arr: List[int]) -> List[int]: """ Sorts an array of positive integers in ascending order without using any built-in sorting methods or functions. Args: arr (List[int]): The ar...
python_sharegpt_23k
12,679
Write a Python function to convert a given decimal number to its binary representation without using any built-in functions or libraries. The function should use only bitwise operations and should not exceed a time complexity of O(log n). 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 number to its binary representation using only bitwise operations. Args: n (int): The decimal number to convert. Returns: str: The bina...
python_sharegpt_23k
12,681
Write a Python function to calculate the mean of a given list of numbers without using any built-in functions or libraries for calculating the mean. Ensure that your solution is efficient and does not have a time complexity higher than O(n). The code should start with: ```python from typing import List def calculate_...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def calculate_mean(numbers: List[int]) -> float: """ Calculate the mean of a given list of numbers without using any built-in functions or libraries. Args: numbers (List[int]): The list of n...
python_sharegpt_23k
12,683
Write a Python function to calculate the Manhattan distance between two points \((x, y)\) and \((a, b)\) in a 2D plane. The coordinates can be positive, negative, or zero. The Manhattan distance is defined as the sum of the absolute differences of their Cartesian coordinates. The code should start with: ```python def ...
Here is the Python code to solve the given problem in the markdown block: ```python def manhattan_distance(x: int, y: int, a: int, b: int) -> int: """ Calculate the Manhattan distance between two points (x, y) and (a, b) in a 2D plane. Args: x (int): The x-coordinate of the first point. y (...
python_sharegpt_23k
12,684
Write a Python function to determine the relationship between two integers `x` and `y` using the ternary operator. The function should return one of the following strings based on the comparison: - "x is greater than y" if `x` is greater than `y` - "x is less than y" if `x` is less than `y` - "x is equal to y" if `x` i...
Here is the Python code to solve the given problem in the markdown block: ```python def determine_relationship(x: int, y: int) -> str: """ Determines the relationship between two integers x and y using the ternary operator. Args: x (int): The first integer to compare. y (int): The second in...
python_sharegpt_23k
12,685
Write a Python function to compress a given string of lowercase alphabets using the Run-Length Encoding (RLE) algorithm. The function should achieve a time complexity of O(n), where n is the length of the input string. The compressed string should represent each run of consecutive characters by the character itself fol...
Here is the Python code to solve the given problem in the markdown block: ```python def compress_string(s: str) -> str: """ Compresses a given string of lowercase alphabets using the Run-Length Encoding (RLE) algorithm. Args: s (str): The input string to be compressed. Returns: str: Th...
python_sharegpt_23k
12,686
Write a Python function to calculate and return the election results from a list of votes. Each vote is represented as a tuple in the format `(candidate_name, number_of_votes)`. The function should return a dictionary where the keys are the candidate names and the values are dictionaries containing the number of votes ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple, Dict, Union def calculate_election_results(votes: List[Tuple[str, int]]) -> Union[Dict[str, Dict[str, Union[int, float]]], None]: """ Calculate and return the election results from a list of vote...
python_sharegpt_23k
12,688
Write a Python function to print all the even numbers from a given list in ascending order, ensuring that the code has a time complexity of O(n) and does not use any built-in Python functions. Additionally, the code should only use a single loop and should not use any additional data structures or variables apart from ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_even_numbers(list_of_numbers: List[int]) -> None: """ Prints all even numbers from the given list in ascending order. Args: list_of_numbers (List[int]): The list of integers from w...
python_sharegpt_23k
12,695
Write a Python function to implement a stable merge sort algorithm for a list of integers that can handle duplicate elements efficiently. The algorithm should have a time complexity of O(n log n) and should not use any built-in sorting functions or data structures. The code should start with: ```python from typing impo...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def stable_merge_sort(arr: List[int]) -> List[int]: """ Sorts a list of integers using a stable merge sort algorithm. Args: arr (List[int]): The list of integers to be sorted. Returns: ...
python_sharegpt_23k
12,696
Write a Python function to return the first two characters of a given string with a time complexity of O(1). The function must not use any built-in string manipulation functions or methods. The function should handle strings of any length, including empty strings. The code should start with: ```python def get_first_tw...
Here is the Python code to solve the given problem in the markdown block: ```python def get_first_two_chars(s: str) -> str: """ Returns the first two characters of the given string. Args: s (str): The input string from which to extract the first two characters. Returns: str: The first ...
python_sharegpt_23k
12,701
Write a Python function that generates a string representing a pattern of dots, where each line contains a number of dots equal to the line number. The function should use only a single loop to achieve this. The output should be returned as a single string with each line separated by a newline character. The code shou...
Here is the Python code to solve the given problem in the markdown block: ```python def generate_dot_pattern(n: int) -> str: """ Generates a string representing a pattern of dots, where each line contains a number of dots equal to the line number. Args: n (int): The number of lines in the pattern. ...
python_sharegpt_23k
12,704
Write a Python function to flatten a two-dimensional list into a one-dimensional list. The two-dimensional list may contain nested lists, and the resulting one-dimensional list should contain all the elements from the nested lists. The function should have a time complexity of O(n^3) and a space complexity of O(n^2), w...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def flatten_list(lst: List[Any]) -> List[Any]: """ Flattens a two-dimensional list into a one-dimensional list. The input list may contain nested lists. The function has a time complexity of O(n...
python_sharegpt_23k
12,708
Write a Python function to find all unique combinations of size three in a given list of integers that are prime numbers. The list will contain at least 10 elements and will not exceed 100 elements. Ensure that the function runs efficiently and does not have a time complexity greater than O(n^3), where n is the number ...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Set, Tuple 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. ""...
python_sharegpt_23k
12,709
Write a Python function to determine whether a given sequence is an arithmetic progression or not. The sequence can have negative numbers and can be of any length. The function should also handle sequences with missing elements, meaning there may be gaps between elements in the sequence. The function should have a time...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_arithmetic_progression(sequence: List[int]) -> bool: """ Determines whether the given sequence is an arithmetic progression. Args: sequence (List[int]): The sequence of integers to ch...
python_sharegpt_23k
12,712
Write a Python function to retrieve unique values from a list of integers, including negative numbers, and return them in descending order. The function should handle lists with both positive and negative integers. The code should start with: ```python from typing import List def get_unique_values_descending(numbers:...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_unique_values_descending(numbers: List[int]) -> List[int]: """ Retrieve unique values from a list of integers and return them in descending order. Args: numbers (List[int]): A list o...
python_sharegpt_23k
12,715
Write a Python function to efficiently append an element to a sorted list while maintaining its sorted order. The function should have a time complexity of O(log n) for appending the element. The code should start with: ```python from typing import List def append_element_to_sorted_list(lst: List[int], element: int) -...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def append_element_to_sorted_list(lst: List[int], element: int) -> List[int]: """ Appends an element to a sorted list while maintaining its sorted order. Args: lst (List[int]): The sorted li...
python_sharegpt_23k
12,716
Write a Python function to flatten a nested dictionary and return all the values in a single list. The dictionary may contain multiple levels of nesting. The function should handle any depth of nested dictionaries and should only include the values, not the keys. The code should start with: ```python from typing impor...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict, List def flatten_nested_dict(dictionary: Dict) -> List: """ Flattens a nested dictionary and returns all the values in a single list. Args: dictionary (Dict): The nested dictionary to be fl...
python_sharegpt_23k
12,719
Write a Python function to calculate the product of all the elements in a nested list. The nested list can contain positive and negative integers, empty lists, and lists with only one element. The function should handle lists of varying lengths and should have a time complexity of O(n), where n is the total number of e...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def product_of_nested_list(nested_list: List[List[int]]) -> int: """ Calculate the product of all elements in a nested list. Args: nested_list (List[List[int]]): A nested list containing int...
python_sharegpt_23k
12,720
Write a Python function to convert all the alphabetical characters in a given string to their corresponding uppercase characters, but exclude any characters that are already uppercase or non-alphabetical. The function should return the modified string. The code should start with: ```python def convert_to_uppercase(str...
Here is the Python code to solve the given problem in the markdown block: ```python def convert_to_uppercase(string: str) -> str: """ Converts all lowercase alphabetical characters in the given string to uppercase, while preserving uppercase and non-alphabetical characters. Args: string (str): ...
python_sharegpt_23k
12,733
Write a Python function to sort a list of dictionary objects by the value of the "age" field in descending order, while excluding any objects where the "age" is less than 30. Additionally, if two or more objects have the same age, sort them by the value of the "name" field in ascending order. The solution should have a...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def sort_dict_objects(objects: List[Dict[str, any]]) -> List[Dict[str, any]]: """ Sorts a list of dictionary objects by the "age" field in descending order, excluding any objects where the "age...
python_sharegpt_23k
12,734
Write a Python function to reverse a given string without using any built-in string manipulation functions, methods, or data structures like lists or arrays. You are only allowed to use basic operations such as accessing individual characters of the string and concatenation. The code should start with: ```python def r...
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string(input_str: str) -> str: """ Reverses a given string without using any built-in string manipulation functions, methods, or data structures. Args: input_str (str): The string to be reversed. Re...
python_sharegpt_23k
12,742
Write a Python function to find the smallest missing positive number in an array of size N, where N can be up to 10^6. The array can contain integers ranging from -10^9 to 10^9. The function should find the missing positive number in O(N) time complexity and O(1) space complexity. If there is no missing positive number...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_smallest_missing_positive(arr: List[int]) -> int: """ Find the smallest missing positive number in an array of size N, where N can be up to 10^6. The array can contain integers ranging from ...
python_sharegpt_23k
12,743
Write a Python function to flatten a given two-dimensional array into a one-dimensional array, remove any duplicate elements, and sort the resulting array in ascending order. The function should adhere to the following constraints: - Time Complexity: O(n log n) - Space Complexity: O(n) The code should start with: ```p...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def flatten_and_sort_unique(arr: List[List[int]]) -> List[int]: """ Flattens a given two-dimensional array into a one-dimensional array, removes any duplicate elements, and sorts the resulting array in a...
python_sharegpt_23k
12,751
Write a Python function to sum up all the positive integers in a given dictionary, where the keys are strings and the values are either positive integers or nested dictionaries following the same format. The dictionary should contain at least 5 key-value pairs and have a maximum of 10 key-value pairs. The function shou...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import Dict def sum_positive_elements(dictionary: Dict[str, int or Dict]) -> int: """ Sum up all the positive integers in a given dictionary, including those in nested dictionaries. Args: dictionary (Di...
python_sharegpt_23k
12,753
Write a Python function that takes two lists as input and returns a new list which contains only the unique elements from both of the lists. The function should also remove any duplicates within each list before combining them. The function should handle large input lists efficiently without causing any performance iss...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_unique_elements(list1: List[int], list2: List[int]) -> List[int]: """ Returns a list of unique elements from both input lists, preserving the original order. Args: list1 (List[int]):...
python_sharegpt_23k
12,754
Write a Python function to sort a given array in descending order using a custom merge sort algorithm. The function should handle arrays containing duplicate elements and should remove any duplicate elements before sorting the array. The time complexity of the sorting algorithm should be O(n log n), and it should not u...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def custom_merge_sort(arr: List[int]) -> List[int]: """ Sorts the given array in descending order using a custom merge sort algorithm. The function removes duplicate elements before sorting. Arg...
python_sharegpt_23k
12,757
Write a Python function to sort an array of integers in descending order and return the sorted array as a string with each element separated by a space. The function should handle an empty array by returning an empty string. The code should start with: ```python from typing import List def sort_and_stringify(arr: Lis...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_and_stringify(arr: List[int]) -> str: """ Sorts the given array of integers in descending order and returns the sorted array as a string with each element separated by a space. Args: ...
python_sharegpt_23k
12,758
Write a Python function `sum_prime_even_to_n` that takes a single integer argument `n` and returns the sum of all prime even numbers from 2 to `n`. The function should ensure that only prime even numbers are included in the sum. The code should start with: ```python def sum_prime_even_to_n(n: int) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_prime_even_to_n(n: int) -> int: """ Calculate the sum of all prime even numbers from 2 to n. Args: n (int): The upper limit of the range (inclusive). Returns: int: The sum of all prime even numb...
python_sharegpt_23k
12,760
Write a Python function to count the occurrences of a specific element in a multi-dimensional list using recursion. The function should handle nested lists and return the total count of the target element. The function should include at least two base cases and demonstrate the proper use of recursive calls. The code s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Any def count_element_recursive(matrix: List[Any], target: Any) -> int: """ Counts the occurrences of a specific element in a multi-dimensional list using recursion. Args: matrix (List[Any]...
python_sharegpt_23k
12,763
Write a Python function to extract and return a list of positive integers from a given list of integers. The resulting list should be sorted in ascending order. The function should have a time complexity of O(n), should not use any built-in functions or libraries for sorting, and should ensure that the resulting list i...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_positive_integers(input_list: List[int]) -> List[int]: """ Extracts and returns a list of positive integers from the input list, sorted in ascending order. Args: input_list (List[int...
python_sharegpt_23k
12,764
Write a Python function to determine if a given number is a prime number without using any built-in functions or libraries for prime number generation or checking. The function should only use basic arithmetic operations, loops, and conditional statements. The function should return `True` if the number is prime and `F...
Here is the Python code to solve the given problem in the markdown block: ```python def is_prime(num: int) -> bool: """ Determines if a given number is a prime number. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False otherwise. "...
python_sharegpt_23k
12,771
Write a Python function to reverse a given string using a stack data structure. The function should reverse the string in-place, meaning it should not use any additional data structures beyond the stack itself. The code should start with: ```python def reverse_string_with_stack(s: str) -> str: ```
Here is the Python code to solve the given problem in the markdown block: ```python def reverse_string_with_stack(s: str) -> str: """ Reverses a given string using a stack data structure. Args: s (str): The input string to be reversed. Returns: str: The reversed string. """ sta...
python_sharegpt_23k
12,772
Write a Python function to determine whether a given string is a palindrome using a stack data structure. The string may contain both lowercase and uppercase letters, special characters, and whitespace. The function should be case-insensitive and should ignore special characters and whitespace. The code should start w...
Here is the Python code to solve the given problem in the markdown block: ```python def is_palindrome_using_stack(string: str) -> bool: """ Determine whether a given string is a palindrome using a stack data structure. The function removes all special characters and whitespace, converts the string to lower...
python_sharegpt_23k
12,773
Write a Python function to reverse a slice of a list up to a given index without using any built-in functions or methods that directly manipulate the list. 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 reverse_s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def reverse_slice(my_list: List[int], end_index: int) -> List[int]: """ Reverses the slice of the list up to the given end index without using built-in functions or methods. Args: my_list (L...
python_sharegpt_23k
12,777
Write a Python function to count the number of distinct words in a given string. 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 Python functions or libraries for string manipulation or counting. The code should start with: ...
Here is the Python code to solve the given problem in the markdown block: ```python def count_distinct_words(string: str) -> int: """ Counts the number of distinct words in a given string. Args: string (str): The input string to count distinct words from. Returns: int: The number of di...
python_sharegpt_23k
12,780
Write a Python function that takes in a list of integers and returns a new list that contains only the even numbers in the original list. The function should have a time complexity of O(n) and should not use any built-in Python functions or methods (such as `filter` or list comprehensions) that directly solve the probl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def get_even_numbers(lst: List[int]) -> List[int]: """ Returns a new list containing only the even numbers from the input list. If a negative integer is encountered, it is included in the output list...
python_sharegpt_23k
12,782
Write a Python function to find all prime numbers up to a given positive integer `n` and return them in descending order. The function should have a time complexity of O(n log log n). The code should start with: ```python from typing import List def find_primes_descending(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_primes_descending(n: int) -> List[int]: """ Find all prime numbers up to a given positive integer `n` and return them in descending order. Args: n (int): The upper limit up to which...
python_sharegpt_23k
12,783
Write a Python function to find the longest string in a given list of strings. If there are multiple strings of the same maximum length, return the first occurrence of such a string. If the input list is empty, return `None`. The input list will contain at most 100 strings, and each string will have at most 50 characte...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Optional def find_longest_string(input_list: List[str]) -> Optional[str]: """ Find the longest string in a given list of strings. If there are multiple strings of the same maximum length, return the...
python_sharegpt_23k
12,790
Write a Python function to calculate the sum of all prime numbers in a given list of integers. The function should start with: ```python from typing import List def sum_of_primes(lst: List[int]) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def is_prime(num: int) -> bool: """ Check if a given number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is prime, False other...
python_sharegpt_23k
12,792
Write a Python function to sort a given list using the quicksort algorithm. You are not allowed to use any built-in sorting functions or libraries. You must implement the quicksort algorithm from scratch. Additionally, you are not allowed to use recursion in your implementation. You must use an iterative approach to im...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def quicksort_iterative(arr: List[int]) -> List[int]: """ Sorts the given list using the quicksort algorithm in an iterative manner. Args: arr (List[int]): The list of integers to be sorted....
python_sharegpt_23k
12,798
Write a Python function to find the top 3 highest numbers from a given array without using any sorting algorithm or built-in functions that directly solve the problem. The array can have a maximum of 1000 elements. The function should start with: ```python from typing import List def find_top_3_highest_numbers(arr: Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def find_top_3_highest_numbers(arr: List[int]) -> List[int]: """ Find the top 3 highest numbers from a given array without using any sorting algorithm or built-in functions. Args: arr (List[...
python_sharegpt_23k
12,800
Write a Python function to generate a timestamp based on the current date and time in the format "YYYY-MM-DD HH:MM:SS.mmm", ensuring that the timestamp is in the UTC timezone and includes milliseconds. The function should return the formatted timestamp as a string. The code should start with: ```python import datetime...
Here is the Python code to solve the given problem in the markdown block: ```python import datetime def generate_utc_timestamp() -> str: """ Generate a timestamp based on the current date and time in the format "YYYY-MM-DD HH:MM:SS.mmm", ensuring that the timestamp is in the UTC timezone and includes milli...
python_sharegpt_23k
12,801
Write a Python function that takes an array of integers as input, sorts the array in ascending order using a custom sorting algorithm, removes any duplicate elements, and then calculates the sum of the first five elements in the sorted and modified array. The function should return the result. You are not allowed to us...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sort_and_sum_unique(arr: List[int]) -> int: """ Sorts the input array in ascending order using a custom sorting algorithm, removes any duplicate elements, and calculates the sum of the first five...
python_sharegpt_23k
12,804
Write a Python function to search for the first occurrence of a specific element in an unsorted array that may contain duplicate elements. The function should return the index of the first occurrence of the element if it is found, otherwise it should return -1. The function should be implemented using a recursive appro...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def search_first_occurrence(arr: List[int], target: int, start: int = 0) -> int: """ Recursively searches for the first occurrence of a target element in an unsorted array. Args: arr (List[i...
python_sharegpt_23k
12,808
Write a Python function to create a dictionary mapping student IDs to their corresponding grades. The function should take two lists as input: `student_ids` and `grades`. The function should handle the following cases: 1. Ensure both lists have the same length. 2. Check for duplicate student IDs in the `student_ids` li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def create_grade_dictionary(student_ids: List[int], grades: List[float]) -> Dict[int, float]: """ Create a dictionary mapping student IDs to their corresponding grades. Args: student_i...
python_sharegpt_23k
12,810
Write a Python function to generate and return a list of all prime numbers between 1 and a given number `n` in reverse order. The function should efficiently determine if a number is prime and collect all prime numbers up to `n` before reversing the list. 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 generate_primes_reverse(n: int) -> List[int]: """ Generate a list of all prime numbers between 1 and n in reverse order. Args: n (int): The upper limit (inclusive) for generating prime n...
python_sharegpt_23k
12,811
Write a Python function to sort an array of integers in ascending order using a recursive merge sort algorithm. The function should not use any built-in sorting functions or data structures, and it should have a time complexity of O(n log n). The array can contain both positive and negative integers. The code should s...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def merge_sort(arr: List[int]) -> List[int]: """ Sorts an array of integers in ascending order using a recursive merge sort algorithm. Args: arr (List[int]): The array of integers to be sort...
python_sharegpt_23k
12,814
Write a Python function to calculate the sum of all positive odd numbers in a given list. The function should ignore non-integer elements and negative odd numbers. If the list is empty or does not contain any positive odd numbers, the function should return 0. The code should start with: ```python from typing import Li...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_positive_odd_numbers(numbers: List[int]) -> int: """ Calculate the sum of all positive odd numbers in a given list. Args: numbers (List[int]): A list of numbers (integers and possibl...
python_sharegpt_23k
12,818
Write a Python function to determine if a given string is an anagram of any word in a provided list of words. The function should return `True` if the string is an anagram of at least one word in the list, and `False` otherwise. The list of words may or may not be alphabetically sorted. 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 is_anagram_in_list(string: str, word_list: List[str]) -> bool: """ Determines if the given string is an anagram of any word in the provided list. Args: string (str): The input string to ...
python_sharegpt_23k
12,822
Write a Python function to calculate the sum of all the digits in a given string, excluding any punctuation marks or special characters. The function should only consider numeric characters and ignore all other characters. The code should start with: ```python def sum_of_digits_in_string(s: str) -> int: ```
Here is the Python code to solve the given problem in the markdown block: ```python def sum_of_digits_in_string(s: str) -> int: """ Calculate the sum of all digits in a given string, excluding any punctuation marks or special characters. Args: s (str): The input string from which to calculate the s...
python_sharegpt_23k
12,823
Write a Python function to create a list of `n` strings, where each string is the string representation of its index in the list. The function should then print each string in a separate line using a single loop, without using any additional loops or list comprehensions. The function should also ensure that the time co...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def print_strings_in_list(n: int) -> None: """ Creates a list of n strings, where each string is the string representation of its index, and prints each string in a separate line using a single loop....
python_sharegpt_23k
12,832
Write a Python function that implements a closure to concatenate a given string with a specified number of exclamation marks. Additionally, create a separate function that takes an array of these closures and a string, and returns a single string by invoking each closure with the given string and concatenating the resu...
Here is the Python code to solve the given problem in the markdown block: ```python def create_closure(string: str, number: int) -> callable: """ Create a closure that concatenates a given string with a specified number of exclamation marks. Args: string (str): The base string to concatenate with e...
python_sharegpt_23k
12,842
Write a Python function to filter out only the words from a given mixed list containing both words and numbers, and calculate the average length of these words rounded up to the nearest integer. The function should return a tuple containing the filtered list of words and the rounded average length. The code should sta...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Tuple import math def filter_words_and_calculate_avg_length(mixed_list: List[object]) -> Tuple[List[str], int]: """ Filters out only the words from a mixed list and calculates the average length of thes...
python_sharegpt_23k
12,844
Write a Python function to filter and sort a list of user details based on specific criteria. The function should filter out users whose age is more than 21 and whose occupation is not 'Engineer'. Additionally, the filtered users should be sorted by their addresses in ascending order. The filtered list should only incl...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def filter_and_sort_users(user_details: List[Dict[str, str]]) -> Dict[str, int]: """ Filters and sorts a list of user details based on specific criteria. Args: user_details (List[Dict[...
python_sharegpt_23k
12,849
Write a Python function to multiply two positive integers without using the multiplication operator. The function should use the Russian Peasant Multiplication algorithm, which has a time complexity of O(log n) and a space complexity of O(1), where n is the larger number. The code should start with: ```python def mult...
Here is the Python code to solve the given problem in the markdown block: ```python def multiply_without_operator(a: int, b: int) -> int: """ Multiplies two positive integers without using the multiplication operator. Uses the Russian Peasant Multiplication algorithm. Args: a (int): The first p...
python_sharegpt_23k
12,852
Write a Python function to remove all numbers from a given list that are divisible by a specified value, and also remove any duplicates that occur in the list. The function should return the modified list. The code should start with: ```python from typing import List def remove_divisible_duplicates(lst: List[int], va...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def remove_divisible_duplicates(lst: List[int], value: int) -> List[int]: """ Removes all numbers from the list that are divisible by the specified value and removes any duplicates. Args: ls...
python_sharegpt_23k
12,855
Write a Python function to divide two positive integers without using the division operator and with a time complexity of O(log N), where N is the number of digits in the dividend. The function should handle special cases such as division by zero and return the quotient as an integer. The code should start with: ```py...
Here is the Python code to solve the given problem in the markdown block: ```python def divide_integers(dividend: int, divisor: int) -> int: """ Divide two positive integers without using the division operator and with a time complexity of O(log N), where N is the number of digits in the dividend. Args...
python_sharegpt_23k
12,857
Write a Python function to calculate the sum of elements in a given array that are divisible by 3, greater than 5, and less than 15. The function should iterate through the array and apply the specified conditions to each element. The code should start with: ```python from typing import List def sum_divisible_by_3_and...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def sum_divisible_by_3_and_in_range(arr: List[int]) -> int: """ Calculate the sum of elements in the given array that are divisible by 3, greater than 5, and less than 15. Args: arr (List[in...
python_sharegpt_23k
12,860
Write a Python function to filter a given list of numbers and return a list of all prime numbers. The function should use an efficient algorithm to determine if a number is prime, with a time complexity of O(sqrt(n)) and space complexity of O(1). The code should start with: ```python from typing import List def filte...
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: """ Efficiently determines if a given number is prime. Args: num (int): The number to check for primality. Returns: bool: True if the number is p...
python_sharegpt_23k
12,864
Write a Python function to clone a given array and reverse the order of its elements without modifying the original array. You are not allowed to use any built-in array manipulation functions such as `reverse` or `slice`. The function should start with: ```python from typing import List def clone_and_reverse(arr: List...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List def clone_and_reverse(arr: List[int]) -> List[int]: """ Clones a given array and reverses the order of its elements without modifying the original array. Args: arr (List[int]): The original ...
python_sharegpt_23k
12,865
Write a Python function to calculate the sum of all positive numbers in a given list. The function should ensure that the input list contains only numeric values (integers or floats). If any non-numeric values are found, the function should raise a `ValueError` with an appropriate error message. 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_sum_of_positives(numbers: List[float]) -> float: """ Calculate the sum of all positive numbers in the given list. Args: numbers (List[float]): A list of numbers (integers or floats)...
python_sharegpt_23k
12,871
Write a Python function to count the number of occurrences of each time value in a given array and determine the positions of each occurrence. The function should return a dictionary where the keys are the time values and the values are lists containing the count of occurrences and the positions of each occurrence. The...
Here is the Python code to solve the given problem in the markdown block: ```python from typing import List, Dict def count_time_occurrences(array: List[str]) -> Dict[str, List[int]]: """ Count the number of occurrences of each time value in the given array and determine their positions. Args: arr...
python_sharegpt_23k
12,873
Write a Python function to determine if a given number `n` is a prime palindrome. A prime palindrome is a number that is both a prime number and a palindrome. The function should return `True` if the number is a prime palindrome, and `False` otherwise. The code should start with: ```python import math def is_prime_pa...
Here is the Python code to solve the given problem in the markdown block: ```python import math def is_prime_palindrome(n: int) -> bool: """ Determine if a given number `n` is a prime palindrome. Args: n (int): The number to check. Returns: bool: True if `n` is a prime palindrome, Fal...