source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 1,673 | Write a Python function to print every third item from a given list of items. The function should iterate through the list and print each item at an index that is a multiple of 3. The code should start with:
```python
from typing import List
def print_every_third_item(items: List[str]) -> None:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def print_every_third_item(items: List[str]) -> None:
"""
Prints every third item from the given list of items.
Args:
items (List[str]): The list of items from which to print every third ite... |
python_sharegpt_23k | 1,674 | Write a Python function to reverse a list without using built-in list functions, without using additional memory space, and without using any looping constructs (such as for loops or while loops). Also, you should not use recursion to solve this problem. The function should start with:
```python
from typing import List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list(lst: List[int]) -> List[int]:
"""
Reverses the given list without using built-in list functions, additional memory space,
looping constructs, or recursion.
Args:
lst (Li... |
python_sharegpt_23k | 1,676 | Write a Python function to remove duplicate elements from a given list while preserving the order of the elements. The list can contain both integers and strings. The function should not use any built-in functions or libraries for removing duplicates or checking for duplicates. The solution should aim for a time comple... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def remove_duplicates(arr: List[Union[int, str]]) -> List[Union[int, str]]:
"""
Removes duplicate elements from a list while preserving the order of the elements.
Args:
arr (List[Unio... |
python_sharegpt_23k | 1,685 | Write a Python function to find the index of the last occurrence of a specific character in a given string. The function should have a time complexity of O(n), where n is the length of the string, and should not use any built-in Python methods that directly solve this problem. The function should start with:
```python
... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_last_occurrence(string: str, char: str) -> int:
"""
Find the index of the last occurrence of a specific character in a given string.
Args:
string (str): The input string to search within.
char (str)... |
python_sharegpt_23k | 1,688 | Write a Python function to filter out prime numbers from a given list of integers. The function should have a time complexity of O(n*sqrt(m)), where n is the length of the input list and m is the maximum value in the list.
The code should start with:
```python
from typing import List
import math
def filter_primes(num... | 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 for primality.
Returns:
bool: True if the number is prime, False oth... |
python_sharegpt_23k | 1,692 | Write a Python function to remove duplicate items from a list of integers without using any built-in functions or additional data structures. The solution should have a time complexity of O(n^2) and a space complexity of O(1). The function should handle cases where the list contains negative numbers and is not sorted i... | 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 duplicate items from a list of integers in-place without using any built-in functions or additional data structures.
Args:
lst... |
python_sharegpt_23k | 1,695 | Write a Python function to find the Greatest Common Divisor (GCD) of all elements in a given array of integers. The function should handle various edge cases, such as an empty array, an array with a single element, and an array containing only zeros. The function should also handle arrays with both positive and negativ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_gcd_of_array(array: List[int]) -> int:
"""
Find the Greatest Common Divisor (GCD) of all elements in the given array.
Args:
array (List[int]): The array of integers for which to fin... |
python_sharegpt_23k | 1,697 | Write a Python function to search for a specific key-value pair in a nested object, but only if the key is a string and the value is a positive integer. The function should return the key-value pair if found, or `None` if not found. The function should have a time complexity of O(n), where n is the number of key-value ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Any, Dict, Optional, Tuple
def search_key_value_pair(search_key: str, obj: Dict[str, Any]) -> Optional[Tuple[str, int]]:
"""
Searches for a specific key-value pair in a nested object where the key is a string... |
python_sharegpt_23k | 1,705 | Write a Python function to generate a list of odd numbers within a specified range `[start, end)`, where both `start` and `end` are inclusive. The function should return a list of odd numbers that are greater than or equal to `start` and less than `end`.
The code should start with:
```python
from typing import List
d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_odd_numbers(start: int, end: int) -> List[int]:
"""
Generate a list of odd numbers within the specified range [start, end).
Args:
start (int): The starting value (inclusive) of ... |
python_sharegpt_23k | 1,707 | Write a Python function to determine the number of days in a given month and year. The function should handle leap years correctly and raise a `ValueError` if the month or year is not within the specified range. The function should only use built-in Python functions and not rely on any external libraries or modules. Th... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_number_of_days(month: int, year: int) -> int:
"""
Returns the number of days in a given month and year.
Args:
month (int): The month (1-12).
year (int): The year (1-9999).
Returns:
int: ... |
python_sharegpt_23k | 1,708 | Write a Python function to reverse a given string without using any built-in functions or methods that directly reverse a string or any form of recursion. The function should handle Unicode characters and special characters correctly, preserving their original positions while reversing the order of the characters. The ... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(input_string: str) -> str:
"""
Reverses the input string without using built-in reverse functions or recursion.
Handles Unicode characters and special characters correctly, preserving their original positi... |
python_sharegpt_23k | 1,714 | Write a Python function to calculate the sum of all values in a given two-dimensional array `arr` of size `m`x`n`. The values in the array are integers, and each value is in the range of -100 to 100, inclusive. The function should handle arrays with dimensions up to 10^6 x 10^6 and compute the sum in a time complexity ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_2d_array(arr: List[List[int]]) -> int:
"""
Calculate the sum of all values in a given two-dimensional array.
Args:
arr (List[List[int]]): A two-dimensional array of integers.
... |
python_sharegpt_23k | 1,718 | Write a Python function to reverse the order of words in a given string without using any built-in string manipulation functions and ensuring constant space complexity (O(1)). The solution should have a time complexity of O(n), where n is the length of the string. The function should start with:
```python
def reverse_w... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_in_string(string: str) -> str:
"""
Reverses the order of words in a given string without using any built-in string manipulation functions.
The solution maintains constant space complexity (O(1)) and has a t... |
python_sharegpt_23k | 1,725 | Write a Python function to remove duplicate elements from a given list while maintaining the original order of the elements. The function should not use any built-in functions that directly solve the problem, and it should be implemented with a time complexity of O(n), where n is the length of the list. The function sh... | 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 duplicate elements from a list while maintaining the original order of the elements.
Args:
lst (List[int]): The list from whic... |
python_sharegpt_23k | 1,733 | Write a Python function to generate Pythagorean Triples up to a given limit that satisfy the following constraints:
1. The sum of the three sides of each triple must be equal to or less than 50.
2. The product of the three sides of each triple must be a prime number.
3. The sides of each triple must form an arithmetic ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import math
def generate_pythagorean_triples(limit: int) -> List[Tuple[int, int, int]]:
"""
Generate Pythagorean Triples up to a given limit that satisfy specific constraints.
Args:
l... |
python_sharegpt_23k | 1,735 | Write a Python function to convert inches to centimeters, ensuring that the input is validated for non-negative values and correct data types. The function should raise specific exceptions for invalid inputs. The conversion formula is: 1 inch = 2.54 centimeters.
The code should start with:
```python
def convert_inches... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_inches_to_cm(inches: float) -> float:
"""
Convert inches to centimeters.
Args:
inches (float): The number of inches to convert. Must be a non-negative float or integer.
Returns:
float: The e... |
python_sharegpt_23k | 1,739 | Write a Python function to calculate the absolute difference between two positive integers, `x` and `y`, where both integers are less than or equal to 1000. The function should not use any built-in functions or operators to find the absolute difference. Instead, it should implement its own algorithm to compute the abso... | Here is the Python code to solve the given problem in the markdown block:
```python
def absolute_difference(x: int, y: int) -> int:
"""
Calculate the absolute difference between two positive integers x and y,
where both integers are less than or equal to 1000. The function does not
use any built-in func... |
python_sharegpt_23k | 1,742 | Write a Python function to sort an array in descending order using the bubble sort algorithm. Ensure that the function correctly implements the swapping logic to achieve the desired sorting order. The code should start with:
```python
from typing import List
def bubble_sort_descending(arr: List[int]) -> List[int]:
```... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts an array in descending order using the bubble sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
... |
python_sharegpt_23k | 1,745 | Write a Python function to generate an array containing 1000 unique random numbers between 0 and 10000, sorted in ascending order, and divided into subarrays of size 100 each. The function should return a list of these subarrays.
The code should start with:
```python
from typing import List
import random
def generate... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_sorted_subarrays(n: int, max_value: int, subarray_size: int) -> List[List[int]]:
"""
Generates an array containing n unique random numbers between 0 and max_value,
sorted i... |
python_sharegpt_23k | 1,748 | Write a Python function to process a list of numbers such that each number is first squared, then cubed, and finally the results are printed in reverse order. Ensure that the time complexity of the function does not exceed O(n^3), where n is the length of the list.
The code should start with:
```python
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def process_numbers(numbers: List[int]) -> None:
"""
Processes a list of numbers by squaring each number, cubing the result, and then printing the results in reverse order.
Args:
numbers (Li... |
python_sharegpt_23k | 1,750 | Write a Python function to format a given floating-point number to two decimal places, round it to the nearest hundredth, convert it to a string with a specific format where the thousand separator is a comma and the decimal separator is a period, and ensure that the output string is exactly 10 characters long. The func... | Here is the Python code to solve the given problem in the markdown block:
```python
def format_number(num: float) -> str:
"""
Formats a given floating-point number to two decimal places, rounds it to the nearest hundredth,
converts it to a string with a specific format where the thousand separator is a comm... |
python_sharegpt_23k | 1,752 | Write a Python function to count the number of integers in a given 2D array that are divisible by 2 and greater than 10. The function should iterate through each element in the 2D array, check if the element meets the criteria, and increment a counter if it does. The function should return the final count.
The code sh... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_divisible_greater_than_10(arr: List[List[int]]) -> int:
"""
Counts the number of integers in a 2D array that are divisible by 2 and greater than 10.
Args:
arr (List[List[int]]): A ... |
python_sharegpt_23k | 1,758 | Write a Python function to perform a linear search in an array, starting from the end of the array and iterating backwards. The function should return the index of the last occurrence of the target number in the array. If the target number is not found, the function should return -1. If there are multiple occurrences o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def linear_search_from_end(arr: List[int], target: int) -> int:
"""
Perform a linear search in an array starting from the end and return the index of the last occurrence of the target number.
If the ... |
python_sharegpt_23k | 1,760 | Write a Python function to implement the optimized bubble sort algorithm. The function should handle the following error scenarios:
- Input is not a list or array
- Input list contains non-integer elements
Additionally, the function should be optimized to minimize the number of comparisons and swaps. The code should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def optimized_bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts the input list using the optimized bubble sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Ret... |
python_sharegpt_23k | 1,763 | Write a Python function to find the most frequent name in a given array of names, considering the names as case-insensitive. The array may contain up to \(10^6\) names, and each name can be up to 100 characters long. The solution should handle names with any combination of uppercase and lowercase letters, spaces, punct... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def most_frequent_name(names: List[str]) -> str:
"""
Find the most frequent name in a given array of names, considering the names as case-insensitive.
Args:
names (List[str]): A list of name... |
python_sharegpt_23k | 1,780 | Write a Python function to remove all extra whitespace from a given string, ensuring that there is exactly one whitespace character between each pair of words, and no leading or trailing whitespace. The function should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the input str... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_extra_whitespace(sentence: str) -> str:
"""
Removes all extra whitespace from the given string, ensuring that there is exactly one whitespace character between each pair of words, and no leading or trailing whitespace... |
python_sharegpt_23k | 1,781 | Write a Python function to generate a sorted array of size `n` (where `n` is a positive integer) filled with unique random integers ranging from 0 to 100. The function should also find and return the median of the array. The algorithm must have a time complexity of O(nlogn) and a space complexity of O(n). Additionally,... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
from typing import List, Union
def generate_sorted_array_with_median(n: int) -> Union[List[int], float]:
"""
Generates a sorted array of size n filled with unique random integers from 0 to 100,
and finds the m... |
python_sharegpt_23k | 1,787 | Write a Python function to sort a list of dictionaries representing people by their age in descending order. If two or more people have the same age, they should be sorted by their names in ascending order. Additionally, the function should calculate and return the average age of all the people in the list.
The code s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Tuple
def sort_and_average_people(people: List[Dict[str, any]]) -> Tuple[List[Dict[str, any]], float]:
"""
Sorts a list of dictionaries representing people by their age in descending order.
If... |
python_sharegpt_23k | 1,790 | Write a Python function to sort a large array of integers in ascending order using an efficient algorithm that ensures a time complexity less than O(n^2) and a space complexity less than O(n). The algorithm should handle arrays containing duplicate integers correctly without sacrificing the time and space complexity re... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def efficient_sort(arr: List[int]) -> List[int]:
"""
Sorts a large array of integers in ascending order using an efficient algorithm.
The algorithm ensures a time complexity less than O(n^2) and a sp... |
python_sharegpt_23k | 1,793 | Write a Python function to square each element of a given list without modifying the original list. The function should return a new list containing the squared elements. The code should start with:
```python
from typing import List
def square_list_elements(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 square_list_elements(numbers: List[int]) -> List[int]:
"""
Returns a new list containing the squared elements of the input list without modifying the original list.
Args:
numbers (List[i... |
python_sharegpt_23k | 1,796 | Write a Python function to sort all strings in a list alphabetically and remove any duplicate strings from the list. Each string in the list should only contain lowercase letters and have a length between 1 and 100 characters. The input list should have at most 10^6 strings.
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 sort_and_remove_duplicates(strings: List[str]) -> List[str]:
"""
Sorts all strings in a list alphabetically and removes any duplicate strings.
Args:
strings (List[str]): A list of string... |
python_sharegpt_23k | 1,797 | Write a Python function to flatten a 2-dimensional array of integers and remove all duplicate elements. The resulting array should be sorted in ascending order. If the input 2-dimensional array is empty, the function should return an empty array. The solution must have a time complexity of O(n log n), where n is the to... | 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 | 1,799 | Write a Python function to find the smallest number in a given list of numbers without using any built-in functions or methods such as `min()`, `sorted()`, or `sort()`. The function should have a time complexity of O(n), where n is the length of the list. The list may contain negative numbers, decimals, and duplicates.... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_number(numbers: List[float]) -> float:
"""
Find the smallest number in a given list of numbers without using built-in functions or methods.
Args:
numbers (List[float]): A l... |
python_sharegpt_23k | 1,802 | Write a Python function to convert a given time from Central Daylight Time (CDT) to Eastern Standard Time (EST). The function should handle the conversion by adding 1 hour to the given time, as CDT is 1 hour ahead of EST. If the resulting time exceeds 24:00, it should wrap around to the next day. The function should re... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_cdt_to_est(cdt_time: str) -> str:
"""
Convert a given time from Central Daylight Time (CDT) to Eastern Standard Time (EST).
Args:
cdt_time (str): The time in Central Daylight Time in the format "HH:MM".
... |
python_sharegpt_23k | 1,805 | Write a Python function to calculate the sum of all odd numbers and the count of odd numbers in a given array of positive integers. The function should meet the following requirements:
- It should have a time complexity of O(n), where n is the number of elements in the array.
- It should have a space complexity of O(1)... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sum_and_count_odd_numbers(arr: List[int]) -> Tuple[int, int]:
"""
Calculate the sum of all odd numbers and the count of odd numbers in a given array of positive integers.
Args:
ar... |
python_sharegpt_23k | 1,809 | Write a Python function to sort an array in descending order without using any built-in sorting functions or methods. The function should use basic operations such as loops, conditional statements, and variable assignments. The input array will contain integers ranging from -1000000 to 1000000, and its length will be b... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_array_descending(arr: List[int]) -> List[int]:
"""
Sorts the input array in descending order using an in-place merge sort algorithm.
Args:
arr (List[int]): The input array of intege... |
python_sharegpt_23k | 1,810 | Write a Python function to find and return a list of all prime numbers between a given range `[start, end]`. The function should use a nested while loop to check for prime numbers. The code should start with:
```python
from typing import List
def find_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 find_primes_in_range(start: int, end: int) -> List[int]:
"""
Find and return a list of all prime numbers between the given range [start, end].
Args:
start (int): The starting number of... |
python_sharegpt_23k | 1,815 | Write a Python function that merges two given dictionaries into one, adhering to the following rules:
- If a key is present in both dictionaries, the corresponding values should be multiplied together.
- If a key is present in only one dictionary, the corresponding value should be squared before being added to the merg... | Here is the Python code to solve the given problem in the markdown block:
```python
def merge_dictionaries(dict1: dict, dict2: dict) -> dict:
"""
Merges two dictionaries into one with specific rules:
- If a key is present in both dictionaries, the corresponding values are multiplied.
- If a key is prese... |
python_sharegpt_23k | 1,817 | Write a Python function to count the occurrences of each character in a given input string, including special characters, whitespace, and punctuation marks. The function should handle strings with a length of up to 10 million characters efficiently. The code should start with:
```python
from typing import Dict
def cou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def count_characters(input_string: str) -> Dict[str, int]:
"""
Counts the occurrences of each character in the given input string, including special characters, whitespace, and punctuation marks.
Ar... |
python_sharegpt_23k | 1,821 | Write a Python function to calculate the sum of all elements in each row of a given matrix and return the sum of all the row sums. The matrix is represented as a list of lists, where each inner list represents a row in the matrix. The matrix may have varying number of elements in each row.
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 sum_of_row_sums(matrix: List[List[int]]) -> int:
"""
Calculate the sum of all elements in each row of a given matrix and return the sum of all the row sums.
Args:
matrix (List[List[int]]... |
python_sharegpt_23k | 1,827 | Write a Python function to generate the Fibonacci sequence up to the nth number using an iterative approach with optimized time and space complexity. The function should have a time complexity of O(n) and a space complexity of O(1), where n is the input number. The function should return a list containing the Fibonacci... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the nth number using an iterative approach with optimized time and space complexity.
Args:
n (int): The... |
python_sharegpt_23k | 1,832 | Write a Python function to find the ASCII code of the character 'X' in a given string. The function should have a time complexity of O(n), where n is the length of the string, and should not use any built-in functions or libraries for ASCII code conversion. Additionally, the function should handle strings with a length... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_ascii_code(string: str) -> int:
"""
Find the ASCII code of the character 'X' in the given string.
Args:
string (str): The input string to search for the character 'X'.
Returns:
int: The ASCII c... |
python_sharegpt_23k | 1,837 | Write a Python function to identify the capital of a given country based on the country's ISO 3166-1 alpha-2 country code. The function should handle multiple country codes as input and return the capitals in a sorted list. Additionally, the function should account for countries with multiple capitals and provide the m... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_capitals(country_codes: List[str]) -> List[str]:
"""
Identify the capitals of given countries based on their ISO 3166-1 alpha-2 country codes.
Args:
country_codes (List[str]): A list... |
python_sharegpt_23k | 1,843 | Write a Python function to calculate the sum of each row in a 2D array, considering only positive integers and ignoring negative numbers. The function should handle cases where the array may contain empty subarrays and return 0 as the sum for those rows. The input array will always have at least 1 row and each row will... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def row_sums_with_positive_only(arr: List[List[int]]) -> List[int]:
"""
Calculate the sum of each row in a 2D array, considering only positive integers and ignoring negative numbers.
Handle cases whe... |
python_sharegpt_23k | 1,847 | Write a Python function to print the square of each odd number between a given range `[start, end]`, excluding a specified number `exclude`. Additionally, print the sum of all the squares at the end of the loop. The function should start with:
```python
def print_odd_squares_and_sum(start: int, end: int, exclude: int) ... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_odd_squares_and_sum(start: int, end: int, exclude: int) -> None:
"""
Prints the square of each odd number between `start` and `end` (inclusive), excluding `exclude`.
Additionally, prints the sum of all the squares ... |
python_sharegpt_23k | 1,849 | Write a Python function to join two dictionaries by multiplying the values with the same key. The result should be a new dictionary where the keys are the intersection of the keys from the two input dictionaries, and the values are the product of the corresponding values from the two input dictionaries. If a key is pre... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def join_dictionaries(d1: Dict[str, int], d2: Dict[str, int]) -> Dict[str, int]:
"""
Joins two dictionaries by multiplying the values with the same key.
Args:
d1 (Dict[str, int]): The first ... |
python_sharegpt_23k | 1,856 | Write a Python function to generate and print the multiplication tables for prime numbers within a given range. The function should take two integers, `start` and `end`, as input and print the multiplication tables for all prime numbers in that range. The multiplication table for each prime number should display the pr... | Here is the Python code to solve the given problem in the markdown block:
```python
def print_prime_multiplication_tables(start: int, end: int) -> None:
"""
Prints the multiplication tables for all prime numbers within the given range [start, end].
Args:
start (int): The starting number of the rang... |
python_sharegpt_23k | 1,858 | Write a Python function to find the common elements between two lists of integers. The function should return a list of integers that are present in both input lists. The function must have a time complexity of O(n), where n is the length of the longer list. Additionally, the function should handle the case when one or... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Find the common elements between two lists of integers.
Args:
list1 (List[int]): The first list of integers.
... |
python_sharegpt_23k | 1,860 | Write a Python function to determine if a given number is a prime number or not. The function should have a time complexity of O(sqrt(n)) and a space complexity of O(1). The function should not use any built-in functions or libraries to determine if a number is prime.
The code should start with:
```python
def is_prime... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(n: int) -> bool:
"""
Determine if a given number is a prime number.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
"""
... |
python_sharegpt_23k | 1,862 | Write a Python function called `sum_of_two_numbers` that takes in two numbers, `a` and `b`, as parameters. The function should return the sum of the two numbers, but only if both numbers are positive integers. If any of the numbers are negative or not integers, the function should raise a `ValueError` with the message ... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_two_numbers(a: int, b: int) -> int:
"""
Calculate the sum of two numbers, but only if both numbers are positive integers.
Args:
a (int): The first number.
b (int): The second number.
Returns:... |
python_sharegpt_23k | 1,873 | Write a Python function to process a given string and count the occurrences of each unique word that is at least 4 characters long, while ignoring punctuation marks. The function should have a time complexity of O(n) and a space complexity of O(m), where n is the length of the string and m is the number of unique words... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
import re
def count_long_words(string: str) -> Dict[str, int]:
"""
Counts the occurrences of each unique word in the given string that is at least 4 characters long,
while ignoring punctuation marks.... |
python_sharegpt_23k | 1,878 | Write a Python function to reverse the order of characters in a given string without using any built-in string manipulation functions or libraries. The function should use a loop or iteration to achieve this. Additionally, you are not allowed to create any new variables or data structures, and you should handle edge ca... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverses the order of characters in a given string without using any built-in string manipulation functions or libraries.
Args:
string (str): The input string to be revers... |
python_sharegpt_23k | 1,884 | Write a Python function to find all prime numbers within a given range [n, m) (i.e., from n to m, not including m). The function should take two positive integers, n and m, as parameters and return a list of all prime numbers in that range. The function should also ensure that n is less than m. If n is not less than m,... | 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 | 1,886 | Write a Python function to generate a list of numbers from 0 to 99 using nested loops. The outer loop should iterate 10 times, and the inner loop should iterate 10 times within each iteration of the outer loop. The function should return a list containing all the numbers generated by the nested loops.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_numbers() -> List[int]:
"""
Generate a list of numbers from 0 to 99 using nested loops.
The outer loop iterates 10 times, and the inner loop iterates 10 times within each iteration of t... |
python_sharegpt_23k | 1,889 | Implement the quicksort algorithm to sort an array of integers in ascending order using the Hoare partition scheme. The implementation should meet the following requirements:
1. Use a random element as the pivot for partitioning.
2. Ensure a worst-case time complexity of O(n log n).
3. Ensure a space complexity of O(lo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def quicksort(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in ascending order using the quicksort algorithm with the Hoare partition scheme.
Args:
arr (List[int... |
python_sharegpt_23k | 1,895 | Write a Python function that processes a list of strings in reverse order, converting each non-empty string to uppercase and printing it. The function should not use any built-in Python functions or methods to reverse the list or convert the elements to uppercase. The function should iterate through the list only once.... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def process_list_in_reverse(my_list: List[str]) -> None:
"""
Processes a list of strings in reverse order, converting each non-empty string to uppercase and printing it.
Args:
my_list (List[... |
python_sharegpt_23k | 1,896 | Write a Python function to remove duplicates from a given array while preserving the order of the elements. The array may contain integers, strings, or a combination of both. The function should handle large input arrays efficiently and maintain a time complexity of O(n) or less.
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, Union
def remove_duplicates_preserve_order(array: List[Union[int, str]]) -> List[Union[int, str]]:
"""
Removes duplicates from the given array while preserving the order of the elements.
Args:
... |
python_sharegpt_23k | 1,897 | Write a Python function to calculate the sum of the values in a given dictionary, where the keys are strings and the values are numbers. The function should ignore keys that contain any uppercase letters, special characters, or numbers. The code should start with:
```python
from typing import Dict
def sum_valid_values... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sum_valid_values(dictionary: Dict[str, int]) -> int:
"""
Calculate the sum of the values in a dictionary where the keys are strings and the values are numbers.
Ignore keys that contain any upperc... |
python_sharegpt_23k | 1,904 | Write a Python function to determine if an array is sorted in a given order (either ascending or descending) without using comparison operators (such as `>`, `<`, `==`). The function should only use arithmetic operations and bitwise operators. The time complexity of the function should be O(n), where n is the number of... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_sorted_without_comparisons(array: List[int]) -> bool:
"""
Determines if an array is sorted in either ascending or descending order without using comparison operators.
Args:
array (Lis... |
python_sharegpt_23k | 1,906 | Write a Python function to count the number of lines of actual code in a given string of code, excluding empty lines, comments, and inline comments. The function should handle nested comments and be efficient with a time complexity of O(N), where N is the number of characters in the code.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_lines_of_code(code: str) -> int:
"""
Count the number of lines of actual code in a given string of code, excluding empty lines, comments, and inline comments.
Args:
code (str): The input code string to be ... |
python_sharegpt_23k | 1,907 | Write a Python function to simulate a task scheduler that processes tasks based on their priority. Each task is represented by a tuple containing a priority value (an integer) and a task description (a string). The scheduler should process tasks in ascending order of their priority, i.e., the task with the lowest prior... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import heapq
def task_scheduler(tasks: List[Tuple[int, str]]) -> List[str]:
"""
Simulate a task scheduler that processes tasks based on their priority.
Args:
tasks (List[Tuple[int, st... |
python_sharegpt_23k | 1,908 | Write a Python function to return the kth element in the Fibonacci sequence with a time complexity of O(log n) and without using recursion. The solution should be space efficient and should not use any extra data structures apart from a constant amount of variables. The function should handle edge cases such as negativ... | Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci(k: int) -> int:
"""
Returns the kth element in the Fibonacci sequence using matrix exponentiation.
Args:
k (int): The position in the Fibonacci sequence to return.
Returns:
int: The kth Fi... |
python_sharegpt_23k | 1,909 | Write a Python function to sort a list of integers in descending order using a stable, in-place merge sort algorithm. The function should have a time complexity of O(n log n) and a space complexity of O(1). The function should not use any built-in sorting functions or libraries.
The code should start with:
```python
f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_descending(arr: List[int]) -> None:
"""
Sorts the given list of integers in descending order using a stable, in-place merge sort algorithm.
Args:
arr (List[int]): The list of ... |
python_sharegpt_23k | 1,910 | Write a Python function to validate if a given integer is a prime number greater than 100 and is divisible by 7. The function should return `True` if the conditions are met, otherwise `False`.
The code should start with:
```python
import math
def is_prime_and_divisible_by_seven(value: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime_and_divisible_by_seven(value: int) -> bool:
"""
Validate if the given integer is a prime number greater than 100 and divisible by 7.
Args:
value (int): The integer to be validated.
Ret... |
python_sharegpt_23k | 1,917 | Write a Python function to remove duplicate elements and all occurrences of the number 0 from a given array without using any built-in functions or data structures. The input array will contain positive and negative integers. The function should return a new array with the specified modifications.
The code should star... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_zeros(arr: List[int]) -> List[int]:
"""
Removes all occurrences of the number 0 and duplicates from the given array.
Args:
arr (List[int]): The input array containi... |
python_sharegpt_23k | 1,921 | Write a Python function to determine if all the elements in a given list of integers are unique without using any additional data structures. The function should have a time complexity of O(n^2) and a space complexity of O(1). The code should start with:
```python
from typing import List
def are_elements_unique(lst: L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def are_elements_unique(lst: List[int]) -> bool:
"""
Determine if all elements in the given list are unique without using any additional data structures.
Args:
lst (List[int]): The list of i... |
python_sharegpt_23k | 1,925 | Write a Python function to check whether a given array contains a specific element. The array can contain integers or floating-point numbers. The function should return `True` if the element is found in the array and `False` otherwise. The function must adhere to the following constraints:
- The array can have a maximu... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def contains_element(arr: List[Union[int, float]], element: Union[int, float]) -> bool:
"""
Checks whether the given array contains the specified element.
Args:
arr (List[Union[int, f... |
python_sharegpt_23k | 1,926 | Write a Python function to transpose a given 2D array of integers. The transposed array is obtained by swapping the rows and columns of the original array. The function should handle rectangular arrays (i.e., arrays where the number of rows and columns may not be equal).
The code should start with:
```python
from typi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def transpose_array(arr: List[List[int]]) -> List[List[int]]:
"""
Transposes a given 2D array of integers.
The transposed array is obtained by swapping the rows and columns of the original array.
... |
python_sharegpt_23k | 1,927 | Write a Python function to sort a list of integers in descending order using a recursive merge sort algorithm. The function should not use any built-in sorting functions or data structures other than basic list operations. The code should start with:
```python
from typing import List
def merge_sort_descending(arr: Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in descending order using a recursive merge sort algorithm.
Args:
arr (List[int]): The list of integers t... |
python_sharegpt_23k | 1,929 | Write a Python function to remove all whitespaces from a given string while maintaining the original order of the characters. The solution should have a time complexity of O(n) and a space complexity of O(1), where n is the length of the string. Implement the solution without using any built-in string manipulation func... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_whitespace(string: str) -> str:
"""
Removes all whitespaces from the given string while maintaining the original order of characters.
Args:
string (str): The input string from which whitespaces are to be ... |
python_sharegpt_23k | 1,936 | Write a Python function to validate whether a given string is a valid IPv6 address. The function should handle the following cases:
1. The input string may contain leading or trailing whitespace characters, which should be ignored.
2. The function should recognize loopback addresses (starting with "::1").
3. The functi... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_valid_ipv6_address(address: str) -> bool:
"""
Validate whether a given string is a valid IPv6 address.
Args:
address (str): The string to be validated as an IPv6 address.
Returns:
bool... |
python_sharegpt_23k | 1,946 | Refactor the given code to create a single function `is_prime_number` that checks whether a given integer `num` is a prime number. The function should return `True` if the number is prime and `False` otherwise. The function should be optimized to have a time complexity of O(sqrt(n)), where n is the value of the input n... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime_number(num: int) -> bool:
"""
Determines whether a given number is a prime number.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False othe... |
python_sharegpt_23k | 1,955 | Write a Python function to filter out even numbers from a given list of integers and return a new list containing only the prime numbers. The function should use a lambda expression and a helper function to check if a number is prime. The code should start with:
```python
from typing import List
def filter_prime_numbe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(num: int) -> bool:
"""
Check if a number is prime.
Args:
num (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
if... |
python_sharegpt_23k | 1,958 | Write a Python function that accepts a list of integers and returns a list of the cubes of the positive integers, sorted in ascending order, without any duplicates. The function should skip any negative integers in the input list. The function should be optimized to have a time complexity of O(n), where n is the number... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def cube_positive_integers(nums: List[int]) -> List[int]:
"""
Returns a list of the cubes of the positive integers from the input list, sorted in ascending order, without duplicates.
Negative integer... |
python_sharegpt_23k | 1,964 | Write a Python function to find the product of all values in a given dictionary that are divisible by 3 and less than 100. If no values meet this condition, return -1. The function should start with:
```python
from typing import Dict
def find_product_of_values(dictionary: Dict[str, int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def find_product_of_values(dictionary: Dict[str, int]) -> int:
"""
Find the product of all values in the given dictionary that are divisible by 3 and less than 100.
If no values meet this condition, ... |
python_sharegpt_23k | 1,974 | Write a Python function to print out a countdown from 100 to 1, displaying only odd numbers that are also prime. Additionally, the function should calculate and display the sum of all the prime numbers found in the countdown. The function should start with:
```python
def countdown_odd_primes():
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def countdown_odd_primes():
"""
Prints a countdown from 100 to 1, displaying only odd numbers that are also prime.
Additionally, calculates and displays the sum of all the prime numbers found in the countdown.
Args:
... |
python_sharegpt_23k | 1,976 | Write a Python function to generate a random sequence of 20 unique numbers from the range 1 to 50, ensuring that the sum of the generated numbers is divisible by 7. The function should return the generated sequence as a list.
The code should start with:
```python
from typing import List
import random
def generate_seq... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_sequence() -> List[int]:
"""
Generates a random sequence of 20 unique numbers from the range 1 to 50, ensuring that the sum of the numbers is divisible by 7.
Returns:
... |
python_sharegpt_23k | 1,981 | Write a Python function to calculate the square root of a positive number without using any built-in function. The output should be rounded to the nearest integer. The algorithm should have a time complexity of O(log n) and a space complexity of O(1).
The code should start with:
```python
def sqrt_without_builtin(num:... | Here is the Python code to solve the given problem in the markdown block:
```python
def sqrt_without_builtin(num: int) -> int:
"""
Calculate the square root of a positive number without using any built-in function.
The output is rounded to the nearest integer.
Args:
num (int): The positive numb... |
python_sharegpt_23k | 1,983 | Write a Python function that checks if two numerical parameters are equal without using any comparison operators (e.g., `==`, `!=`, `<`, `>`), built-in functions (e.g., `equals()`, `compareTo()`), arithmetic operators (e.g., `+`, `-`, `*`, `/`), bitwise operators (e.g., `&`, `|`, `^`, `>>`, `<<`), or loops and recursio... | Here is the Python code to solve the given problem in the markdown block:
```python
def check_equality(a: int, b: int) -> bool:
"""
Checks if two numerical parameters are equal without using comparison, arithmetic, or bitwise operators,
and without using built-in functions, loops, or recursion.
Args:
... |
python_sharegpt_23k | 1,987 | Write a Python function to aggregate and calculate the mean of scores from a given array of dictionaries, grouped by the key 'region'. The function should exclude any scores below 8 and return the top 10 regions with the highest mean score.
The code should start with:
```python
from typing import List, Dict, Tuple
imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Tuple
import numpy as np
def top_10_regions_by_mean_score(array: List[Dict[str, any]]) -> List[Tuple[str, float]]:
"""
Aggregate and calculate the mean of scores from a given array of dictionaries... |
python_sharegpt_23k | 1,989 | Write a Python function to convert a given list of strings into a dictionary where the key is a string in the list, and the value is a list of indices where the string occurs in the list. The function should maintain the case sensitivity when counting the frequency. The function should have a time complexity of O(n), w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def string_indices(lst: List[str]) -> Dict[str, List[int]]:
"""
Converts a list of strings into a dictionary where the key is a string in the list,
and the value is a list of indices where the ... |
python_sharegpt_23k | 1,990 | Write a Python function to convert a given string to an all lowercase string without using any built-in string manipulation functions or methods such as `toLowerCase()`, `lower()`, or any similar ones. Additionally, you are not allowed to use any iteration or recursion in your algorithm. You should implement a purely f... | Here is the Python code to solve the given problem in the markdown block:
```python
def to_lowercase(s: str) -> str:
"""
Converts a given string to an all lowercase string without using any built-in string manipulation functions or methods.
Args:
s (str): The input string to be converted to lowerca... |
python_sharegpt_23k | 1,996 | Write a Python function to process a given string by converting it to all uppercase, removing any punctuation marks, counting the frequency of each word, and returning the words in descending order of their frequency. The function should return a list of tuples where each tuple contains a word and its frequency.
The c... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
import re
from collections import Counter
def process_text(text: str) -> List[Tuple[str, int]]:
"""
Processes the given text by converting it to uppercase, removing punctuation,
counting the f... |
python_sharegpt_23k | 2,008 | Write a Python function to determine if one string (`str_1`) is contained within another string (`str_2`). The function should be case-insensitive, ignore any leading or trailing whitespace, and also ignore any punctuation marks and special characters in both strings. The function should have a time complexity of O(n),... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_contained(str_1: str, str_2: str) -> bool:
"""
Determines if one string (`str_1`) is contained within another string (`str_2`).
The function is case-insensitive, ignores leading/trailing whitespace, and
... |
python_sharegpt_23k | 2,024 | Write a Python function to find and return a list of all prime numbers between 1 and a given integer N. The function should efficiently handle large values of N, up to 100,000. The function should also print each prime number as it is identified. The code should start with:
```python
from typing import List
def find_a... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def is_prime(num: int) -> bool:
"""
Check if a given number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime,... |
python_sharegpt_23k | 2,025 | Write a Python function to calculate the sum of the squares of all the numbers from 1 to a given integer `n`. The function should have a time complexity of O(n), where `n` is the given integer. Additionally, provide a piece of erroneous code within the function that, instead of calculating the sum of the squares, calcu... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_squares(n: int) -> int:
"""
Calculate the sum of the squares of all numbers from 1 to n.
Args:
n (int): The upper limit of the range (inclusive).
Returns:
int: The sum of the squares of all n... |
python_sharegpt_23k | 2,031 | Write a Python function to add an element to the beginning of a given array without using any built-in functions or methods that directly add elements to an array (e.g., `append`, `insert`, `push`, `unshift`). The function should ensure that the resulting array is modified in-place with a time complexity of O(1) and do... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def add_to_beginning(arr: List[int], element: int) -> None:
"""
Adds an element to the beginning of the given array without using any built-in functions or methods that directly add elements to an array.... |
python_sharegpt_23k | 2,032 | Write a Python function to list out the unique prime numbers in a given array and count the frequency of each unique prime number. The function should return the unique prime numbers and their frequencies as a dictionary. Additionally, the function should also calculate the sum of the prime numbers and return it as a s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Tuple
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 ot... |
python_sharegpt_23k | 2,034 | Write a Python function to create a deep copy of a multi-dimensional array using only list comprehension and without using any built-in functions or methods. The function should ensure that the deep copy maintains the original order of elements within each dimension of the array. The time complexity of the function sho... | Here is the Python code to solve the given problem in the markdown block:
```python
def deep_copy_multi_dimensional_array(arr):
"""
Creates a deep copy of a multi-dimensional array using only list comprehension.
Args:
arr (list): The multi-dimensional array to be deep copied.
Returns:
... |
python_sharegpt_23k | 2,035 | Write a Python function to concatenate two strings after removing any leading or trailing whitespace from each string. The function should handle cases where one or both of the input strings are null or empty, and return an appropriate error message. The time complexity of the function should be O(n), where n is the to... | Here is the Python code to solve the given problem in the markdown block:
```python
def concatenate_strings(str1: str, str2: str) -> str:
"""
Concatenates two strings after removing any leading or trailing whitespace from each string.
Handles cases where one or both of the input strings are null or empty by... |
python_sharegpt_23k | 2,040 | Write a Python function to capitalize the first character of each word in a given string, while preserving the rest of the characters in their original case. The input string may contain punctuation marks and multiple spaces between words. The function should handle these cases appropriately.
The code should start wit... | Here is the Python code to solve the given problem in the markdown block:
```python
def capitalize_words(string: str) -> str:
"""
Capitalizes the first character of each word in the given string while preserving the rest of the characters in their original case.
Args:
string (str): The input string... |
python_sharegpt_23k | 2,049 | Write a Python function to generate the first `n` Fibonacci numbers efficiently with a time complexity of O(n) and a space complexity of O(1), excluding the space required to store the output list. The function should not use recursion and should only use a constant amount of extra space.
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 efficiently with a time complexity of O(n) and a space complexity of O(1), excluding the space required to store th... |
python_sharegpt_23k | 2,054 | Write a Python function to reverse a given string without using any built-in string reversal functions or external libraries. The function should have a time complexity of O(n), where n is the length of the string, and should use constant space, i.e., it should not create any additional data structures with a size depe... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(s: str) -> str:
"""
Reverses the given string without using any built-in string reversal functions or external libraries.
Args:
s (str): The input string to be reversed.
Returns:
str:... |
python_sharegpt_23k | 2,058 | Write a Python function to find the minimum element in an array of positive integers without using any built-in functions or methods that directly provide the minimum element. The solution must have a time complexity of O(n) and must be solved in-place, without creating any additional data structures or variables apart... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_min_element(arr: List[int]) -> int:
"""
Find the minimum element in an array of positive integers without using any built-in functions or methods.
Args:
arr (List[int]): The array o... |
python_sharegpt_23k | 2,070 | Write a Python function to filter out prime numbers from a given list of integers. The function should have a time complexity of O(n^2), where n is the length of the input list. The function should start with:
```python
from typing import List
def get_prime_numbers(lst: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Check if a given number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise... |
python_sharegpt_23k | 2,073 | Write a Python function to loop through a given data structure containing dictionaries and extract the value of the "name" property from each dictionary. If the "name" property is missing, append "Unknown" to the result list. If the "name" property exists but is empty, append "Empty" to the result list. The function sh... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def print_names(data: List[Dict[str, str]]) -> List[str]:
"""
Extracts the "name" property from each dictionary in the given list and handles missing or empty "name" values.
Args:
data... |
python_sharegpt_23k | 2,074 | Write a Python function to find the highest and the lowest numbers in a list of positive integers. The function should also check if the highest number is divisible by the lowest number. If it is divisible, the function should return a tuple with the highest number as the first element and the lowest number as the seco... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional, Tuple
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 ... |
python_sharegpt_23k | 2,076 | Write a Python function to combine two dictionaries, `dict1` and `dict2`, into a new dictionary `combined_dict`. The function should handle various scenarios, including merging lists, recursively merging dictionaries, and raising a `TypeError` if the values for the same key are of different types. The function should s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, Any
def combine_dicts(dict1: Dict[Any, Any], dict2: Dict[Any, Any]) -> Dict[Any, Any]:
"""
Combines two dictionaries, dict1 and dict2, into a new dictionary combined_dict.
Args:
dict1 (Dict... |
python_sharegpt_23k | 2,082 | Write a Python function to filter a list of numbers by removing all odd numbers, keeping only even numbers that are divisible by 3, and sorting the remaining numbers in descending order. The final result should be a string where each number is separated by a comma and enclosed in square brackets. The function should ha... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_and_sort_numbers(numbers: List[int]) -> str:
"""
Filters a list of numbers to remove all odd numbers, keeps only even numbers divisible by 3,
sorts the remaining numbers in descending orde... |
python_sharegpt_23k | 2,084 | Write a Python function to find the minimum value and its index in a given list of numbers. The function should return a tuple containing the minimum value and its index. The code should start with:
```python
from typing import List, Tuple
def find_min_value_and_index(numbers: List[int]) -> Tuple[int, int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_min_value_and_index(numbers: List[int]) -> Tuple[int, int]:
"""
Find the minimum value and its index in a given list of numbers.
Args:
numbers (List[int]): The list of number... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.