source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 10,893 | Write a Python function to determine if a given string is a palindrome. A palindrome is a string that reads the same forward and backward, ignoring non-alphanumeric characters and considering case insensitivity. The function should have a time complexity of O(n), where n is the length of the input string, and should no... | 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, ignoring non-alphanumeric characters and considering case insensitivity.
Args:
s (str): The input string to check for palindrome pr... |
python_sharegpt_23k | 10,895 | Write a Python function to generate and print the multiplication table of a given number, ensuring that the table only includes the multiples of 3 and ends at the 20th multiple. Additionally, the table should only display the multiples of 3 that are also divisible by 2. The function should start with:
```python
def gen... | Here is the Python code to solve the given problem in the markdown block:
```python
def generate_multiplication_table(number: int) -> None:
"""
Generates and prints a filtered multiplication table for the given number.
The table includes only the multiples of 3 that are also divisible by 2,
and it ends ... |
python_sharegpt_23k | 10,897 | Write a Python function to find the greatest common factor (GCF) of three prime numbers efficiently. The function should have a time complexity of O(n), where n is the largest prime number among the three. The code should start with:
```python
from typing import List
def find_gcf_of_three_primes(prime_numbers: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_gcf_of_three_primes(prime_numbers: List[int]) -> int:
"""
Find the greatest common factor (GCF) of three prime numbers efficiently.
Args:
prime_numbers (List[int]): A list of three ... |
python_sharegpt_23k | 10,900 | Write a Python function to remove the first element from a given list and shift all the remaining elements to fill the empty space. The function should not use any built-in array functions or additional data structures. The code should start with:
```python
from typing import List
def remove_and_shift(arr: List[int]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_and_shift(arr: List[int]) -> List[int]:
"""
Removes the first element from the given list and shifts all the remaining elements to fill the empty space.
Args:
arr (List[int]): The... |
python_sharegpt_23k | 10,901 | Write a Python function to calculate the total number of digits in a given number. The number can be positive or negative. If the number is negative, consider the negative sign as a digit as well. For example, the number -1234 has a total of 5 digits.
The code should start with:
```python
def count_digits(number: int)... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_digits(number: int) -> int:
"""
Calculate the total number of digits in a given number.
Args:
number (int): The number for which to count the digits.
Returns:
int: The total number of digits i... |
python_sharegpt_23k | 10,903 | Write a Python function to find the kth smallest element in an array without using any sorting algorithms or built-in functions. The function must solve the problem in O(n) time complexity and use only a constant amount of extra space. The original array should not be modified or copied.
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 partition(arr: List[int], start: int, end: int) -> int:
"""
Partitions the array around a pivot such that elements less than or equal to the pivot
are on the left, and elements greater than the p... |
python_sharegpt_23k | 10,904 | Write a Python function to insert a new element into a list at a specified index without using any built-in list methods such as `insert()` or `append()`. The function should handle cases where the given index is out of range by either raising a custom exception or returning an appropriate error message. The function s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def insert_element(lst: List[int], index: int, element: int) -> Union[List[int], str]:
"""
Inserts a new element into a list at the specified index without using built-in list methods.
Args:
... |
python_sharegpt_23k | 10,908 | Write a Python function to convert a given string to uppercase without using any built-in functions or methods that directly convert a string to uppercase. The function should handle all types of characters, including special characters and non-alphabetic characters, and should not modify the original string. It should... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_uppercase(string: str) -> str:
"""
Converts a given string to uppercase without using any built-in functions or methods that directly convert a string to uppercase.
Args:
string (str): The input strin... |
python_sharegpt_23k | 10,911 | Write a Python function to convert a given string to uppercase without using any built-in string methods or any other built-in functions that directly manipulate strings. Additionally, you are not allowed to use any external libraries or modules. The function should handle all types of characters, including alphabets, ... | Here is the Python code to solve the given problem in the markdown block:
```python
def to_uppercase(char: str) -> str:
"""
Converts a single character to uppercase if it is a lowercase letter.
Args:
char (str): The character to convert.
Returns:
str: The uppercase character if... |
python_sharegpt_23k | 10,912 | Write a Python function to extract the first three unique elements from a given list without using built-in slicing functionality. Instead, manually iterate through the list and extract the elements one by one. The function should handle cases where the list may contain duplicate elements and should return a list with ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def extract_first_three_unique_elements(lst: List[int]) -> List[int]:
"""
Extracts the first three unique elements from the given list.
Args:
lst (List[int]): The list from which to extract ... |
python_sharegpt_23k | 10,915 | Write a Python function to generate a random string of a specified length, where each character must be a lowercase letter. The string should contain at least one vowel and one consonant. The function should handle cases where the specified length is less than 2 or greater than 100.
The code should start with:
```pyth... | 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, containing at least one vowel and one consonant.
Args:
length (int): The length of t... |
python_sharegpt_23k | 10,916 | Write a Python function to check if a given string has all unique characters. The function should have a time complexity of O(n), where n is the length of the string, and it should not use any additional data structures.
The code should start with:
```python
def has_unique_chars(string: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def has_unique_chars(string: str) -> bool:
"""
Check if a given string has all unique characters using bit manipulation.
Args:
string (str): The string to check for unique characters.
Returns:
bool: Tru... |
python_sharegpt_23k | 10,920 | Write a Python function to retrieve the kth smallest element from a given list, where k is an integer greater than or equal to 1 and less than or equal to the length of the list. The list may contain duplicate elements. The function should be efficient and handle edge cases such as an empty list or invalid k values.
T... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_kth_smallest(lst: List[int], k: int) -> int:
"""
Retrieve the kth smallest element from a given list.
Args:
lst (List[int]): The list of integers from which to find the kth smallest... |
python_sharegpt_23k | 10,924 | Write a Python function to determine if a given string is a palindrome. The function should ignore any non-alphanumeric characters, be case-insensitive, and handle strings with leading or trailing spaces. The function should be implemented without using any built-in string manipulation functions such as `reverse()`.
T... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(string: str) -> bool:
"""
Determine if a given string is a palindrome, ignoring non-alphanumeric characters, case, and leading/trailing spaces.
Args:
string (str): The input string to check for pal... |
python_sharegpt_23k | 10,927 | Write a Python function to validate a given credit card number and return the type of the credit card (Visa, MasterCard, American Express, Discover) based on the card number prefix. Additionally, the function should validate the credit card number using the Luhn algorithm. The function should return a tuple containing ... | Here is the Python code to solve the given problem in the markdown block:
```python
def validate_credit_card(card_number: str) -> tuple:
"""
Validates a given credit card number and returns the type of the credit card (Visa, MasterCard, American Express, Discover) based on the card number prefix.
Also valid... |
python_sharegpt_23k | 10,929 | Write a Python function to calculate the `n`th number in the Fibonacci sequence using only basic arithmetic operations and logical operators. The function should not use recursion, iteration, or any built-in mathematical functions (such as `pow`, `sqrt`, etc.). The time complexity of the solution should be O(1) and the... | Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci_direct(n: int) -> int:
"""
Calculate the nth Fibonacci number using only basic arithmetic operations.
Args:
n (int): The position in the Fibonacci sequence to calculate.
Returns:
int: The ... |
python_sharegpt_23k | 10,935 | Write a Python function to sort a list of integers using the insertion sort algorithm. The function should take a list of integers as input and return the sorted list in ascending order. The insertion sort algorithm should be implemented as described in the given code snippet.
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
def insertion_sort(nums: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the insertion sort algorithm.
Args:
nums (List[int]): The list of integers to be sorte... |
python_sharegpt_23k | 10,936 | Write a Python function to produce a list containing tuples where each tuple is the sum of the first and last element of each inner list inside a list of lists. The inner lists can contain both positive and negative integers. Additionally, the function should handle cases where the inner lists contain an odd number of ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sum_first_last_elements(lst: List[List[int]]) -> List[Tuple[int, int]]:
"""
Produces a list containing tuples where each tuple is the sum of the first and last element of each inner list insid... |
python_sharegpt_23k | 10,943 | Write a Python function to remove duplicate elements from a given list while maintaining the original order of elements and without using any additional data structures. The function should have a time complexity of O(n^2).
The code should start with:
```python
from typing import List
def remove_duplicates_in_place(a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_place(arr: List[int]) -> List[int]:
"""
Removes duplicate elements from the given list while maintaining the original order and without using additional data structures.
Arg... |
python_sharegpt_23k | 10,944 | Write a Python function that takes in two lists as input and returns a new list that contains all the elements from both lists, with duplicates removed. The function should concatenate the lists using a for loop and without using any built-in methods such as `extend()` or `+`. Additionally, the function should ensure t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def concatenate_lists_without_duplicates(list1: List[int], list2: List[int]) -> List[int]:
"""
Concatenates two lists and returns a new list containing all unique elements from both lists.
Args:
... |
python_sharegpt_23k | 10,946 | Write a Python function to reverse a given list without using the built-in `reverse()` method, slicing syntax, or any other built-in function or method to reverse the list. The function should use a two-pointer technique to swap elements from the beginning and end of the list until the middle is reached.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list_in_place(lst: List[int]) -> List[int]:
"""
Reverses the given list in place without using built-in reverse methods or slicing.
Args:
lst (List[int]): The list to be reversed... |
python_sharegpt_23k | 10,949 | Write a Python function to sort a list of dictionaries by the values associated with the key 'age' in ascending order. Ensure that all dictionaries in the list contain the key 'age' and that the values associated with this key are integers. If any dictionary does not meet these criteria, raise a `ValueError`.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_dict_list_by_age(dicts: List[Dict[str, any]]) -> List[Dict[str, any]]:
"""
Sorts a list of dictionaries by the 'age' key in ascending order.
Args:
dicts (List[Dict[str, any]])... |
python_sharegpt_23k | 10,952 | Write a Python function to add a key-value pair to a given dictionary where the key is a tuple of integers and the value is a list of tuples, where each tuple contains a string and a boolean value. The function should ensure that the dictionary is updated correctly and return the updated dictionary.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Tuple
def add_key_value_pair(my_dict: Dict[int, str], key: Tuple[int, int], value: List[Tuple[str, bool]]) -> Dict[int, str]:
"""
Adds a key-value pair to the given dictionary where the key is a t... |
python_sharegpt_23k | 10,953 | Write a Python function to determine if a string contains all unique characters. The function should return `True` if all characters in the string are unique, and `False` otherwise. The string should not contain any whitespace characters. The function should be case-sensitive, meaning 'A' and 'a' are considered differe... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_unique_string(string: str) -> bool:
"""
Determines if a string contains all unique characters.
Args:
string (str): The input string to check for unique characters.
Returns:
bool: True if all char... |
python_sharegpt_23k | 10,956 | Write a Python function to remove all elements with a value of 0 from a given list and then sort the remaining elements in ascending order. The function should meet the following requirements:
1. The time complexity should be O(n log n) or better.
2. The space complexity should be O(1) or better.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_zeros_and_sort_list(lst: List[int]) -> List[int]:
"""
Removes all zeros from the given list and sorts the remaining elements in ascending order.
Args:
lst (List[int]): The list fr... |
python_sharegpt_23k | 10,957 | Write a Python function to find the reverse of a given string without using any built-in string manipulation methods, loops, or recursion. The function should have a time complexity of O(n), where n is the length of the string. 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 the given string without using built-in string manipulation methods, loops, or recursion.
Args:
s (str): The input string to be reversed.
Returns:
str: Th... |
python_sharegpt_23k | 10,958 | Write a Python function to calculate the Fibonacci series up to `n` numbers efficiently. The function should handle large values of `n` (e.g., `n > 10000`) without causing a stack overflow or excessive computation time. The algorithm should have a time complexity of O(n) and a space complexity of O(1).
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number efficiently using an iterative approach.
Args:
n (int): The position in the Fibonacci series to calculate.
Returns:
int: Th... |
python_sharegpt_23k | 10,960 | Write a Python function to implement a binary search algorithm that finds the index of the last occurrence of a target element in a sorted array. The array may contain duplicate elements. If the target element is not found, the function should return -1. The function 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 find_last_occurrence(arr: List[int], target: int) -> int:
"""
Finds the index of the last occurrence of the target element in a sorted array.
Args:
arr (List[int]): A sorted list of inte... |
python_sharegpt_23k | 10,961 | Write a Python function to determine if one string is a substring of another without using any built-in string searching functions or libraries. The function should handle cases where the input strings are empty or have special characters, and it should be efficient enough to handle very long strings (more than 1 milli... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_substring(str1: str, str2: str) -> str:
"""
Determine if one string is a substring of another without using built-in string searching functions.
Args:
str1 (str): The first string.
str2 (str): The sec... |
python_sharegpt_23k | 10,962 | Write a Python function to print the even numbers from a given list using a while loop. The function should not use the modulus operator (%) to check if a number is even. Instead, it should use bitwise operators to determine if a number is divisible by 2.
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 print_even_numbers(numbers: List[int]) -> None:
"""
Prints the even numbers from the given list using a while loop and bitwise operators.
Args:
numbers (List[int]): The list of integers ... |
python_sharegpt_23k | 10,965 | Write a Python function to print the words "hello world" five times, but each occurrence must be capitalized and separated by a comma. The function should use a loop to print the words instead of repeating the print statement five times. However, you are not allowed to use any string manipulation functions or methods t... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_hello_world_five_times():
"""
Prints the words "hello world" five times, each capitalized and separated by a comma.
This function uses a loop to print the words instead of repeating the print statement five times.... |
python_sharegpt_23k | 10,966 | Write a Python function to correctly calculate the sum of two numbers, ensuring that it handles both numeric and string inputs appropriately. The function should return the sum of two numeric inputs and concatenate two string inputs. If the inputs are of mixed types (one numeric and one string), the function should ret... | Here is the Python code to solve the given problem in the markdown block:
```python
def add(a, b):
"""
Calculate the sum of two numbers or concatenate two strings.
Args:
a: The first input, which can be a number or a string.
b: The second input, which can be a number or a string.
Retur... |
python_sharegpt_23k | 10,973 | Write a Python function to calculate the sum of all even numbers in a given list of integers, the count of even numbers, and the average of all even numbers rounded to two decimal places. The function should use a while loop and must not use any built-in functions or methods that directly handle lists or numbers, such ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sum_even_numbers(numbers: List[int]) -> Tuple[int, int, float]:
"""
Calculate the sum of all even numbers in the given list, the count of even numbers,
and the average of all even numbers ... |
python_sharegpt_23k | 10,976 | Write a Python function to determine if a given number `x` is a prime number and another number `y` is a perfect square. The function should return a boolean value indicating whether both conditions are satisfied.
The code should start with:
```python
import math
def is_prime(x: int) -> bool:
# Function to check ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime(x: int) -> bool:
"""
Check if the given number x is a prime number.
Args:
x (int): The number to check for primality.
Returns:
bool: True if x is a prime number, False otherwis... |
python_sharegpt_23k | 10,978 | Write a Python function to find the highest positive value in a given list of numbers while ensuring that the list does not contain any duplicates. If duplicates are found, raise a `ValueError`. The input list will have at least 5 numbers and at most 1000 numbers. The function should efficiently find the highest value ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_highest_positive_value(numbers: List[int]) -> int:
"""
Find the highest positive value in a list of numbers while ensuring no duplicates.
Args:
numbers (List[int]): A list of intege... |
python_sharegpt_23k | 10,983 | Write a Python function to generate a random string of a specified length composed of numbers, lowercase letters, and special characters. The function should ensure that the generated string contains at least one number, one lowercase letter, and one special character. The code should start with:
```python
import rando... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
import string
def generate_secure_random_string(length: int) -> str:
"""
Generate a random string of the specified length composed of numbers, lowercase letters, and special characters.
The string will contain... |
python_sharegpt_23k | 10,984 | Write a Python function to compute the sum of integers and floats in a given list, and count the number of strings. The list may contain a mix of integers, floats, and strings, and its maximum length is 100 elements. The function should return a tuple containing the sum of the numeric values and the count of the string... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sum_numeric_and_count_strings(lst: List[object]) -> Tuple[float, int]:
"""
Compute the sum of integers and floats in a list and count the number of strings.
Args:
lst (List[object... |
python_sharegpt_23k | 10,985 | Write a Python function to print all prime numbers up to a given limit using a while loop. The function should check if each number is prime and print it if it is. The code should start with:
```python
def print_primes_up_to(limit: int):
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_primes_up_to(limit: int):
"""
Prints all prime numbers up to the given limit.
Args:
limit (int): The upper limit up to which prime numbers should be printed.
Returns:
None: The function prints... |
python_sharegpt_23k | 10,999 | Write a Python function to identify the longest common prefix among a list of strings, considering case sensitivity. The function should efficiently handle large inputs with up to \(10^6\) strings, each with a maximum length of \(10^4\) characters. The function should have a time complexity of \(O(N \times M)\), where ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def longest_common_prefix(strings: List[str]) -> str:
"""
Identify the longest common prefix among a list of strings, considering case sensitivity.
Args:
strings (List[str]): A list of strin... |
python_sharegpt_23k | 11,001 | Write a Python function to generate and return the Fibonacci sequence up to the nth number. The function should take an integer `n` as input and return a list containing the first `n` Fibonacci numbers. The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the nth number.
Args:
n (int): The number of Fibonacci numbers to generate.
Returns:
List[... |
python_sharegpt_23k | 11,009 | Write a Python function to convert a given positive integer from base 10 to base 16. The function should handle the case where the input number is zero and return "0". The code should start with:
```python
def convert_to_base16(number: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_base16(number: int) -> str:
"""
Convert a given positive integer from base 10 to base 16.
Args:
number (int): The positive integer to convert.
Returns:
str: The hexadecimal representation... |
python_sharegpt_23k | 11,011 | Write a Python function to filter out and return a new list containing only the positive numbers from a given list of integers and floating-point numbers. The function should have a time complexity of O(n) and a space complexity of O(1), meaning it should not use any additional data structures that grow with the input ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_positive_numbers(nums: List[float]) -> List[float]:
"""
Filters out and returns a new list containing only the positive numbers from the given list of integers and floating-point numbers.
Ar... |
python_sharegpt_23k | 11,017 | Write a Python function to find the maximum number in a given array without using any inbuilt functions, loops, or recursion. The array may contain negative numbers. The code should start with:
```python
from typing import List
def find_max_number(arr: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_number(arr: List[int]) -> int:
"""
Find the maximum number in a given array without using any inbuilt functions, loops, or recursion.
Args:
arr (List[int]): The array of integer... |
python_sharegpt_23k | 11,022 | Write a Python function called `swap_and_multiply` that takes in two integers, `a` and `b`, as arguments. Inside the function, swap the values of `a` and `b` using a bitwise XOR operation without using any temporary variable. Then, multiply `a` by the original value of `b` and return the result.
To increase the diffic... | Here is the Python code to solve the given problem in the markdown block:
```python
def swap_and_multiply(a: int, b: int) -> int:
"""
Swaps the values of two integers `a` and `b` using bitwise XOR and then multiplies the swapped value of `a` by the original value of `b`.
Args:
a (int): The first in... |
python_sharegpt_23k | 11,024 | Write a Python function to filter out positive numbers greater than 10 from a given array and sort the resulting array in ascending order using a custom merge sort algorithm. The solution should not use any built-in Python functions or libraries for sorting and should have a time complexity of O(n log n).
The code sho... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_and_sort_positive_numbers(arr: List[int]) -> List[int]:
"""
Filters out positive numbers greater than 10 from the given array and sorts the resulting array in ascending order using a custom me... |
python_sharegpt_23k | 11,033 | Write a Python function to print the first 20 positive integers in reverse order, excluding any numbers divisible by both 2 and 3. Instead of printing the integers themselves, calculate and print the sum of their squares.
The code should start with:
```python
def print_sum_of_squares_of_filtered_numbers():
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def print_sum_of_squares_of_filtered_numbers():
"""
Prints the sum of the squares of the first 20 positive integers in reverse order,
excluding any numbers divisible by both 2 and 3.
This function iterates from 20 down ... |
python_sharegpt_23k | 11,047 | Write a Python function to convert all strings in a given list to uppercase, ensuring the function adheres to the following constraints:
1. The function should have a time complexity of O(n), where n is the total number of characters in all the strings.
2. The function should have a space complexity of O(1), excluding ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_to_uppercase(strings: List[str]) -> List[str]:
"""
Convert all strings in the given list to uppercase.
Args:
strings (List[str]): A list of strings to be converted to uppercase.
... |
python_sharegpt_23k | 11,048 | Write a Python function to calculate the cumulative sum of an array of integers in place. The function should modify the input array such that each element at index `i` contains the cumulative sum of all elements from the start of the array up to index `i`. The function should handle arrays of any size, ranging from 0 ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cumulative_sum_in_place(arr: List[int]) -> None:
"""
Modifies the input array in place to store the cumulative sum at each index.
Args:
arr (List[int]): The input array of integers.
... |
python_sharegpt_23k | 11,052 | Write a Python function to output the middle 3 elements of a given array. The function should not use any built-in array functions or methods to determine the length or index of the array. Additionally, it should not use any additional variables or data structures to store the middle 3 elements. The solution should hav... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_middle_3_elements(arr):
"""
Prints the middle 3 elements of a given array.
Args:
arr (list): The array from which to print the middle 3 elements.
Returns:
None: The function prints the middle ... |
python_sharegpt_23k | 11,065 | Write a Python function that generates a personalized greeting message based on the provided name and a set of predefined greeting templates. The function should randomly select a greeting template from the list and insert the provided name into the template. The function should return the generated greeting message.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_greeting(name: str, templates: List[str]) -> str:
"""
Generates a personalized greeting message by randomly selecting a template from the provided list and inserting the name.
... |
python_sharegpt_23k | 11,067 | Write a Python function to generate a list of `n` strings, each containing random characters of length between `min_length` and `max_length`, inclusive. The function should return the list of generated strings.
The code should start with:
```python
from typing import List
import random
import string
def generate_rand... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
import string
def generate_random_strings(n: int, min_length: int, max_length: int) -> List[str]:
"""
Generate a list of `n` strings, each containing random characters of length between `mi... |
python_sharegpt_23k | 11,070 | Write a Python function that takes a list of integers and returns a list containing only the odd numbers from the original list. The resulting list should be sorted in descending order and should not contain any duplicates. Implement the solution without using any built-in functions or methods for sorting or removing d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_sorted_odd_numbers(nums: List[int]) -> List[int]:
"""
Extracts all odd numbers from the input list, removes duplicates, and sorts them in descending order.
Args:
nums (List[int]): Th... |
python_sharegpt_23k | 11,072 | Write a Python function that demonstrates the concept of lexical closure. The function should accept a parameter and return another function that also accepts a parameter. The returned function should compute and return a value using both its own parameter and the parameter from the outer function. The code should star... | Here is the Python code to solve the given problem in the markdown block:
:
```python
def create_closure(outer_param):
"""
Creates a closure function that uses a parameter from the outer function.
Args:
outer_param (int): The parameter to be used in the closure function.
Returns:
funct... |
python_sharegpt_23k | 11,073 | Write a Python function to find the sum of all prime numbers from 2 to a given integer `n`, and also return a list of all the prime numbers within that range. The function should be efficient and handle edge cases such as when `n` is less than 2.
The code should start with:
```python
from typing import List, Tuple
de... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import math
def is_prime(num: int) -> bool:
"""
Helper function to check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if ... |
python_sharegpt_23k | 11,081 | Write a Python function to find the index of the element that appears an odd number of times in a given array of integers. The array will contain at most \(10^6\) elements and each element will be an integer between \(-10^9\) and \(10^9\) (inclusive). The time complexity of your solution should be \(O(n)\) and the spac... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_odd_occurrence_index(arr: List[int]) -> int:
"""
Find the index of the element that appears an odd number of times in the given array.
Args:
arr (List[int]): The array of integers w... |
python_sharegpt_23k | 11,082 | Write a Python function to create a copy of a list while also reversing the order of its elements, without using any built-in list methods or functions like `reverse()`, `reversed()`, or slicing (`[::-1]`). The function should iterate through the original list and construct the reversed copy manually.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_copy_list(original_list: List[int]) -> List[int]:
"""
Creates a copy of the given list with its elements in reverse order.
Args:
original_list (List[int]): The original list to b... |
python_sharegpt_23k | 11,085 | Write a Python function to compare two strings recursively and determine if they are equal, considering case sensitivity and ignoring leading and trailing white spaces. The function should not use any built-in string comparison methods or functions. The code should start with:
```python
def compare_strings(s1: str, s2:... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(s1: str, s2: str) -> bool:
"""
Recursively compares two strings for equality, considering case sensitivity and ignoring leading and trailing white spaces.
Args:
s1 (str): The first string to comp... |
python_sharegpt_23k | 11,087 | Write a Python function to calculate the nth Fibonacci number using a recursive approach with memoization to optimize performance. Additionally, handle negative input values by raising a `ValueError` with a custom error message. The function should start with:
```python
def fibonacci(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number using a recursive approach with memoization.
Args:
n (int): The position in the Fibonacci sequence to calculate.
Returns:
int: The nt... |
python_sharegpt_23k | 11,091 | Write a Python function to determine if an array has duplicate elements or not. The function should have a time complexity of O(n log n) and a space complexity of O(n). The array can contain integer elements ranging from 1 to 10^6.
The code should start with:
```python
from typing import List
def has_duplicates(arr: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def has_duplicates(arr: List[int]) -> bool:
"""
Determines if an array has duplicate elements.
Args:
arr (List[int]): The array to check for duplicates.
Returns:
bool: True if t... |
python_sharegpt_23k | 11,092 | Write a Python function to find the maximum sum of a subarray within a given array of integers. The subarray must consist of consecutive elements from the original array. The function should have a time complexity of O(n), where n is the length of the input array. You are not allowed to use any built-in functions or li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
"""
Find the maximum sum of a subarray within the given array of integers.
Args:
arr (List[int]): The input array of integers.
Returns:
... |
python_sharegpt_23k | 11,096 | Implement a sorting algorithm to sort a list of integers in descending order. Your algorithm should have a time complexity of O(n log n) or better. You are not allowed to use any built-in sorting functions or libraries. Additionally, your algorithm should be space efficient and use constant extra space, i.e., it should... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_descending(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using an in-place merge sort algorithm.
Args:
arr (List[int]): The list of integers to be s... |
python_sharegpt_23k | 11,101 | Write a Python function to compare the performance of the bottom-up merge sort algorithm and the quicksort algorithm using Hoare's partition scheme. The function should return a dictionary with the following keys: "time_complexity", "space_complexity", "stability", and "average_case_performance". Each key should have a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def compare_sorting_algorithms(arr: List[int]) -> Dict[str, tuple]:
"""
Compares the bottom-up merge sort and quicksort algorithms using Hoare's partition scheme.
Args:
arr (List[int])... |
python_sharegpt_23k | 11,103 | Write a Python function to determine if a given integer `x` is a prime number. The function should return `True` if `x` is a prime number and `False` otherwise. The function should handle edge cases such as negative numbers, 0, and 1, which are not prime numbers. The code should start with:
```python
def is_prime(x: in... | Here is the Python code to solve the given problem in the markdown block:
:
```python
def is_prime(x: int) -> bool:
"""
Determine if a given integer `x` is a prime number.
Args:
x (int): The integer to check for primality.
Returns:
bool: True if `x` is a prime number, False otherwise.
... |
python_sharegpt_23k | 11,104 | Write a Python function to generate a list of lists where each sublist contains the values generated by the inner loop of a nested for-loop. The outer loop should iterate from 1 to a given number `n`, and the inner loop should iterate from the current value of the outer loop variable to the current value multiplied by ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_nested_loop_values(n: int) -> List[List[int]]:
"""
Generate a list of lists where each sublist contains the values generated by the inner loop of a nested for-loop.
The outer loop iterat... |
python_sharegpt_23k | 11,106 | Write a Python function to check if a string contains only unique characters without using any additional data structures. The function should have a time complexity of O(n^2) and should not use any built-in library functions. The string will only contain lowercase alphabets. Additionally, the function should return th... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_unique_with_counts(string: str) -> tuple[bool, dict[str, int]]:
"""
Check if a string contains only unique characters and return the count of each unique character.
Args:
string (str): The input string contai... |
python_sharegpt_23k | 11,110 | Write a Python function to perform a Depth-First Search (DFS) traversal on a given graph represented by an adjacency list. The function should return the order in which the nodes are visited during the DFS traversal. The function should handle cases where the graph is empty or the starting node is not present in the gr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Any
def dfs(graph: Dict[Any, List[Any]], start_node: Any) -> List[Any]:
"""
Perform a Depth-First Search (DFS) traversal on a given graph represented by an adjacency list.
Args:
graph... |
python_sharegpt_23k | 11,113 | Write a Python function to determine if a given number is a prime number with a time complexity of O(sqrt(n)), where n is the given number. The function should efficiently check for primality by only iterating up to the square root of the number and skipping even numbers after checking for divisibility by 2.
The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime_efficient(n: int) -> bool:
"""
Determines if a given number is a prime number with a time complexity of O(sqrt(n)).
Args:
n (int): The number to check for primality.
Returns:
b... |
python_sharegpt_23k | 11,118 | Write a Python function to find the index of the first occurrence of a given element in a list of integers. If the element is not present in the list, the function should return -1. The input list should have a length of at least 3. The function should only use basic array operations and not rely on any built-in functi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_first_occurrence(lst: List[int], element: int) -> int:
"""
Find the index of the first occurrence of a given element in a list of integers.
If the element is not present in the list, return ... |
python_sharegpt_23k | 11,119 | Write a Python function to sort an array of numbers in descending order using only recursion. The function should be based on the merge sort algorithm, which recursively splits the array into halves, sorts them, and then merges them back together in descending order. The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def recursive_merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts an array of numbers in descending order using a recursive merge sort algorithm.
Args:
arr (List[int]): The array o... |
python_sharegpt_23k | 11,122 | Write a Python function to compare two given strings and return the longer one. If the strings are of equal length, return the first string. The function should have a time complexity of O(n), where n is the length of the longer string. The length of the strings can be up to 1 million characters.
The code should start... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_longer_string(string1: str, string2: str) -> str:
"""
Compare two given strings and return the longer one. If the strings are of equal length, return the first string.
Args:
string1 (str): The first string ... |
python_sharegpt_23k | 11,123 | Write a Python function called `calculate_fibonacci` that takes in an integer `n` as a parameter and returns the nth Fibonacci number. The function should use type hints to indicate that the parameter `n` is an integer and the return value is also an integer. The Fibonacci sequence is a series of numbers in which each ... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number using an iterative approach.
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones.
The se... |
python_sharegpt_23k | 11,126 | Write a Python function to convert a list of tuples into a dictionary. Each tuple in the list contains exactly two elements: a key and a value. The resulting dictionary should have the keys from the tuples as the dictionary keys, and the corresponding values from the tuples as the dictionary values.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Dict
def convert_list_to_dict(array: List[Tuple[str, int]]) -> Dict[str, int]:
"""
Converts a list of tuples into a dictionary. Each tuple in the list contains exactly two elements: a key (string... |
python_sharegpt_23k | 11,127 | Write a Python function to search for a target integer in a list of integers and return the first index at which the target integer is found. If the target integer is not found in the list, the function should return -1. The function should handle edge cases such as an empty list or a list that does not contain the tar... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def search_list(lst: List[int], target: int) -> int:
"""
Searches for the first index of the target integer in the given list.
Args:
lst (List[int]): The list of integers to search.
... |
python_sharegpt_23k | 11,129 | Write a Python function to add two non-negative integers and handle various input validation scenarios. The function should raise appropriate exceptions for invalid inputs such as `None`, non-integer types, and negative integers. The code should start with:
```python
def add_two_numbers(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def add_two_numbers(a: int, b: int) -> int:
"""
Adds two non-negative integers and raises appropriate exceptions for invalid inputs.
Args:
a (int): The first non-negative integer.
b (int): The second non-neg... |
python_sharegpt_23k | 11,130 | Write a Python function to find and output all prime numbers up to a given number `n`, skipping any number that is divisible by 5. Additionally, the function should calculate and output the sum of all the prime numbers found, and keep track of the total count of prime numbers, which should also be output at the end.
T... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_prime_numbers(n: int) -> None:
"""
Find and output all prime numbers up to a given number n, skipping any number that is divisible by 5.
Additionally, calculate and output the sum of all the prime numbers found, and... |
python_sharegpt_23k | 11,132 | Write a Python function to find the second largest number in a given list of integers without using any built-in sorting functions or functions that directly find the maximum or minimum value. The function should handle edge cases such as lists with fewer than two elements and lists with duplicate values.
The code sho... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_second_largest(numbers: List[int]) -> Optional[int]:
"""
Find the second largest number in a list of integers without using built-in sorting or maximum/minimum functions.
Args:
... |
python_sharegpt_23k | 11,133 | Write a Python function to check if a given matrix is skew-symmetric using NumPy. A matrix is skew-symmetric if it is equal to the negation of its transpose. In other words, the element at position (i, j) should be equal to the negation of the element at position (j, i) for all i and j. If the matrix is skew-symmetric,... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def is_skew_symmetric(matrix: np.ndarray) -> bool:
"""
Check if a given matrix is skew-symmetric.
A matrix is skew-symmetric if it is equal to the negation of its transpose.
In other words, the eleme... |
python_sharegpt_23k | 11,135 | Write a Python function to calculate the modulus of two positive integers `n1` and `n2` without using the modulus operator `%`. The function should handle cases where `n2` is zero and return `None` in such cases. The function should also handle cases where `n1` or `n2` are negative by converting them to their absolute ... | Here is the Python code to solve the given problem in the markdown block:
```python
def modulo_without_operator(n1: int, n2: int) -> int:
"""
Calculate the modulus of two integers without using the modulus operator.
Args:
n1 (int): The dividend.
n2 (int): The divisor.
Returns:
... |
python_sharegpt_23k | 11,137 | Write a Python function to initialize a multi-dimensional array of size `rows` x `columns` such that each element in the array is initialized to the sum of its row and column indices multiplied by the given `value`. The function should start with:
```python
from typing import List
def initialize_multi_array(rows: int,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def initialize_multi_array(rows: int, columns: int, value: int) -> List[List[int]]:
"""
Initializes a multi-dimensional array of size `rows` x `columns` such that each element
is initialized to the s... |
python_sharegpt_23k | 11,139 | Write a Python function to modify a list of strings such that each string has its first letter capitalized and all other letters converted to lowercase. The function should handle strings with mixed cases, special characters, digits, and spaces. The code should start with:
```python
from typing import List
def capital... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def capitalize_names(names: List[str]) -> List[str]:
"""
Modifies a list of strings such that each string has its first letter capitalized and all other letters converted to lowercase.
Args:
... |
python_sharegpt_23k | 11,147 | Write a Python function to calculate the final amount to be paid by a customer based on their membership level and the total amount of their purchase. The function should apply different discounts based on the membership level and return the final amount after applying the discount. The membership levels and their corr... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_final_amount(membership_level: str, total_amount: float) -> float:
"""
Calculate the final amount to be paid by a customer based on their membership level and the total amount of their purchase.
Args:
... |
python_sharegpt_23k | 11,154 | Write a Python function to compare two binary strings and return the number of differences between them. The length of the two input strings can be up to \(10^7\) characters. Additionally, you must implement the function without using any string comparison or built-in functions for comparing characters. The function sh... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_binary_differences(str1: str, str2: str) -> int:
"""
Compares two binary strings and returns the number of differences between them.
If the lengths of the strings are not equal, returns -1.
Args:
str1 ... |
python_sharegpt_23k | 11,155 | Write a Python function to find the prime factorization of a given number `n` efficiently. The function should run in O(sqrt(n)) time complexity and use O(1) space complexity, excluding the space used to store the factors. The code should start with:
```python
from typing import List
import math
def prime_factorizatio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def prime_factorization(n: int) -> List[int]:
"""
Returns the prime factorization of a given number n.
Args:
n (int): The number to be factorized.
Returns:
List[int]... |
python_sharegpt_23k | 11,156 | Write a Python function to create an array of 1000 random integers ranging from -1000 to 1000, sort the array in descending order, and then find the mode value of the array. The mode value is the most frequently occurring element in the array.
The code should start with:
```python
from typing import List, Tuple
import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import random
from collections import Counter
def generate_and_analyze_array() -> Tuple[List[int], int]:
"""
Generates an array of 1000 random integers from -1000 to 1000, sorts it in descending o... |
python_sharegpt_23k | 11,157 | Write a Python function to implement a binary search algorithm. The function should take a sorted list of integers and a target value as input, and return the index of the target value in the list if it exists. If the target value is not found, the function should return -1.
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 binary_search(arr: List[int], target: int) -> int:
"""
Perform a binary search on a sorted array to find the index of the target value.
Args:
arr (List[int]): A sorted list of integers.
... |
python_sharegpt_23k | 11,160 | Write a Python function to reverse the order of elements in a given list without using any built-in functions or methods for list manipulation. You can only use basic programming constructs like loops and conditional statements. The time complexity of your solution should be O(n), where n is the length of the list.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list(my_list: List[int]) -> List[int]:
"""
Reverses the order of elements in a given list without using built-in functions or methods.
Args:
my_list (List[int]): The list of inte... |
python_sharegpt_23k | 11,168 | Write a Python function to sort a list of numbers and remove any duplicates from the sorted list. The function should have a time complexity of O(n log n) and should not use any built-in sorting functions or libraries. The code should start with:
```python
from typing import List
def sort_and_remove_duplicates(numbers... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_and_remove_duplicates(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers and removes any duplicates from the sorted list.
Args:
numbers (List[int]): The list of numbers t... |
python_sharegpt_23k | 11,169 | Write a Python function that takes in a list of strings and a target string. The function should iterate through the list and print only the strings that contain the target string as a substring, ignoring case sensitivity. Additionally, the function should return the count of the strings that contain the target string ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_strings_with_substring(lst: List[str], target: str) -> int:
"""
Iterates through a list of strings and prints only those strings that contain the target string as a substring, ignoring case sens... |
python_sharegpt_23k | 11,176 | Write a Python function to classify the given code as either a while loop or a for loop. Additionally, the function should output the sum of all the prime numbers printed during the loop. The output must be stored in a variable named `total`. The function should only print prime numbers during the loop. The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
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 otherwise.
"""
if num ... |
python_sharegpt_23k | 11,181 | Write a Python function to find the median of a given list of integers. The function should handle cases where the list is empty or has an even number of elements. The median is defined as the middle value in a sorted list if the list has an odd number of elements, or the average of the two middle values if the list ha... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_median(lst: List[int]) -> Optional[float]:
"""
Find the median of a given list of integers.
Args:
lst (List[int]): The list of integers from which to find the median.
... |
python_sharegpt_23k | 11,183 | Write a Python function to invert the case of each character in a given string. The function should not use any built-in functions or methods for case conversion. Additionally, the function should be implemented in a single line using a lambda function.
The code should start with:
```python
invert_case = lambda s: ...... | Here is the Python code to solve the given problem in the markdown block:
```python
invert_case = lambda s: ''.join([chr(ord(c) + 32) if 65 <= ord(c) <= 90 else chr(ord(c) - 32) if 97 <= ord(c) <= 122 else c for c in s])
"""
Inverts the case of each character in the given string.
Args:
s (str): The input string w... |
python_sharegpt_23k | 11,185 | Write a Python function to generate a unique random number in the range of 0 to 100, such that it is not divisible by any prime number less than 10. The function should ensure that the generated number meets this criterion.
The code should start with:
```python
import random
from typing import List
def generate_uniqu... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
from typing import List
def generate_unique_random_number(primes: List[int]) -> int:
"""
Generates a unique random number in the range of 0 to 100 that is not divisible by any prime number less than 10.
Args:... |
python_sharegpt_23k | 11,190 | Write a Python function to convert a given amount of currency from one type to another using a provided exchange rate. The function should handle cases where the input currency value includes decimal places and should return the converted amount rounded to two decimal places. The code should start with:
```python
def c... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_currency(amount: float, exchange_rate: float) -> float:
"""
Converts a given amount of currency using a provided exchange rate and rounds the result to two decimal places.
Args:
amount (float): The amoun... |
python_sharegpt_23k | 11,192 | Write a Python function to find the value that appears most frequently in a given list of integers and non-integer values. The list may contain negative numbers and may be very large with millions of elements. The algorithm should have a time complexity of O(n). The function should handle the case where the list contai... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
from collections import defaultdict
def find_most_frequent_value(nums: List[Union[int, float]]) -> Union[int, float]:
"""
Find the value that appears most frequently in a given list of integers an... |
python_sharegpt_23k | 11,197 | Write a Python function to calculate the sum of the squares of all positive integers in a given list, but only if the list contains valid inputs. The list should only contain positive integers less than or equal to 100. If the list contains any negative numbers, non-integer elements, or any number greater than 100, rai... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_squares_of_positives(numbers: List[int]) -> int:
"""
Calculate the sum of the squares of all positive integers in the given list.
Args:
numbers (List[int]): The list of integers t... |
python_sharegpt_23k | 11,203 | Write a Python function to generate the nth element in a Fibonacci series with a time complexity of O(log n) and a space complexity of O(log n). The function should not use iteration or recursion directly. Instead, it should leverage matrix exponentiation to achieve the desired performance.
The code should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci_matrix(n: int) -> int:
"""
Generate the nth element in the Fibonacci series using matrix exponentiation.
Args:
n (int): The position in the Fibonacci series to generate.
Returns:
int: The ... |
python_sharegpt_23k | 11,205 | Write a Python function to detect and count the number of occurrences of a particular substring in a given string, while ignoring any occurrences that are part of a larger word. The function should handle case-insensitive matching and should have a time complexity of O(n) and a space complexity of O(1).
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_substring_occurrences(string: str, substring: str) -> int:
"""
Counts the number of occurrences of a substring in a string, ignoring occurrences that are part of a larger word.
The matching is case-insensitive.
... |
python_sharegpt_23k | 11,218 | Write a Python function that takes two strings as arguments and returns the longest string after removing any duplicate characters. The function should not use any built-in functions or libraries that directly remove duplicate characters. Instead, you need to implement the logic to remove duplicates manually.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_string_without_duplicates(str1: str, str2: str) -> str:
"""
Returns the longest string between two given strings after removing any duplicate characters.
Args:
str1 (str): The first string.
str2 ... |
python_sharegpt_23k | 11,219 | Write a Python function to find the maximum of three given integers without using any comparison operators (e.g., `>`, `<`, `>=`, `<=`). The function should use bitwise operations to determine the maximum value.
The code should start with:
```python
def find_maximum_without_comparison(a: int, b: int, c: int) -> int:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_maximum_without_comparison(a: int, b: int, c: int) -> int:
"""
Find the maximum of three integers without using any comparison operators.
Args:
a (int): The first integer.
b (int): The second intege... |
python_sharegpt_23k | 11,221 | Write a Python function to remove all duplicates from a given list of integers while maintaining the order of the elements. The function should return a new list containing only the unique elements in the order they first appear in the original list. The function should be efficient, with a time complexity of O(n), whe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates(nums: List[int]) -> List[int]:
"""
Removes duplicates from a list of integers while maintaining the order of the first occurrence of each element.
Args:
nums (List[int]... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.