source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 7,263 | Write a Python function to calculate the median of a given array of numbers. The array will always have an odd number of elements, and you are not allowed to use any built-in functions or libraries to find the median. The function should start with:
```python
from typing import List
def calculate_median(array: List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_median(array: List[int]) -> int:
"""
Calculate the median of a given array of numbers with an odd number of elements.
Args:
array (List[int]): The array of integers with an odd... |
python_sharegpt_23k | 7,264 | Write a Python function to filter a list of numbers, removing all odd numbers and including only even numbers that are divisible by 3. The remaining numbers should be sorted in descending order. The function should start with:
```python
from typing import List
def filter_and_sort_numbers(numbers: List[int]) -> List[in... | 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]) -> List[int]:
"""
Filters a list of numbers to include only even numbers that are divisible by 3,
and sorts the remaining numbers in descending order.
... |
python_sharegpt_23k | 7,266 | 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... |
python_sharegpt_23k | 7,267 | Write a Python function to remove all whitespace characters from a given string without using any built-in string manipulation functions or regular expressions. The function should iterate through each character in the string and construct a new string that excludes any whitespace characters.
The code should start wit... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_whitespace(string: str) -> str:
"""
Removes all whitespace characters from the given string.
Args:
string (str): The input string from which whitespace characters are to be removed.
Returns:
... |
python_sharegpt_23k | 7,273 | Write a Python function to generate and return the HTML code for a table with a specified number of rows and columns. Each cell in the table should contain a string indicating its row and column position. The function should start with:
```python
def generate_html_table(rows: int, cols: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def generate_html_table(rows: int, cols: int) -> str:
"""
Generates and returns the HTML code for a table with the specified number of rows and columns.
Each cell in the table contains a string indicating its row and column ... |
python_sharegpt_23k | 7,277 | Write a Python function that implements a nested loop structure where the outer loop starts from 1 and increments up to 5, and the inner loop only iterates when the outer loop variable is even. The inner loop should also increment from 0 to 5. The function should return a list of tuples, where each tuple contains the v... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def nested_loop_even_outer(n: int) -> List[Tuple[int, int]]:
"""
Implements a nested loop structure where the outer loop starts from 1 and increments up to n,
and the inner loop only iterates ... |
python_sharegpt_23k | 7,278 | Write a Python function to calculate the sum of the squares of all valid integers in a given array. The array may contain elements of different types, and only integers should be considered for the sum of squares. The function should ignore any non-integer elements.
The code should start with:
```python
from typing im... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_squares(arr: List[int]) -> int:
"""
Calculate the sum of the squares of all valid integers in the given array.
Args:
arr (List[int]): The array containing elements of different ty... |
python_sharegpt_23k | 7,283 | Write a Python function to calculate the sum of all even numbers in a given list of integers. If the list contains no even numbers, the function should return 0. The function should be efficient and handle edge cases such as an empty list or a list with only odd numbers.
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 sum_of_evens(numbers: List[int]) -> int:
"""
Calculate the sum of all even numbers in the given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The sum of... |
python_sharegpt_23k | 7,287 | Write a Python function to transform a given string by swapping all lowercase characters with their respective uppercase characters and vice versa. The function should handle any string, including those with non-alphabetic characters, which should remain unchanged.
The code should start with:
```python
def transform_s... | Here is the Python code to solve the given problem in the markdown block:
```python
def transform_string(string: str) -> str:
"""
Transforms the input string by swapping the case of each alphabetic character.
Non-alphabetic characters remain unchanged.
Args:
string (str): The input string to be... |
python_sharegpt_23k | 7,291 | Write a Python function to remove all duplicate tuples from a given list of tuples, where each tuple consists of integers. The function should only remove exact duplicate tuples, not tuples that have the same values in a different order. The code should start with:
```python
from typing import List, Tuple
def remove_d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def remove_duplicate_tuples(tuples_list: List[Tuple[int, ...]]) -> List[Tuple[int, ...]]:
"""
Removes all duplicate tuples from a given list of tuples.
Args:
tuples_list (List[Tuple[i... |
python_sharegpt_23k | 7,296 | Write a Python function to determine the day of the week for a given date string in the format "dd/mm/yyyy". The function should correctly parse the date string, account for the correct order of day and month, and return the corresponding day of the week.
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
from datetime import datetime
def get_day_of_week(date_string: str) -> str:
"""
Determine the day of the week for a given date string in the format "dd/mm/yyyy".
Args:
date_string (str): The... |
python_sharegpt_23k | 7,297 | Write a Python function to find the greatest common divisor (GCD) of two integers using the Euclidean algorithm. The function should have a time complexity of O(log(min(a, b))) and a space complexity of O(1). The function should be able to handle both positive and negative integers.
The code should start with:
```pyth... | Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two integers using the Euclidean algorithm.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
... |
python_sharegpt_23k | 7,298 | Write a Python function to remove all duplicates from a given list of integers and sort the list in ascending order. The function should return the modified list. The code should start with:
```python
from typing import List
def remove_duplicates_and_sort(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 remove_duplicates_and_sort(lst: List[int]) -> List[int]:
"""
Removes all duplicates from the given list of integers and sorts the list in ascending order.
Args:
lst (List[int]): The li... |
python_sharegpt_23k | 7,301 | Write a Python function to replace each alphabetic character in a given string with its next consecutive character. The function should handle both uppercase and lowercase letters separately and wrap around from 'z' to 'a' or 'Z' to 'A' when necessary. Non-alphabetic characters should remain unchanged.
The code should... | Here is the Python code to solve the given problem in the markdown block:
```python
def replace_with_next_consecutive_char(string: str) -> str:
"""
Replace each alphabetic character in the given string with its next consecutive character.
Handles both uppercase and lowercase letters separately and wraps aro... |
python_sharegpt_23k | 7,305 | Write a Python function to calculate the sum of the elements of a given array with float values, excluding any negative values, and rounding the final sum to two decimal places. The function should handle an empty array by returning 0.00.
The code should start with:
```python
from typing import List
def sum_positive_... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_positive_floats(arr: List[float]) -> float:
"""
Calculate the sum of the positive float elements in the given array,
excluding any negative values, and round the final sum to two decimal plac... |
python_sharegpt_23k | 7,311 | Write a Python function to calculate the length of a given string without using the built-in `len()` function. The function should iterate through each character in the string and count the number of characters manually.
The code should start with:
```python
def calculate_string_length(string: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def calculate_string_length(string: str) -> int:
"""
Calculate the length of a given string without using the built-in len() function.
Args:
string (str): The input string whose length is to be calculated.
Retu... |
python_sharegpt_23k | 7,314 | Write a Python function to remove trailing spaces from a given string. The input string may contain alphanumeric characters, spaces, and punctuation. You are not allowed to use built-in functions or libraries to solve this problem. The code should start with:
```python
def remove_trailing_spaces(input_string: str) -> s... | Here is the Python code to solve the given problem in the markdown block:
```python
def remove_trailing_spaces(input_string: str) -> str:
"""
Removes trailing spaces from the given input string.
Args:
input_string (str): The input string from which trailing spaces need to be removed.
Returns:
... |
python_sharegpt_23k | 7,317 | Write a Python function to remove all empty strings from a given list while maintaining the order of the remaining elements. The function should take a list of strings as input and return a new list with all empty strings removed.
The code should start with:
```python
from typing import List
def remove_empty_strings(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_empty_strings(input_list: List[str]) -> List[str]:
"""
Removes all empty strings from the given list while maintaining the order of the remaining elements.
Args:
input_list (List[... |
python_sharegpt_23k | 7,318 | Write a Python function to flatten a list of lists and ensure that all elements in the flattened list are unique. The function should handle nested lists of arbitrary depth and return a list containing all unique elements in the order they first appear.
The code should start with:
```python
from typing import List, An... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def flatten_unique_list(lst: List[List[Any]]) -> List[Any]:
"""
Flattens a list of lists and ensures that all elements in the flattened list are unique.
Args:
lst (List[List[Any]]): A l... |
python_sharegpt_23k | 7,323 | Write a Python function to round a given decimal number to the nearest whole number. The function should handle decimal numbers with up to 5 decimal places and return the result rounded to the nearest whole number. The code should start with:
```python
def round_decimal(number: float) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def round_decimal(number: float) -> int:
"""
Rounds a given decimal number to the nearest whole number.
Args:
number (float): The decimal number to be rounded.
Returns:
int: The rounded whole number.
... |
python_sharegpt_23k | 7,329 | Write a Python function to generate and return a list of prime numbers up to a given maximum number `n` using the modulo operator. The function should efficiently determine if a number is prime by checking divisibility from 2 up to the square root of the number.
The code should start with:
```python
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def generate_primes(n: int) -> List[int]:
"""
Generate a list of prime numbers up to a given maximum number n.
Args:
n (int): The maximum number up to which to generate prime num... |
python_sharegpt_23k | 7,330 | Write a Python function to calculate the sum of all even numbers in a given array using a single loop. The function should have a time complexity of O(n) and a space complexity of O(1). If there are no even numbers in the array, the function should return 0.
The code should start with:
```python
from typing import Lis... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_even_numbers(arr: List[int]) -> int:
"""
Calculate the sum of all even numbers in the given array.
Args:
arr (List[int]): The array of integers from which to calculate the sum of eve... |
python_sharegpt_23k | 7,331 | Write a Python function to decompose a given number into its prime factors and calculate the sum of these prime factors. The function should return both the list of prime factors and their sum.
The code should start with:
```python
def decompose_and_calculate_sum(num: int) -> tuple:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decompose_and_calculate_sum(num: int) -> tuple:
"""
Decompose a given number into its prime factors and calculate their sum.
Args:
num (int): The number to decompose into prime factors.
Returns:
tup... |
python_sharegpt_23k | 7,335 | Write a Python function to convert a given string of space-separated integers into a list of unique integers. The function should handle any valid input string containing space-separated integers and return a list of unique integers in ascending order.
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 unique_sorted_integers(input_string: str) -> List[int]:
"""
Converts a string of space-separated integers into a list of unique integers sorted in ascending order.
Args:
input_string (st... |
python_sharegpt_23k | 7,337 | Write a Python function to generate a sequence of prime numbers between a given range using the Sieve of Eratosthenes algorithm. Additionally, create a function to determine the sum of all prime numbers in the generated sequence.
The code should start with:
```python
from typing import List
def generate_primes(start:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_primes(start: int, end: int) -> List[int]:
"""
Generate a list of prime numbers between the given start and end range (inclusive) using the Sieve of Eratosthenes algorithm.
Args:
... |
python_sharegpt_23k | 7,341 | Write a Python function to convert a given string to upper case using only basic string manipulation operations. The string may contain punctuation marks, special characters, and spaces. The function should not use the built-in `upper()` method. Instead, it should iterate through each character in the string, check if ... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_upper_case(s: str) -> str:
"""
Converts a given string to upper case using only basic string manipulation operations.
Args:
s (str): The input string to be converted to upper case.
Returns:
... |
python_sharegpt_23k | 7,342 | Write a Python function to compute the squares of each element in a given list of integers. The function should iterate through the input list using a for loop and calculate the square of each element. The function should return a new list containing the squared elements. The time complexity of the function should be O... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def square_elements(input_list: List[int]) -> List[int]:
"""
Computes the squares of each element in the given list of integers.
Args:
input_list (List[int]): A list of integers for which th... |
python_sharegpt_23k | 7,343 | Write a Python function to construct a regex pattern that matches any string that starts with "ab" and ends with "cd". The function should take a list of strings as input and return a list of strings that match the constructed regex pattern.
The code should start with:
```python
from typing import List
import re
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def match_ab_cd_strings(strings: List[str]) -> List[str]:
"""
Constructs a regex pattern to match any string that starts with "ab" and ends with "cd".
Returns a list of strings from the inp... |
python_sharegpt_23k | 7,346 | Write a Python function to swap the values of three variables such that the value of the first variable becomes the value of the third variable, the value of the second variable becomes the value of the first variable, and the value of the third variable becomes the value of the second variable. The function should ret... | Here is the Python code to solve the given problem in the markdown block:
```python
def swap_three_variables(a: int, b: int, c: int) -> tuple:
"""
Swaps the values of three variables such that:
- The new value of `a` becomes the old value of `c`.
- The new value of `b` becomes the old value of `a`.
... |
python_sharegpt_23k | 7,347 | Write a Python function to compare two dictionaries and return `True` if they have the same set of keys and values, and `False` otherwise. The function should have a time complexity of O(n), where n is the number of key-value pairs in the dictionaries. Additionally, the function should be able to handle larger input si... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_dicts(dict1: dict, dict2: dict) -> bool:
"""
Compare two dictionaries and return True if they have the same set of keys and values, and False otherwise.
Args:
dict1 (dict): The first dictionary to compar... |
python_sharegpt_23k | 7,352 | Write a Python function to find the maximum and minimum numbers in a given list of integers without using any built-in functions or methods such as `max()`, `min()`, or sorting algorithms. 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 ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def find_max_min(numbers: List[int]) -> Tuple[int, int]:
"""
Find the maximum and minimum numbers in a list of integers without using built-in functions or sorting algorithms.
Args:
n... |
python_sharegpt_23k | 7,358 | Write a Python function to safely divide two numbers and handle potential division by zero errors. If a division by zero occurs, the function should return a custom error message. The function should also handle other exceptions by returning a generic error message.
The code should start with:
```python
def safe_divis... | Here is the Python code to solve the given problem in the markdown block:
```python
def safe_division(a: float, b: float) -> str:
"""
Safely divides two numbers and handles potential division by zero errors.
Args:
a (float): The numerator.
b (float): The denominator.
Returns:
s... |
python_sharegpt_23k | 7,362 | Write a Python function to print the contents of a list in reverse order, ensuring that all even numbers are printed first, followed by the odd numbers. The solution should have a time complexity of O(n) and should not use any additional space other than the input list. 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_reversed_even_odd(arr: List[int]) -> None:
"""
Prints the contents of a list in reverse order, with all even numbers printed first, followed by odd numbers.
Args:
arr (List[int]): ... |
python_sharegpt_23k | 7,363 | Write a Python function to sort an array of string elements such that all numeric strings are placed at the beginning of the array, followed by the sorted non-numeric strings. The numeric strings should also be sorted in ascending order.
The code should start with:
```python
from typing import List
def sort_array_wit... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_array_with_numbers_first(arr: List[str]) -> List[str]:
"""
Sorts an array of string elements such that all numeric strings are placed at the beginning,
followed by the sorted non-numeric str... |
python_sharegpt_23k | 7,366 | Write a Python function to generate a list of all prime numbers between 1 and a given upper limit `n` (inclusive). The function should use a helper function to determine if a number is prime. The code should start with:
```python
from typing import List
def generate_primes(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def is_prime(n: int) -> bool:
"""
Helper function to determine if a number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is pri... |
python_sharegpt_23k | 7,367 | Write a Python function to calculate the sum of all odd numbers in a given array of integers. The array will contain at least one odd number and at most 1000 elements.
The code should start with:
```python
from typing import List
def sum_of_odd_numbers(arr: 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_odd_numbers(arr: List[int]) -> int:
"""
Calculate the sum of all odd numbers in the given array.
Args:
arr (List[int]): The array of integers containing at least one odd number.
... |
python_sharegpt_23k | 7,368 | Write a Python function to parse a given timestamp string in the format "YYYY-MM-DD HH:MM:SS" and optionally including milliseconds, indicated by a decimal point followed by up to 3 digits. The function should convert this string into a `datetime` object in Python, including the milliseconds if present.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from datetime import datetime
def parse_timestamp(timestamp_str: str) -> datetime:
"""
Parse a given timestamp string in the format "YYYY-MM-DD HH:MM:SS" and optionally including milliseconds,
indicated by a decimal point f... |
python_sharegpt_23k | 7,372 | Write a Python function to determine if any number in a given list of integers is a prime number. The list can contain both positive and negative integers, and the length of the list can be as large as \(10^6\). The input numbers can be as large as \(10^{12}\). The function should be efficient with a time complexity of... | 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 | 7,374 | Implement a Python function to sort a list of integers using the quicksort algorithm. The function should handle lists with only integers and should not include any non-integer elements. The code should start with:
```python
from typing import List
def quicksort(arr: List[int]) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def quicksort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the quicksort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
List[i... |
python_sharegpt_23k | 7,375 | Write a Python function to encrypt a given string using a Caesar cipher with a specified shift value. The function should handle both uppercase and lowercase letters, preserving their case, and leave non-alphabetic characters unchanged. Additionally, write a function to decrypt the encrypted string using the same shift... | Here is the Python code to solve the given problem in the markdown block:
```python
def caesar_cipher_encrypt(text: str, shift: int) -> str:
"""
Encrypts the given text using a Caesar cipher with the specified shift.
Args:
text (str): The text to be encrypted.
shift (int): The number of pos... |
python_sharegpt_23k | 7,382 | Write a Python function to parse a string containing lines of key/value pairs and return a dictionary of the values paired with the keys. The keys should be case-sensitive. The string may contain duplicate keys, and in such cases, the value associated with the last occurrence of the key should be stored in the dictiona... | Here is the Python code to solve the given problem in the markdown block:
```python
def parse_string_to_dict(input_string: str) -> dict:
"""
Parses a string containing lines of key/value pairs and returns a dictionary.
Args:
input_string (str): The input string containing lines of key/value pairs.
... |
python_sharegpt_23k | 7,383 | Write a Python function to format a given list of numbers with commas as thousands separators and round each number to the nearest hundredth decimal place. The function should return a list of formatted strings.
The code should start with:
```python
from typing import List
def format_numbers(numbers: List[float]) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def format_numbers(numbers: List[float]) -> List[str]:
"""
Formats a list of numbers with commas as thousands separators and rounds each number to the nearest hundredth decimal place.
Args:
... |
python_sharegpt_23k | 7,385 | Write a Python function to find and return a list of unique items from a given list without using any built-in functions or data structures like sets, dictionaries, or list comprehensions. The function should implement its own logic to detect and remove duplicates.
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 find_unique_items(lst: List[int]) -> List[int]:
"""
Returns a list of unique items from the given list without using any built-in functions or data structures.
Args:
lst (List[int]): The... |
python_sharegpt_23k | 7,390 | Write a Python function to determine if there are two distinct elements in a given array that add up to a specified target sum `k`. The array may contain negative numbers and duplicates. The function should return `True` if such a pair exists, otherwise `False`.
The code should start with:
```python
from typing import... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def has_pair_with_sum(arr: List[int], k: int) -> bool:
"""
Determines if there are two distinct elements in the array that add up to the specified target sum k.
Args:
arr (List[int]): The ar... |
python_sharegpt_23k | 7,397 | Write a Python function that takes in two lists of equal length and forms a new list by taking the elements at the same index from both lists. The function should then remove any duplicate elements from the new list and return the sorted result. The code should start with:
```python
from typing import List
def merge_a... | 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 of equal length by taking elements at the same index from both lists,
removes any duplicate elements, a... |
python_sharegpt_23k | 7,399 | 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 be efficient and should only check divisibility up to the square root of the number.
The code should start with:
```python
import math
def is_pr... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
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 otherwis... |
python_sharegpt_23k | 7,401 | Write a Python function to determine whether a given array contains an odd number of prime elements. The array can contain both positive and negative integers. The function should return `True` if the number of prime elements is odd, and `False` otherwise. Consider edge cases such as an empty array or an array with onl... | 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 given number is prime.
Args:
n (int): The number to check for primality.
Returns:
bool: True if the number is prime, Fal... |
python_sharegpt_23k | 7,402 | Write a Python function to generate a list of integers between a given start and end value, inclusive. The start and end values can be negative, and the range can be large (up to 10^9). The resulting list should be sorted in descending order. The time complexity of the solution should be O(n log n), where n is the numb... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_sorted_list(start: int, end: int) -> List[int]:
"""
Generate a list of integers between the given start and end values, inclusive, and sort it in descending order.
Args:
start (... |
python_sharegpt_23k | 7,407 | Write a Python function to find the maximum difference between a lower element and an upper element in an array, where the lower element must come before the upper element. The solution must have a time complexity of O(n) and a space complexity of O(1).
The code should start with:
```python
from typing import List
de... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def max_difference(arr: List[int]) -> int:
"""
Find the maximum difference between a lower element and an upper element in an array,
where the lower element must come before the upper element.
A... |
python_sharegpt_23k | 7,410 | Write a Python function to create a nested dictionary with a specified structure. The dictionary should contain five elements, one of which is a nested dictionary with at least three key-value pairs. The function should return the constructed dictionary.
The code should start with:
```python
def create_nested_dictiona... | Here is the Python code to solve the given problem in the markdown block:
```python
def create_nested_dictionary() -> dict:
"""
Creates a dictionary with a nested dictionary structure.
Returns:
dict: A dictionary containing five elements, one of which is a nested dictionary with at least three key-... |
python_sharegpt_23k | 7,413 | Write a Python function to calculate the sum of each row in a given 2D array, adhering to the following constraints:
- The function should have a time complexity of O(n), where n is the total number of elements in the array.
- The function should have a space complexity of O(1), meaning it should not use any additional... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_of_each_row(arr: List[List[int]]) -> List[int]:
"""
Calculate the sum of each row in a given 2D array.
Args:
arr (List[List[int]]): A 2D array of integers.
Returns:
List... |
python_sharegpt_23k | 7,414 | Write a Python function to merge two sorted lists into a single sorted list. The function should efficiently combine the elements from both lists while maintaining the sorted order. The code should start with:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def merge_sorted_lists(list1: List[int], list2: List[int]) -> List[int]:
"""
Merges two sorted lists into a single sorted list.
Args:
list1 (List[int]): The first sorted list.
list2 ... |
python_sharegpt_23k | 7,416 | Implement Strassen's Matrix Multiplication Algorithm in Python. The algorithm should efficiently multiply two square matrices of size \(2^n \times 2^n\) using a divide-and-conquer approach. The function should handle matrices of any size that is a power of 2.
The code should start with:
```python
import numpy as np
d... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def strassen_matrix_multiply(a: np.ndarray, b: np.ndarray) -> np.ndarray:
"""
Multiplies two square matrices of size 2^n x 2^n using Strassen's algorithm.
Args:
a (np.ndarray): The first matrix.
... |
python_sharegpt_23k | 7,420 | Write a Python function to extract and sort all negative numbers from a given list in ascending order using the Bubble Sort algorithm. The function should not use any built-in sorting functions or libraries and should have a time complexity of O(n^2). The code should start with:
```python
from typing import List
def s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sort_negative_numbers(lst: List[int]) -> List[int]:
"""
Extracts and sorts all negative numbers from the given list in ascending order using the Bubble Sort algorithm.
Args:
lst (List[in... |
python_sharegpt_23k | 7,423 | Write a Python function to find the length of the longest common subsequence (LCS) between two given strings. The function should use dynamic programming to efficiently compute the result.
The code should start with:
```python
def longest_common_subsequence_length(str1: str, str2: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_subsequence_length(str1: str, str2: str) -> int:
"""
Calculate the length of the longest common subsequence between two strings.
Args:
str1 (str): The first string.
str2 (str): The second ... |
python_sharegpt_23k | 7,425 | Write a Python function to extract all valid email addresses from a given text using regular expressions. The function should return a list of all email addresses found in the text. The code should start with:
```python
from typing import List
import re
def extract_emails(text: str) -> List[str]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def extract_emails(text: str) -> List[str]:
"""
Extracts all valid email addresses from the given text using regular expressions.
Args:
text (str): The text from which to extract e... |
python_sharegpt_23k | 7,427 | Write a Python function to calculate the sum of all elements in a given list of integers. The function should handle any list of integers, including empty lists and lists with negative numbers. The code should start with:
```python
from typing import List
def sum_of_elements(arr: 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_elements(arr: List[int]) -> int:
"""
Calculate the sum of all elements in a given list of integers.
Args:
arr (List[int]): The list of integers to sum.
Returns:
int... |
python_sharegpt_23k | 7,428 | Write a Python function to determine if a given number is a prime number without using any built-in prime number checking functions or libraries. The function should return `True` if the number is prime and `False` otherwise. The code should start with:
```python
def is_prime(number: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(number: int) -> bool:
"""
Determines 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, False otherwise.... |
python_sharegpt_23k | 7,429 | Write a Python function to generate the Fibonacci sequence up to the nth term and return both the sequence and the sum of all the numbers in the sequence. The function should handle edge cases such as when n is 0 or 1. The code should start with:
```python
from typing import List, Tuple
def fibonacci_sequence_and_sum(... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def fibonacci_sequence_and_sum(n: int) -> Tuple[List[int], int]:
"""
Generate the Fibonacci sequence up to the nth term and return the sequence along with the sum of all terms.
Args:
... |
python_sharegpt_23k | 7,432 | Write a Python function to sort a list of positive integers in ascending order using the Bubble Sort algorithm. The function should not use any built-in sorting functions or methods, and it should have a time complexity of O(n^2). The code should start with:
```python
from typing import List
def bubble_sort(arr: List[... | 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 positive integers in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The list of positive integers t... |
python_sharegpt_23k | 7,433 | Write a Python function to perform a binary search on a sorted array to find the index of a given target number. The function should have a time complexity of O(log n). If the target number is not found in the array, the function should return -1.
The code should start with:
```python
from typing import List
def bina... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def binary_search(arr: List[int], target: int) -> int:
"""
Perform a binary search on a sorted array to find the index of a given target number.
Args:
arr (List[int]): A sorted list of integ... |
python_sharegpt_23k | 7,438 | Write a Python function to find the index of the first occurrence of a given value in an array. The array can contain duplicates and both positive and negative integers. If the value is not found in the array, the function should return -1. The code should start with:
```python
from typing import List
def find_first_o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_first_occurrence(array: List[int], value: int) -> int:
"""
Find the index of the first occurrence of a given value in an array.
Args:
array (List[int]): The array to search in.
... |
python_sharegpt_23k | 7,440 | Write a Python function to count the number of vowels in a given string, ignoring case distinctions. The function should handle both uppercase and lowercase letters and return the count of vowels.
The code should start with:
```python
def count_vowels(s: str) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def count_vowels(s: str) -> int:
"""
Count the number of vowels in a given string, ignoring case distinctions.
Args:
s (str): The input string to count vowels in.
Returns:
int: The number of vowels in t... |
python_sharegpt_23k | 7,444 | Write a Python function to correctly convert a given string into its inverse form. The inverse form of a string is obtained by reversing the order of its characters. The function should handle any string input, including empty strings.
The code should start with:
```python
def inverse_string(input_string: str) -> str:... | Here is the Python code to solve the given problem in the markdown block:
```python
def inverse_string(input_string: str) -> str:
"""
Converts a given string into its inverse form by reversing the order of its characters.
Args:
input_string (str): The string to be inverted.
Returns:
st... |
python_sharegpt_23k | 7,446 | Write a Python function to separate odd numbers from a given array and store them in a different array. The function should ignore negative numbers, non-numeric values, and decimal numbers. If the input array is empty or does not contain any odd numbers, return an empty array. The output array should be sorted in ascen... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def separate_odd_numbers(arr: List[int]) -> List[int]:
"""
Separate odd numbers from a given array and return them in a sorted list.
Args:
arr (List[int]): The input array containing integer... |
python_sharegpt_23k | 7,448 | Write a Python function to compare two strings and return "yes" if they are equal, otherwise return "no". The comparison should be case-sensitive, meaning that "Apple" and "apple" should be considered different strings. The function should not use any built-in string comparison functions or methods, and the time comple... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(str1: str, str2: str) -> str:
"""
Compares two strings for equality in a case-sensitive manner.
Args:
str1 (str): The first string to compare.
str2 (str): The second string to compare.
... |
python_sharegpt_23k | 7,449 | Write a Python function to validate if a given string contains any numbers or special characters. The function should return `True` if the string contains only alphabetic characters (both uppercase and lowercase), and `False` otherwise. The code should start with:
```python
import re
def validate_string(input_string: ... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def validate_string(input_string: str) -> bool:
"""
Validate if the input string contains only alphabetic characters (both uppercase and lowercase).
Args:
input_string (str): The string to be validated.
... |
python_sharegpt_23k | 7,451 | Write a Python function to sort a list of tuples based on the second element (age) in ascending order using the bubble sort algorithm. 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 bubble_sort_by_age(data: List[Tu... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def bubble_sort_by_age(data: List[Tuple[str, int]]) -> List[Tuple[str, int]]:
"""
Sorts a list of tuples based on the second element (age) in ascending order using the bubble sort algorithm.
... |
python_sharegpt_23k | 7,455 | Write a Python function to check if the brackets in a given string are correctly matched and nested. The string may contain other characters besides brackets. The function should handle different types of brackets such as `{}`, `[]`, and `()`. The brackets must be correctly nested and closed in the correct order. The f... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_brackets_matched(string: str) -> bool:
"""
Checks if the brackets in the given string are correctly matched and nested.
Args:
string (str): The string to check for correctly matched and nested brackets.
... |
python_sharegpt_23k | 7,466 | Write a Python function to remove all elements from a given list using the `filter` function. The function should take a list as input and return a new list with all elements removed. The original list should not be modified. The code should start with:
```python
from typing import List
def remove_all_elements(array: ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_all_elements(array: List[int]) -> List[int]:
"""
Removes all elements from the given list using the filter function.
Args:
array (List[int]): The list from which all elements are ... |
python_sharegpt_23k | 7,467 | Implement the merge sort algorithm in Python. The merge sort algorithm should take a list of integers as input and sort the list in ascending order. Your implementation should have a time complexity of O(n log n) and a space complexity of O(n).
The code should start with:
```python
from typing import List
def merge_s... | 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 using the merge sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.
Returns:
List... |
python_sharegpt_23k | 7,471 | Write a Python function to reverse the order of words in a given input string. The function should handle strings with multiple spaces between words and leading/trailing spaces. The code should start with:
```python
def reverse_words_in_string(input_str: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_in_string(input_str: str) -> str:
"""
Reverses the order of words in a given input string.
Args:
input_str (str): The input string containing words to be reversed.
Returns:
str: A stri... |
python_sharegpt_23k | 7,482 | Write a Python function to calculate the product of all elements in a list of positive integers, with specific constraints on the list length and handling of zeros. The list must contain at least 3 elements and have a maximum length of 10. The function should return an error message if the list contains more than one z... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_product(numbers: List[int]) -> int or str:
"""
Calculate the product of all elements in a list of positive integers, with specific constraints.
Args:
numbers (List[int]): A lis... |
python_sharegpt_23k | 7,490 | Write a Python function to check if a given array is sorted in non-decreasing order with a time complexity of O(n log n) and a space complexity of O(1). The function should return `True` if the array is sorted and `False` otherwise.
The code should start with:
```python
from typing import List
def is_sorted_non_decre... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def is_sorted_non_decreasing(arr: List[int]) -> bool:
"""
Check if a given array is sorted in non-decreasing order with a time complexity of O(n log n) and a space complexity of O(1).
Args:
... |
python_sharegpt_23k | 7,491 | Write a Python function to delete all occurrences of a given element from a list. The list can contain duplicates. The function should modify the original list in-place and have a time complexity of O(n). Additionally, it should not use any extra space.
The code should start with:
```python
from typing import List
de... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def delete_element(arr: List[int], element: int) -> List[int]:
"""
Deletes all occurrences of the given element from the list in-place.
Args:
arr (List[int]): The list from which to delete t... |
python_sharegpt_23k | 7,492 | Write a Python function to find the two most frequent words in a given paragraph. The paragraph can contain any number of words and can have a maximum length of 10^6 characters. The words are case-insensitive, and all non-alphabetic characters should be ignored. If two or more words have the same frequency, return them... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
from collections import Counter
def find_two_most_frequent_words(paragraph: str) -> List[str]:
"""
Find the two most frequent words in a given paragraph.
Args:
paragraph (str): The... |
python_sharegpt_23k | 7,493 | Write a Python function to generate the next line of Pascal's Triangle given the parent line and a threshold value. The function should return `None` if the sum of the parent line is not greater than the threshold. Additionally, the function should raise a `ValueError` if the parent line is invalid (i.e., if it is empt... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def generate_next_line(parent_line: List[int], threshold: int) -> Optional[List[int]]:
"""
Generate the next line of Pascal's Triangle given the parent line and a threshold value.
Args:
... |
python_sharegpt_23k | 7,494 | Write a Python function to calculate the sum of a list of numbers using a single loop and without using any built-in sum functions or operators. The function should take a list of integers as input and return the sum of the numbers.
The code should start with:
```python
from typing import List
def calculate_sum(numbe... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_sum(numbers: List[int]) -> int:
"""
Calculate the sum of a list of numbers using a single loop and without using any built-in sum functions or operators.
Args:
numbers (List[in... |
python_sharegpt_23k | 7,498 | Write a Python function to generate an array of length `n` with unique random numbers in the range 0 to 100, sorted in descending order. The function should ensure that the generated numbers are unique and the array is sorted.
The code should start with:
```python
from typing import List
import random
def generate_un... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_sorted_array(n: int) -> List[int]:
"""
Generates an array of length n with unique random numbers in the range 0 to 100, sorted in descending order.
Args:
n ... |
python_sharegpt_23k | 7,507 | Write a Python function to extract and return a list of keys from a given dictionary, while ignoring any keys that start with the letter 'a' and have a value that is not a string. The extracted keys should be sorted in descending order. The dictionary may contain nested dictionaries as values.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def extract_keys(dictionary: Dict) -> List[str]:
"""
Extracts keys from a dictionary while ignoring keys that start with 'a' and have non-string values.
The extracted keys are sorted in descend... |
python_sharegpt_23k | 7,508 | Write a Python function `concatenate_arrays` that takes in two lists `a` and `b`, and concatenates them into a new list `c` using the `+` operator. Ensure that the length of `c` is equal to the sum of the lengths of `a` and `b`. Also, ensure that all elements in `c` are integers. The function should handle lists contai... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def concatenate_arrays(a: List[int], b: List[int]) -> List[int]:
"""
Concatenates two lists `a` and `b` into a new list `c` ensuring all elements are integers.
Args:
a (List[int]): The first... |
python_sharegpt_23k | 7,510 | Write a Python function to find the sum of all prime numbers between 0 and a given integer `n` (inclusive). The function should use a helper function to determine if a number is prime.
The code should start with:
```python
def sum_of_primes(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime(num: int) -> bool:
"""
Check if a given number is prime.
Args:
num (int): The number to check for primality.
Returns:
bool: True if the number is prime, False otherwise.
"""
if num ... |
python_sharegpt_23k | 7,511 | Write a Python function to check if a given text contains a specified word at least a certain number of times. The function should return a boolean value indicating whether the condition is met.
The code should start with:
```python
def check_word_frequency(text: str, word: str, frequency: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def check_word_frequency(text: str, word: str, frequency: int) -> bool:
"""
Check if the given text contains the specified word at least the specified number of times.
Args:
text (str): The text in which to search f... |
python_sharegpt_23k | 7,519 | Write a Python function to find the smallest and largest numbers from a given list of integers without using any built-in functions or libraries for finding the minimum and maximum values. The function should return a tuple containing the smallest and largest numbers.
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, Tuple
def find_min_max(numbers: List[int]) -> Tuple[int, int]:
"""
Finds the smallest and largest numbers from a given list of integers without using built-in functions or libraries.
Args:
... |
python_sharegpt_23k | 7,520 | Write a Python function to compare two strings and return the shortest one. If both strings have the same length, return the first string. The function should handle cases where the input strings are empty or have different lengths.
The code should start with:
```python
def find_shortest_string(string1: str, string2: ... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_shortest_string(string1: str, string2: str) -> str:
"""
Compare two strings and return the shortest one. If both strings have the same length, return the first string.
Args:
string1 (str): The first string ... |
python_sharegpt_23k | 7,523 | Write a Python function to convert a given number from base 10 to base 8, handling both positive and negative numbers. The function should return the converted number as a string. The code should start with:
```python
def convert_to_base8(number: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_base8(number: int) -> str:
"""
Converts a given base 10 number to its base 8 (octal) representation.
Args:
number (int): The base 10 number to be converted.
Returns:
str: The base 8 repre... |
python_sharegpt_23k | 7,531 | Write a Python function to convert a given decimal number to its binary representation. The function should take an integer as input and return the binary string representation of the input. The code should start with:
```python
def decimal_to_binary(decimal: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def decimal_to_binary(decimal: int) -> str:
"""
Converts a given decimal number to its binary representation.
Args:
decimal (int): The decimal number to be converted.
Returns:
str: The binary string rep... |
python_sharegpt_23k | 7,533 | Write a Python function to count the number of even elements that appear before any odd elements in a given array of integers. The array can contain both positive and negative integers. The function should efficiently handle arrays with a length of up to \(10^6\) and elements ranging from \(-10^9\) to \(10^9\).
The co... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def count_even_before_odd(arr: List[int]) -> int:
"""
Counts the number of even elements that appear before any odd elements in the given array.
Args:
arr (List[int]): The array of integers ... |
python_sharegpt_23k | 7,535 | Write a Python function to filter and return a list of odd numbers from a given list of integers. The function should iterate through the list and check each element to determine if it is odd. If an element is odd, it should be included in the result list. The function should start with:
```python
from typing import Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def filter_odd_numbers(numbers: List[int]) -> List[int]:
"""
Filters and returns a list of odd numbers from the given list of integers.
Args:
numbers (List[int]): A list of integers to filte... |
python_sharegpt_23k | 7,543 | Write a Python function to generate all prime numbers up to a given positive integer `n` using the Sieve of Eratosthenes algorithm. The function should have a time complexity of O(n^1.5) and should not use any external libraries or built-in functions to check for prime numbers. The function should handle edge cases suc... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_primes(n: int) -> List[int]:
"""
Generate all prime numbers up to a given positive integer n using the Sieve of Eratosthenes algorithm.
Args:
n (int): The upper limit up to whic... |
python_sharegpt_23k | 7,544 | Write a Python function to sort a given list of integers in ascending order using the **Selection Sort** algorithm. The algorithm should have a time complexity of O(n^2). The code should start with:
```python
from typing import List
def selection_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 selection_sort(arr: List[int]) -> List[int]:
"""
Sorts a list of integers in ascending order using the Selection Sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.... |
python_sharegpt_23k | 7,547 | Write a Python function to insert a target integer into an array of integers in ascending order while maintaining the ascending order of the array. If there are duplicate target integers in the array, insert the target integer immediately after the last occurrence of the target integer. Return the modified array.
The ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def insert_into_sorted_array(arr: List[int], target: int) -> List[int]:
"""
Inserts a target integer into a sorted array of integers while maintaining the ascending order.
If the target integer is al... |
python_sharegpt_23k | 7,548 | Write a Python function to check if an email address is valid based on the given conditions. The function should validate the email address without using any built-in libraries or regular expressions. The function should start with:
```python
def is_valid_email(email: str) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_valid_email(email: str) -> bool:
"""
Check if an email address is valid based on the given conditions.
Args:
email (str): The email address to be validated.
Returns:
bool: True if the email addre... |
python_sharegpt_23k | 7,550 | Write a Python function to check if a certain element is present in a given array without using any built-in array functions or methods (e.g., `in` operator, `index()` method). The function should return `True` if the element is found, and `False` otherwise. 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 is_element_present(arr: List[int], element: int) -> bool:
"""
Checks if a certain element is present in a given array without using any built-in array functions or methods.
Args:
arr (Li... |
python_sharegpt_23k | 7,551 | Write a Python function to identify the top K most commonly used words in a given text, where K is an input parameter. The function should handle punctuation and case sensitivity by converting the text to lowercase and removing punctuation marks. The function should return a list of the top K words in descending order ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import re
def top_k_most_common_words(text: str, k: int) -> List[str]:
"""
Identify the top K most commonly used words in a given text.
Args:
text (str): The input text from which to extract... |
python_sharegpt_23k | 7,556 | Write a Python function to generate a multiplication table for numbers from 1 to n, where n is a positive integer provided by the user. The function should print the multiplication table in a formatted manner, with each row representing the multiplication of a number from 1 to n by all numbers from 1 to n. The code sho... | Here is the Python code to solve the given problem in the markdown block:
:
```python
def generate_multiplication_table(n: int) -> None:
"""
Generates and prints a multiplication table for numbers from 1 to n.
Args:
n (int): The upper limit of the multiplication table.
Returns:
None: T... |
python_sharegpt_23k | 7,557 | Write a Python function to calculate `a` raised to the power of `b` without using any built-in functions or operators for exponentiation (such as `**` or `Math.pow()`). Implement the exponentiation algorithm manually. The function should handle both positive and negative exponents.
The code should start with:
```pytho... | Here is the Python code to solve the given problem in the markdown block:
```python
def power(a: float, b: int) -> float:
"""
Calculate a raised to the power of b without using built-in exponentiation functions or operators.
Args:
a (float): The base number.
b (int): The exponent.
Retu... |
python_sharegpt_23k | 7,561 | Write a Python function to merge two given lists by alternating elements from each list. If one list is longer than the other, the remaining elements from the longer list should be appended to the end of the merged list. The function should start with:
```python
from typing import List, Any
def merge_lists(arr1: List[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Any
def merge_lists(arr1: List[Any], arr2: List[Any]) -> List[Any]:
"""
Merge two lists by alternating elements from each list. If one list is longer than the other,
the remaining elements from the ... |
python_sharegpt_23k | 7,567 | Write a Python function to remove duplicates from a given list of integers and return the distinct values in descending order. The function should handle any list of integers, including empty lists and lists with all elements being the same.
The code should start with:
```python
from typing import List
def remove_dup... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_duplicates_and_sort(nums: List[int]) -> List[int]:
"""
Removes duplicates from the given list of integers and returns the distinct values in descending order.
Args:
nums (List[int... |
python_sharegpt_23k | 7,568 | Write a Python function to allocate an array of a given size `n` and store the numbers from 1 to `n` in it. The function should then sort the array in descending order and return the sorted array. The code should start with:
```python
from typing import List
def allocate_and_sort_array(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def allocate_and_sort_array(n: int) -> List[int]:
"""
Allocates an array of size n and stores numbers from 1 to n in it.
Then sorts the array in descending order and returns the sorted array.
Ar... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.