source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 9,742 | Write a Python function that processes a list of integers according to the following rules:
1. Each odd number in the list should be multiplied by 5.
2. Each even number in the list should be replaced with its square.
3. The resulting list should maintain the order of the original list.
The function should start with:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def process_list(lst: List[int]) -> List[int]:
"""
Processes a list of integers by multiplying each odd number by 5 and replacing each even number with its square.
Args:
lst (List[int]): The... |
python_sharegpt_23k | 9,744 | Write a Python function to remove all non-alphanumeric characters from a given input string and return the remaining alphanumeric characters in reverse order. The function should handle any input string and return a string with only alphanumeric characters in reverse order.
The code should start with:
```python
import... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def reverse_alphanumeric_string(input_str: str) -> str:
"""
Removes all non-alphanumeric characters from the input string and returns the remaining alphanumeric characters in reverse order.
Args:
input_st... |
python_sharegpt_23k | 9,747 | Write a Python function to count the number of occurrences of a specified number in a given array of integers. The function should have a time complexity of O(n), where n is the length of the array.
The code should start with:
```python
from typing import List
def count_occurrences(arr: List[int], num: int) -> int:
`... | 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], num: int) -> int:
"""
Counts the number of occurrences of a specified number in a given array of integers.
Args:
arr (List[int]): The array of integers ... |
python_sharegpt_23k | 9,748 | Write a Python function to determine if a given number is a prime number efficiently with a time complexity of O(sqrt(n)), where n is the given number. The function should return `True` if the number is prime and `False` otherwise. The code should start with:
```python
import math
def is_prime_efficient(n: int) -> boo... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_prime_efficient(n: int) -> bool:
"""
Determine if a given number is a prime number efficiently with a time complexity of O(sqrt(n)).
Args:
n (int): The number to check for primality.
Returns... |
python_sharegpt_23k | 9,749 | Write a Python function to rank submissions based on a composite score that takes into account user ratings, date of submission, and the number of views. The function should prioritize recent submissions with higher ratings and a larger number of views. The function should return a list of submissions sorted by their c... | Here is the Python code to solve the given problem in the markdown block:
```python
from datetime import datetime
from typing import List, Dict
def rank_submissions(submissions: List[Dict[str, float]]) -> List[Dict[str, float]]:
"""
Rank submissions based on a composite score that takes into account user ratin... |
python_sharegpt_23k | 9,750 | Write a Python function to search for a given integer element in a list of integers. The function should return `True` if the element is found in the list and `False` otherwise. The function should handle edge cases such as an empty list or a list with a single element. The function should return the first occurrence o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def search_element(lst: List[int], element: int) -> bool:
"""
Searches for a given integer element in a list of integers.
Args:
lst (List[int]): The list of integers to search in.
el... |
python_sharegpt_23k | 9,752 | Write a Python function to remove duplicates from a given list while maintaining the original order of elements using only a single loop. The function should use a set to keep track of seen elements and a list to store the unique elements in the order they appear.
The code should start with:
```python
from typing impo... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_order(lst: List[int]) -> List[int]:
"""
Removes duplicates from the given list while maintaining the original order of elements.
Args:
lst (List[int]): The list from... |
python_sharegpt_23k | 9,757 | Write a Python function to determine if a given number is a prime number. The function should return `True` if the number is prime and `False` otherwise. The function should handle edge cases such as negative numbers, 0, and 1, which are not prime numbers. The code should start with:
```python
import math
def is_prime... | Here is the Python code to solve the given problem in the markdown block:
:
```python
import math
def is_prime(number: int) -> bool:
"""
Determine if a given number is a prime number.
Args:
number (int): The number to check for primality.
Returns:
bool: True if the number is prime, Fa... |
python_sharegpt_23k | 9,759 | Write a Python function to generate the first `n` Fibonacci numbers that are also prime numbers. The function should return a list of these prime Fibonacci numbers. The code should start with:
```python
from typing import List
def generate_prime_fibonacci_sequence(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Check if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
... |
python_sharegpt_23k | 9,762 | Write a Python function to sort an array of characters in alphabetic order with the following constraints:
1. The array must be sorted in ascending order.
2. The sorting should be case insensitive.
3. All vowels (a, e, i, o, u) should be placed before consonants.
4. The sorting algorithm should have a time complexity o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_vowel(char: str) -> bool:
"""
Helper function to determine if a character is a vowel.
Args:
char (str): The character to check.
Returns:
bool: True if the character is a ... |
python_sharegpt_23k | 9,763 | Write a Python function to merge two lists by alternating elements, ensuring that the merged list does not contain any duplicates, and then sort the merged list in ascending order. The function should start with:
```python
from typing import List
def merge_and_sort_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_and_sort_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merges two lists by alternating elements, removes duplicates, and sorts the merged list in ascending order.
Args:
... |
python_sharegpt_23k | 9,764 | Write a Python function to sort a given list of integers in ascending order using the bubble sort algorithm. The bubble sort algorithm works by repeatedly iterating through the list and swapping adjacent elements if they are in the wrong order. The process is repeated until the list is fully sorted. The code should sta... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the bubble sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
... |
python_sharegpt_23k | 9,766 | Write a Python function to generate a list of all even numbers from 1 to a given positive integer `n`, where `n` is greater than or equal to 10. The function should return a string of these even numbers separated by a space.
The code should start with:
```python
def generate_even_numbers(n: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def generate_even_numbers(n: int) -> str:
"""
Generate a string of all even numbers from 1 to n, separated by a space.
Args:
n (int): A positive integer greater than or equal to 10.
Returns:
str: A stri... |
python_sharegpt_23k | 9,768 | Implement a Python function to sort a given array in ascending order using the Merge Sort algorithm. The function should have a time complexity of O(n log n).
The code should start with:
```python
from typing import List
def merge_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 merge_sort(arr: List[int]) -> List[int]:
"""
Sorts the given array in ascending order using the Merge Sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
... |
python_sharegpt_23k | 9,778 | Write a Python function to find the longest palindromic substring in a given string. A palindromic substring is a substring that reads the same forwards and backwards. The function should return the longest such substring found in the input string.
The code should start with:
```python
def longest_palindromic_substrin... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_palindromic_substring(s: str) -> str:
"""
Find the longest palindromic substring in the given string.
Args:
s (str): The input string in which to find the longest palindromic substring.
Returns:
... |
python_sharegpt_23k | 9,782 | Write a Python function to swap key-value pairs in a dictionary, but only for keys that are strings and values that are integers. The function should return a new dictionary with the swapped pairs and retain the original pairs for keys that are not strings or values that are not integers.
The code should start with:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def swap_key_value_pairs(dictionary: Dict) -> Dict:
"""
Swaps key-value pairs in a dictionary only for keys that are strings and values that are integers.
Args:
dictionary (Dict): The origin... |
python_sharegpt_23k | 9,784 | Implement the Quick Sort algorithm to sort a given array of integers. The function should be able to handle arrays of varying lengths and should return the sorted array. The code should start with:
```python
from typing import List
def quick_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 quick_sort(arr: List[int]) -> List[int]:
"""
Sorts the given array of integers using the Quick Sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
Returns:
... |
python_sharegpt_23k | 9,787 | Write a Python function to find the maximum and minimum values in an array of numbers with a time complexity of O(n), where n is the number of elements in the array. The function should not use any built-in functions for finding the maximum and minimum values. Additionally, the function should handle arrays with duplic... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_min(arr: List[int]) -> Tuple[int, int]:
"""
Find the maximum and minimum values in an array of integers with a time complexity of O(n).
Args:
arr (List[int]): The array o... |
python_sharegpt_23k | 9,789 | Write a Python function to find the top 3 values of a given array in descending order without using any built-in sorting functions or libraries. The function should iterate through the array to find the maximum values and remove them from the array to find the next maximum.
The code should start with:
```python
from t... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def top_3_values(nums: List[int]) -> List[int]:
"""
Find the top 3 values of a given array in descending order without using any built-in sorting functions or libraries.
Args:
nums (List[int... |
python_sharegpt_23k | 9,791 | Write a Python function to sort an array of integers from largest to smallest using a merge sort algorithm. The function should ensure that the time complexity of the sorting algorithm is O(n log n) or better. The code should start with:
```python
from typing import List
def 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 sort_descending(arr: List[int]) -> List[int]:
"""
Sorts an array of integers from largest to smallest using a merge sort algorithm.
Args:
arr (List[int]): The array of integers to be sor... |
python_sharegpt_23k | 9,792 | Write a Python function to insert a specific element at the beginning of a fixed-length array and shift all other elements to the right. The array should have a fixed length of 10 and should only store integers between 1 and 100 (inclusive). If the array is already full, the oldest element should be removed before inse... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insert_element_at_beginning(arr: List[int], element: int) -> List[int]:
"""
Inserts a specific element at the beginning of a fixed-length array and shifts all other elements to the right.
The arr... |
python_sharegpt_23k | 9,801 | Write a Python function to count the number of vowels (both uppercase and lowercase) in a given string. The function should be case-insensitive and should return the total count of vowels present in the string.
The code should start with:
```python
def count_vowels(string: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_vowels(string: str) -> int:
"""
Counts the number of vowels (both uppercase and lowercase) in a given string.
Args:
string (str): The input string in which to count the vowels.
Returns:
int: T... |
python_sharegpt_23k | 9,804 | Write a Python function to generate the powerset of a given set of elements without using recursion. The powerset is the set of all subsets, including the empty set and the set itself. The function should be efficient and handle sets of varying sizes.
The code should start with:
```python
from typing import List
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_powerset(elements: List[int]) -> List[List[int]]:
"""
Generate the powerset of a given set of elements without using recursion.
Args:
elements (List[int]): The list of elements ... |
python_sharegpt_23k | 9,808 | Write a Python function to find the k-th smallest element in a given list of integers. The function should efficiently handle the task without sorting the entire list. The code should start with:
```python
from typing import List
def find_kth_smallest(numbers: List[int], k: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
import heapq
def find_kth_smallest(numbers: List[int], k: int) -> int:
"""
Find the k-th smallest element in a list of integers without sorting the entire list.
Args:
numbers (List[int]): ... |
python_sharegpt_23k | 9,809 | Write a Python function to perform vector addition for two 1D arrays. The function should handle cases where the lengths of the two arrays are not equal by padding the shorter array with zeros. The function should return the resulting array after performing the addition.
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 vector_addition(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Perform vector addition for two 1D arrays. If the lengths of the arrays are not equal,
pad the shorter array with zeros to make... |
python_sharegpt_23k | 9,811 | Write a Python function to replace all consecutive whitespaces in a given string with a single space, while also ignoring any whitespaces at the beginning or end of the string. The function should use regular expressions to achieve this.
The code should start with:
```python
import re
def replace_consecutive_whitespa... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def replace_consecutive_whitespaces(string: str) -> str:
"""
Replaces all consecutive whitespaces in a given string with a single space,
while also ignoring any whitespaces at the beginning or end of the string.
... |
python_sharegpt_23k | 9,813 | Write a Python function to determine if a given string contains a valid US phone number. The phone number must be in the format "(XXX) XXX-XXXX" and the first digit of the area code cannot be 0 or 1. The function should return `True` if the phone number is valid and `False` otherwise.
The code should start with:
```py... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_valid_us_phone_number(phone_number: str) -> bool:
"""
Determine if a given string is a valid US phone number in the format "(XXX) XXX-XXXX"
with the additional constraint that the first digit of the area co... |
python_sharegpt_23k | 9,818 | Write a Python function to generate a random string of a specified length `n`, where `n` is a positive integer less than or equal to 100. The string should consist of a mix of uppercase letters, lowercase letters, and digits. The function should handle invalid input gracefully by returning a specific error message.
Th... | Here is the Python code to solve the given problem in the markdown block:
```python
import random
import string
def generate_random_string(n: int) -> str:
"""
Generate a random string of length n consisting of uppercase letters, lowercase letters, and digits.
Args:
n (int): The length of the rando... |
python_sharegpt_23k | 9,821 | Write a Python function that counts up to a given number and returns a list of all numbers in that range. The function should start with:
```python
from typing import List
def count_up_to(number: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_up_to(number: int) -> List[int]:
"""
Counts up to the given number and returns a list of all numbers in that range.
Args:
number (int): The number up to which to count.
Return... |
python_sharegpt_23k | 9,824 | Write a Python function to sort an array in descending order using the bubble sort algorithm. The function should repeatedly pass through the array, swapping adjacent elements if they are in the wrong order, until the array is fully sorted. The function should return the sorted array.
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 bubble_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts the given array in descending order using the bubble sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Re... |
python_sharegpt_23k | 9,825 | Write a Python function to find the index of a target integer in an array of integers with a time complexity requirement of O(n^2), where n is the length of the array. If the target integer is not found in the array, the function should return -1.
The code should start with:
```python
from typing import List
def find... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_index_with_quadratic_time(arr: List[int], target: int) -> int:
"""
Find the index of the target integer in the array with a time complexity of O(n^2).
Args:
arr (List[int]): The arr... |
python_sharegpt_23k | 9,829 | Write a Python function to find and return a list of odd numbers between a given range `[start, end]` that are divisible by a specified divisor and greater than a specified threshold. The function should handle edge cases such as invalid ranges or divisors.
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 find_odd_numbers_divisible_by(start: int, end: int, divisor: int, threshold: int) -> List[int]:
"""
Find and return a list of odd numbers between `start` and `end` (inclusive) that are divisible by `... |
python_sharegpt_23k | 9,830 | Write a Python function to generate a 2D array of dimensions `[N, M]` filled with random numbers between 0 and 1, and then calculate the sum of all the numbers in the array. The function should return both the generated array and the sum.
The code should start with:
```python
from typing import Tuple
import numpy as n... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Tuple
import numpy as np
def generate_random_array_and_sum(N: int, M: int) -> Tuple[np.ndarray, float]:
"""
Generates a 2D array of dimensions [N, M] filled with random numbers between 0 and 1,
and calcul... |
python_sharegpt_23k | 9,836 | Write a Python function to calculate the sum of all elements in a given list of integers. The list can contain both positive and negative integers. The function should handle an empty list by returning 0. The code should start with:
```python
from typing import List
def sum_of_list_elements(num_list: List[int]) -> int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_list_elements(num_list: List[int]) -> int:
"""
Calculate the sum of all elements in a given list of integers.
Args:
num_list (List[int]): The list of integers to sum.
Returns... |
python_sharegpt_23k | 9,837 | Write a Python function to check if all items in a given list are unique without using any built-in functions or data structures like sets, dictionaries, or list comprehensions. The function should iterate through the list and compare each element with every other element to determine if there are any duplicates.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def are_items_unique(lst: List[int]) -> bool:
"""
Check if all items in the given list are unique.
Args:
lst (List[int]): The list of integers to check for uniqueness.
Returns:
... |
python_sharegpt_23k | 9,839 | Write a Python function to sort an array of integers in descending order using a modified Bubble Sort algorithm. The function should take a list of integers as input and return the sorted list in descending order. The code should start with:
```python
from typing import List
def bubble_sort_descending(arr: List[int]) ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def bubble_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts an array of integers in descending order using a modified Bubble Sort algorithm.
Args:
arr (List[int]): The list of integer... |
python_sharegpt_23k | 9,840 | Write a Python function that takes in an array of integers and returns a string with each number in reverse order, separated by a comma. The function should not modify the original array and should handle edge cases such as an empty array or an array with a single element.
The code should start with:
```python
from ty... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_and_stringify(arr: List[int]) -> str:
"""
Converts an array of integers into a string with each number in reverse order, separated by a comma.
Args:
arr (List[int]): The array of... |
python_sharegpt_23k | 9,841 | Write a Python function to filter an array of strings and return a new array containing only the strings that start with the letter "a". If there are no strings starting with "a", return an empty array. The function should be case-insensitive, meaning it should consider both "a" and "A" as valid starting letters.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_strings_starting_with_a(strings: List[str]) -> List[str]:
"""
Filters an array of strings and returns a new array containing only the strings that start with the letter "a" (case-insensitive).... |
python_sharegpt_23k | 9,843 | Write a Python function to find the index of the first occurrence of the smallest common element in two given arrays of integers. If there is no common element, return -1. The function should start with:
```python
from typing import List
def find_first_smallest_common_index(array1: List[int], array2: List[int]) -> int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_first_smallest_common_index(array1: List[int], array2: List[int]) -> int:
"""
Find the index of the first occurrence of the smallest common element in two arrays.
Args:
array1 (List... |
python_sharegpt_23k | 9,846 | Write a Python function to calculate the sum of each row in a given matrix and return a list of these sums. The matrix is represented as a list of lists, where each inner list represents a row. The function should handle matrices of any size, including empty matrices.
The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def row_sums(matrix: List[List[int]]) -> List[int]:
"""
Calculate the sum of each row in a given matrix and return a list of these sums.
Args:
matrix (List[List[int]]): A 2D list representin... |
python_sharegpt_23k | 9,850 | Write a Python function that takes two arguments: a list of integers and an integer. The function should add the given integer to each element of the list and return the new list. The function should have a time complexity of O(n), where n is the length of the list. The code should start with:
```python
from typing imp... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def add_integer_to_list(lst: List[int], integer: int) -> List[int]:
"""
Adds a given integer to each element of a list of integers and returns the new list.
Args:
lst (List[int]): The list o... |
python_sharegpt_23k | 9,852 | Write a Python function to find the first occurrence of a specified element in a given list or string, and return its index. If the element is not found, return -1. Additionally, write a function to calculate the length of a given list or string.
The code should start with:
```python
from typing import Union
def find... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Union
def find_index(data: Union[list, str], element) -> int:
"""
Find the index of the first occurrence of the specified element in the given list or string.
If the element is not found, return -1.
... |
python_sharegpt_23k | 9,854 | Write a Python function to create a matrix of a given shape (rows x columns) and initialize all its elements to a specified value. The function should return the constructed matrix.
The code should start with:
```python
from typing import List
def create_matrix(rows: int, cols: int, value: int) -> List[List[int]]:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def create_matrix(rows: int, cols: int, value: int) -> List[List[int]]:
"""
Create a matrix of the given shape (rows x columns) and initialize all its elements to the specified value.
Args:
... |
python_sharegpt_23k | 9,856 | Implement a function to perform Bubble Sort on an array of integers with a time complexity of O(n^2) and a space complexity of O(1). The function should sort the array in ascending order. 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 an array of integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 9,858 | Write a Python function to determine whether two given arrays are equal or not without using any built-in comparison functions. The function should compare the arrays element by element and return `True` if they are equal and `False` otherwise. The code should start with:
```python
from typing import List
def are_arra... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def are_arrays_equal(arr1: List[int], arr2: List[int]) -> bool:
"""
Determine whether two given arrays are equal by comparing their elements without using built-in comparison functions.
Args:
... |
python_sharegpt_23k | 9,862 | Write a Python function to convert an octal number to a binary number using recursion. The function should take an octal number as input and return its binary representation as a string. The function should handle octal numbers of any length and should not use any built-in conversion functions.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
def octal_to_binary(octal: int) -> str:
"""
Convert an octal number to its binary representation using recursion.
Args:
octal (int): The octal number to be converted.
Returns:
str: The binary representa... |
python_sharegpt_23k | 9,868 | Write a Python function to search for the index of the first occurrence of a given integer `item` in a list of integers `items`. If the `item` is found, return its index; otherwise, return -1. The function should have a time complexity of O(n) or better, where n is the length of `items`.
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 search(items: List[int], item: int) -> int:
"""
Searches for the index of the first occurrence of a given integer in a list of integers.
Args:
items (List[int]): The list of integers to ... |
python_sharegpt_23k | 9,876 | Write a Python function to clean binary data by retaining only printable characters and converting any lowercase letters to uppercase. The function should take a binary string as input and return a cleaned string.
The code should start with:
```python
def clean_binary_data(binary_data: bytes) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def clean_binary_data(binary_data: bytes) -> str:
"""
Cleans binary data by retaining only printable characters and converting any lowercase letters to uppercase.
Args:
binary_data (bytes): The binary data to be cle... |
python_sharegpt_23k | 9,877 | Write a Python function to filter out non-string elements from a list of names and return a new list containing only the valid string names. If an element is not a string, it should be ignored and not included in the result. The function should handle any iterable containing elements of different types.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Iterable
def filter_names(names: Iterable[object]) -> List[str]:
"""
Filters out non-string elements from the given iterable and returns a list of valid string names.
Args:
names (Iterable[... |
python_sharegpt_23k | 9,881 | Write a Python function to sort the elements of a list based on their frequency in descending order. If multiple elements have the same frequency, they should be sorted in ascending order. The function should have a time complexity of O(n log n) and a space complexity of O(n).
The code should start with:
```python
fro... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_by_frequency(arr: List[int]) -> List[int]:
"""
Sorts the elements of a list based on their frequency in descending order.
If multiple elements have the same frequency, they are sorted in asc... |
python_sharegpt_23k | 9,883 | Write a Python function to calculate the average of all numbers in a given array. The function should handle arrays of any length and ensure that the input array is not empty. If the input array is empty, the function should return `None`.
The code should start with:
```python
from typing import List, Optional
def ca... | 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 all numbers in a given array.
Args:
numbers (List[int]): The array of integers for which ... |
python_sharegpt_23k | 9,884 | Write a Python function to sort a given array of positive integers in descending order using a recursive approach with O(n) time complexity and constant space complexity. The function should not use any additional arrays or data structures to aid in the sorting process.
The code should start with:
```python
from typin... | 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 a given array of positive integers in descending order using a recursive approach with O(n) time complexity and constant space complexity... |
python_sharegpt_23k | 9,885 | Write a Python function to iterate through a list of dictionaries and perform a specified operation on each dictionary's values, without using any built-in functions or methods like `for` loops, list comprehensions, or `map()`. The function should start with:
```python
from typing import List, Dict, Callable
def itera... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Callable
def iterate_dicts(data: List[Dict], operation: Callable[[str, int], None]) -> None:
"""
Iterates through a list of dictionaries and performs a specified operation on each dictionary's val... |
python_sharegpt_23k | 9,888 | Write a Python function to find and return all unique elements from a given array of integers, sorted in ascending order. The function should have a time complexity of O(n log n), where n is the size of the input array.
The code should start with:
```python
from typing import List
def find_unique_elements(arr: List[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_unique_elements(arr: List[int]) -> List[int]:
"""
Finds and returns all unique elements from a given array of integers, sorted in ascending order.
Args:
arr (List[int]): The input a... |
python_sharegpt_23k | 9,890 | Write a Python function called `string_operations` that takes in two parameters: a string called `string` and a boolean called `capitalize`. The function should replace all occurrences of the word 'old' with the word 'new' in the input string. Additionally, it should remove any whitespace characters from the right end ... | Here is the Python code to solve the given problem in the markdown block:
```python
def string_operations(string: str, capitalize: bool) -> str:
"""
Modifies the input string by replacing all occurrences of 'old' with 'new' and removing trailing whitespace.
If the 'capitalize' parameter is True, it also cap... |
python_sharegpt_23k | 9,892 | Write a Python function to calculate the Levenshtein distance between two strings without using any built-in string manipulation functions or libraries. The Levenshtein distance is defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the othe... | Here is the Python code to solve the given problem in the markdown block:
```python
def levenshtein_distance(word1: str, word2: str) -> int:
"""
Calculate the Levenshtein distance between two strings.
The Levenshtein distance is the minimum number of single-character edits
(insertions, deletions, or su... |
python_sharegpt_23k | 9,900 | Write a Python function to compute the sum of the first `n` even numbers and the first `n` odd numbers. The function should return a tuple containing the sum of the first `n` even numbers and the sum of the first `n` odd numbers.
The code should start with:
```python
def sum_of_first_n_even_and_odd(n: int) -> tuple:
`... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_first_n_even_and_odd(n: int) -> tuple:
"""
Compute the sum of the first n even numbers and the first n odd numbers.
Args:
n (int): The number of terms to sum.
Returns:
tuple: A tuple containi... |
python_sharegpt_23k | 9,901 | Write a Python function to count the total number of non-overlapping occurrences of a pattern `p` in a given string `S`. Both `S` and `p` will only contain lowercase letters. The function should start with:
```python
def count_non_overlapping_occurrences(S: str, p: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_non_overlapping_occurrences(S: str, p: str) -> int:
"""
Counts the total number of non-overlapping occurrences of a pattern `p` in a string `S`.
Args:
S (str): The string in which to search for the pattern... |
python_sharegpt_23k | 9,904 | Write a Python function to compute the n-th Fibonacci number using an iterative approach. The function should efficiently calculate the Fibonacci number without using recursion or memoization. The code should start with:
```python
def fibonacci_iterative(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def fibonacci_iterative(n: int) -> int:
"""
Compute the n-th Fibonacci number using an iterative approach.
Args:
n (int): The position in the Fibonacci sequence to compute.
Returns:
int: The n-th Fibona... |
python_sharegpt_23k | 9,905 | Write a Python function to check if a given substring is present in a string and return the starting index of the substring if it is found. If the substring is not found, return -1.
The code should start with:
```python
def find_substring_index(string: str, substring: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def find_substring_index(string: str, substring: str) -> int:
"""
Finds the starting index of a substring within a string.
Args:
string (str): The string in which to search for the substring.
substring (str)... |
python_sharegpt_23k | 9,906 | Write a Python function to find the common elements between two arrays of integers. The arrays may contain duplicate elements, and the common elements should be returned in ascending order without any duplicates. The time complexity of the solution should be less than O(n^2), where n is the length of the longer array.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(arr1: List[int], arr2: List[int]) -> List[int]:
"""
Find the common elements between two arrays of integers and return them in ascending order without duplicates.
Args:
... |
python_sharegpt_23k | 9,908 | Write a Python function to determine whether each number in a given list is a perfect square or not. Return a new list with boolean values corresponding to whether each number is a perfect square.
The code should start with:
```python
from typing import List
def is_perfect_square_list(numbers: List[int]) -> List[bool... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
import math
def is_perfect_square_list(numbers: List[int]) -> List[bool]:
"""
Determines whether each number in the given list is a perfect square.
Args:
numbers (List[int]): A list of int... |
python_sharegpt_23k | 9,909 | Write a Python function to classify all elements in an integer array as even or odd without using the modulus operator (%). Instead, use bitwise operations to determine the parity of each element. The function should return two lists: one containing all even numbers and the other containing all odd numbers.
The code s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def classify_parity(arr: List[int]) -> Tuple[List[int], List[int]]:
"""
Classify all elements in an integer array as even or odd using bitwise operations.
Args:
arr (List[int]): The i... |
python_sharegpt_23k | 9,910 | Write a Python function to remove all special characters from a given string, except for the '@' symbol. The function should preserve the '@' symbol and any alphanumeric characters (letters and digits) and spaces. The code should start with:
```python
import re
def remove_special_characters(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
import re
def remove_special_characters(string: str) -> str:
"""
Removes all special characters from the given string, except for the '@' symbol, alphanumeric characters, and spaces.
Args:
string (str): The input... |
python_sharegpt_23k | 9,911 | Write a Python function to generate n-grams from a given string of words. An n-gram is a contiguous sequence of n items from a given sample of text or speech. The function should take a string and an integer `gram_size` as input and return a list of n-grams. Each n-gram should consist of `gram_size` consecutive words f... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_ngrams(string: str, gram_size: int) -> List[str]:
"""
Generate n-grams from a given string of words.
Args:
string (str): The input string from which to generate n-grams.
... |
python_sharegpt_23k | 9,912 | Write a Python function to determine if a given number `n` can be expressed as the power of some other number. Specifically, the function should check if there exist two integers `a` and `b` (where `a > 1` and `b > 1`) such that `a^b = n`. If such integers exist, the function should return `True`; otherwise, it should ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def is_power_of_some_number(n: int) -> bool:
"""
Determine if a given number n can be expressed as a^b for integers a > 1 and b > 1.
Args:
n (int): The number to check.
Returns:
bool: True ... |
python_sharegpt_23k | 9,914 | Write a Python function to determine whether a given number is a palindrome. The function should adhere to the following constraints:
- The time complexity should be O(log n), where n is the number of digits in the input number.
- The space complexity should be O(1), meaning the function should use a constant amount of... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome_number(n: int) -> bool:
"""
Determine whether a given number is a palindrome.
Args:
n (int): The number to check.
Returns:
bool: True if the number is a palindrome, False otherwise.
... |
python_sharegpt_23k | 9,916 | Write a Python function to calculate the sum of all elements in a given two-dimensional array (matrix). The function should iterate through each element in the matrix and accumulate the sum. The code should start with:
```python
from typing import List
def sum_of_matrix(matrix: List[List[int]]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_matrix(matrix: List[List[int]]) -> int:
"""
Calculate the sum of all elements in a given two-dimensional array (matrix).
Args:
matrix (List[List[int]]): A two-dimensional array (m... |
python_sharegpt_23k | 9,919 | Write a Python function to find the maximum 3 elements from a given list of numbers without using any built-in sorting functions. The function should return a list containing the three largest elements in descending order.
The code should start with:
```python
from typing import List
def find_max_3_elements(lst: List... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_3_elements(lst: List[int]) -> List[int]:
"""
Find the maximum 3 elements from a given list of numbers without using any built-in sorting functions.
Args:
lst (List[int]): The li... |
python_sharegpt_23k | 9,923 | Write a Python function to determine if two given strings have the same length and do not contain any duplicate characters. The function should return `True` if both conditions are met, otherwise it should return `False`.
The code should start with:
```python
def check_strings(string1: str, string2: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def check_strings(string1: str, string2: str) -> bool:
"""
Determines if two strings have the same length and do not contain any duplicate characters.
Args:
string1 (str): The first string to be checked.
str... |
python_sharegpt_23k | 9,924 | Write a Python function to create a dictionary of a specified length where all keys are strings and all values are integers. The function should ensure that the keys are unique and the values are within a given range.
The code should start with:
```python
from typing import Dict, List
def create_dict_with_constraints... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
import random
import string
def create_dict_with_constraints(length: int, value_range: List[int]) -> Dict[str, int]:
"""
Creates a dictionary of the specified length where all keys are strings and ... |
python_sharegpt_23k | 9,927 | Write a Python function to generate a random grammatically correct sentence based on a given set of nouns, pronouns, verbs, and objects. The sentence should be generated in constant time (O(1)) and should not require additional space beyond the input sets and the final sentence output (O(1)). The function should handle... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Set
import random
def generate_random_sentence(nouns: Set[str], pronouns: Set[str], verbs: Set[str], objects: Set[str]) -> str:
"""
Generates a random grammatically correct sentence based on the given sets of... |
python_sharegpt_23k | 9,934 | Write a Python function to transform each string in a given list to uppercase and then sort the list in descending order based on the length of the strings. The function should return the sorted list.
The code should start with:
```python
from typing import List
def transform_and_sort_strings(strings: List[str]) -> L... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def transform_and_sort_strings(strings: List[str]) -> List[str]:
"""
Transforms each string in the input list to uppercase and sorts the list in descending order based on the length of the strings.
... |
python_sharegpt_23k | 9,935 | Write a Python function to generate a random string of length `n` using only uppercase letters and numbers. The function should ensure that the generated string contains at least one uppercase letter and one number. The code should start with:
```python
from typing import List
import random
import string
def generate_... | Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
import random
import string
def generate_secure_random_string(n: int) -> str:
"""
Generates a random string of length n using only uppercase letters and numbers.
The string is guaranteed to contain... |
python_sharegpt_23k | 9,936 | Write a Python function to combine two lists by summing their corresponding elements. The function should validate that both input lists contain only integers and have the same length. If the input lists are empty or have different lengths, or if they contain non-integer elements, the function should return an appropri... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Union
def combine_lists(list1: List[int], list2: List[int]) -> Union[List[int], str]:
"""
Combines two lists by summing their corresponding elements.
Args:
list1 (List[int]): The first list... |
python_sharegpt_23k | 9,937 | Write a Python function to implement the bubble sort algorithm to sort an array of integers in increasing order. The function should handle cases where the array is empty or has only one element. 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 an array of integers in increasing order using the bubble sort algorithm.
Args:
arr (List[int]): The array of integers to be sorted.
... |
python_sharegpt_23k | 9,946 | Write a Python function to count the occurrences of each unique word in a given string, ignoring punctuation and considering words in a case-insensitive manner. The function should return a dictionary where the keys are the unique words 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 re
from collections import Counter
def count_unique_words(text: str) -> Dict[str, int]:
"""
Count the occurrences of each unique word in a given string, ignoring punctuation and case sensitivity.
... |
python_sharegpt_23k | 9,947 | Write a Python function to sort a given list of tuples by the second element of each tuple in descending order. The function should take a list of tuples as input and return the sorted list. The code should start with:
```python
from typing import List, Tuple
def sort_tuples_by_second_element(tuples_list: List[Tuple[i... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_tuples_by_second_element(tuples_list: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
"""
Sorts a given list of tuples by the second element of each tuple in descending order.
Args:... |
python_sharegpt_23k | 9,950 | Write a Python function to find the minimum and maximum values among a list of integers and return the sum of their absolute differences. The function should start with:
```python
from typing import List
def sum_of_min_max_difference(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_min_max_difference(numbers: List[int]) -> int:
"""
Calculate the sum of the absolute difference between the minimum and maximum values in a list of integers.
Args:
numbers (List[i... |
python_sharegpt_23k | 9,955 | Write a Python function to validate an email address based on the following criteria:
1. The email address must have a minimum length of 5 characters and a maximum length of 50 characters.
2. It must contain exactly one "@" symbol.
3. The domain part (after the "@") must have at least 2 characters.
4. The email address... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def is_valid_email(email: str) -> bool:
"""
Validate an email address based on the following criteria:
1. The email address must have a minimum length of 5 characters and a maximum length of 50 characters.
2. ... |
python_sharegpt_23k | 9,956 | Write a Python function to calculate the factorial of a positive integer using recursion. The function should have a time complexity of O(n) and a space complexity of O(n). The code should start with:
```python
def factorial_recursive(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def factorial_recursive(n: int) -> int:
"""
Calculate the factorial of a positive integer using recursion.
Args:
n (int): The positive integer for which to calculate the factorial.
Returns:
int: The fac... |
python_sharegpt_23k | 9,959 | Write a Python function to iterate through a given list and print the value of each item in reverse order. The function should meet the following requirements:
- The time complexity of the solution should be O(n).
- The space complexity of the solution should be O(1).
The code should start with:
```python
from typing ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def print_list_reverse(my_list: List[int]) -> None:
"""
Prints the elements of the given list in reverse order.
Args:
my_list (List[int]): The list of integers to be printed in reverse order... |
python_sharegpt_23k | 9,960 | Write a Python function to generate a random sample of a specified size from a given distribution using `numpy`. The function should allow the user to specify the size of the sample, the mean and standard deviation of the distribution, and optionally set a seed for reproducibility. The code should start with:
```python... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def generate_random_sample(size: int, mean: float = 0.0, std_dev: float = 1.0, seed: int = None) -> np.ndarray:
"""
Generate a random sample of a specified size from a normal distribution.
Args:
... |
python_sharegpt_23k | 9,966 | Write a Python function to detect if a particular character is included in a string or not, without using any built-in functions or methods for string manipulation or character checking. The function should iterate through the string and compare each character manually.
The code should start with:
```python
def is_cha... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_character_in_string(string: str, character: str) -> bool:
"""
Detects if a particular character is included in a string without using built-in functions or methods.
Args:
string (str): The string to search wi... |
python_sharegpt_23k | 9,967 | Write a Python function to calculate the frequency of occurrence of each letter in a given sentence, taking into account punctuation marks and case sensitivity. The function should return a dictionary where the keys are the lowercase letters and the values are their respective frequencies.
The code should start with:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def letter_frequency(sentence: str) -> Dict[str, int]:
"""
Calculate the frequency of occurrence of each letter in a given sentence, ignoring punctuation marks and case sensitivity.
Args:
se... |
python_sharegpt_23k | 9,970 | Write a Python function to find the maximum and minimum numbers from a sequence of numbers. The sequence must contain at least 5 numbers and cannot contain any duplicates. The function should return a tuple containing the maximum and minimum numbers. If the sequence does not meet the criteria, the function should retur... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_min(sequence: List[int]) -> Tuple[int, int] or str:
"""
Finds the maximum and minimum numbers from a sequence of numbers.
Args:
sequence (List[int]): A list of integers c... |
python_sharegpt_23k | 9,972 | Write a Python function to find the maximum sum of a contiguous subarray within a given array of integers using a dynamic programming approach. The function should handle edge cases such as an empty array and arrays with all negative numbers.
The code should start with:
```python
from typing import List
def max_subar... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_subarray_sum(arr: List[int]) -> int:
"""
Find the maximum sum of a contiguous subarray within the given array of integers.
Args:
arr (List[int]): The array of integers.
Returns:... |
python_sharegpt_23k | 9,973 | Write a Python function to generate the Fibonacci sequence up to the nth number. The function should return a list containing the Fibonacci sequence. The code should start with:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_fibonacci(n: int) -> List[int]:
"""
Generate the Fibonacci sequence up to the nth number.
Args:
n (int): The number of Fibonacci numbers to generate.
Returns:
List[... |
python_sharegpt_23k | 9,975 | Write a Python function to reverse a list in place without using the built-in `reverse()` method or any other built-in functions that directly reverse a list. The function should use two pointers to swap elements from the beginning and the end of the list until the entire list is reversed.
The code should start with:
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def reverse_list_in_place(lst: List[int]) -> None:
"""
Reverses the given list in place without using the built-in reverse() method.
Args:
lst (List[int]): The list to be reversed.
Retu... |
python_sharegpt_23k | 9,977 | Write a Python function to calculate the number of different ways to climb `n` stairs, given that you can only climb 1, 2, or 3 stairs at a time. The function should be efficient and use dynamic programming to avoid redundant calculations.
The code should start with:
```python
def count_ways_to_climb_stairs(n: int) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_ways_to_climb_stairs(n: int) -> int:
"""
Calculate the number of different ways to climb n stairs, given that you can only climb 1, 2, or 3 stairs at a time.
Args:
n (int): The number of stairs to climb.
... |
python_sharegpt_23k | 9,978 | Write a Python function to add elements from one list to another list in reverse order. The function should take two lists as input and return the first list with the elements of the second list appended in reverse order.
The code should start with:
```python
from typing import List
def add_elements_in_reverse(list1:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def add_elements_in_reverse(list1: List[int], list2: List[int]) -> List[int]:
"""
Adds elements from the second list to the first list in reverse order.
Args:
list1 (List[int]): The list to ... |
python_sharegpt_23k | 9,979 | Write a Python program to analyze student scores across multiple subjects. The program should perform the following tasks:
1. Calculate the average score for each student across all subjects.
2. Calculate the average score for each subject across all students.
3. Determine the highest scoring student for each subject.
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def calculate_student_average(scores: Dict[str, List[int]]) -> Dict[str, float]:
"""
Calculate the average score for each student across all subjects.
Args:
scores (dict): A dictionary... |
python_sharegpt_23k | 9,983 | Write a Python function to remove duplicate elements from a given list while maintaining the order of the first occurrence of each element. The function should have a time complexity of O(n) and a space complexity of O(1). The code should start with:
```python
from typing import List
def remove_duplicates_in_order(num... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_in_order(nums: List[int]) -> List[int]:
"""
Removes duplicate elements from the given list while maintaining the order of the first occurrence of each element.
Args:
nu... |
python_sharegpt_23k | 9,986 | Write a Python function to filter out the unique odd numbers from a given list of integers. The function should return a list containing only the unique odd numbers in ascending order. The code should start with:
```python
from typing import List
def get_unique_odd_numbers(numbers: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def get_unique_odd_numbers(numbers: List[int]) -> List[int]:
"""
Filters out the unique odd numbers from a given list of integers and returns them in ascending order.
Args:
numbers (List[i... |
python_sharegpt_23k | 9,987 | Write a Python function to find all common elements in two given lists and store them in a new list. The common elements should be in ascending order and should not contain any duplicates. The function should have a time complexity of O(n), where n is the total number of elements in both lists.
The code should start w... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Find all common elements in two lists and return them in ascending order without duplicates.
Args:
list1 (List[... |
python_sharegpt_23k | 9,992 | Write a Python function to delete the third element (index 2) from a given list and shift all the remaining elements to the left to fill the empty space. The function should handle edge cases such as an empty list or a list with fewer than 3 elements. The function should not use any built-in functions or methods that d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def delete_third_element(arr: List[int]) -> List[int]:
"""
Deletes the third element (index 2) from the given list and shifts all remaining elements to the left.
Args:
arr (List[int]): The i... |
python_sharegpt_23k | 9,993 | Write a Python function to merge two dictionaries into a new dictionary. If a key is present in both dictionaries, the value from the second dictionary should overwrite the value from the first dictionary. The original dictionaries should not be modified. The function should start with:
```python
from typing import Dic... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def merge_dictionaries(dict1: Dict, dict2: Dict) -> Dict:
"""
Merges two dictionaries into a new dictionary. If a key is present in both dictionaries,
the value from the second dictionary will overwr... |
python_sharegpt_23k | 9,995 | Write a Python function to rearrange a given array of integers in ascending order using the Bubble Sort algorithm. The function should take a list of integers as input and return the sorted list. The code should start with:
```python
from typing import List
def bubble_sort(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 given array of integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The array of integers to be sor... |
python_sharegpt_23k | 9,996 | Write a Python function to compute the sum of three numbers. The function should take three integers as input and return their sum. The code should start with:
```python
def sum_of_three_numbers(a: int, b: int, c: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_three_numbers(a: int, b: int, c: int) -> int:
"""
Compute the sum of three given integers.
Args:
a (int): The first integer.
b (int): The second integer.
c (int): The third integer.
R... |
python_sharegpt_23k | 9,999 | Write a Python function to combine two given strings into one string in the given order without using any built-in string concatenation or joining functions. The final string should be stored in a separate string variable. The function should start with:
```python
def combine_strings(string1: str, string2: str) -> str:... | Here is the Python code to solve the given problem in the markdown block:
```python
def combine_strings(string1: str, string2: str) -> str:
"""
Combines two given strings into one string in the given order without using any built-in string concatenation or joining functions.
Args:
string1 (str): Th... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.