source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 13,278 | Write a Python function to count the number of occurrences of a given word in an array of strings, considering both case sensitivity and word boundaries. The function should return the count of exact matches of the target word in the array, where case sensitivity is preserved but word boundaries are not enforced.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_word_occurrences(words: List[str], target_word: str) -> int:
"""
Counts the number of occurrences of a given word in an array of strings, considering case sensitivity.
Args:
words ... |
python_sharegpt_23k | 13,286 | Write a Python function to count the number of duplicate elements in a given list of integers. The function should not modify the original list and should count each duplicate element only once. The code should start with:
```python
from typing import List
def count_duplicates(lst: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_duplicates(lst: List[int]) -> int:
"""
Counts the number of duplicate elements in a given list of integers.
Args:
lst (List[int]): The list of integers to check for duplicates.
... |
python_sharegpt_23k | 13,288 | Write a Python function to find the largest prime number from a given list of integers. The list can contain up to \(10^7\) integers, and each integer can range from 1 to \(10^9\). The function should have a time complexity of \(O(N)\) and a space complexity of \(O(1)\).
The code should start with:
```python
from typi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
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,... |
python_sharegpt_23k | 13,289 | Write a Python function to flatten a nested dictionary. The function should take a dictionary as input and return a flattened dictionary where any nested dictionaries are flattened into a single level dictionary. The keys of the output dictionary should be in the format "parentkey_childkey" for nested keys. The functio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Union
def flatten_nested_dict(d: Dict[str, Union[str, int, bool, Dict]]) -> Dict[str, Union[str, int, bool]]:
"""
Flatten a nested dictionary into a single-level dictionary.
Args:
d (Dict[s... |
python_sharegpt_23k | 13,298 | Write a Python function to identify a duplicate element in a list without using additional data structures such as sets or dictionaries. The solution should have a time complexity of O(n) and a space complexity of O(1). The function should start with:
```python
from typing import List
def find_duplicate_in_list(nums: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_duplicate_in_list(nums: List[int]) -> int:
"""
Identifies a duplicate element in a list without using additional data structures.
The solution has a time complexity of O(n) and a space compl... |
python_sharegpt_23k | 13,299 | Write a Python function to find the intersecting elements of two lists, ensuring that the program can handle input lists with up to 1000 elements. The function should return the intersecting elements in descending order based on their frequency in the combined input lists. Additionally, the function should handle cases... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
from collections import Counter
def find_intersecting_elements(list1: List[int], list2: List[int]) -> List[Tuple[int, int]]:
"""
Find the intersecting elements of two lists and return them in desc... |
python_sharegpt_23k | 13,307 | Write a Python function to create a deep copy of a given set and perform the following operations:
1. Initialize an empty set called `copy_set`.
2. Iterate through each element in the original set.
3. Add each element to the `copy_set` using the `add()` method.
4. Return a tuple containing the original set, the `copy_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set, Tuple
def deep_copy_set(original_set: Set[int]) -> Tuple[Set[int], Set[int], bool]:
"""
Creates a deep copy of a given set and checks if the copy is equal to the original set.
Args:
original... |
python_sharegpt_23k | 13,308 | Write a Python function to calculate the sum of each pair of adjacent elements from a given list of integers. The function should return a new list containing these sums. If the input list has an odd number of elements, the last element should be ignored.
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 sum_of_adjacent_pairs(original_list: List[int]) -> List[int]:
"""
Calculate the sum of each pair of adjacent elements from a given list of integers.
Args:
original_list (List[int]): The ... |
python_sharegpt_23k | 13,311 | Write a Python function to remove duplicates from a given array and return a new array with the remaining elements sorted in ascending order. The input array will not contain any negative numbers and will have a length of at most 100. The function must solve the problem in O(n) time complexity.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(arr: List[int]) -> List[int]:
"""
Removes duplicates from the given array and returns a new array with the remaining elements sorted in ascending order.
Args:
... |
python_sharegpt_23k | 13,317 | Write a Python function to perform a binary search in a sorted array of strings with up to 10 million elements. The array may contain duplicate strings, and the search should return the index of the first occurrence of the target string. The time complexity of the code should be O(log n), where n is the number of eleme... | 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[str], target: str) -> int:
"""
Perform a binary search to find the index of the first occurrence of the target string in a sorted array.
Args:
arr (List[s... |
python_sharegpt_23k | 13,322 | Write a Python function to combine two lists without duplication and in alphabetical order, without using any built-in sorting or deduplication functions/methods. The function should start with:
```python
from typing import List
def combine_and_sort_lists(list_one: List[str], list_two: List[str]) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def combine_and_sort_lists(list_one: List[str], list_two: List[str]) -> List[str]:
"""
Combines two lists without duplication and returns them in alphabetical order.
Args:
list_one (List[str... |
python_sharegpt_23k | 13,331 | Write a Python function to check if a given email address contains at least one uppercase letter, one lowercase letter, and one digit. The function should return `True` if all conditions are met and `False` otherwise. The code should start with:
```python
def check_email_validity(email: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def check_email_validity(email: str) -> bool:
"""
Checks if the given email address contains at least one uppercase letter, one lowercase letter, and one digit.
Args:
email (str): The email address to be checked.
... |
python_sharegpt_23k | 13,334 | Write a Python function to find the index of the first occurrence of an element in a given array that appears consecutively at least three times in a row. If no such element exists, return -1.
The code should start with:
```python
from typing import List
def first_consecutive_index(arr: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def first_consecutive_index(arr: List[int]) -> int:
"""
Find the index of the first occurrence of an element in the given array that appears consecutively at least three times in a row.
Args:
... |
python_sharegpt_23k | 13,335 | Write a Python function to determine if a given string is a palindrome. The function should ignore spaces, punctuation, and capitalization, but should consider non-alphabetic characters (such as digits and special characters) as part of the palindrome if they are in the original input string. The function should return... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_palindrome(string: str) -> bool:
"""
Determine if a given string is a palindrome.
This function ignores spaces, punctuation, and capitalization, but considers
non-alphabetic characters (such as digits ... |
python_sharegpt_23k | 13,337 | Write a Python function to remove all occurrences of a specified value from a given list and return the updated list along with the count of removed elements. The function should have a time complexity of O(n), where n is the length of the list.
The code should start with:
```python
from typing import List, Tuple
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def remove_elements(array: List[int], value: int) -> Tuple[List[int], int]:
"""
Removes all occurrences of a specified value from a given list and returns the updated list along with the count of ... |
python_sharegpt_23k | 13,341 | Write a Python function to get the second-to-last element of a given list without using any built-in list methods. The list may contain up to \(10^6\) elements, and all elements will be integers between 1 and \(10^6\) (inclusive). The function should handle edge cases such as an empty list or a list with fewer than two... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_second_to_last_element(lst: list) -> int:
"""
Returns the second-to-last element of the given list without using any built-in list methods.
Args:
lst (list): The list from which to extract the second-to-last... |
python_sharegpt_23k | 13,349 | Write a Python function to extract words ending with 'sion' from a given paragraph of text. The words should be preceded by a space or a punctuation mark and followed by a space or a punctuation mark. The paragraph may contain up to 1000 words and should be case-insensitive. The extracted words should be sorted in alph... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def extract_sion_words(text: str) -> List[str]:
"""
Extracts words ending with 'sion' from a given paragraph of text.
The words should be preceded by a space or punctuation mark and followe... |
python_sharegpt_23k | 13,352 | Write a Python function to calculate the average age of a group of people, while also finding the maximum and minimum ages. The function should handle cases where the age can be any positive integer. The code should start with:
```python
from typing import List, Tuple
def calculate_age_stats(ages: List[int]) -> Tuple[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def calculate_age_stats(ages: List[int]) -> Tuple[float, int, int]:
"""
Calculate the average age, minimum age, and maximum age from a list of ages.
Args:
ages (List[int]): A list of ... |
python_sharegpt_23k | 13,359 | Write a Python function to filter out palindromic strings from a given list of strings. A palindrome is a word or phrase that reads the same backward as forward, ignoring spaces, punctuation, and capitalization. The function should have a time complexity of O(n), where n is the length of the input list.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import string
def find_palindromes(strings: List[str]) -> List[str]:
"""
Filters out palindromic strings from a given list of strings.
Args:
strings (List[str]): A list of strings to be chec... |
python_sharegpt_23k | 13,360 | Write a Python function to compare two dictionaries by both key and value, ensuring that the keys are strings and the values are positive integers. The function should return a string indicating whether the dictionaries are the same, if they have different keys, or if they differ at a specific key. If the keys are not ... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_dictionaries(dict1: dict, dict2: dict) -> str:
"""
Compare two dictionaries by both key and value, ensuring that the keys are strings and the values are positive integers.
Args:
dict1 (dict): The first d... |
python_sharegpt_23k | 13,371 | Write a Python function to calculate the sum of the squares of the elements in a given list. The function should take a list of integers as input and return the sum of the squares of those integers. The code should start with:
```python
from typing import List
def sum_of_squares(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_squares(numbers: List[int]) -> int:
"""
Calculate the sum of the squares of the elements in a given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int:... |
python_sharegpt_23k | 13,372 | Write a Python function to implement an in-place Merge Sort algorithm that sorts a large array with a time complexity of O(n log n) and uses a constant amount of additional memory. The function should modify the input array directly without returning a new array. Additionally, provide a piece of code that showcases an ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def in_place_merge_sort(arr: List[int]) -> None:
"""
Sorts the input array in-place using the Merge Sort algorithm with a time complexity of O(n log n)
and using a constant amount of additional memor... |
python_sharegpt_23k | 13,373 | Write a Python function to shuffle a list randomly without using any built-in functions or libraries. The function should have a time complexity of O(n) and should not use any additional space beyond the input list. Additionally, ensure that the shuffled list is always guaranteed to be in a different order than the ori... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def shuffle_list(lst: List[int]) -> List[int]:
"""
Shuffles the given list randomly in-place without using any built-in functions or libraries.
The time complexity is O(n) and the space complexity is... |
python_sharegpt_23k | 13,375 | Write a Python function to find the longest common substring between two given strings. The function should return the longest contiguous sequence of characters that appears in both strings. The code should start with:
```python
def find_longest_common_substring(s1: str, s2: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_longest_common_substring(s1: str, s2: str) -> str:
"""
Find the longest common substring between two given strings.
Args:
s1 (str): The first string.
s2 (str): The second string.
Returns:
... |
python_sharegpt_23k | 13,377 | Write a Python function to insert a value into a Binary Search Tree (BST) and ensure the tree remains balanced. The function should handle the insertion of a new value and maintain the BST properties. Additionally, provide an example of erroneous code that could be used when implementing this data structure, and explai... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Optional
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert_value(value: int, tree: Optional[BSTNode]) -> BSTNode:
"""
Inse... |
python_sharegpt_23k | 13,388 | Write a Python function to find the median of a list of integers with an odd number of elements. The function should have a time complexity of O(n log n).
The code should start with:
```python
from typing import List
def find_median_of_list(nums: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_median_of_list(nums: List[int]) -> int:
"""
Find the median of a list of integers with an odd number of elements.
Args:
nums (List[int]): A list of integers with an odd number of el... |
python_sharegpt_23k | 13,400 | Write a Python function to perform a linear search on a list of elements and return the index of the first occurrence of the target element. If the target element is not found, the function should return -1. The function should terminate as soon as it finds the first occurrence of the target element.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def linear_search(arr: List[int], target: int) -> int:
"""
Perform a linear search on the given list to find the index of the first occurrence of the target element.
Args:
arr (List[int]): T... |
python_sharegpt_23k | 13,402 | Write a Python function to count the frequency of each character in a given string, considering case sensitivity and ignoring whitespace characters. The function should raise appropriate exceptions for empty strings and non-string inputs. The code should start with:
```python
from typing import Dict
def count_characte... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_character_frequency(input_string: str) -> Dict[str, int]:
"""
Counts the frequency of each character in the given string, considering case sensitivity and ignoring whitespace characters.
A... |
python_sharegpt_23k | 13,403 | Write a Python function that demonstrates the concept of closures. The function should take an integer as an argument and return another function that also takes an integer as an argument. The returned function should add the two integers together. Additionally, write a function to test if a given function is a closure... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Callable
import inspect
def create_closure(x: int) -> Callable[[int], int]:
"""
Creates a closure function that adds a given integer to another integer.
Args:
x (int): The integer to be added.
... |
python_sharegpt_23k | 13,407 | Write a Python function to calculate the sum of two numbers and ensure that the function has a time complexity of O(1). The function should be concise and free of any redundant parentheses. The code should start with:
```python
def sum_two_numbers(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_two_numbers(a: int, b: int) -> int:
"""
Calculate the sum of two integers.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The sum of the two integers.
"... |
python_sharegpt_23k | 13,409 | Write a Python function named `increment_divisible_by_three` that takes an integer `x` as input. The function should continuously increment `x` by 1 as long as `x` is greater than 10 and is divisible by 3. The function should return the final value of `x` after the loop terminates.
The code should start with:
```pytho... | Here is the Python code to solve the given problem in the markdown block:
```python
def increment_divisible_by_three(x: int) -> int:
"""
Increment the integer x by 1 as long as x is greater than 10 and divisible by 3.
Args:
x (int): The integer to be incremented.
Returns:
int: The fina... |
python_sharegpt_23k | 13,410 | Write a Python function to sort a list of integers in ascending order using the Bubble Sort algorithm. The function should meet the following requirements:
- The function should take a list of integers as input and return a sorted list in ascending order.
- The function should use the Bubble Sort algorithm to sort the ... | 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 | 13,416 | Write a Python function to find and return all prime numbers between 1 and a given upper limit `n`. The function should be optimized to only iterate through odd numbers greater than 2, since even numbers greater than 2 cannot be prime. The function should use a helper function to check if a number is prime by dividing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
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, Fal... |
python_sharegpt_23k | 13,417 | Write a Python function to reverse the order of elements in a given array without using any inbuilt functions. The function should modify the array in place and have a time complexity of O(n), where n is the length of the input array. Additionally, the function should use constant space complexity, meaning it should no... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array(nums: List[int]) -> None:
"""
Reverses the order of elements in the given array in place.
Args:
nums (List[int]): The array to be reversed.
Returns:
None: The ... |
python_sharegpt_23k | 13,419 | Write a Python function to generate a random user agent string that includes the name, version, and operating system of a specific web browser. Additionally, the user agent string should contain a randomly generated string of characters as a unique identifier for each user. The solution should have a time complexity of... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
import string
def generate_user_agent(browser_name: str, browser_version: str, os_name: str) -> str:
"""
Generate a random user agent string that includes the name, version, and operating system of a specific web ... |
python_sharegpt_23k | 13,422 | Write a Python function to extract and return a list of unique even numbers from a given list of integers, sorted in ascending order. The function should have a time complexity of O(n log n) or better, where n is the length of the input list.
The code should start with:
```python
from typing import List
def get_sorte... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_sorted_unique_evens(input_list: List[int]) -> List[int]:
"""
Extracts and returns a list of unique even numbers from the input list, sorted in ascending order.
Args:
input_list (List... |
python_sharegpt_23k | 13,423 | Write a Python function to clean a given sentence by removing all spaces and punctuation marks, and converting all letters to lowercase. The sentence should contain at least one word and should not exceed 100 characters in length. The function should return the cleaned sentence.
The code should start with:
```python
i... | Here is the Python code to solve the given problem in the markdown block:
```python
import string
def clean_sentence(sentence: str) -> str:
"""
Cleans a given sentence by removing all spaces and punctuation marks, and converting all letters to lowercase.
Args:
sentence (str): The sentence to be cl... |
python_sharegpt_23k | 13,427 | Write a Python function that generates a key-value pair for a given array. The key should be the sum of all the integer elements in the array, and the value should be a list of all the even integer elements in the array, sorted in descending order. The function should handle cases where the array contains non-integer e... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def generate_key_value_pair(arr: List[int]) -> Dict[int, List[int]]:
"""
Generates a key-value pair from the given array.
The key is the sum of all integer elements in the array.
The value... |
python_sharegpt_23k | 13,429 | Write a Python function to determine if a given substring is present in a string and return the indices at which the substring is found. If the substring is found multiple times, the function should return a list of all the indices.
The code should start with:
```python
from typing import List
def find_substring_indi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_substring_indices(string: str, substring: str) -> List[int]:
"""
Finds all occurrences of a substring within a string and returns a list of indices where the substring starts.
Args:
... |
python_sharegpt_23k | 13,430 | Write a Python function to reverse the elements of a given list without modifying the original list. The function should create a new list containing the reversed elements. The function should be optimized to use a time complexity of O(N) and a space complexity of O(N/2).
The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list(arr: List[int]) -> List[int]:
"""
Reverses the elements of a given list without modifying the original list.
Args:
arr (List[int]): The list of integers to be reversed.
... |
python_sharegpt_23k | 13,431 | Write a Python function to filter out prime numbers from a given list of integers. The function should have a time complexity of O(n*sqrt(m)), where n is the length of the original list and m is the maximum value in the original list.
The code should start with:
```python
from typing import List
import math
def get_p... | 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 given number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime,... |
python_sharegpt_23k | 13,435 | Write a Python function to find the index of the last occurrence of a target integer in a given list of integers. If the target integer is not present in the list, the function should return -1. The list is not guaranteed to be sorted.
The code should start with:
```python
from typing import List
def find_last_occurr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_last_occurrence(nums: List[int], target: int) -> int:
"""
Find the index of the last occurrence of a target integer in a list.
Args:
nums (List[int]): The list of integers to search... |
python_sharegpt_23k | 13,446 | Write a Python function to iterate over a 2-dimensional list and print out each element in a specific format. Additionally, modify the function to include an extra condition that only prints out the elements if the sum of their row is greater than a given threshold value.
The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def print_elements_with_threshold(two_dim_list: List[List[int]], threshold: int) -> None:
"""
Iterates over a 2-dimensional list and prints each element in the format "Element: {element}"
if the sum ... |
python_sharegpt_23k | 13,450 | Write a Python function to generate a list of prime numbers less than a given integer `n`. The function should ensure that the input is a positive integer greater than 1. If the input does not meet these criteria, the function should raise a `ValueError`.
The code should start with:
```python
def primes_less_than(n: i... | Here is the Python code to solve the given problem in the markdown block:
```python
def primes_less_than(n: int) -> list:
"""
Generate a list of prime numbers less than the given integer `n`.
Args:
n (int): The upper limit (exclusive) for generating prime numbers.
Returns:
list: A list... |
python_sharegpt_23k | 13,451 | Write a Python function to create a matrix of a specified shape filled with a specified value using the NumPy library. The function should take two parameters: the shape of the matrix (a tuple of integers) and the value to fill the matrix with (a float or integer). The function should return the created matrix.
The co... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_matrix(shape: tuple, value: float) -> np.ndarray:
"""
Create a matrix of the specified shape filled with the specified value.
Args:
shape (tuple): A tuple of integers representing the ... |
python_sharegpt_23k | 13,453 | Write a Python function to calculate the factorial of a given non-negative integer `n` using an iterative approach. The function should have a time complexity of O(n) and a space complexity of O(1). Additionally, it should handle the case when the input is a negative integer by returning `None`.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_iterative(n: int) -> int:
"""
Calculate the factorial of a given non-negative integer n using an iterative approach.
Args:
n (int): The non-negative integer for which to calculate the factorial.
R... |
python_sharegpt_23k | 13,463 | Write a Python function to remove duplicate elements from a given array while maintaining the original order of the elements. The array may contain both integers and strings. The function should return the modified array with duplicates removed.
The code should start with:
```python
from typing import List, Union
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def remove_duplicates(arr: List[Union[int, str]]) -> List[Union[int, str]]:
"""
Removes duplicate elements from the given array while maintaining the original order.
Args:
arr (List[U... |
python_sharegpt_23k | 13,464 | Write a Python function to build a dictionary that contains all even numbers from a given list. The list may contain duplicate numbers and negative numbers as well. The dictionary should only include even numbers that are greater than 5, and the value for each key should be the count of occurrences of that number in th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def build_even_dictionary(numbers: List[int]) -> Dict[int, int]:
"""
Builds a dictionary containing all even numbers greater than 5 from the given list.
The value for each key is the count of o... |
python_sharegpt_23k | 13,466 | Write a Python function to generate and print a series of numbers starting from 1, incrementing by 4, and terminating when the sum of the numbers in the series exceeds 100. The function should print each number in the series.
The code should start with:
```python
def print_series_until_sum_exceeds_100():
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_series_until_sum_exceeds_100():
"""
Generates and prints a series of numbers starting from 1, incrementing by 4,
and terminates when the sum of the numbers in the series exceeds 100.
The function prints each n... |
python_sharegpt_23k | 13,468 | Write a Python function to remove all duplicates from a given array of positive integers in-place and sort the remaining unique elements in descending order. The function should modify the original array and return the new length of the array. The solution should not use any additional data structures, such as sets or ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(arr: List[int]) -> int:
"""
Remove all duplicates from the given array of positive integers in-place and sort the remaining unique elements in descending order.
Args:
... |
python_sharegpt_23k | 13,469 | Write a Python function to check if a given list of integers is sorted in non-decreasing order using recursion. The function should return `True` if the list is sorted correctly, and `False` otherwise. The function should handle edge cases, such as an empty list and a list with duplicate elements, correctly. You are no... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_sorted_recursive(arr: List[int]) -> bool:
"""
Recursively checks if a given list of integers is sorted in non-decreasing order.
Args:
arr (List[int]): The list of integers to check.
... |
python_sharegpt_23k | 13,470 | Write a Python function to find all prime numbers within a given range. The function should take two integers as parameters, representing the start and end of the range (inclusive). If the start integer is greater than the end integer, the function should return an empty list. The function should not use any exception ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def find_prime_numbers(start: int, end: int) -> List[int]:
"""
Find all prime numbers within a given range [start, end].
Args:
start (int): The start of the range (inclusive).
... |
python_sharegpt_23k | 13,474 | Write a Python function to compute the Greatest Common Divisor (GCD) of two numbers without using any built-in functions, loops, or recursion. The numbers can be positive or negative and can have up to \(10^{18}\) digits. The function should handle special cases where one or both numbers are zero.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Compute the Greatest Common Divisor (GCD) of two numbers without using any built-in functions, loops, or recursion.
Args:
a (int): The first number.
b (int): The second nu... |
python_sharegpt_23k | 13,477 | Write a Python function to extract a substring from a given string based on two indices `a` and `b`. The function should not use any built-in substring or slice functions. Instead, it should manually iterate over the characters in the string and construct the substring. The function should handle edge cases such as emp... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_substring(string: str, a: int, b: int) -> str:
"""
Extracts a substring from the given string based on the indices a and b.
Args:
string (str): The input string from which to extract the substring.
a... |
python_sharegpt_23k | 13,480 | Write a Python function to detect outliers in a given array of at least 20 elements. An outlier is defined as a value that is either greater than 5 times the interquartile range (IQR) above the third quartile or less than 5 times the IQR below the first quartile. The function should return a list of all detected outlie... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import numpy as np
def detect_outliers(arr: List[int]) -> List[int]:
"""
Detects outliers in a given array of at least 20 elements.
An outlier is defined as a value that is either greater than 5 time... |
python_sharegpt_23k | 13,481 | Write a Python function to calculate the power of a number using a recursive approach with a time complexity of O(log n), where n is the exponent. The function should handle both positive and negative exponents and return a float if the result is a fraction. The function should not use the built-in power function or an... | Here is the Python code to solve the given problem in the markdown block:
```python
def power_recursive(base: float, exponent: int) -> float:
"""
Calculate the power of a number using a recursive approach with O(log n) time complexity.
Args:
base (float): The base number.
exponent (int): Th... |
python_sharegpt_23k | 13,485 | Write a Python function that uses the `enumerate` function to process a list of items and return a list of tuples where each tuple contains the index and the item from the original list. The function should handle lists of different types (e.g., strings, integers, floats). The code should start with:
```python
from typ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Any
def enumerate_items(items: List[Any]) -> List[Tuple[int, Any]]:
"""
Returns a list of tuples where each tuple contains the index and the item from the original list.
Args:
items ... |
python_sharegpt_23k | 13,486 | Write a Python function to demonstrate the difference between shallow copy and deep copy in Python. The function should take a list as input and return a tuple containing the original list, a shallow copy of the list, and a deep copy of the list. The function should also modify the nested list inside the shallow copy a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import copy
def demonstrate_copy_types(original_list: List) -> Tuple[List, List, List]:
"""
Demonstrates the difference between shallow copy and deep copy in Python.
Args:
original_li... |
python_sharegpt_23k | 13,491 | Write a Python function to compare two strings and determine if they are equal and if they are the same object in memory. The function should return a tuple where the first element is a boolean indicating if the strings are equal (using the `==` operator) and the second element is a boolean indicating if the strings ar... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(str1: str, str2: str) -> tuple:
"""
Compare two strings and determine if they are equal and if they are the same object in memory.
Args:
str1 (str): The first string to compare.
str2 (str... |
python_sharegpt_23k | 13,493 | Write a Python function to print each character of a given string in a new line, followed by its ASCII value, in reverse order. The function should have a time complexity of O(n^2) and a space complexity of O(1).
The code should start with:
```python
def print_characters_and_ascii_reverse(s: str) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_characters_and_ascii_reverse(s: str) -> None:
"""
Prints each character of the given string in reverse order, followed by its ASCII value.
Args:
s (str): The input string to process.
Returns:
... |
python_sharegpt_23k | 13,494 | Write a Python function to count the number of unique elements in a given list, ensuring that the solution has a time complexity of O(n^2) and does not use any additional data structures. The function should iterate through the list and check for each element if it is unique by comparing it with all subsequent elements... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_unique_elements(lst: List[int]) -> int:
"""
Counts the number of unique elements in a given list with a time complexity of O(n^2) and without using additional data structures.
Args:
... |
python_sharegpt_23k | 13,502 | Write a Python function to determine if a given string is a palindrome, considering only alphanumeric characters and ignoring case. You are not allowed to use any built-in string manipulation functions or methods. The function should start with:
```python
def is_palindrome(s: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(s: str) -> bool:
"""
Determine if a given string is a palindrome, considering only alphanumeric characters and ignoring case.
Args:
s (str): The input string to check.
Returns:
bool: T... |
python_sharegpt_23k | 13,504 | Write a Python function to calculate the average of positive integers in a given array. The function should only consider positive integers and should have a time complexity of O(n) and a space complexity of O(1). If there are no positive integers in the array, the function should return 0.
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 average_of_positive_numbers(arr: List[int]) -> float:
"""
Calculate the average of positive integers in a given array.
Args:
arr (List[int]): The array of integers to calculate the avera... |
python_sharegpt_23k | 13,505 | Write a Python function to find the second largest distinct number in a given list of integers. If there is no second largest distinct number, the function should return -1. The function should handle lists with duplicates and empty lists.
The code should start with:
```python
from typing import List
def second_large... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def second_largest_distinct(numbers: List[int]) -> int:
"""
Find the second largest distinct number in a list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
... |
python_sharegpt_23k | 13,507 | Write a Python function to reverse an array of length `n` using a divide-and-conquer approach. The function should return an array of the same length with elements in reverse order. The function should use constant extra space and have a time complexity of O(n log n). The code should start with:
```python
from typing i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array(arr: List[int]) -> List[int]:
"""
Reverses an array of length n using a divide-and-conquer approach.
Args:
arr (List[int]): The input array to be reversed.
Returns:
... |
python_sharegpt_23k | 13,510 | Write a Python function to convert a binary number represented as a string to its decimal equivalent without using any built-in functions or libraries that directly convert binary to decimal, and without using loops or recursion. The function should start with:
```python
def binary_to_decimal(binary: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def binary_to_decimal(binary: str) -> int:
"""
Converts a binary number represented as a string to its decimal equivalent.
Args:
binary (str): The binary number to be converted.
Returns:
int: The decima... |
python_sharegpt_23k | 13,516 | Write a Python function to calculate the sum of the squares of numbers between 1 and a given integer `n`, while also keeping track of the number of function calls made during the calculation. The function should handle negative input values by converting them to positive values before calculating the sum. Additionally,... | Here is the Python code to solve the given problem in the markdown block:
```python
# Global dictionary to store previously calculated results
memo = {}
def sum_of_squares(n: int, call_count: dict = None) -> int or str:
"""
Calculate the sum of the squares of numbers between 1 and n, while also keeping track o... |
python_sharegpt_23k | 13,520 | Write a Python function to implement an iterative binary search algorithm that searches for a target element in a sorted array. The algorithm should have a time complexity of O(log n) and a space complexity of O(1), where n is the size of the array. The function should return the index of the target element if it is fo... | 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 an iterative binary search to find the index of the target element in a sorted array.
Args:
arr (List[int]): A sorted list ... |
python_sharegpt_23k | 13,521 | Write a Python function to find the maximum element in a given list without using any built-in functions. The function should handle the case where the list may contain duplicate elements. The input list will always contain at least two elements, and the maximum element will always be unique. The function should have a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_element(lst: List[int]) -> int:
"""
Find the maximum element in a given list without using any built-in functions.
Args:
lst (List[int]): The list of integers from which to find... |
python_sharegpt_23k | 13,523 | Write a Python function to sort a list in ascending or descending order while removing any duplicate elements. The function should have a time complexity of O(n log n) and should be implemented using only a single loop and without using any built-in sorting functions or data structures. The function should start with:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_list_without_duplicates(given_list: List[int], sort_order: str) -> List[int]:
"""
Sorts a list in ascending or descending order while removing any duplicate elements.
The function has a time... |
python_sharegpt_23k | 13,525 | Write a Python function to rotate a given singly linked list by a specified number of positions to the right. The function should modify the original linked list in-place and return the new head of the rotated linked list.
The linked list node is defined as follows:
```python
class Node:
def __init__(self, data=No... | Here is the Python code to solve the given problem in the markdown block:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
def rotate_linked_list(head: Node, k: int) -> Node:
"""
Rotates a given singly linked list to the right by k positions.
Args:... |
python_sharegpt_23k | 13,527 | Write a Python function to calculate the sum of three numbers, ensuring that the function handles the input correctly and avoids the reference error present in the given code snippet. The function should accept three parameters and return their sum. The code should start with:
```python
def get_sum(a: int, b: int, c: i... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_sum(a: int, b: int, c: int) -> int:
"""
Calculate the sum of three integers.
Args:
a (int): The first integer.
b (int): The second integer.
c (int): The third integer.
Returns:
i... |
python_sharegpt_23k | 13,529 | Write a Python function to calculate the sum of all unique numbers in a given list. The function should handle cases where the list contains duplicate numbers and should return the sum of all distinct numbers in the list.
The code should start with:
```python
from typing import List
def sum_unique_numbers(numbers: Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_unique_numbers(numbers: List[int]) -> int:
"""
Calculate the sum of all unique numbers in the given list.
Args:
numbers (List[int]): The list of integers from which to calculate the ... |
python_sharegpt_23k | 13,532 | Write a Python function to find the maximum value in a given list of integers without using any built-in functions or methods for finding the maximum value, such as `max()` or `sort()`. The function should use only loops and conditional statements to determine the maximum value.
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_max_value(arr: List[int]) -> int:
"""
Find the maximum value in a given list of integers without using built-in functions or methods.
Args:
arr (List[int]): The list of integers fro... |
python_sharegpt_23k | 13,536 | Write a Python function to generate and return the first 1000 prime numbers. Additionally, implement a helper function that checks whether a given number is prime or not, and use this function within your main function to determine the prime numbers. Optimize your program to minimize the time complexity of the prime nu... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
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, Fal... |
python_sharegpt_23k | 13,537 | Write a Python function that iterates through a range of numbers from 0 to 9, prints each number, and keeps track of their sum. The loop should break when the number is greater than 5. After the loop, print the sum of all the numbers that were printed.
The code should start with:
```python
def print_numbers_and_sum():... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_numbers_and_sum():
"""
Iterates through a range of numbers from 0 to 9, prints each number, and keeps track of their sum.
The loop breaks when the number is greater than 5. After the loop, prints the sum of all the... |
python_sharegpt_23k | 13,538 | Write a Python function to determine whether a given positive integer is a prime number. A prime number is defined as a positive integer greater than 1 that has no positive divisors other than 1 and itself. The function should return `True` if the number is prime and `False` otherwise. The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime_number(n: int) -> bool:
"""
Determines whether a given positive integer is a prime number.
Args:
n (int): The positive integer to check for primality.
Returns:
bool: True if the number is p... |
python_sharegpt_23k | 13,539 | Write a Python function to perform a binary search on a sorted list of integers. The function should take two parameters: the list of integers and the target integer to search for. If the target integer is found in the list, the function should return its index. If the target integer is not found, the function should r... | 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 list of integers to find the index of the target integer.
Args:
arr (List[int]): The sorted lis... |
python_sharegpt_23k | 13,542 | Write a Python function to print the even numbers from a given range in reverse order, each on its own line. The function should take two integers, `start` and `end`, as input and print the even numbers from `start` to `end` (inclusive) in reverse order. If `start` is less than `end`, the function should print an empty... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_even_numbers_reverse(start: int, end: int) -> None:
"""
Prints the even numbers from `start` to `end` (inclusive) in reverse order, each on its own line.
If `start` is less than `end`, prints an empty line.
Ar... |
python_sharegpt_23k | 13,544 | Write a Python function to remove all duplicated entries from a given list of integers and sort the remaining numbers in descending order. The input list can contain up to \(10^6\) elements. The sorting algorithm used must have a time complexity of \(O(n \log n)\) or better.
The code should start with:
```python
from ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort_descending(num_list: List[int]) -> List[int]:
"""
Removes all duplicated entries from the given list of integers and sorts the remaining numbers in descending order.
A... |
python_sharegpt_23k | 13,557 | Write a Python function to find the largest prime number in a given array of integers. The array can contain duplicate values, and the function should handle cases where there are multiple largest prime numbers in the array. The function should return `None` if no prime numbers are found in the array.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
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, F... |
python_sharegpt_23k | 13,560 | Write a Python function to sort an array of integers in descending 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_descending(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_descending(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in descending order using the bubble sort algorithm.
Args:
arr (List[int]): The array of integers to b... |
python_sharegpt_23k | 13,568 | Write a Python function to reverse a given string recursively without using built-in library functions and without using additional data structures. The solution should have a time complexity of O(n) and a space complexity of O(1).
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 recursively without using built-in library functions and without using additional data structures.
Args:
s (str): The string to be reversed.
Return... |
python_sharegpt_23k | 13,574 | Write a Python function to extract unique strings from a given list while maintaining the original order of their first appearance. The input list can contain up to 1 million elements, so the solution should be efficient.
The code should start with:
```python
from typing import List
def get_unique_strings(input_list:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_unique_strings(input_list: List[str]) -> List[str]:
"""
Extracts unique strings from a given list while maintaining the order of their first appearance.
Args:
input_list (List[str]):... |
python_sharegpt_23k | 13,575 | Write a Python function to find the number of times a given target element appears in an array and return the indices at which the target element occurs. The array can contain both positive and negative integers, and its length can be up to 1 million elements. The target element can be any integer value. The time compl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def findOccurrences(array: List[int], target: int) -> List[int]:
"""
Find the number of times a given target element appears in an array and return the indices where it occurs.
Args:
array (... |
python_sharegpt_23k | 13,578 | Write a Python function to generate a random string of a given length that meets the following criteria:
1. The string should contain at least one uppercase letter, one lowercase letter, one digit, and one special character.
2. The string should not contain any repeating characters.
3. The function should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
import string
def generate_random_string(length: int) -> str:
"""
Generate a random string of the specified length that contains at least one uppercase letter,
one lowercase letter, one digit, and one special ... |
python_sharegpt_23k | 13,590 | Write a Python function to determine if a given positive integer is a perfect number. A perfect number is defined as a number whose sum of its proper divisors (excluding the number itself) equals the number. The function should efficiently handle large numbers up to \(10^{12}\) and should have a time complexity of \(O(... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_perfect_number(num: int) -> bool:
"""
Determines if a given positive integer is a perfect number.
A perfect number is a number that is equal to the sum of its proper divisors (excluding the number itself... |
python_sharegpt_23k | 13,592 | Write a Python function to generate a list of prime numbers within a given range [start, end]. The function should use a while loop to iterate through the range and check if each number is prime. If a number is prime, it should be added to the list of primes. The function should return the list of prime numbers.
The c... | 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 prime numbers within the given range [start, end].
Args:
start (int): The starting number ... |
python_sharegpt_23k | 13,593 | Write a Python function to sort a given list of unique numbers in descending order using a custom implementation of the bubble sort algorithm. The list should contain at least 5 numbers and no more than 10 numbers, and each number should be within the range of 1 to 100. The function should not use any built-in sorting ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_bubble_sort_descending(numbers: List[int]) -> List[int]:
"""
Sorts a given list of unique numbers in descending order using a custom bubble sort algorithm.
Args:
numbers (List[int... |
python_sharegpt_23k | 13,596 | Write a Python function to multiply all elements in a given list of integers without using the `*` operator or any built-in multiplication functions. You are allowed to use addition, subtraction, and bit shifting operations. The list may contain positive and negative integers. The code should start with:
```python
from... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def multiply_elements(my_list: List[int]) -> int:
"""
Multiplies all elements in a given list of integers without using the '*' operator.
Args:
my_list (List[int]): The list of integers to b... |
python_sharegpt_23k | 13,600 | Write a Python function to determine the day of the week for a given date in the format "YYYY-MM-DD" without using any built-in date or time libraries. The function should handle leap years correctly and have a time complexity of O(1).
The code should start with:
```python
def get_day_of_week(date: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def get_day_of_week(date: str) -> str:
"""
Determine the day of the week for a given date in the format "YYYY-MM-DD".
Args:
date (str): The date in the format "YYYY-MM-DD".
Returns:
str: The day of the ... |
python_sharegpt_23k | 13,601 | Write a Python function to count the number of unique characters in a given string, ignoring characters that are surrounded by either single or double quotation marks. The function should have a time complexity of O(n), where n is the length of the input string.
The code should start with:
```python
def count_unique_c... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_unique_characters(string: str) -> int:
"""
Counts the number of unique characters in a given string, ignoring characters that are surrounded by quotation marks.
Args:
string (str): The input string to coun... |
python_sharegpt_23k | 13,602 | Write a Python function to calculate the sum of all unique integers in a given list. The function should have a time complexity of O(n), where n is the length of the input list. The code should start with:
```python
from typing import List
def sum_unique_integers(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_unique_integers(lst: List[int]) -> int:
"""
Calculate the sum of all unique integers in the given list.
Args:
lst (List[int]): The list of integers from which to calculate the sum of... |
python_sharegpt_23k | 13,605 | Write a Python function to separate words and numbers from a given input string into two separate lists. The input string can contain multiple special characters. The words should be separated into a list of strings, while the numbers should be separated into a list of floats. Any special characters should be ignored. ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import re
def separate_words_and_numbers(input_string: str) -> Tuple[List[str], List[float]]:
"""
Separates words and numbers from a given input string into two separate lists.
Args:
... |
python_sharegpt_23k | 13,611 | Write a Python function to determine if a given string contains only vowels. The function should have a time complexity less than O(n log n) and a space complexity of O(1). The function should be case-insensitive and should return `True` if the string contains only vowels, and `False` otherwise.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
def contains_only_vowels(s: str) -> bool:
"""
Determines if a given string contains only vowels.
Args:
s (str): The input string to be checked.
Returns:
bool: True if the string contains only vowels, Fa... |
python_sharegpt_23k | 13,612 | Write a Python function to copy an array of integers efficiently, where the array size can be in the range of \(10^6\) to \(10^7\). The function should not use any built-in array functions or methods for copying the array. Instead, it should manually iterate through the entire array and copy each element to a new array... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def copy_array(original_array: List[int]) -> List[int]:
"""
Efficiently copies an array of integers without using built-in array functions or methods.
Args:
original_array (List[int]): The o... |
python_sharegpt_23k | 13,613 | Write a Python function to reverse the order of elements in a list using only recursion. The function should have a time complexity of O(n) and should not use any additional data structures. The code should start with:
```python
from typing import List
def reverse_list_recursive(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 reverse_list_recursive(lst: List[int]) -> List[int]:
"""
Reverses the order of elements in a list using recursion.
Args:
lst (List[int]): The list to be reversed.
Returns:
L... |
python_sharegpt_23k | 13,616 | Write a Python function to format a string for the discounted price based on the given constraints. The function should take the original price and the discount percentage as inputs and return a formatted string that includes the discounted price, the discount percentage, and a special message if the discount percentag... | Here is the Python code to solve the given problem in the markdown block:
```python
def format_discounted_price(original_price: float, discount_percentage: float) -> str:
"""
Formats a string for the discounted price based on the given constraints.
Args:
original_price (float): The original price o... |
python_sharegpt_23k | 13,621 | Write a Python function to calculate the maximum, minimum, and median of a list of numbers, excluding any outliers when calculating the median. The list may contain duplicates and negative numbers. The function should handle cases where the list is empty or contains only one element. The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Optional
def calculate_statistics(numbers: List[int]) -> Tuple[Optional[int], Optional[int], Optional[float]]:
"""
Calculate the maximum, minimum, and median of a list of numbers, excluding any o... |
python_sharegpt_23k | 13,628 | Write a Python function to print out each element of a given list in reverse order. The list must contain at least 5 elements and should only include even numbers. The function should validate the input list to ensure it meets these criteria before printing the elements.
The code should start with:
```python
from typi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def print_even_numbers_reverse(numbers: List[int]) -> None:
"""
Prints each element of a given list in reverse order. The list must contain at least 5 elements and should only include even numbers.
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.