source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 6,442 | Write a Python function to calculate the binomial coefficient for two non-negative integers, `n` and `r`, where `n` is greater than or equal to `r`. The function should return the binomial coefficient as an integer. The binomial coefficient, often denoted as "n choose r" or \( C(n, r) \), is the number of ways to choos... | Here is the Python code to solve the given problem in the markdown block:
```python
def binomial_coefficient(n: int, r: int) -> int:
"""
Calculate the binomial coefficient for two non-negative integers n and r, where n >= r.
The binomial coefficient, denoted as C(n, r), is the number of ways to choose r el... |
python_sharegpt_23k | 6,443 | Write a Python function to encrypt a given message using the Caesar cipher with a specified key. The Caesar cipher shifts each letter in the message by a certain number of positions down the alphabet. Non-alphabet characters should remain unchanged. The encryption should be case-sensitive, meaning that uppercase and lo... | Here is the Python code to solve the given problem in the markdown block:
```python
def caesar_cipher(message: str, key: int) -> str:
"""
Encrypts a given message using the Caesar cipher with a specified key.
The Caesar cipher shifts each letter in the message by a certain number of positions down the alph... |
python_sharegpt_23k | 6,444 | Write a Python function to find the maximum value in a given array without using any built-in functions or methods for finding the maximum. The array can contain both positive and negative numbers, and may be of any length. The function should handle edge cases such as an empty array and arrays with a single element.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_maximum(arr: List[int]) -> Optional[int]:
"""
Find the maximum value in a given array without using any built-in functions or methods.
Args:
arr (List[int]): The array of ... |
python_sharegpt_23k | 6,445 | Write a Python function to determine if a given integer is a Narcissistic number. A Narcissistic number (also known as a pluperfect number, an Armstrong number, or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. The input integer will always be with... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_narcissistic_number(num: int) -> bool:
"""
Determines if a given integer is a Narcissistic number.
A Narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits.... |
python_sharegpt_23k | 6,447 | Write a Python function to calculate the product of all elements in a given list of integers. The function should handle lists of any length, including empty lists. If the list is empty, the function should return 1.
The code should start with:
```python
from typing import List
def product_of_list(nums: List[int]) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def product_of_list(nums: List[int]) -> int:
"""
Calculate the product of all elements in a given list of integers.
Args:
nums (List[int]): The list of integers for which the product is to b... |
python_sharegpt_23k | 6,448 | Write a Python function to delete all even numbers from a given list in-place without using any built-in array manipulation functions or creating a new array. The function should iterate backwards starting from the last index of the list.
The code should start with:
```python
from typing import List
def remove_even_n... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_even_numbers(arr: List[int]) -> None:
"""
Removes all even numbers from the given list in-place.
Args:
arr (List[int]): The list from which to remove even numbers.
Returns:
... |
python_sharegpt_23k | 6,450 | Write a Python function to sort an array of integers in ascending order without using any built-in sorting functions or methods. The array can contain both positive and negative integers. The function should be implemented using a counting sort algorithm.
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 sort_array(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in ascending order using a counting sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 6,453 | Write a Python function to calculate the average of a list of numbers with a time complexity of O(n) and a space complexity of O(1), where n is the length of the input list. The function should handle an empty list by returning `None`.
The code should start with:
```python
from typing import List, Optional
def calcul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def calculate_average(numbers: List[int]) -> Optional[float]:
"""
Calculate the average of a list of numbers with a time complexity of O(n) and a space complexity of O(1).
Args:
nu... |
python_sharegpt_23k | 6,458 | Write a Python function to create a dictionary from a given list of tuples, where each key in the dictionary is a string and each value is a list of integers. The input list will have at most 1000 tuples. Each tuple will have a string of length at most 10 as the key, and a randomly generated integer between 1 and 1000 ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple, Dict
def create_sorted_dictionary(lst: List[Tuple[str, int]]) -> Dict[str, List[int]]:
"""
Create a dictionary from a list of tuples and sort it in descending order based on the sum of the values... |
python_sharegpt_23k | 6,464 | Write a Python function to determine if a given list of integers is sorted in ascending order. The function should return `True` if the list is sorted in ascending order, and `False` otherwise. The time complexity of the function should be O(n), where n is the length of the list.
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
def is_sorted_ascending(lst: List[int]) -> bool:
"""
Determines if a given list of integers is sorted in ascending order.
Args:
lst (List[int]): The list of integers to check.
Returns:
... |
python_sharegpt_23k | 6,466 | Write a Python function to generate an array of `n` unique random numbers between `min_val` and `max_val`, where each number must be divisible by a given divisor `div` and not divisible by another given divisor `not_div`. The function should ensure that the generated numbers are unique and meet the specified conditions... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_random_numbers(n: int, min_val: int, max_val: int, div: int, not_div: int) -> List[int]:
"""
Generates an array of n unique random numbers between min_val and max_val,
... |
python_sharegpt_23k | 6,469 | Write a Python function to filter an array and return all elements that are divisible by both 3 and 7. The input array should contain only positive integers. The function should be efficient and handle large arrays.
The code should start with:
```python
from typing import List
def filter_divisible_by_3_and_7(arr: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_divisible_by_3_and_7(arr: List[int]) -> List[int]:
"""
Filters the input array and returns a list of elements that are divisible by both 3 and 7.
Args:
arr (List[int]): The input ... |
python_sharegpt_23k | 6,470 | Write a Python function to find the most common string in a given array of strings. The array can contain duplicates and may have a large number of elements. The function should optimize for a time complexity of O(n), where n is the length of the input array.
The code should start with:
```python
from typing import Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_most_common_string(strings: List[str]) -> str:
"""
Find the most common string in a given array of strings.
Args:
strings (List[str]): The array of strings to search.
Returns:
... |
python_sharegpt_23k | 6,472 | Write a Python function to implement the Bubble Sort algorithm. The function should take a list of integers as input and return the list sorted in ascending order. The function should use the Bubble Sort algorithm to sort the list.
The code should start with:
```python
from typing import List
def bubble_sort(lst: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(lst: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Bubble Sort algorithm.
Args:
lst (List[int]): The list of integers to be sorted.
... |
python_sharegpt_23k | 6,473 | Write a Python function to count the number of occurrences of each character in a given string and return the characters sorted in descending order of their occurrence count. If two characters have the same count, they should be sorted in ascending order of their ASCII values.
The code should start with:
```python
fro... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def count_and_sort_characters(s: str) -> List[Tuple[str, int]]:
"""
Count the number of occurrences of each character in a given string and return the characters sorted in descending order of thei... |
python_sharegpt_23k | 6,474 | Write a Python function to find the length of the longest common subsequence (LCS) between two given strings. The function should be optimized for performance and clarity. The code should start with:
```python
def longest_common_subsequence(a: str, b: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_subsequence(a: str, b: str) -> int:
"""
Calculate the length of the longest common subsequence between two strings.
Args:
a (str): The first string.
b (str): The second string.
Return... |
python_sharegpt_23k | 6,476 | Write a Python function to generate the Fibonacci sequence of a given length `n`, where `n` is a prime number between 10 and 100. The function should return the Fibonacci sequence as a list. Additionally, write a helper function to check if a number is prime. The code should start with:
```python
from typing import Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a given number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False other... |
python_sharegpt_23k | 6,477 | Write a Python function to determine if two given strings are anagrams of each other, without using any built-in sorting or hashing functions or libraries. The function should handle case insensitivity and ignore spaces.
The code should start with:
```python
def are_anagrams(str1: str, str2: str) -> bool:
```
| 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 without using built-in sorting or hashing functions.
Args:
str1 (str): The first string to compare.
str... |
python_sharegpt_23k | 6,480 | Write a Python function to generate an array of a specified size filled with unique random numbers within a given range. The function should ensure that no two numbers in the array are the same. The code should start with:
```python
from typing import List
import random
def generate_unique_random_numbers(size: int, lo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_random_numbers(size: int, lower_bound: int, upper_bound: int) -> List[int]:
"""
Generate an array of a specified size filled with unique random numbers within a given ra... |
python_sharegpt_23k | 6,481 | Write a Python function to compress a given string by replacing consecutive repeated characters with the character followed by the count of its occurrences. The function should have a time complexity of O(n), where n is the length of the input string, and a space complexity of O(1), meaning it should compress the strin... | Here is the Python code to solve the given problem in the markdown block:
```python
def compress_string(s: str) -> str:
"""
Compresses a given string by replacing consecutive repeated characters with the character followed by the count of its occurrences.
Args:
s (str): The input string to be compr... |
python_sharegpt_23k | 6,486 | Write a Python function to compute the Fibonacci sequence up to `n`, where `n` is a positive integer less than or equal to 1000. The solution should have a time complexity of O(n) and a space complexity of O(1). The function should return a list containing the first `n` Fibonacci numbers.
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 fibonacci_sequence(n: int) -> List[int]:
"""
Compute the Fibonacci sequence up to n, where n is a positive integer less than or equal to 1000.
The solution has a time complexity of O(n) and a spa... |
python_sharegpt_23k | 6,496 | Write a Python function to count the number of non-overlapping occurrences of a substring in a given string. The function should be case-sensitive and should account for leading and trailing whitespace characters. The substring must have a length between 2 and 10 characters and can contain alphanumeric characters, punc... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_non_overlapping_substring(string: str, substring: str) -> int:
"""
Counts the number of non-overlapping occurrences of a substring in a given string.
Args:
string (str): The string in which to search for t... |
python_sharegpt_23k | 6,497 | Write a Python function to return the character at a given index in a string. The string can contain alphanumeric characters and special symbols. The index must be a non-negative integer and should be within the range of the string length. If the index is out of range, the function should return an error message.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_character_at_index(string: str, index: int) -> str:
"""
Returns the character at the given index in the string.
Args:
string (str): The input string from which to retrieve the character.
index (int):... |
python_sharegpt_23k | 6,498 | Write a Python function to sort an array of integers from smallest to largest using the bubble sort algorithm. The function should take an array of integers as input and return the sorted array. The implementation should use a loop to iterate through the array and compare adjacent elements to determine the correct orde... | 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 using the bubble sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
Returns:
... |
python_sharegpt_23k | 6,499 | Write a Python function to find the index of the point in a list of points that lies closest to the origin (0, 0). The points are represented as a list of lists, where each inner list contains two integers representing the x and y coordinates of a point. If there are multiple points with the same closest distance, you ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_closest_point_to_origin(points: List[List[int]]) -> int:
"""
Find the index of the point in the list that is closest to the origin (0, 0).
Args:
points (List[List[int]]): A list of ... |
python_sharegpt_23k | 6,504 | Write a Python function to count the number of elements in a given list without using the built-in `len()` function. The function should iterate through the list and increment a counter for each element encountered. The code should start with:
```python
from typing import List
def count_elements(lst: List) -> int:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_elements(lst: List) -> int:
"""
Counts the number of elements in a given list without using the built-in len() function.
Args:
lst (List): The list whose elements are to be counted... |
python_sharegpt_23k | 6,505 | Write a Python function to add two non-negative integers without using any arithmetic operators or built-in functions specifically designed for addition. The function should use bitwise operations to achieve the addition.
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 using bitwise operations without using any arithmetic operators.
Args:
a (int): The first non-negative integer.
b (int): The sec... |
python_sharegpt_23k | 6,506 | Write a Python function to remove consecutive duplicates from a given list. The function should return a new list with all consecutive duplicate elements removed, preserving the order of the remaining elements. The code should start with:
```python
from typing import List
def remove_consecutive_duplicates(lst: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_consecutive_duplicates(lst: List[int]) -> List[int]:
"""
Removes consecutive duplicate elements from the given list.
Args:
lst (List[int]): The list from which consecutive duplica... |
python_sharegpt_23k | 6,507 | Write a Python function to sort a given array of numbers in descending order using the selection sort algorithm. The function should be able to handle arrays with duplicate numbers and negative numbers. The code should start with:
```python
from typing import List
def selection_sort_descending(arr: List[int]) -> List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def selection_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts the given array of numbers in descending order using the selection sort algorithm.
Args:
arr (List[int]): The array of n... |
python_sharegpt_23k | 6,509 | Write a Python function to compute the squares of the elements in a given array of integers, ensuring that the result includes only positive values. The function should handle both positive and negative integers.
The code should start with:
```python
from typing import List
def square_array(arr: List[int]) -> List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def square_array(arr: List[int]) -> List[int]:
"""
Compute the squares of the elements in a given array of integers, ensuring the result is always positive.
Args:
arr (List[int]): The input ... |
python_sharegpt_23k | 6,510 | Write a Python function to produce a copy of a given list while ensuring that the copy is sorted in ascending order and contains only unique elements. The function should handle lists with duplicate elements and return a new list that is sorted and has no duplicates.
The code should start with:
```python
from typing i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sorted_unique_copy(arr: List[int]) -> List[int]:
"""
Produces a copy of the given list with unique elements sorted in ascending order.
Args:
arr (List[int]): The original list of integer... |
python_sharegpt_23k | 6,511 | Write a Python function to sort the elements of a given array in descending order using the quicksort algorithm. The function should be recursive and should handle arrays of any length, including empty arrays. The code should start with:
```python
from typing import List
def quicksort_descending(arr: List[int]) -> Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort_descending(arr: List[int]) -> List[int]:
"""
Sorts the elements of a given array in descending order using the quicksort algorithm.
Args:
arr (List[int]): The array to be sorte... |
python_sharegpt_23k | 6,516 | Write a Python function to calculate the number of times each word appears in a given string, where the string can contain punctuation marks and the word must be matched exactly (case-sensitive). The function should return a dictionary where the keys are the unique words found in the string and the values are the numbe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
import string
def count_word_occurrences(text: str) -> Dict[str, int]:
"""
Calculate the number of times each word appears in a given string, considering case sensitivity and ignoring punctuation.
A... |
python_sharegpt_23k | 6,517 | Write a Python function to generate a list of `n` unique random numbers within a specified range `[min_value, max_value]`. The function should ensure that no duplicates are present in the generated list. The code should start with:
```python
from typing import List
import random
def generate_unique_random_numbers(n: i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_random_numbers(n: int, min_value: int, max_value: int) -> List[int]:
"""
Generate a list of `n` unique random numbers within the specified range `[min_value, max_value]`... |
python_sharegpt_23k | 6,520 | Write a Python function to determine if two words are anagrams, considering words with different letter cases as anagrams. The function should also ignore spaces in the words. The code should start with:
```python
def are_anagrams(word1: str, word2: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def are_anagrams(word1: str, word2: str) -> bool:
"""
Determines if two words are anagrams, considering case insensitivity and ignoring spaces.
Args:
word1 (str): The first word to compare.
word2 (str): The ... |
python_sharegpt_23k | 6,521 | Write a Python function to find the number that appears the most times in a given list of integers. If multiple numbers have the same maximum frequency, return the smallest one. If the list is empty, return `None`.
The code should start with:
```python
from typing import List, Optional
def most_frequent_number(number... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def most_frequent_number(numbers: List[int]) -> Optional[int]:
"""
Find the number that appears the most times in a given list of integers.
If multiple numbers have the same maximum frequen... |
python_sharegpt_23k | 6,532 | Write a Python function to generate a specified number of random numbers within a given range. The function should support generating random integers or random floating-point numbers. The code should start with:
```python
from typing import List
import random
def generate_random_numbers(count: int, start: float, end: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_random_numbers(count: int, start: float, end: float, is_integer: bool = False) -> List[float]:
"""
Generates a specified number of random numbers within a given range.
Arg... |
python_sharegpt_23k | 6,533 | Write a Python function to compress a given string by counting the occurrences of each character. The compressed string should be represented as a combination of the character and its count, but only for characters that occur more than once in the original string. The compressed string should maintain the order of appe... | Here is the Python code to solve the given problem in the markdown block:
```python
def compress_string(string: str) -> str:
"""
Compresses a given string by counting the occurrences of each character.
The compressed string includes only characters that occur more than once,
represented as a combination... |
python_sharegpt_23k | 6,545 | Write a Python function to calculate and return a list of the square roots of the numbers from 1 to n, where n is a positive integer. The function should handle the case where n is less than 1 by returning an empty list.
The code should start with:
```python
from typing import List
import math
def calculate_square_ro... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def calculate_square_roots(n: int) -> List[float]:
"""
Calculate and return a list of the square roots of the numbers from 1 to n.
Args:
n (int): The upper limit of the range of ... |
python_sharegpt_23k | 6,546 | Write a Python function to count the number of occurrences of a specific value in a large array efficiently. The array can have up to \(10^9\) elements, and the elements can be any integer from \(-10^9\) to \(10^9\). The specific value to count can also be any integer from \(-10^9\) to \(10^9\). The time complexity of ... | 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], specific_value: int) -> int:
"""
Counts the number of occurrences of a specific value in a large array efficiently.
Args:
arr (List[int]): The array in ... |
python_sharegpt_23k | 6,547 | Write a Python function to reverse a given string using loops. The input string can be up to \(10^6\) characters long. The solution should have a time complexity of \(O(n)\) and should not use any built-in string manipulation functions. The code should start with:
```python
def reverse_string(input_string: str) -> str:... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(input_string: str) -> str:
"""
Reverses the given input string using loops.
Args:
input_string (str): The string to be reversed.
Returns:
str: The reversed string.
"""
# Initi... |
python_sharegpt_23k | 6,552 | Write a Python function to remove all elements from a given list that meet a specified condition while maintaining the order of the remaining elements. The function should take a list and a condition (as a lambda function) as input and return the modified list.
The code should start with:
```python
from typing import ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Callable
def remove_elements_with_condition(lst: List[int], condition: Callable[[int], bool]) -> List[int]:
"""
Removes all elements from the given list that meet the specified condition while maintaini... |
python_sharegpt_23k | 6,553 | Write a Python function to generate and return a list of 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 function should handle cases where `n` is less than or equal to 0 by returning an empty 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 generate_fibonacci(n: int) -> List[int]:
"""
Generate the first n Fibonacci numbers.
Args:
n (int): The number of Fibonacci numbers to generate.
Returns:
List[int]: A list c... |
python_sharegpt_23k | 6,555 | Write a Python function to reorder an array of numbers in descending order without using any built-in sorting functions or methods. The function should have a time complexity of O(n log n), where n is the length of the input array. The function should be named `reorderArray` and should take a single parameter, `arr`, w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge(left: List[int], right: List[int]) -> List[int]:
"""
Merge two sorted lists into a single sorted list in descending order.
Args:
left (List[int]): The first sorted list.
ri... |
python_sharegpt_23k | 6,556 | Write a Python function to reverse the order of a list without using the built-in reverse function and any additional list or array for temporary storage. The function should modify the input list in place.
The code should start with:
```python
from typing import List
def reverse_list_in_place(lst: List[int]) -> List... | 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 order of a list in place without using the built-in reverse function or additional storage.
Args:
lst (List[int]): Th... |
python_sharegpt_23k | 6,558 | Write a Python function to check if a given string is a valid IP address without using any built-in IP address validation functions or regular expressions. The function should ensure that the IP address consists of exactly four parts separated by dots, each part being a valid integer between 0 and 255, inclusive.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_valid_ip_address(ip_address: str) -> bool:
"""
Check if the given string is a valid IP address.
An IP address is considered valid if it consists of exactly four parts
separated by dots, and each part is a valid i... |
python_sharegpt_23k | 6,559 | Write a Python function to calculate the absolute difference between a given positive integer and its reverse. The function should handle numbers less than \(10^9\). The code should start with:
```python
def find_reverse_difference(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_reverse_difference(n: int) -> int:
"""
Calculate the absolute difference between a given positive integer and its reverse.
Args:
n (int): A positive integer less than 10^9.
Returns:
int: The ab... |
python_sharegpt_23k | 6,560 | Write a Python function called `swap_values` that takes in two variables as arguments and swaps their values. The function should return the updated values of the variables after the swap. You are not allowed to use any built-in functions or temporary variables. Your implementation should be efficient and in-place.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
def swap_values(a: int, b: int) -> tuple:
"""
Swaps the values of two integers without using any built-in functions or temporary variables.
Args:
a (int): The first integer.
b (int): The second integer.
... |
python_sharegpt_23k | 6,561 | Write a Python function to generate a random string of a given length without using any built-in functions or libraries. The string should consist of alphanumeric characters (both uppercase and lowercase letters, and digits). The code should start with:
```python
def generate_random_string(length: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def generate_random_string(length: int) -> str:
"""
Generates a random string of the given length without using any built-in functions or libraries.
Args:
length (int): The length of the random string to generate.
... |
python_sharegpt_23k | 6,562 | Write a Python function to calculate the sum of the squares of even numbers in a given list. The function should use a single loop to iterate through the list and should not use any built-in Python functions or methods for finding even numbers or calculating squares. 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 sum_of_squares_of_even_numbers(lst: List[int]) -> int:
"""
Calculate the sum of the squares of even numbers in a given list.
Args:
lst (List[int]): The list of integers from which to cal... |
python_sharegpt_23k | 6,569 | Write a Python function to generate a list of prime numbers between a given range `[start, end]`. The function should return a list of all prime numbers within the specified range. The code should start with:
```python
from typing import List
def generate_primes_in_range(start: int, end: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
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 number is prime... |
python_sharegpt_23k | 6,573 | Write a Python function to merge two sorted lists of integers into a single sorted list. The function should efficiently combine the two lists while maintaining the sorted order. The code should start with:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
```
| 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]:
"""
Merges two sorted lists of integers into a single sorted list.
Args:
list1 (List[int]): The first sorted list o... |
python_sharegpt_23k | 6,576 | Write a Python function to determine whether a given array of integers and floating-point numbers is a palindrome. The function should handle arrays containing negative integers and floating-point numbers. The function should return `True` only if the array remains the same when reversed, considering both the order and... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def is_palindrome_array(arr: List[Union[int, float]]) -> bool:
"""
Determine whether the given array of integers and floating-point numbers is a palindrome.
Args:
arr (List[Union[int,... |
python_sharegpt_23k | 6,577 | Write a Python function to extract and return a set of unique even numbers from a given list of integers. The function should handle any list of integers, including empty lists and lists with no even numbers. The code should start with:
```python
from typing import List, Set
def get_unique_even_numbers(numbers: List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Set
def get_unique_even_numbers(numbers: List[int]) -> Set[int]:
"""
Extracts and returns a set of unique even numbers from the given list of integers.
Args:
numbers (List[int]): A list of ... |
python_sharegpt_23k | 6,578 | Implement an efficient sorting algorithm in Python that sorts a list of integers in ascending order. Your algorithm should have a time complexity of O(n log n) and should not use the built-in `sort()` function or any other pre-existing sorting function.
In addition to the sorting algorithm, write a function called `c... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Merge Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Re... |
python_sharegpt_23k | 6,579 | Write a Python function to convert a decimal number to its binary representation using only bitwise operations. The input number will be in the range of 0 to 255. The function should return a string representing the binary number.
The code should start with:
```python
def decimal_to_binary_bitwise(n: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary_bitwise(n: int) -> str:
"""
Convert a decimal number to its binary representation using only bitwise operations.
Args:
n (int): The decimal number to convert, in the range of 0 to 255.
Ret... |
python_sharegpt_23k | 6,587 | Write a Python function to calculate the sum and average of all odd elements in a given list. The function should not use any built-in functions for calculating the sum or average, and it should not use the modulus operator (%) to check if a number is odd. Instead, use bitwise operations to determine if a number is odd... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sum_and_average_of_odds(numbers: List[int]) -> Tuple[int, float]:
"""
Calculate the sum and average of all odd elements in the given list.
Args:
numbers (List[int]): The list of i... |
python_sharegpt_23k | 6,588 | Write a Python function to find the maximum element from a given list without using any built-in functions or libraries for finding the maximum element. The list will contain both positive and negative integers, and may have duplicate values. The code should be efficient and have a time complexity of O(n), where n is t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def find_maximum(lst: List[int]) -> Optional[int]:
"""
Find the maximum element in a given list of integers without using any built-in functions or libraries.
Args:
lst (List[int])... |
python_sharegpt_23k | 6,589 | Write a Python function to remove all duplicates from a given list while maintaining the order of the elements. The list can contain integers, floats, strings, or a combination of these data types. The solution should have a time complexity of O(n), where n is the length of the input 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, Union
def remove_duplicates(lst: List[Union[int, float, str]]) -> List[Union[int, float, str]]:
"""
Removes all duplicates from the given list while maintaining the order of the elements.
Args:
... |
python_sharegpt_23k | 6,590 | Write a Python function to generate an n x n identity matrix where the elements of the diagonal are restricted to be non-zero. The function should return the resulting matrix as a list of lists.
The code should start with:
```python
from typing import List
def generate_identity_matrix(n: int) -> List[List[int]]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_identity_matrix(n: int) -> List[List[int]]:
"""
Generate an n x n identity matrix where the elements of the diagonal are non-zero.
Args:
n (int): The size of the identity matrix... |
python_sharegpt_23k | 6,593 | Write a Python function to transpose a given two-dimensional array (matrix). The transpose of a matrix is obtained by exchanging its rows with columns. The function should handle matrices of any size, including non-square matrices.
The code should start with:
```python
from typing import List
def transpose_matrix(mat... | 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]]:
"""
Transposes a given two-dimensional array (matrix).
The transpose of a matrix is obtained by exchanging its rows with columns.
... |
python_sharegpt_23k | 6,594 | Write a Python function to calculate the sum of the elements in two given arrays of integers. The function should return the sum of the elements in both arrays. If either of the arrays is empty, the function should return -1. The length of each array is between 1 and 1000, and the sum of the elements in each array will... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_arrays(array1: List[int], array2: List[int]) -> int:
"""
Calculate the sum of the elements in two given arrays of integers.
Args:
array1 (List[int]): The first array of integers.
... |
python_sharegpt_23k | 6,598 | Write a Python function to create a list comprehension that filters out odd numbers from a given list, removes duplicates, and sorts the resulting list in descending order without using any built-in sorting or duplicate removal functions. The original list can contain up to 1 million elements.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_and_sort_odds(original_list: List[int]) -> List[int]:
"""
Filters out odd numbers from the given list, removes duplicates, and sorts the resulting list in descending order.
Args:
... |
python_sharegpt_23k | 6,602 | Write a Python function to implement the Bubble Sort algorithm correctly. The function should take a list of integers as input and return the sorted list. The code should start with:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
Li... |
python_sharegpt_23k | 6,603 | Write a Python function to capitalize the first letter of each word in a list of strings. The function should handle a larger list of strings efficiently and return a new list with each string modified as required.
The code should start with:
```python
from typing import List
def capitalize_list_of_strings(string_lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def capitalize_list_of_strings(string_list: List[str]) -> List[str]:
"""
Capitalizes the first letter of each word in a list of strings.
Args:
string_list (List[str]): A list of strings wher... |
python_sharegpt_23k | 6,604 | Write a Python function to find the most common character in a list of strings. The input list may contain up to 1 million strings, each with a maximum length of 100 characters. The strings are case-sensitive and can contain any printable ASCII characters. The solution should have a time complexity of O(n log n), where... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
from collections import defaultdict
def most_common_character(strings: List[str]) -> str:
"""
Find the most common character in a list of strings.
Args:
strings (List[str]): A list of string... |
python_sharegpt_23k | 6,606 | Write a Python function to find the maximum value in an array of integers with a time complexity of O(n), where n is the length of the array. The array can contain duplicates. The function should start with:
```python
from typing import List
def find_max_value(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_value(arr: List[int]) -> int:
"""
Find the maximum value in an array of integers with a time complexity of O(n).
Args:
arr (List[int]): The array of integers from which to find ... |
python_sharegpt_23k | 6,608 | Write a Python function to extract the digits of a given number and return them in descending order. You are not allowed to use any built-in functions or methods that directly manipulate or convert the number into a string or list. The function should start with:
```python
def extract_digits_descending(num: int) -> lis... | Here is the Python code to solve the given problem in the markdown block:
```python
def extract_digits_descending(num: int) -> list:
"""
Extracts the digits of a given number and returns them in descending order.
Args:
num (int): The number from which to extract the digits.
Returns:
li... |
python_sharegpt_23k | 6,609 | Implement a sorting algorithm in Python that meets the following requirements:
- The sorting algorithm must have a time complexity of O(n log n).
- The sorting algorithm must not use any built-in sorting functions or libraries.
- The sorting algorithm must be able to handle large input sizes (e.g., an array with 10^6 e... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def custom_sort(arr: List[int]) -> List[int]:
"""
Sorts the given array using the Merge Sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
List[int]: The so... |
python_sharegpt_23k | 6,610 | Write a Python function to sort an array of integers in ascending order without using the built-in `sort()` function, loops, or recursion. The function should leverage Python's higher-order functions and comprehensions to achieve the sorting.
The code should start with:
```python
from typing import List
def sort_arra... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_array(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in ascending order without using loops, recursion, or the built-in sort() function.
Args:
arr (List[int]): The arr... |
python_sharegpt_23k | 6,611 | Write a Python function to check if two given variables have the same type and, if both variables are lists, also check if they have the same length. The function should return `True` if both conditions are met, otherwise `False`.
The code should start with:
```python
def check_type_and_length(var1, var2) -> bool:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_type_and_length(var1, var2) -> bool:
"""
Checks if two variables have the same type and, if both are lists, also checks if they have the same length.
Args:
var1: The first variable to compare.
var2... |
python_sharegpt_23k | 6,614 | Write a Python function to reverse a given string in place with a constant amount of extra space and a time complexity of O(n), where n is the length of the string. The function should not use any additional data structures that grow with the input size, such as lists or arrays, and should directly modify the input str... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string_in_place(s: str) -> str:
"""
Reverses the given string in place with a constant amount of extra space and a time complexity of O(n).
Args:
s (str): The input string to be reversed.
Returns:
... |
python_sharegpt_23k | 6,615 | Write a Python function to count the occurrences of each unique element in a given array of integers. The function should return a dictionary where the keys are the unique elements from the array, and the values are their respective counts. The function should also ensure that the input array is processed to remove any... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_unique_elements(arr: List[int]) -> Dict[int, int]:
"""
Counts the occurrences of each unique element in the given array of integers.
Args:
arr (List[int]): The input array of... |
python_sharegpt_23k | 6,617 | Write a Python function to remove duplicate elements from a given array of integers and return the sorted array in ascending order. The function should handle any size of input array and ensure that the output array contains only unique elements in sorted order.
The code should start with:
```python
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(arr: List[int]) -> List[int]:
"""
Removes duplicate elements from the given array and returns the sorted array in ascending order.
Args:
arr (List[int]): The i... |
python_sharegpt_23k | 6,618 | Write a Python function to filter out elements with the value 0 from a given list. If an element is greater than 5, increment it by 1 before filtering. The function should return the filtered list.
The code should start with:
```python
from typing import List
def filter_array(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 filter_array(arr: List[int]) -> List[int]:
"""
Filters out elements with the value 0 from a given list. If an element is greater than 5, it is incremented by 1 before filtering.
Args:
ar... |
python_sharegpt_23k | 6,620 | Write a Python function to find all indices of a given element in a list that may contain duplicates. The function should return a list of indices where the element is found.
The code should start with:
```python
from typing import List
def find_all_indices(array: List[int], element: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_all_indices(array: List[int], element: int) -> List[int]:
"""
Find all indices of a given element in a list that may contain duplicates.
Args:
array (List[int]): The list in which t... |
python_sharegpt_23k | 6,624 | Write a Python function to merge two dictionaries into a new dictionary, ensuring that the merged dictionary only contains key-value pairs where the key is a string and the value is an integer. If a key-value pair in either `dict1` or `dict2` does not meet this requirement, it should be skipped.
The code should start ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def merge_dictionaries(dict1: Dict[str, any], dict2: Dict[str, any]) -> Dict[str, int]:
"""
Merge two dictionaries into a new dictionary, ensuring that the merged dictionary only contains key-value pairs... |
python_sharegpt_23k | 6,629 | Write a Python function to calculate the sum of a list of numbers, ignoring any numbers that are divisible by 3. The function should handle both positive and negative integers. The code should start with:
```python
from typing import List
def sum_excluding_divisible_by_three(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_excluding_divisible_by_three(numbers: List[int]) -> int:
"""
Calculate the sum of a list of numbers, ignoring any numbers that are divisible by 3.
Args:
numbers (List[int]): A list o... |
python_sharegpt_23k | 6,636 | Write a Python function to generate the first n prime numbers. The function should return a list of the first n prime numbers. The code should start with:
```python
from typing import List
def generate_first_n_primes(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
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 otherwise.
... |
python_sharegpt_23k | 6,638 | Write a Python function to print the first n even integers starting from a given integer k. The function should handle cases where n is zero or negative by printing nothing. The function should also handle cases where k is negative by starting from 0.
The code should start with:
```python
def print_first_n_even_intege... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_first_n_even_integers(k: int, n: int) -> None:
"""
Prints the first n even integers starting from the given integer k.
Args:
k (int): The starting integer. If k is negative, the function starts from 0.
... |
python_sharegpt_23k | 6,639 | Write a Python function to perform a binary search on a sorted list of integers to find the index of a target element. If the target element is found, the function should return its index; otherwise, it should return -1. The function should have a time complexity of O(log n), where n is the length of the list.
The cod... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search(arr: List[int], target: int) -> int:
"""
Perform a binary search on a sorted list to find the index of a target element.
Args:
arr (List[int]): A sorted list of integers.
... |
python_sharegpt_23k | 6,653 | Write a Python function to calculate the sum of the first `n` prime numbers. The function should take an integer `n` as input and return the sum of the first `n` prime numbers. The function should be efficient and avoid unnecessary computations.
The code should start with:
```python
def sum_of_first_n_primes(n: int) -... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_first_n_primes(n: int) -> int:
"""
Calculate the sum of the first n prime numbers.
Args:
n (int): The number of prime numbers to sum.
Returns:
int: The sum of the first n prime numbers.
"... |
python_sharegpt_23k | 6,660 | Write a Python function to sort a given array using the quicksort algorithm recursively. You are not allowed to use any built-in sorting functions or libraries. The function should start with:
```python
from typing import List
def quicksort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort(arr: List[int]) -> List[int]:
"""
Sorts a given array using the quicksort algorithm recursively.
Args:
arr (List[int]): The array to be sorted.
Returns:
List[int]:... |
python_sharegpt_23k | 6,661 | Write a Python function to compute the occurrence of each unique alphanumeric character in a given string, excluding any whitespace characters. The function should return a dictionary where the keys are the unique alphanumeric characters and the values are their respective counts.
The code should start with:
```python... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
import string
def count_alphanumeric_occurrences(input_string: str) -> Dict[str, int]:
"""
Compute the occurrence of each unique alphanumeric character in a given string, excluding any whitespace charact... |
python_sharegpt_23k | 6,665 | Write a Python function to remove the last item from a given array of integers without using any built-in array methods or functions such as `pop()` or `slice()`. The function should return the modified array.
The code should start with:
```python
from typing import List
def remove_last_item(arr: List[int]) -> List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_last_item(arr: List[int]) -> List[int]:
"""
Removes the last item from the given array of integers and returns the modified array.
Args:
arr (List[int]): The input array of intege... |
python_sharegpt_23k | 6,666 | Write a Python function to generate a list of `n` random numbers between 1 and 6, with the given length `n`, while ensuring that the time complexity is O(n) and the space complexity is O(1). The function should not use any additional space proportional to `n` beyond the list itself.
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
from typing import List
def roll_dice_optimized(n: int) -> List[int]:
"""
Generates a list of n random numbers between 1 and 6 with optimized time and space complexity.
Args:
n (int): The number of ra... |
python_sharegpt_23k | 6,667 | Write a Python function to check if a given element exists in a list or a string. The function should handle both lists and strings as input and return a boolean indicating whether the element is present. If the input type is not supported (neither a list nor a string), the function should return `None`.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union, List, Optional
def element_exists(element: Union[int, str], container: Union[List[Union[int, str]], str]) -> Optional[bool]:
"""
Check if a given element exists in a list or a string.
Args:
... |
python_sharegpt_23k | 6,669 | Write a Python function to convert a tuple of integers into a list of their corresponding English words. The function should handle tuples of varying lengths and return a list where each integer in the tuple is replaced by its English word representation.
The code should start with:
```python
from typing import Tuple,... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Tuple, List
def tuple_to_english_words(t: Tuple[int, ...]) -> List[str]:
"""
Converts a tuple of integers into a list of their corresponding English words.
Args:
t (Tuple[int, ...]): A tuple of i... |
python_sharegpt_23k | 6,672 | Write a Python function to calculate the length of a linked list without using any additional memory. The function should take the head node of the linked list as input and return the length of the linked list. The function should have a time complexity of O(n) and a space complexity of O(1), where n is the length of t... | Here is the Python code to solve the given problem in the markdown block:
```python
class Node:
def __init__(self, val, next=None):
self.val = val
self.next = next
def get_linked_list_length(head: Node) -> int:
"""
Calculate the length of a linked list starting from the given head node.
... |
python_sharegpt_23k | 6,675 | Write a Python function to extract all the numbers from a given string and return them as a list of integers. The function should handle multiple consecutive digits as a single number.
The code should start with:
```python
from typing import List
def extract_numbers(string: str) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def extract_numbers(string: str) -> List[int]:
"""
Extract all the numbers from a given string and return them as a list of integers.
Args:
string (str): The input string from which to extra... |
python_sharegpt_23k | 6,678 | Write a Python function to generate a list of even numbers within a specified range `[start, end]`. The function should handle cases where `start` and `end` are not necessarily even numbers and should include both `start` and `end` if they are even. The code should start with:
```python
from typing import List
def gen... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_even_numbers(start: int, end: int) -> List[int]:
"""
Generate a list of even numbers within the specified range [start, end].
Args:
start (int): The starting integer of the rang... |
python_sharegpt_23k | 6,679 | Write a Python function to find the maximum score from a given set of scores without using any built-in sorting functions or methods. The function should iterate through the set and determine the maximum score using basic comparison operations.
The code should start with:
```python
def find_max_score(scores: set) -> i... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_max_score(scores: set) -> int:
"""
Find the maximum score from a given set of scores without using any built-in sorting functions or methods.
Args:
scores (set): A set of integers representing the scores.
... |
python_sharegpt_23k | 6,681 | Write a Python function to generate a random word from a given list of words such that each word has an equal probability of being selected. The function should take a list of words as input and return a randomly selected word. The code should start with:
```python
from typing import List
import random
def get_random_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def get_random_word(words: List[str]) -> str:
"""
Selects a random word from a given list of words with equal probability.
Args:
words (List[str]): A list of words from which t... |
python_sharegpt_23k | 6,686 | Write a Python function to sum two floating-point numbers and round the result to the nearest whole number. The function should return the rounded integer.
The code should start with:
```python
def sum_and_round(num1: float, num2: float) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_and_round(num1: float, num2: float) -> int:
"""
Sums two floating-point numbers and rounds the result to the nearest whole number.
Args:
num1 (float): The first floating-point number.
num2 (float): T... |
python_sharegpt_23k | 6,687 | Write a Python function to compute the sum of all numbers from 1 to `n` using a for loop. Additionally, you are not allowed to use any arithmetic operators in your code. The function should start with:
```python
def sum_of_numbers(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_numbers(n: int) -> int:
"""
Compute the sum of all numbers from 1 to n using a for loop without using any arithmetic operators.
Args:
n (int): The upper limit of the range (inclus... |
python_sharegpt_23k | 6,688 | Write a Python function to generate all prime numbers less than a given number `n` efficiently. The function should handle inputs up to `10^6` efficiently using the Sieve of Eratosthenes algorithm.
The code should start with:
```python
from typing import List
def generate_primes_less_than_n(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 generate_primes_less_than_n(n: int) -> List[int]:
"""
Generate all prime numbers less than a given number n using the Sieve of Eratosthenes algorithm.
Args:
n (int): The uppe... |
python_sharegpt_23k | 6,690 | Write a Python function to find the k most frequent words in a given list of strings. If multiple words have the same frequency, return them in lexicographical order. The function should start with:
```python
from typing import List
def find_k_most_frequent_words(words: List[str], k: int) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_k_most_frequent_words(words: List[str], k: int) -> List[str]:
"""
Find the k most frequent words in a given list of strings. If multiple words have the same frequency,
return them in lexicog... |
python_sharegpt_23k | 6,691 | Write a Python function to validate if a given string represents a valid integer according to the following constraints:
- The string can be preceded by an optional positive or negative sign.
- The string can contain leading and trailing whitespaces.
- The string can only consist of digits.
- The string cannot exceed a... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_valid_integer(string: str) -> bool:
"""
Validates if a given string represents a valid integer according to the specified constraints.
Args:
string (str): The string to be validated.
Returns:
boo... |
python_sharegpt_23k | 6,692 | Write a Python function to find and return all the prime numbers between a given range `[start, end]`. The function should handle edge cases such as negative numbers, zero, and one, which are not prime numbers. The function should also handle cases where `start` is greater than `end`.
The code should start with:
```py... | 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 | 6,694 | Write a Python function to find the maximum number of overlapping intervals in a given set of intervals. Each interval is represented by a pair of integers denoting its start and end points. The function should consider intervals that partially overlap as well. The input intervals may contain duplicates, and the functi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_overlapping_intervals(intervals: List[Tuple[int, int]]) -> int:
"""
Find the maximum number of overlapping intervals in a given set of intervals.
Args:
intervals (List[Tu... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.