source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 8,373 | Write a Python function to count the number of unique words in a given string, considering case-insensitivity and ignoring punctuation marks or special characters. The function should handle leading and trailing spaces in the string.
The code should start with:
```python
def count_unique_words(string: str) -> int:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_unique_words(string: str) -> int:
"""
Counts the number of unique words in a given string, considering case-insensitivity and ignoring punctuation marks or special characters.
Args:
string (str): The input... |
python_sharegpt_23k | 8,375 | Write a Python function to compute the transpose of a given matrix. The matrix can contain integers, decimals, or characters. The function should handle matrices with dimensions ranging from 1x1 to 100x100 and can contain up to 10^6 elements. The elements of the matrix can have values from -1000 to 1000.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def transpose_matrix(matrix: List[List[int]]) -> List[List[int]]:
"""
Compute the transpose of a given matrix.
Args:
matrix (List[List[int]]): The input matrix to be transposed.
Returns... |
python_sharegpt_23k | 8,376 | Write a Python function to calculate the greatest common divisor (GCD) of two positive integers using the Euclidean algorithm. The function should run in O(log(min(a, b))) time complexity, where `a` and `b` are the input integers.
The code should start with:
```python
def gcd(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two positive integers using the Euclidean algorithm.
Args:
a (int): The first positive integer.
b (int): The second positive... |
python_sharegpt_23k | 8,378 | Write a Python function to sort a list of numbers in ascending order using the **Bubble Sort** algorithm. The function should not use any built-in sorting functions or methods. The code should start with:
```python
from typing import List
def bubble_sort(numbers: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(numbers: List[int]) -> List[int]:
"""
Sorts a list of numbers in ascending order using the Bubble Sort algorithm.
Args:
numbers (List[int]): The list of numbers to be sorted.... |
python_sharegpt_23k | 8,379 | Write a Python function to format a given phone number in the canonical format "+1-123-456-7890". The function should handle phone numbers provided as strings with additional characters such as spaces, parentheses, or dashes. The function should also handle phone numbers provided as integers. The code should start with... | Here is the Python code to solve the given problem in the markdown block:
```python
def format_phone_number(phone_number: str) -> str:
"""
Formats a given phone number into the canonical format "+1-123-456-7890".
Args:
phone_number (str): The phone number to be formatted. It can contain additional ... |
python_sharegpt_23k | 8,380 | Write a Python function to reverse a given singly linked list in place. The linked list can contain duplicates. Additionally, you are not allowed to use any extra data structures or recursion in your solution. The function should start with:
```python
class ListNode:
def __init__(self, value=0, next=None):
... | Here is the Python code to solve the given problem in the markdown block:
```python
class ListNode:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def reverse_linked_list(head: ListNode) -> ListNode:
"""
Reverses a given singly linked list in place.
Args:
... |
python_sharegpt_23k | 8,381 | Write a Python function to filter and sort a list of integers based on the following criteria:
1. Include only even numbers.
2. Include only numbers greater than 10.
3. Return the filtered list sorted in descending order.
The code should start with:
```python
from typing import List
def filter_and_sort_even_greater_t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_and_sort_even_greater_than_10(numbers: List[int]) -> List[int]:
"""
Filters and sorts a list of integers based on the criteria:
1. Include only even numbers.
2. Include only numbers gr... |
python_sharegpt_23k | 8,384 | Write a Python function to generate a random array of size `n` containing unique elements sorted in ascending order. The function should ensure that the elements are unique and the array is sorted. The code should start with:
```python
from typing import List
import random
def generate_sorted_unique_array(n: int) -> L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_sorted_unique_array(n: int) -> List[int]:
"""
Generates a random array of size `n` containing unique elements sorted in ascending order.
Args:
n (int): The size of... |
python_sharegpt_23k | 8,387 | Write a Python function to extract a substring from a given string based on two indices `a` and `b`. The function should handle various edge cases, such as empty strings, invalid indices, and indices that exceed the length of the string. The code should start with:
```python
def get_substring(string: str, a: int, b: in... | 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 starting at index `a` and ending at index `b` (inclusive).
Args:
string (str): The input string from which to ext... |
python_sharegpt_23k | 8,392 | Write a Python function to access the nth element of a list using list comprehension. The function should handle cases where the list does not have enough elements by returning `None`. The code should start with:
```python
from typing import List, Optional
def get_nth_element(lst: List[int], n: int) -> Optional[int]:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def get_nth_element(lst: List[int], n: int) -> Optional[int]:
"""
Access the nth element of a list using list comprehension.
Args:
lst (List[int]): The list from which to extract t... |
python_sharegpt_23k | 8,394 | Write a Python function to detect outliers in a given array. An outlier is defined as a value that is either greater than 3 times the interquartile range (IQR) above the third quartile or less than 3 times the IQR below the first quartile. The input array should have at least 10 elements.
The code should start with:
`... | 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. An outlier is defined as a value that is either greater than 3 times the interquartile range (I... |
python_sharegpt_23k | 8,395 | Write a Python function to calculate the power of a number 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 code should start with:
```python
def power_of_number(base: float, exponent: int) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
def power_of_number(base: float, exponent: int) -> float:
"""
Calculate the power of a number with a time complexity of O(log n), where n is the exponent.
Args:
base (float): The base number.
exponent (int):... |
python_sharegpt_23k | 8,396 | Write a Python function to calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm. The function should have a time complexity of O(log(min(n1, n2))) and a space complexity of O(1). The function should handle both positive and negative integers.
The code should start with:
```python
de... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(n1: int, n2: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
Args:
n1 (int): The first number.
n2 (int): The second number.
Returns:
... |
python_sharegpt_23k | 8,401 | Write a Python function that processes a list of items and returns a list of tuples where each tuple contains the index and the item from the original list. The function should use the `enumerate` function to achieve this. The code should start with:
```python
from typing import List, Tuple
def enumerate_items(items: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def enumerate_items(items: List[str]) -> List[Tuple[int, str]]:
"""
Processes a list of items and returns a list of tuples where each tuple contains the index and the item from the original list.
... |
python_sharegpt_23k | 8,403 | Write a Python function to demonstrate the difference between deep copy and shallow copy in Python. The function should take a nested list as input and return a tuple containing the original list, the shallow copy, and the deep copy. The function should also modify the first element of both the shallow copy and the dee... | 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[List[int]]) -> Tuple[List[List[int]], List[List[int]], List[List[int]]]:
"""
Demonstrates the difference between deep copy and shallow co... |
python_sharegpt_23k | 8,409 | Write a Python function to generate a Fibonacci sequence starting with two given numbers, `start_1` and `start_2`, up to the nth term using recursion. The function should handle invalid inputs by raising appropriate exceptions. The code should start with:
```python
from typing import List
def fibonacci_sequence(start_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def fibonacci_sequence(start_1: int, start_2: int, n: int) -> List[int]:
"""
Generate a Fibonacci sequence starting with start_1 and start_2 up to the nth term using recursion.
Args:
start_1... |
python_sharegpt_23k | 8,410 | Write a Python function to calculate the sum of all odd numbers in a given list of integers. The function should handle an empty list and return 0 in that case. The code should start with:
```python
from typing import List
def sum_of_odds(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_odds(numbers: List[int]) -> int:
"""
Calculate the sum of all odd numbers in the given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The sum of a... |
python_sharegpt_23k | 8,411 | Write a Python function to count the number of unique elements in a given list with a time complexity of O(n log n) and without using any additional data structures. The function should modify the input list in place by sorting it.
The code should start with:
```python
from typing import List
def count_unique_element... | 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 log n) and without using additional data structures.
Args:
... |
python_sharegpt_23k | 8,415 | Write a Python function to create a dictionary from two given lists. The first list should be the keys and the second list should be the values. Ensure that the keys are unique and the values are of type string. If the lists are not of the same length, return an error message. If a duplicate key is found, return an err... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Union
def create_dictionary(keys: List[str], values: List[str]) -> Union[Dict[str, str], str]:
"""
Create a dictionary from two given lists. The first list should be the keys and the second list s... |
python_sharegpt_23k | 8,416 | Write a Python function to delete all empty values from a given dictionary and sort the remaining key-value pairs in descending order based on their keys. Empty values include empty strings, `False`, `0`, and `None`. The function should return the modified dictionary.
The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def clean_and_sort_dict(my_dict: Dict[str, any]) -> Dict[str, any]:
"""
Deletes all empty values from the given dictionary and sorts the remaining key-value pairs in descending order based on their keys.... |
python_sharegpt_23k | 8,418 | Write a Python function to determine if two lists contain the same items in different orders. The function should have a time complexity of O(n log n), where n is the length of the lists. The function should return `True` if the lists contain the same items in different orders, and `False` otherwise.
The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def same_items_in_different_order(list1: List[int], list2: List[int]) -> bool:
"""
Determines if two lists contain the same items in different orders.
Args:
list1 (List[int]): The first list... |
python_sharegpt_23k | 8,422 | Write a Python function to determine if a given string is a palindrome, considering only alphanumeric characters and ignoring case. The function should return `True` if the string is a palindrome and `False` otherwise. The code should start with:
```python
def is_palindrome(string: str) -> bool:
```
| 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, considering only alphanumeric characters and ignoring case.
Args:
string (str): The input string to check.
Returns:
... |
python_sharegpt_23k | 8,425 | Write a Python function to reverse an array in place with a time complexity of O(n) and using only constant extra space. The function should take an array of integers as input and return the reversed array. The code should start with:
```python
from typing import List
def reverse_array_in_place(arr: List[int]) -> List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array_in_place(arr: List[int]) -> List[int]:
"""
Reverses the input array in place with a time complexity of O(n) and using only constant extra space.
Args:
arr (List[int]): The ... |
python_sharegpt_23k | 8,427 | Write a Python function to calculate the exponent of a given number `x` raised to the power `y` efficiently. The function should have a time complexity of O(log y) and a space complexity of O(1). The function should handle both positive and negative values of `y`.
The code should start with:
```python
def calculate_ex... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_exponent(x: float, y: int) -> float:
"""
Calculate the exponent of a given number x raised to the power y efficiently.
Args:
x (float): The base number.
y (int): The exponent.
Returns:
... |
python_sharegpt_23k | 8,429 | 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. 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, represented as a string of '0's and '... |
python_sharegpt_23k | 8,430 | Write a Python function to find the maximum element in a given unsorted array using a linear search approach. The array can contain both positive and negative integers, and it can have duplicate elements. The code should start with:
```python
from typing import List
def find_maximum_element(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_maximum_element(arr: List[int]) -> int:
"""
Find the maximum element in a given unsorted array using a linear search approach.
Args:
arr (List[int]): The unsorted array of integers.... |
python_sharegpt_23k | 8,431 | Write a Python function to create and validate a dictionary that only allows unique keys and values restricted to positive integers less than or equal to 100. The function should take a list of tuples, where each tuple contains a key and a value, and return a dictionary with the valid key-value pairs. If a key is dupli... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Dict
def create_valid_dict(key_value_pairs: List[Tuple[str, int]]) -> Dict[str, int]:
"""
Create a dictionary with unique keys and values restricted to positive integers less than or equal to 100... |
python_sharegpt_23k | 8,439 | Write a Python function to calculate the final price after applying both a cash coupon and a percentage coupon to an initial price. The function should handle non-negative values for the price, cash coupon, and percentage coupon. If the price is less than the cash coupon, the discount should be equal to the price. Othe... | Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_final_price(price: float, cash_coupon: float, percentage_coupon: float) -> float:
"""
Calculate the final price after applying both a cash coupon and a percentage coupon to an initial price.
Args:
pric... |
python_sharegpt_23k | 8,440 | Write a Python function to perform a binary search on a sorted list to find the index of a given element `x`. The function should return the index of `x` if it is present in the list, otherwise it should return `-1`. The function should have a time complexity of O(log n), where n is the number of elements in the list.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search(vector: List[int], x: int) -> int:
"""
Perform a binary search to find the index of the element `x` in a sorted list.
Args:
vector (List[int]): A sorted list of integers.
... |
python_sharegpt_23k | 8,441 | 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). The code should start with:
```python
from typing import List
def sort_list_with_duplicates(given_list: List[int], sort_order: str) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_list_with_duplicates(given_list: List[int], sort_order: str) -> List[int]:
"""
Sorts a list in ascending or descending order while removing duplicate elements.
Args:
given_list (Lis... |
python_sharegpt_23k | 8,442 | Write a Python function to multiply two integers using only bitwise operators. The function should not use the multiplication operator or any other arithmetic operators. The code should start with:
```python
def bitwise_multiply(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def bitwise_multiply(a: int, b: int) -> int:
"""
Multiplies two integers using only bitwise operators.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The product of... |
python_sharegpt_23k | 8,445 | Write a Python function to generate a random letter between 'a' and 'z', excluding the letters 'e' and 'q'. The function should return the randomly selected letter.
The code should start with:
```python
import random
from typing import List
def generate_random_letter() -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import random
from typing import List
def generate_random_letter() -> str:
"""
Generates a random letter between 'a' and 'z', excluding 'e' and 'q'.
Returns:
str: A randomly selected letter from the allowed letters... |
python_sharegpt_23k | 8,448 | Write a Python function to handle cases when the array contains more than two numbers and return the sum of all the numbers in the array. If the array contains exactly two numbers and they are the same, the function should return the square of the number. If the array contains exactly two numbers and they are different... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def add_numbers(nums: List[int]) -> int:
"""
Returns the sum of all numbers in the array, with special handling for arrays containing exactly two numbers.
Args:
nums (List[int]): The list of... |
python_sharegpt_23k | 8,453 | Write a Python function to find and return all prime numbers between a given range `[start, end]`. The function should use nested loops to check for prime numbers and return a list of all prime numbers found within the specified range.
The code should start with:
```python
from typing import List
def find_primes_in_r... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_primes_in_range(start: int, end: int) -> List[int]:
"""
Find and return all prime numbers between the given range [start, end].
Args:
start (int): The starting number of the range (... |
python_sharegpt_23k | 8,454 | Write a Python function to print the first `n` prime numbers. Additionally, implement a helper function that checks whether a given number is prime or not, and use this function within your loop to determine the prime numbers.
The code should start with:
```python
def is_prime(num: int) -> bool:
"""
Check if a... | 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 | 8,456 | 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(lst: List[int], target: int) -> int:
"""
Perform a binary search on a sorted list of integers to find the target integer.
Args:
lst (List[int]): A sorted list of integers.
... |
python_sharegpt_23k | 8,457 | Write a Python function to convert a list of strings to their uppercase equivalents without using built-in string manipulation functions (such as `str.upper()`) or list comprehension. The function should return a new list containing the uppercase versions of the input strings.
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 convert_to_uppercase(strings: List[str]) -> List[str]:
"""
Converts a list of strings to their uppercase equivalents without using built-in string manipulation functions.
Args:
strings (... |
python_sharegpt_23k | 8,459 | Write a Python function to reverse a given list without using the built-in `reverse()` function or slicing. The function should use an iterative approach to reverse the list in place.
The code should start with:
```python
from typing import List
def reverse_list(lst: List[int]) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list(lst: List[int]) -> None:
"""
Reverses the given list in place without using the built-in reverse() function or slicing.
Args:
lst (List[int]): The list to be reversed.
... |
python_sharegpt_23k | 8,463 | Write a Python function to remove all duplicated entries from a given list of numbers and sort the remaining numbers in descending order. The input list can contain up to \(10^6\) elements. The function should start with:
```python
from typing import List
def remove_duplicates_and_sort_descending(num_list: List[int]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort_descending(num_list: List[int]) -> List[int]:
"""
Removes all duplicated entries from the given list of numbers and sorts the remaining numbers in descending order.
Ar... |
python_sharegpt_23k | 8,464 | Write a Python function to identify all prime numbers between 0 and a given large number `n` (up to 10^9) using the Sieve of Eratosthenes algorithm. The function should have a time complexity of O(n log log n).
The code should start with:
```python
def find_prime_numbers(n: int) -> list:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_prime_numbers(n: int) -> list:
"""
Generate a list of all prime numbers between 0 and n using the Sieve of Eratosthenes algorithm.
Args:
n (int): The upper limit up to which prime numbers are to be found.
... |
python_sharegpt_23k | 8,466 | Write a Python function to filter out all even numbers from a given list of integers, sort the remaining odd numbers in descending order, and return the sorted list along with the sum of the remaining odd numbers. The function should have a time complexity of O(n log n) or better.
The code should start with:
```python... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def filter_and_sort_odd_numbers(arr: List[int]) -> Tuple[List[int], int]:
"""
Filters out all even numbers from the given list, sorts the remaining odd numbers in descending order,
and returns... |
python_sharegpt_23k | 8,477 | Write a Python function to generate a random number between two specified numbers. The function should raise a `ValueError` if the lower bound is greater than or equal to the upper bound. The function should start with:
```python
import random
def generate_random_number(lower_bound: int, upper_bound: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import random
def generate_random_number(lower_bound: int, upper_bound: int) -> int:
"""
Generates a random integer between the specified lower and upper bounds.
Args:
lower_bound (int): The lower bound of the rang... |
python_sharegpt_23k | 8,478 | Write a Python function to find the largest prime number in a given array of integers. The function should return `None` if no prime numbers are present in the array.
The code should start with:
```python
from typing import List, Optional
def find_largest_prime(array: List[int]) -> Optional[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
import math
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwi... |
python_sharegpt_23k | 8,481 | Write a Python function to sort a given list of integers in descending order. The function should take a list of integers as input and return a new list with the same integers sorted in descending order. The original list should not be modified.
The code should start with:
```python
from typing import List
def sort_d... | 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 given list of integers in descending order and returns the sorted list.
The original list is not modified.
Args:
arr (List[i... |
python_sharegpt_23k | 8,489 | Write a Python function to remove all duplicate words from a given string and return the modified string with duplicate words removed. The function should handle any string input and ensure that the order of the first occurrence of each word is preserved.
The code should start with:
```python
def remove_duplicate_word... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_duplicate_words(input_string: str) -> str:
"""
Removes all duplicate words from the given input string and returns the modified string.
The order of the first occurrence of each word is preserved.
Args:
... |
python_sharegpt_23k | 8,493 | Write a Python function to reverse a given string without using built-in library functions (like `reversed`, `join`, etc.) and without using additional data structures. The solution should have a time complexity of O(n) and a space complexity of O(1). The function should start with:
```python
def reverse_string(string:... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverses a given string without using built-in library functions and without using additional data structures.
Args:
string (str): The input string to be reversed.
Re... |
python_sharegpt_23k | 8,496 | Write a Python function to remove all adjacent duplicate characters from a given string and return the modified string. Adjacent duplicate characters are consecutive characters in the string that are the same. The function should start with:
```python
def remove_adjacent_duplicates(s: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def remove_adjacent_duplicates(s: str) -> str:
"""
Removes all adjacent duplicate characters from the given string and returns the modified string.
Args:
s (str): The input string from which adjacent duplicate cha... |
python_sharegpt_23k | 8,498 | Write a Python function to remove duplicate strings from a given list while maintaining the original order of the strings. The function should be efficient enough to handle a large input size (e.g., 100,000 elements).
The code should start with:
```python
from typing import List
def remove_duplicates_keep_order(arr: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_keep_order(arr: List[str]) -> List[str]:
"""
Removes duplicate strings from the given list while maintaining the original order of the strings.
Args:
arr (List[str]): T... |
python_sharegpt_23k | 8,499 | Write a Python function to count the number of occurrences of a given element in a large array efficiently. The array can contain both positive and negative integers, and its length can be up to 1 million elements. The element to search for can be any integer value. The time complexity of the solution should be O(n) or... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_occurrences(arr: List[int], target: int) -> int:
"""
Counts the number of occurrences of a given element in a large array.
Args:
arr (List[int]): The array containing integers, whi... |
python_sharegpt_23k | 8,500 | Write a Python function to generate and print two patterns of stars as described below. The function should take an integer `n` as input, which represents the number of rows in each pattern.
Pattern 1:
```
*
**
***
****
*****
```
Pattern 2:
```
*****
****
***
**
*
```
The code should start with:
```python
def print_... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_star_patterns(n: int):
"""
Prints two patterns of stars based on the given number of rows.
Pattern 1:
*
**
***
****
*****
Pattern 2:
*****
****
***
**
*
Args:
... |
python_sharegpt_23k | 8,502 | Write a Python function to generate and print a specific numeric pattern based on the number of rows `n`. The pattern should start with `1` in the first row, `22` in the second row, `333` in the third row, and so on, up to `n` rows. The function should take an integer `n` as input and print the pattern directly.
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_numeric_pattern(n: int) -> None:
"""
Prints a numeric pattern based on the number of rows `n`.
The pattern starts with '1' in the first row, '22' in the second row, '333' in the third row,
and so on, up to `n`... |
python_sharegpt_23k | 8,503 | Write a Python function to generate a random string of a given length that includes at least one uppercase letter, one lowercase letter, one digit, and one special character. The function should ensure that the generated string meets these criteria and should be random in nature.
The code should start with:
```python
... | 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 that includes at least one uppercase letter,
one lowercase letter, one digit, and one s... |
python_sharegpt_23k | 8,504 | Write a Python function that takes two parameters: a number and a string. The function should return a list containing these two values if the string is valid and contains only alphabetical characters. If the string is not valid or contains non-alphabetical characters, the function should return an empty list.
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def create_valid_array(num: Union[int, float], string: str) -> List[Union[int, float, str]]:
"""
Create a list containing a number and a string if the string is valid and contains only alphabetica... |
python_sharegpt_23k | 8,510 | Write a Python function to filter out and return a new list containing only the even numbers from a given list of integers. The function should handle an empty list and return an empty list in such cases. The code should start with:
```python
from typing import List
def get_even_numbers(numbers: List[int]) -> List[int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_even_numbers(numbers: List[int]) -> List[int]:
"""
Filters out and returns a new list containing only the even numbers from the given list of integers.
Args:
numbers (List[int]): A l... |
python_sharegpt_23k | 8,514 | 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^6\).
The code should start with:
```python
def ... | 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 positive integer that is equal to the sum of its proper divisors (excluding the num... |
python_sharegpt_23k | 8,516 | Write a Python function to count the occurrences of specific words in a given sentence, considering variations in capitalization and punctuation. The function should handle multiple words and return the count of each word in the sentence. The function should also handle multiple spaces between words and ensure that the... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
import re
def count_word_occurrences(sentence: str, words: List[str]) -> Dict[str, int]:
"""
Counts the occurrences of specific words in a given sentence, considering variations in capitalization a... |
python_sharegpt_23k | 8,517 | Write a Python function to sort a given list of numbers in descending order and return the sorted list. The list should contain at least 5 numbers and no more than 10 numbers. Each number in the list should be unique and within the range of 1 to 100. The function should validate the input list to ensure it meets these ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_descending(numbers: List[int]) -> List[int]:
"""
Sorts a given list of numbers in descending order and returns the sorted list.
The input list must meet the following criteria:
- Contai... |
python_sharegpt_23k | 8,519 | Write a Python function to check if the first element of a given array is a prime number and return it if it is. If the first element is not a prime number, return `None`.
The code should start with:
```python
from typing import List, Optional
import math
def first_prime_element(arr: List[int]) -> Optional[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
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... |
python_sharegpt_23k | 8,521 | Write a Python function to multiply all elements in a given list of positive integers without using the `*` operator or any built-in multiplication functions. You are only allowed to use addition, subtraction, and bit shifting operations. The function should start with:
```python
from typing import List
def multiply_e... | 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 positive integers without using the '*' operator.
Args:
my_list (List[int]): A list of positi... |
python_sharegpt_23k | 8,535 | Write a Python function to determine if a given string contains only vowels. The function should be efficient, with a time complexity less than O(n^2). The function should be case-insensitive and should return `True` if the string contains only vowels, and `False` otherwise.
The code should start with:
```python
def c... | 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 | 8,538 | Write a Python function to retrieve the name associated with a given user ID from a predefined dictionary. If the user ID is not found in the dictionary, return a default string 'Unknown'. The function should handle only positive integer user IDs.
The code should start with:
```python
def get_user_name(user_id: int) -... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_user_name(user_id: int) -> str:
"""
Retrieve the name associated with a given user ID from a predefined dictionary.
If the user ID is not found, return 'Unknown'.
Args:
user_id (int): The ID of the user ... |
python_sharegpt_23k | 8,539 | Write a Python function to sort a list of numbers without using Python's built-in `sort` function and without using any other built-in functions or libraries. The function should implement the **Selection Sort** algorithm. The code should start with:
```python
from typing import List
def selection_sort(nums: List[int]... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort(nums: List[int]) -> List[int]:
"""
Sorts a list of integers using the Selection Sort algorithm.
Args:
nums (List[int]): The list of integers to be sorted.
Returns:
... |
python_sharegpt_23k | 8,544 | Write a Python function to determine if all elements of a smaller list `B` are present in a larger list `A` in the same order. The function should return a boolean value indicating whether `B`'s elements are all present in `A` in the same order.
The code should start with:
```python
from typing import List
def is_sub... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_subset_in_order(A: List[int], B: List[int]) -> bool:
"""
Determines if all elements of list B are present in list A in the same order.
Args:
A (List[int]): The larger list containing ... |
python_sharegpt_23k | 8,548 | Write a Python function to sort a given hashtable (dictionary) by its values in descending order. The function should return a new dictionary with the same keys and values, but sorted by the values in descending order.
The code should start with:
```python
from typing import Dict
def sort_hashtable_by_values(hashtabl... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_hashtable_by_values(hashtable: Dict[int, str]) -> Dict[int, str]:
"""
Sorts a given hashtable (dictionary) by its values in descending order.
Args:
hashtable (Dict[int, str]): The i... |
python_sharegpt_23k | 8,549 | Write a Python function to print the elements of a given list in reverse order. The list must contain at least 5 elements. The function should validate the length of the list and raise an error if the list does not meet the minimum length requirement.
The code should start with:
```python
from typing import List
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def print_reverse_list(lst: List[int]) -> None:
"""
Prints the elements of a given list in reverse order.
Args:
lst (List[int]): The list of integers to be printed in reverse order.
Rai... |
python_sharegpt_23k | 8,550 | Write a Python function to find the common elements between two lists, `lst1` and `lst2`, and return a new list containing only these common elements. The function should not use any built-in Python functions or libraries such as `numpy` or `itertools`. The code should start with:
```python
from typing import List
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(lst1: List[int], lst2: List[int]) -> List[int]:
"""
Find and return a list of elements that are common to both lst1 and lst2.
Args:
lst1 (List[int]): The first list ... |
python_sharegpt_23k | 8,558 | Write a Python function to extract email addresses from a list of text strings using a regular expression. The function should handle cases where emails are followed by punctuation marks or other special characters. The code should start with:
```python
from typing import List
import re
def extract_emails(text_list: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def extract_emails(text_list: List[str]) -> List[str]:
"""
Extracts email addresses from a list of text strings using a regular expression.
Args:
text_list (List[str]): A list of t... |
python_sharegpt_23k | 8,559 | Write a Python function to count the number of words in a given string. A word is defined as a sequence of characters separated by whitespace (spaces, tabs, or newlines). The function should handle various edge cases, such as strings with multiple spaces, leading or trailing spaces, and empty strings.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_words(string: str) -> int:
"""
Counts the number of words in a given string. A word is defined as a sequence of characters separated by whitespace.
Args:
string (str): The input string to count words from.... |
python_sharegpt_23k | 8,560 | Write a Python function to find the top K frequent elements in a given list of integers. The function should return a list of the K most frequent elements in descending order of their frequency. If there are ties in frequency, the elements should be returned in the order they first appear in the list.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
from collections import Counter
def top_k_frequent_elements(nums: List[int], k: int) -> List[int]:
"""
Find the top K frequent elements in a given list of integers.
Args:
nums (List[int]): T... |
python_sharegpt_23k | 8,561 | Write a Python function to calculate the sum of all elements in a given array using a constant amount of space and with a time complexity of O(n), where n is the number of elements in the array. The function should handle an empty array by returning 0.
The code should start with:
```python
from typing import List
def... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_array(arr: List[int]) -> int:
"""
Calculate the sum of all elements in the given array using a constant amount of space and O(n) time complexity.
Args:
arr (List[int]): The array ... |
python_sharegpt_23k | 8,567 | Write a Python function to sort a list of integers in ascending order using the Bubble Sort algorithm. The function should modify the input list in place and return the sorted list. The code should start with:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts 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 | 8,571 | Write a Python function to determine if two given strings are anagrams of each other. The function should handle strings with special characters and whitespace, be case-insensitive, and ignore leading or trailing whitespace. The function should return `False` if the strings are of different lengths.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(str1: str, str2: str) -> bool:
"""
Determine if two strings are anagrams of each other.
Args:
str1 (str): The first string to compare.
str2 (str): The second string to compare.
Returns:... |
python_sharegpt_23k | 8,573 | Write a Python function to merge two sorted lists and remove duplicates. The function should have a time complexity of O(n+m), where n and m are the lengths of the two input lists. The merged list should also be sorted in ascending order. The code should start with:
```python
from typing import List
def merge_sorted_l... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merge two sorted lists and remove duplicates. The merged list is also sorted in ascending order.
Args:
list1 (Lis... |
python_sharegpt_23k | 8,574 | Write a Python function to filter a given list of strings and return a new list containing only the strings that have a length greater than a specified number and do not contain any special characters. Special characters are defined as any character that is not a letter or a digit.
The code should start with:
```pytho... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings(lst: List[str], length: int) -> List[str]:
"""
Filters a list of strings and returns a new list containing only the strings
that have a length greater than the specified length and... |
python_sharegpt_23k | 8,575 | Write a Python function to subtract two given numbers. The function should check if the input numbers are integers. If the numbers are not integers, the function should raise a `ValueError` with an appropriate error message. The function should return the result of the subtraction if both inputs are integers.
The code... | Here is the Python code to solve the given problem in the markdown block:
```python
def subtract_numbers(num1, num2) -> int:
"""
Subtracts two given numbers if both are integers.
Args:
num1 (int): The first number to subtract from.
num2 (int): The second number to subtract.
Returns:
... |
python_sharegpt_23k | 8,579 | Write a Python function to generate an array of the first `n` prime numbers. The function should return a list of prime numbers. The code should start with:
```python
from typing import List
def generate_primes(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def is_prime(num: int) -> bool:
"""
Helper function to check if a number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the num... |
python_sharegpt_23k | 8,584 | Write a Python function to remove all spaces from a given string without using any built-in string manipulation functions or libraries. The function should iterate through each character of the string and construct a new string that excludes spaces.
The code should start with:
```python
def remove_spaces(input_string:... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_spaces(input_string: str) -> str:
"""
Removes all spaces from the given input string without using any built-in string manipulation functions.
Args:
input_string (str): The string from which spaces are to... |
python_sharegpt_23k | 8,588 | Write a Python function to calculate the sums of all columns in a given 2D array with a time complexity of O(n), where n is the total number of elements in the array. The function should handle arrays with varying numbers of rows and columns.
The code should start with:
```python
from typing import List
def column_su... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def column_sums(array: List[List[int]]) -> List[int]:
"""
Calculate the sums of all columns in a given 2D array with a time complexity of O(n),
where n is the total number of elements in the array.
... |
python_sharegpt_23k | 8,589 | Write a Python function to reverse the elements of a given array without using the built-in `reverse()` function or any other built-in functions that directly reverse an array. You must implement your own algorithm to reverse the array. The function should start with:
```python
from typing import List
def reverse_arra... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_array(arr: List[int]) -> List[int]:
"""
Reverses the elements of a given array without using built-in reverse functions.
Args:
arr (List[int]): The array to be reversed.
Ret... |
python_sharegpt_23k | 8,591 | Write a Python function to find all duplicates in an array of positive integers where each integer is between 1 and 100, inclusive. The array may contain up to 10^6 elements. The function should return the duplicates in ascending order.
The code should start with:
```python
from typing import List
def find_duplicates... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_duplicates(arr: List[int]) -> List[int]:
"""
Find all duplicates in an array of positive integers where each integer is between 1 and 100, inclusive.
The array may contain up to 10^6 element... |
python_sharegpt_23k | 8,595 | Write a Python function to compute the average of all positive numbers in a given array. The array contains integers between -100 and 100, and its size is limited to a maximum of 1000 elements. If the array does not contain any positive numbers, the function should return 0.
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 compute_average_of_positive_numbers(arr: List[int]) -> float:
"""
Computes the average of all positive numbers in the given array.
Args:
arr (List[int]): The array of integers, where eac... |
python_sharegpt_23k | 8,598 | Write a Python function to remove all duplicate elements from a given array while maintaining the order of the unique elements. The array can contain integers and/or strings. The function should return a new list containing only the unique elements in the order they first appeared in the original array.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def remove_duplicates_keep_order(arr: List[Union[int, str]]) -> List[Union[int, str]]:
"""
Removes all duplicate elements from the given array while maintaining the order of the unique elements.
... |
python_sharegpt_23k | 8,604 | Write a Python function to sort a list of numbers in ascending order without using any built-in sorting functions or methods. The function should implement the selection sort algorithm.
The code should start with:
```python
from typing import List
def sort_list_without_builtins(n_list: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_list_without_builtins(n_list: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the selection sort algorithm.
Args:
n_list (List[int]): The list of inte... |
python_sharegpt_23k | 8,606 | Write a Python function to remove duplicate elements from a given array while maintaining the order of the remaining elements. The algorithm should have a time complexity of O(n) and use only a constant amount of additional space.
The code should start with:
```python
from typing import List
def remove_duplicates_in_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_order(arr: List[int]) -> List[int]:
"""
Removes duplicate elements from the given array while maintaining the order of the remaining elements.
The algorithm has a time comple... |
python_sharegpt_23k | 8,609 | Write a Python function to remove all duplicate elements from a given list of integers and return a sorted list of distinct elements. The function should handle any list of integers, including empty lists and lists with all elements being the same. The code should start with:
```python
from typing import List
def remo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(nums: List[int]) -> List[int]:
"""
Removes all duplicate elements from the given list of integers and returns a sorted list of distinct elements.
Args:
nums (L... |
python_sharegpt_23k | 8,611 | Write a Python function to count the total number of characters in a given string, excluding any whitespace characters. The string may contain uppercase and lowercase letters, numbers, special characters, and punctuation marks. The function should start with:
```python
def count_non_whitespace_characters(text: str) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_non_whitespace_characters(text: str) -> int:
"""
Counts the total number of characters in a given string, excluding any whitespace characters.
Args:
text (str): The input string to count non-whitespace cha... |
python_sharegpt_23k | 8,618 | Write a Python function to perform bubble sort on an array of integers in ascending order. The function should not use any built-in sorting functions or libraries. The input array can contain duplicate elements and may have a length of up to 10^6. Your implementation should have a time complexity of O(n^2), where n is ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in ascending order using the bubble sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 8,622 | Write a Python function to calculate the sum of all numbers in a given list without using any built-in functions such as `sum()` or `reduce()`. The function should iterate through the list and accumulate the sum manually.
The code should start with:
```python
from typing import List
def calculate_sum(numbers: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_sum(numbers: List[int]) -> int:
"""
Calculate the sum of all numbers in a given list without using built-in functions.
Args:
numbers (List[int]): A list of integers for which t... |
python_sharegpt_23k | 8,623 | Write a Python function to find all the prime numbers in a given list using list comprehension. The function should take a list of integers as input and return a list of prime numbers. The code should start with:
```python
from typing import List
def find_primes(numbers: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 8,628 | Write a Python function to perform an in-order traversal of a binary tree without using recursion. The function should use an iterative approach with a stack to simulate the recursive calls. The binary tree is represented by nodes with `value`, `left`, and `right` attributes. The function should return a list of node v... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
class TreeNode:
def __init__(self, value: int, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
self.value = value
self.left = left
self.right = right... |
python_sharegpt_23k | 8,633 | Write a Python function that exchanges the first and last elements of a given list and removes any duplicate elements from the list while maintaining the order of the remaining elements. The function should handle lists of any length, including empty lists.
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 exchange_and_remove_duplicates(lst: List[int]) -> List[int]:
"""
Exchanges the first and last elements of the given list and removes any duplicate elements,
while maintaining the order of the rem... |
python_sharegpt_23k | 8,634 | Write a Python function to check if a given substring exists within a string, while ignoring the case of the characters. The function should return `True` if the substring is found, and `False` otherwise. The code should start with:
```python
def contains_substring_ignore_case(string: str, substring: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def contains_substring_ignore_case(string: str, substring: str) -> bool:
"""
Checks if a given substring exists within a string, ignoring the case of the characters.
Args:
string (str): The string in which to search... |
python_sharegpt_23k | 8,638 | Write a Python function to reverse a given list in place without using any predefined functions. The solution should have a time complexity of O(n) and a space complexity of O(1). The code should start with:
```python
from typing import List
def reverse_list_in_place(arr: List[int]) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list_in_place(arr: List[int]) -> None:
"""
Reverses the given list in place.
Args:
arr (List[int]): The list to be reversed.
Returns:
None: The function modifies the... |
python_sharegpt_23k | 8,640 | Write a Python function to remove duplicates from a given list without using built-in methods like `set()`, `list.remove()`, or `list.append()`. The function should use a single loop and should not modify the original list. The resulting list should maintain the original order of elements.
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 remove_duplicates(lst: List[int]) -> List[int]:
"""
Removes duplicates from the given list while maintaining the original order of elements.
The function uses a single loop and does not modify th... |
python_sharegpt_23k | 8,641 | Write a Python function to multiply each element in a given array of integers by the smallest prime number greater than 3 and return the new array. The function should handle the case where the input array is empty by returning an empty array.
The code should start with:
```python
from typing import List
def multiply... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def multiply_by_smallest_prime_greater_than_3(arr: List[int]) -> List[int]:
"""
Multiplies each element in the given array by the smallest prime number greater than 3 (which is 5) and returns the new arr... |
python_sharegpt_23k | 8,643 | Write a Python function to sort a list of integers in reverse order using the insertion sort algorithm. The list can contain integers ranging from -10^6 to 10^6, and the length of the list can be between 10^3 and 10^6. The function should modify the list in place and return the sorted list.
The code should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_insertion_sort(arr: List[int]) -> List[int]:
"""
Sorts the given list of integers in reverse order using the insertion sort algorithm.
Args:
arr (List[int]): The list of integers... |
python_sharegpt_23k | 8,645 | Write a Python function to create a dictionary containing the characters from a given string as keys and their frequency in the string as values. The dictionary should be sorted in descending order based on the frequency of each character. If two characters have the same frequency, they should be sorted alphabetically.... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_and_sort_characters(string: str) -> dict:
"""
Counts the frequency of each character in the given string and returns a dictionary
sorted in descending order based on the frequency of each character. If two characte... |
python_sharegpt_23k | 8,650 | Write a Python function to generate a random 8-character PIN that satisfies the following conditions:
1. At least one character must be a lowercase letter.
2. At least one character must be an uppercase letter.
3. At least one character must be a digit.
4. At least one character must be a special character (!@#$%^&*).
... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
def generate_random_pin() -> str:
"""
Generates a random 8-character PIN that satisfies the following conditions:
1. At least one character must be a lowercase letter.
2. At least one character must be an ... |
python_sharegpt_23k | 8,655 | Write a Python function to generate a list of length `n` containing all numbers from 1 to `n` without using any built-in functions or libraries, ensuring that the time complexity is O(n) and the space complexity is O(1). The function should start with:
```python
def generate_list(n: int) -> list:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def generate_list(n: int) -> list:
"""
Generate a list of length n containing all numbers from 1 to n without using any built-in functions or libraries.
Args:
n (int): The length of the list and the maximum number t... |
python_sharegpt_23k | 8,656 | Write a Python function to update a given array of numbers such that each element is replaced by its square, and any number that is divisible by 3 is excluded from the updated array. The function should start with:
```python
from typing import List
def update_array(array: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def update_array(array: List[int]) -> List[int]:
"""
Update the given array such that each element is replaced by its square,
and any number that is divisible by 3 is excluded from the updated array.... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.