source stringclasses 6
values | id int64 0 111k | instruction stringlengths 167 4.39k | response stringlengths 244 6.1k |
|---|---|---|---|
python_sharegpt_23k | 7,575 | Write a Python function to calculate the length of a given list of integers without using any built-in functions or methods. The function should iterate through the list and count the number of elements manually.
The code should start with:
```python
from typing import List
def calculate_length(lst: List[int]) -> int... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_length(lst: List[int]) -> int:
"""
Calculate the length of a given list of integers without using any built-in functions or methods.
Args:
lst (List[int]): The list of integers... |
python_sharegpt_23k | 7,576 | Write a Python function to calculate the sum of the first `n` natural numbers using a while loop. The function should take an integer `n` as input and return the sum of all natural numbers from 1 to `n`.
The code should start with:
```python
def sum_of_first_n_numbers(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_first_n_numbers(n: int) -> int:
"""
Calculate the sum of the first n natural numbers using a while loop.
Args:
n (int): The number of natural numbers to sum.
Returns:
int: The sum of the firs... |
python_sharegpt_23k | 7,577 | Write a Python function to calculate the average rating of a book based on the ratings provided in the reviews, and another function to sort the chapters in ascending order based on their page count. The book information is stored in a JSON-like dictionary. The function to calculate the average rating should have a tim... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List
def calculate_average_rating(book: Dict) -> float:
"""
Calculate the average rating of the book based on the ratings provided in the reviews.
Args:
book (dict): A dictionary containing... |
python_sharegpt_23k | 7,578 | Write a Python function to calculate the sum of the digits of a given positive integer and count the number of unique digits present in the number. The function should return both the sum of the digits and the count of unique digits.
The code should start with:
```python
def sum_and_count_unique_digits(n: int) -> tupl... | Here is the Python code to solve the given problem in the markdown block:
```python
def sum_and_count_unique_digits(n: int) -> tuple:
"""
Calculate the sum of the digits of a given positive integer and count the number of unique digits present in the number.
Args:
n (int): The positive integer whos... |
python_sharegpt_23k | 7,582 | Write a Python function to swap the values of two variables without using a third variable. The function should take two integers as input and return the swapped values as a tuple. The code should start with:
```python
def swap_variables(a: int, b: int) -> tuple:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def swap_variables(a: int, b: int) -> tuple:
"""
Swaps the values of two integers without using a third variable.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
tuple: A... |
python_sharegpt_23k | 7,585 | Write a Python function to sort a given array of numbers in descending order and calculate the sum of all the even numbers in the array. The function should return a tuple containing the sorted array and the sum of the even numbers.
The code should start with:
```python
from typing import List, Tuple
def sort_and_sum... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def sort_and_sum_even(arr: List[int]) -> Tuple[List[int], int]:
"""
Sorts the given array of numbers in descending order and calculates the sum of all the even numbers.
Args:
arr (Lis... |
python_sharegpt_23k | 7,586 | Write a Python function to insert a new element into an existing unsorted array without using any built-in array functions or methods. The function should ensure that the new element is inserted at the correct position in the array to maintain the order of elements. 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 insert_element_in_order(arr: List[int], element: int) -> List[int]:
"""
Inserts a new element into an existing unsorted array while maintaining the order of elements.
Args:
arr (List[int... |
python_sharegpt_23k | 7,590 | Write a Python function to iterate through a list and perform a specified operation on each element. The function should take a list and a function as arguments, apply the function to each element of the list, and return a new list containing the results. The code should start with:
```python
from typing import List, C... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Callable
def apply_function_to_list(lst: List[int], func: Callable[[int], int]) -> List[int]:
"""
Applies a given function to each element of a list and returns a new list with the results.
Args:
... |
python_sharegpt_23k | 7,596 | Write a Python function to find the middle element of a given linked list. If the length of the linked list is odd, return the middle element. If the length of the linked list is even, return the second middle element. The function should start with:
```python
from typing import Optional
class ListNode:
def __init... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Optional
class ListNode:
def __init__(self, value=0, next=None):
self.value = value
self.next = next
def find_middle_element(head: Optional[ListNode]) -> Optional[int]:
"""
Find the middl... |
python_sharegpt_23k | 7,600 | Write a Python function to reverse the order of words in a given text while preserving the order of characters within each word. The function should start with:
```python
def reverse_words_order(text: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_words_order(text: str) -> str:
"""
Reverses the order of words in the given text while preserving the order of characters within each word.
Args:
text (str): The input text containing words to be reverse... |
python_sharegpt_23k | 7,601 | Write a Python function to find the largest number in a given array of positive integers. If the array contains any negative numbers, return an error message stating that negative numbers are not allowed. The function should start with:
```python
from typing import List
def find_largest_number(arr: List[int]) -> int o... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_largest_number(arr: List[int]) -> int or str:
"""
Find the largest number in a given array of positive integers.
If the array contains any negative numbers, return an error message.
Arg... |
python_sharegpt_23k | 7,603 | Write a Python function to sort a given list of integers in descending order without using any built-in sorting functions or methods. The function should implement a sorting algorithm manually.
The code should start with:
```python
from typing import List
def sort_list_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_list_descending(arr: List[int]) -> List[int]:
"""
Sorts a given list of integers in descending order using a selection sort algorithm.
Args:
arr (List[int]): The list of integers to... |
python_sharegpt_23k | 7,606 | Write a Python function to create an NxN matrix filled with zeros, where N is a prime number. The function should validate that N is a prime number and return `None` if it is not. The code should start with:
```python
from typing import List, Optional
def create_prime_matrix(N: int) -> Optional[List[List[int]]]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def create_prime_matrix(N: int) -> Optional[List[List[int]]]:
"""
Create an NxN matrix filled with zeros, where N is a prime number.
Args:
N (int): The size of the matrix. Must be ... |
python_sharegpt_23k | 7,607 | Write a Python function to find the minimum value in a given array with a time complexity requirement of O(n log n), where n is the length of the array. The function should use a divide-and-conquer approach to achieve this time complexity.
The code should start with:
```python
from typing import List
def find_minimum... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_minimum_in_array(arr: List[int]) -> int:
"""
Find the minimum value in a given array using a divide-and-conquer approach with a time complexity of O(n log n).
Args:
arr (List[int]):... |
python_sharegpt_23k | 7,608 | Write a Python function to calculate the greatest common divisor (GCD) of two given integers using the Euclidean algorithm. You are not allowed to use any built-in functions or libraries to calculate the GCD. The function should start with:
```python
def gcd(a: int, b: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def gcd(a: int, b: int) -> int:
"""
Calculate the greatest common divisor (GCD) of two integers using the Euclidean algorithm.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
... |
python_sharegpt_23k | 7,610 | Write a Python function to sort an array using the QuickSort algorithm. The function should handle arrays with duplicate 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 an array using the QuickSort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
List[int]: The sorted array... |
python_sharegpt_23k | 7,611 | Write a Python function to generate a random sequence of unique numbers from a given list, ensuring that no number is repeated in the generated sequence. The function should take a list of integers as input and return a list of integers. The code should start with:
```python
from typing import List
import random
def g... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def generate_unique_random_sequence(numbers: List[int]) -> List[int]:
"""
Generates a random sequence of unique numbers from the given list.
Args:
numbers (List[int]): The list... |
python_sharegpt_23k | 7,618 | Write a Python function to validate if a given string meets the following criteria:
1. The string must contain at least one digit.
2. The string must contain at least one letter.
3. The string must be at least 6 characters long.
The function should use regular expressions to perform the validation.
The code should st... | Here is the Python code to solve the given problem in the markdown block:
```python
import re
def validate_string(s: str) -> bool:
"""
Validate if a given string meets the following criteria:
1. Contains at least one digit.
2. Contains at least one letter.
3. Is at least 6 characters long.
Arg... |
python_sharegpt_23k | 7,620 | Write a Python function to extract all digits from a given string and return them as a list of integers. The input string may contain both letters and digits, and may have leading or trailing whitespace. The output list should contain the digits in the same order as they appear in the string. You should aim for an effi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def extract_digits(string: str) -> List[int]:
"""
Extracts all digits from a given string and returns them as a list of integers.
Args:
string (str): The input string which may contain both ... |
python_sharegpt_23k | 7,622 | Write a Python function to multiply each element of a given array of positive integers by three and then sort the resulting array in descending order. The function should handle arrays of varying lengths and ensure that the output is always sorted correctly.
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 multiply_and_sort_descending(arr: List[int]) -> List[int]:
"""
Multiplies each element of the input list by three and returns the resulting list sorted in descending order.
Args:
arr (Li... |
python_sharegpt_23k | 7,623 | Write a Python function to sort a given list of integers into descending order using a custom merge sort algorithm. The function should not use any built-in sorting functions or methods. The code should start with:
```python
from typing import List
def custom_merge_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 custom_merge_sort_descending(arr: List[int]) -> List[int]:
"""
Sorts a given list of integers into descending order using a custom merge sort algorithm.
Args:
arr (List[int]): The list o... |
python_sharegpt_23k | 7,625 | Write a Python function to implement the Quick Sort algorithm using an iterative approach instead of a recursive approach. The function should sort a list of integers in ascending order. The code should start with:
```python
from typing import List
def quick_sort_iterative(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_iterative(arr: List[int]) -> List[int]:
"""
Sorts a list of integers using the Quick Sort algorithm in an iterative approach.
Args:
arr (List[int]): The list of integers to be... |
python_sharegpt_23k | 7,628 | Write a Python function to find the maximum value in a given list of integers without using any built-in functions or methods to find the maximum value. The function should handle lists containing negative numbers and return the correct maximum value.
The code should start with:
```python
from typing import List
def ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_max_value(lst: List[int]) -> int:
"""
Find the maximum value in a given list of integers without using built-in functions or methods.
Args:
lst (List[int]): The list of integers fro... |
python_sharegpt_23k | 7,631 | Write a Python function to generate a list of numbers within a given range, inclusive of both the minimum and maximum values. The function should handle both positive and negative ranges efficiently. The code should start with:
```python
from typing import List
def generate_range(minimum: int, maximum: int) -> List[in... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_range(minimum: int, maximum: int) -> List[int]:
"""
Generate a list of numbers within the given range [minimum, maximum], inclusive.
Args:
minimum (int): The starting value of t... |
python_sharegpt_23k | 7,632 | Write a Python function to calculate the total sum of all even numbers in a given list using an accumulator pattern. The function should take a list of integers as input and return the sum of all even numbers in the list. The code should start with:
```python
from typing import List
def sum_of_evens(numbers: List[int]... | 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 total sum of all even numbers in a given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The su... |
python_sharegpt_23k | 7,633 | Write a Python function to find and return all prime numbers between 0 and a given number `n` using a while loop. The function should utilize a helper function to check if a number is prime. The code should start with:
```python
def find_primes_up_to(n: int) -> list:
```
| 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,635 | Write a Python function to sort a given dictionary of strings alphabetically in reverse order based on its keys. The function should return a new dictionary with the same key-value pairs but sorted in reverse alphabetical order of the keys.
The code should start with:
```python
from typing import Dict
def sort_dictio... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sort_dictionary_reverse(dictionary: Dict[str, str]) -> Dict[str, str]:
"""
Sorts a given dictionary of strings alphabetically in reverse order based on its keys.
Args:
dictionary (Dict[s... |
python_sharegpt_23k | 7,636 | Write a Python function to create a dictionary with items and their corresponding prices, where each price is stored as a tuple with two decimal places precision. The function should take a list of items and their prices as input and return the dictionary.
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, Tuple, Dict
def create_price_dict(items_prices: List[Tuple[str, float]]) -> Dict[str, Tuple[float]]:
"""
Create a dictionary with items and their corresponding prices, where each price is stored as a tu... |
python_sharegpt_23k | 7,638 | Write a Python function to compare two arrays for equality and determine the number of elements that are equal at the same position in both arrays. The function should return a tuple containing a boolean indicating whether the arrays are completely equal, the count of equal elements, and a list of indices where the ele... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Tuple
def compare_arrays(arr1: List[int], arr2: List[int]) -> Tuple[bool, int, List[int]]:
"""
Compare two arrays for equality and determine the number of elements that are equal at the same position.
... |
python_sharegpt_23k | 7,640 | Write a Python function to sort an array in ascending order without using any built-in sorting methods or functions. The function should implement the Bubble Sort algorithm.
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 in ascending order using the Bubble Sort algorithm.
Args:
arr (List[int]): The array to be sorted.
Returns:
List... |
python_sharegpt_23k | 7,643 | Write a Python function to output the binary representation of a given non-negative integer using only bitwise operations. The function should return a string representing the binary number. The code should start with:
```python
def binary_representation(number: int) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def binary_representation(number: int) -> str:
"""
Returns the binary representation of a given non-negative integer using only bitwise operations.
Args:
number (int): The non-negative integer to convert to binary.
... |
python_sharegpt_23k | 7,646 | Write a Python function to calculate the Manhattan distance between two points in a 2D plane. The Manhattan distance is defined as the sum of the absolute differences of their Cartesian coordinates. The function should take four integers as input, representing the coordinates of the two points, and return an integer re... | Here is the Python code to solve the given problem in the markdown block:
```python
def manhattan_distance(x1: int, y1: int, x2: int, y2: int) -> int:
"""
Calculate the Manhattan distance between two points (x1, y1) and (x2, y2) in a 2D plane.
Args:
x1 (int): The x-coordinate of the first point.
... |
python_sharegpt_23k | 7,654 | Write a Python function to sum a list of numbers without using the built-in addition operator (`+`). Instead, use basic arithmetic operations such as subtraction, multiplication, and division. The function should handle both positive and negative integers and maintain a time complexity of O(n), where n is the length of... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def sum_numbers_without_addition(list_of_numbers: List[int]) -> int:
"""
Sums a list of numbers without using the built-in addition operator.
Args:
list_of_numbers (List[int]): A list of int... |
python_sharegpt_23k | 7,658 | Write a Python function to return the first two characters of a given string with a time complexity of O(1). The function should handle edge cases such as empty strings and strings with fewer than two characters by returning an empty string.
The code should start with:
```python
def get_first_two_characters(s: str) ->... | Here is the Python code to solve the given problem in the markdown block:
```python
def get_first_two_characters(s: str) -> str:
"""
Returns the first two characters of the given string.
Args:
s (str): The input string from which to extract the first two characters.
Returns:
st... |
python_sharegpt_23k | 7,660 | Write a Python function to reverse a given string using a stack. The input string will always contain only alphabetic characters. Ensure that your implementation has a time complexity of O(n), where n is the length of the string. The code should start with:
```python
def reverse_string_with_stack(input_string: str) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string_with_stack(input_string: str) -> str:
"""
Reverses a given string using a stack.
Args:
input_string (str): The string to be reversed. It contains only alphabetic characters.
Returns:
... |
python_sharegpt_23k | 7,664 | Write a Python function to convert each string in a given list to uppercase, handle empty strings and non-string elements gracefully, and return the final list sorted in descending order alphabetically. The solution should be implemented using a list comprehension and should not modify the original list. The function s... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_to_uppercase_and_sort(strings: List[str]) -> List[str]:
"""
Converts each string in the given list to uppercase, handles empty strings and non-string elements gracefully,
and returns the ... |
python_sharegpt_23k | 7,671 | Write a Python function to retrieve unique values from a list of integers, including negative numbers. The function should return a list of unique integers in the order they first appear in the input list.
The code should start with:
```python
from typing import List
def get_unique_values(input_list: List[int]) -> Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_unique_values(input_list: List[int]) -> List[int]:
"""
Retrieve unique values from a list of integers, preserving the order of their first appearance.
Args:
input_list (List[int]): T... |
python_sharegpt_23k | 7,673 | Write a Python function to remove all occurrences of a specified element from a given list, and also remove any duplicates of that element in the list. The function should return the modified list. The code should start with:
```python
from typing import List
def remove_all_duplicates(lst: List[int], element: int) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def remove_all_duplicates(lst: List[int], element: int) -> List[int]:
"""
Removes all occurrences of the specified element and any duplicates of that element from the given list.
Args:
lst (... |
python_sharegpt_23k | 7,674 | Write a Python function to recursively retrieve all the values from a nested dictionary and return them in a flattened list. The dictionary may contain other dictionaries as values, and the function should handle any level of nesting. The code should start with:
```python
from typing import Dict, List, Any
def flatten... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict, List, Any
def flatten_nested_dict_values(data_dict: Dict[Any, Any]) -> List[Any]:
"""
Recursively retrieves all the values from a nested dictionary and returns them in a flattened list.
Args:
... |
python_sharegpt_23k | 7,679 | Write a Python function to find the most frequent item in a given list of integers. If there are multiple items with the same highest frequency, return the item that appears first in the list. The function should not use any built-in Python functions for counting occurrences or finding the most frequent item. The code ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def most_frequent_item(num_list: List[int]) -> int:
"""
Find the most frequent item in a list of integers. If there are multiple items with the same highest frequency,
return the item that appears fi... |
python_sharegpt_23k | 7,680 | Write a Python function to find the most commonly used element in a given list that appears at least twice. 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 import List, Optional
def most_common_element(lst: List[int]) -> Optional[... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Optional
def most_common_element(lst: List[int]) -> Optional[int]:
"""
Find the most commonly used element in a given list that appears at least twice.
Args:
lst (List[int]): The list of in... |
python_sharegpt_23k | 7,685 | Write a Python function to get the first n prime numbers efficiently. The function should have a time complexity of O(n^2) or better.
The code should start with:
```python
from typing import List
def get_first_n_primes(n: int) -> List[int]:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
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 | 7,687 | Write a Python function to search for a specific keyword within a given string and return the index of the first occurrence of the keyword. The search should be case insensitive. If the keyword is not found, the function should return -1.
The code should start with:
```python
def find_keyword_index(text: str, keyword:... | Here is the Python code to solve the given problem in the markdown block:
```python
def find_keyword_index(text: str, keyword: str) -> int:
"""
Searches for a keyword within a given text and returns the index of the first occurrence of the keyword.
The search is case insensitive.
Args:
text (st... |
python_sharegpt_23k | 7,688 | Write a Python function to flatten a 2D array and sort the elements in ascending order while removing any duplicate elements from the flattened array. The input array will always contain at least one sub-array, and each sub-array will contain at least one element.
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 flatten_and_sort_unique(arr: List[List[int]]) -> List[int]:
"""
Flattens a 2D array, removes duplicate elements, and sorts the elements in ascending order.
Args:
arr (List[List[int]]): A... |
python_sharegpt_23k | 7,692 | Write a Python function to count the occurrences of each item in a given list and return the result as a dictionary. The dictionary should have the items as keys and their respective counts as values. The code should start with:
```python
from typing import List, Dict
def count_items(lst: List[int]) -> Dict[int, int]:... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def count_items(lst: List[int]) -> Dict[int, int]:
"""
Counts the occurrences of each item in the given list and returns a dictionary with items as keys and their counts as values.
Args:
... |
python_sharegpt_23k | 7,694 | Write a Python function to sort a list of dictionary objects by the value of the "age" field in descending order, while excluding any objects where the "age" is less than 30. The function should return the sorted and filtered list.
The code should start with:
```python
from typing import List, Dict
def sort_and_filte... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def sort_and_filter_by_age(data: List[Dict[str, any]]) -> List[Dict[str, any]]:
"""
Sorts a list of dictionary objects by the "age" field in descending order and filters out objects where the "age"... |
python_sharegpt_23k | 7,695 | Write a Python function to create a NumPy array that contains numbers from 1 to n, but ensure that the array is in descending order. The function should take an integer `n` as input and return a NumPy array.
The code should start with:
```python
import numpy as np
def create_descending_array(n: int) -> np.ndarray:
``... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def create_descending_array(n: int) -> np.ndarray:
"""
Create a NumPy array that contains numbers from 1 to n in descending order.
Args:
n (int): The upper limit of the range (inclusive).
Re... |
python_sharegpt_23k | 7,696 | Write a Python function to reverse a given string without using any built-in string manipulation functions or methods. The function should iterate through the string and construct the reversed string manually.
The code should start with:
```python
def reverse_string(string: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def reverse_string(string: str) -> str:
"""
Reverses a given string without using any built-in string manipulation functions or methods.
Args:
string (str): The input string to be reversed.
Returns:
str... |
python_sharegpt_23k | 7,699 | Write a Python function to find and return the common elements between two given lists without using any built-in Python functions like `set()` or `intersection()`. The function should iterate through both lists and identify elements that appear in both.
The code should start with:
```python
from typing import List
d... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_common_elements(list1: List[int], list2: List[int]) -> List[int]:
"""
Find and return the common elements between two given lists.
Args:
list1 (List[int]): The first list of integer... |
python_sharegpt_23k | 7,707 | Write a Python function to flatten a given two-dimensional array into a one-dimensional array, remove any duplicate elements, and sort the resulting array in ascending order. The function should handle any size of two-dimensional array and ensure the output is a sorted list of unique elements.
The code should start wi... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def flatten_and_sort_unique(arr: List[List[int]]) -> List[int]:
"""
Flattens a given two-dimensional array into a one-dimensional array, removes any duplicate elements, and sorts the resulting array in a... |
python_sharegpt_23k | 7,708 | Write a Python function to convert a Roman numeral to an integer. The function should adhere to the rules of the Roman numeral system, which includes handling subtraction cases where smaller symbols precede larger symbols. The function should take a string representing a Roman numeral as input and return its correspond... | Here is the Python code to solve the given problem in the markdown block:
```python
def roman_to_int(roman: str) -> int:
"""
Convert a Roman numeral string to an integer.
Args:
roman (str): A string representing a valid Roman numeral.
Returns:
int: The integer value corresponding to th... |
python_sharegpt_23k | 7,712 | Write a Python function to sum up all the positive values in a given dictionary. The dictionary should contain string keys and positive integer values. The function should handle dictionaries with a minimum of 5 and a maximum of 10 key-value pairs.
The code should start with:
```python
from typing import Dict
def sum... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def sum_positive_values(dictionary: Dict[str, int]) -> int:
"""
Sum up all the positive values in a given dictionary.
Args:
dictionary (Dict[str, int]): A dictionary with string keys and pos... |
python_sharegpt_23k | 7,716 | Write a Python function to calculate the nth Fibonacci number using recursion. The function should include at least two base cases and demonstrate the proper use of recursive calls. The code should start with:
```python
def fibonacci(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
def fibonacci(n: int) -> int:
"""
Calculate the nth Fibonacci number using recursion.
Args:
n (int): The position in the Fibonacci sequence to calculate.
Returns:
int: The nth Fibonacci number.
""... |
python_sharegpt_23k | 7,717 | Write a Python function to calculate the sum of the even numbers from 1 to a given positive integer `n`. The function should return the sum of all even numbers in this range.
The code should start with:
```python
def sum_of_even_numbers(n: int) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def sum_of_even_numbers(n: int) -> int:
"""
Calculate the sum of all even numbers from 1 to n.
Args:
n (int): The upper limit of the range (inclusive).
Returns:
int: The sum of all even numbers from 1 t... |
python_sharegpt_23k | 7,721 | Write a Python function to compute the Fibonacci sequence up to a given maximum value `n` efficiently with a time complexity of O(n). The function should return a list of Fibonacci numbers that do not exceed `n`.
The code should start with:
```python
from typing import List
def compute_fibonacci_up_to_n(n: int) -> Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def compute_fibonacci_up_to_n(n: int) -> List[int]:
"""
Compute the Fibonacci sequence up to a given maximum value `n` efficiently.
Args:
n (int): The maximum value up to which the Fibonacci... |
python_sharegpt_23k | 7,723 | Write a Python function to find and return a list of all prime numbers within a given range `[start, end]`. The function should efficiently determine if a number is prime by checking divisibility up to the square root of the number.
The code should start with:
```python
from typing import List
import math
def find_pr... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import math
def find_primes_in_range(start: int, end: int) -> List[int]:
"""
Find and return a list of all prime numbers within the given range [start, end].
Args:
start (int): The starting ... |
python_sharegpt_23k | 7,732 | Write a Python function to combine two dictionaries based on a common property value, ensuring that the property value is case-sensitive. If the property values differ only in case, the value from the second dictionary should overwrite the value from the first dictionary. The function should return the combined diction... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import Dict
def combine_objects(obj1: Dict[str, any], obj2: Dict[str, any], key: str) -> Dict[str, any]:
"""
Combines two dictionaries based on a common property value, ensuring that the property value is case-sensi... |
python_sharegpt_23k | 7,736 | Write a Python function to extract all even numbers from a given list of integers. The function should have a time complexity of O(n) and should not use any built-in Python functions or methods (such as `filter` or list comprehensions) that directly solve the problem. The function 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 get_even_numbers(numbers: List[int]) -> List[int]:
"""
Extracts all even numbers from a given list of integers.
Args:
numbers (List[int]): The list of integers from which to extract even... |
python_sharegpt_23k | 7,740 | Write a Python function to generate a sorted array of numbers in descending order from a given range. The function should take two integers, `start` and `end`, as input and return a list of numbers from `start` to `end` (inclusive) sorted in descending order. If `start` is greater than `end`, the function should return... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def generate_descending_array(start: int, end: int) -> List[int]:
"""
Generates a sorted array of numbers in descending order from the given range [start, end].
Args:
start (int): The starti... |
python_sharegpt_23k | 7,748 | Write a Python function to calculate the sum of all odd numbers in a given list of integers. The function should handle an empty list and return 0 in that case. The code should start with:
```python
from typing import List
def sum_of_odds(numbers: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
:
```python
from typing import List
def sum_of_odds(numbers: List[int]) -> int:
"""
Calculate the sum of all odd numbers in the given list.
Args:
numbers (List[int]): A list of integers.
Returns:
int: The sum of... |
python_sharegpt_23k | 7,749 | Write a Python function to sort a list in ascending order using the Bubble Sort algorithm. The function should not use any built-in sorting functions or methods. The code should start with:
```python
from typing import List
def bubble_sort(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 bubble_sort(lst: List[int]) -> List[int]:
"""
Sorts a list in ascending order using the Bubble Sort algorithm.
Args:
lst (List[int]): The list of integers to be sorted.
Returns:
... |
python_sharegpt_23k | 7,750 | Write a Python function to generate a 5-dimensional array of a specified size using the numpy library and calculate the sum of all the elements in the array. The function should return the sum of the elements.
The code should start with:
```python
import numpy as np
def sum_of_elements_in_array(dimensions: tuple) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
def sum_of_elements_in_array(dimensions: tuple) -> float:
"""
Generate a 5-dimensional array of the specified size and calculate the sum of all its elements.
Args:
dimensions (tuple): A tuple of ... |
python_sharegpt_23k | 7,751 | Write a Python function to generate and return a list of the first `n` Fibonacci numbers, where `n` is a positive integer. The function should ensure that it does not generate more than 100 Fibonacci numbers, even if `n` is greater than 100.
The code should start with:
```python
from typing import List
def generate_f... | 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 and return a list of the first `n` Fibonacci numbers, with a maximum of 100 numbers.
Args:
n (int): The number of Fibonacci numbers ... |
python_sharegpt_23k | 7,755 | Write a Python function to find the top 3 highest numbers from a given array without using any sorting algorithm. The array can have a maximum of 1000 elements. The function should return a list containing the top 3 highest numbers in descending 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 find_top_3_highest_numbers(arr: List[int]) -> List[int]:
"""
Find the top 3 highest numbers from a given array without using any sorting algorithm.
Args:
arr (List[int]): The input array... |
python_sharegpt_23k | 7,757 | Write a Python function to swap the values of two variables without using any temporary variables. The function should handle swapping values of variables with different data types. The code should start with:
```python
def swap_values(x, y):
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def swap_values(x, y):
"""
Swaps the values of two variables without using any temporary variables.
Args:
x: The first variable to be swapped.
y: The second variable to be swapped.
Returns:
tupl... |
python_sharegpt_23k | 7,759 | Write a Python function to create a random matrix using NumPy where the elements have 2 decimal places. Additionally, compute and return the sum of the matrix elements, the product of the matrix elements, and the average of the matrix elements as a tuple.
The code should start with:
```python
import numpy as np
from t... | Here is the Python code to solve the given problem in the markdown block:
```python
import numpy as np
from typing import Tuple
def create_and_analyze_matrix(rows: int, cols: int) -> Tuple[float, float, float]:
"""
Create a random matrix with elements rounded to 2 decimal places and compute the sum, product, a... |
python_sharegpt_23k | 7,763 | Write a Python function to simulate the behavior of a nested loop with a `break` statement, where the outer loop is a `while` loop and the inner loop is a `for` loop. The function should print the values of the outer loop variable `i` and the inner loop variable `j` for each iteration, and break out of the inner loop w... | Here is the Python code to solve the given problem in the markdown block:
```python
def nested_loop_with_break(max_i: int, break_j: int) -> None:
"""
Simulate a nested loop with a break statement. The outer loop is a while loop,
and the inner loop is a for loop. The function prints the values of the outer
... |
python_sharegpt_23k | 7,765 | Write a Python function to convert a given list of strings into a dictionary where each key is a string from the list and the corresponding value is the length of that string. The function should handle an empty list by returning an empty dictionary.
The code should start with:
```python
from typing import List, Dict
... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def convert_list_to_dict(strings: List[str]) -> Dict[str, int]:
"""
Converts a list of strings into a dictionary where each key is a string from the list and the value is the length of that string.... |
python_sharegpt_23k | 7,766 | Write a Python function to search for a specific element in an unsorted array that may contain duplicate elements. The function should return the index of the first occurrence of the element if it is found, otherwise it should return -1. The function should perform the search efficiently.
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_element(array: List[int], element: int) -> int:
"""
Searches for the first occurrence of a specific element in an unsorted array that may contain duplicate elements.
Args:
array (... |
python_sharegpt_23k | 7,767 | Write a Python function to determine if a given integer is odd. The function should return `True` if the integer is odd and `False` otherwise. The function should handle both positive and negative integers.
The code should start with:
```python
def is_odd(x: int) -> bool:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
def is_odd(x: int) -> bool:
"""
Determine if a given integer is odd.
Args:
x (int): The integer to check for oddness.
Returns:
bool: True if the integer is odd, False otherwise.
"""
return x % 2... |
python_sharegpt_23k | 7,769 | Write a Python function to create a Nim game board with size `n`, where `n` is an integer greater than 1. Each box on the board should contain a random number of stones between 1 and `k` (inclusive). The function should return the created Nim game board as a list, where each element of the list represents the number of... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
import random
def create_nim_board(n: int, k: int) -> List[int]:
"""
Creates a Nim game board with size n, where each box contains a random number of stones between 1 and k (inclusive).
Args:
... |
python_sharegpt_23k | 7,770 | Write a Python function to create a dictionary mapping student IDs to their corresponding grades. The function should take two lists as input: one containing student IDs and the other containing their grades. The function should handle cases where the lengths of the input lists are not equal. If the lengths are not equ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict
def create_grade_dictionary(student_ids: List[int], grades: List[float]) -> Dict[int, float]:
"""
Creates a dictionary mapping student IDs to their corresponding grades.
Args:
student_... |
python_sharegpt_23k | 7,773 | Write a Python function to sort a given list of integers in ascending order using a recursive 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 a list of integers in ascending order using a recursive merge sort algorithm.
Args:
arr (List[int]): The list of integers to be sorted.... |
python_sharegpt_23k | 7,774 | Write a Python function to compare two given strings and determine if they match, considering both case-sensitive and case-insensitive comparisons. The function should return a tuple where the first element is a boolean indicating whether the strings match case-sensitively, and the second element is a boolean indicatin... | Here is the Python code to solve the given problem in the markdown block:
```python
def compare_strings(str1: str, str2: str) -> tuple:
"""
Compare two strings and return a tuple indicating whether they match case-sensitively and case-insensitively.
Args:
str1 (str): The first string to compare.
... |
python_sharegpt_23k | 7,775 | Write a Python function to find the elements at even indexes in a given list and return them in reverse order. The function should handle lists of any length and ensure that the elements at even indexes are correctly identified and returned in descending order.
The code should start with:
```python
from typing import ... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_elements_at_even_indexes_reverse(lst: List[int]) -> List[int]:
"""
Finds the elements at even indexes in a given list and returns them in reverse order.
Args:
lst (List[int]): The i... |
python_sharegpt_23k | 7,779 | Write a Python function to calculate the sum of all odd numbers in a given list of integers. If the list is empty or does not contain any odd numbers, the function should return 0. The function should be efficient and handle edge cases appropriately.
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 sum_of_odds(numbers: List[int]) -> int:
"""
Calculate the sum of all odd numbers in the given list of integers.
Args:
numbers (List[int]): A list of integers.
Returns:
int: ... |
python_sharegpt_23k | 7,784 | Write a Python function to sort the elements in an integer array in non-decreasing order using the Bubble Sort algorithm. The function should use only constant space complexity and should not use any built-in sorting functions or libraries. The code should start with:
```python
from typing import List
def bubble_sort(... | 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]) -> None:
"""
Sorts the elements in the given integer array in non-decreasing order using the Bubble Sort algorithm.
Args:
arr (List[int]): The integer array t... |
python_sharegpt_23k | 7,785 | Write a Python function to convert a given string to all lowercase characters, while ignoring any special characters or numbers in the string. The function should preserve the positions of the special characters and numbers in the original string.
The code should start with:
```python
def convert_to_lowercase(string: ... | Here is the Python code to solve the given problem in the markdown block:
```python
def convert_to_lowercase(string: str) -> str:
"""
Converts all alphabetic characters in the given string to lowercase, while preserving the positions of special characters and numbers.
Args:
string (str): The input ... |
python_sharegpt_23k | 7,786 | Write a Python function to find the index of the smallest number in a given array of integers. If there are multiple occurrences of the smallest number, return the index of the first occurrence.
The code should start with:
```python
from typing import List
def find_smallest_index(array: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_smallest_index(array: List[int]) -> int:
"""
Find the index of the smallest number in the given array. If there are multiple occurrences of the smallest number, return the index of the first occ... |
python_sharegpt_23k | 7,789 | Write a Python function to convert the elements of a given array of strings to integers, excluding any elements that are not numeric strings. The converted integers should then be sorted in descending order and stored in a new array. You are not allowed to use any built-in sorting functions or libraries.
The code shou... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def convert_and_sort_descending(arr: List[str]) -> List[int]:
"""
Converts the elements of a given array of strings to integers, excluding any elements that are not numeric strings.
The converted int... |
python_sharegpt_23k | 7,797 | Write a Python function to find the longest common substring between two given strings `s1` and `s2`. The function should return the longest substring that appears in both strings. The strings may contain uppercase and lowercase letters, numbers, and special characters.
The code should start with:
```python
def longes... | Here is the Python code to solve the given problem in the markdown block:
```python
def longest_common_substring(s1: str, s2: str) -> str:
"""
Find the longest common substring between two given strings.
Args:
s1 (str): The first string.
s2 (str): The second string.
Returns:
st... |
python_sharegpt_23k | 7,798 | Write a Python function to sort a list using the merge sort algorithm. The function should be able to handle lists of integers and return the sorted list. The code should start with:
```python
from typing import List
def merge_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 merge_sort(lst: List[int]) -> List[int]:
"""
Sorts a list of integers using the merge sort algorithm.
Args:
lst (List[int]): The list of integers to be sorted.
Returns:
List... |
python_sharegpt_23k | 7,799 | Write a Python function that demonstrates the concept of closures by creating a function that returns another function. The returned function should be able to access and use variables from its enclosing scope. The task is to create a function `create_multiplier` that takes a single argument `factor` and returns a new ... | Here is the Python code to solve the given problem in the markdown block:
```python
def create_multiplier(factor: int) -> callable:
"""
Creates and returns a function that multiplies its argument by a given factor.
Args:
factor (int): The factor to multiply by.
Returns:
callable: A fun... |
python_sharegpt_23k | 7,800 | Write a Python function to clean a given string by removing all non-alphanumeric characters and converting all uppercase letters to lowercase. The function should return the cleaned string.
The code should start with:
```python
import re
def clean_string(text: str) -> str:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
import re
def clean_string(text: str) -> str:
"""
Cleans the given string by removing all non-alphanumeric characters and converting all uppercase letters to lowercase.
Args:
text (str): The input string to be clea... |
python_sharegpt_23k | 7,801 | Write a Python function to find all the distinct values in a given list of integers and return them in ascending order. The input list can contain duplicate values, but the output list should only contain the distinct values in ascending order. The function should handle non-empty lists.
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 find_distinct_values(nums: List[int]) -> List[int]:
"""
Find all the distinct values in a given list of integers and return them in ascending order.
Args:
nums (List[int]): A list of int... |
python_sharegpt_23k | 7,802 | Write a Python function to generate a new list containing only the even numbers from a given list of integers. The function should use bitwise operations to determine if a number is even, without using any built-in functions or operators for checking if a number is even.
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 filter_even_numbers(numbers: List[int]) -> List[int]:
"""
Generates a new list containing only the even numbers from the given list of integers.
Uses bitwise operations to determine if a number i... |
python_sharegpt_23k | 7,806 | Write a Python function to compute the sum of all prime numbers between 0 and a given upper limit `n`. The function should use an efficient algorithm to determine if a number is prime and accumulate the sum of all prime numbers up to `n`.
The code should start with:
```python
import math
def sum_of_primes(n: int) -> ... | Here is the Python code to solve the given problem in the markdown block:
```python
import math
def sum_of_primes(n: int) -> int:
"""
Compute the sum of all prime numbers between 0 and n.
Args:
n (int): The upper limit of the range to consider for prime numbers.
Returns:
int: The sum ... |
python_sharegpt_23k | 7,807 | Write a Python function to extract and sort all even numbers from a given array of integers. The array can contain positive and negative integers ranging from -10^9 to 10^9, and its length can be between 1 and 10^5 (inclusive). The function should return a list of even numbers sorted in ascending order.
The code shoul... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def get_sorted_even_numbers(arr: List[int]) -> List[int]:
"""
Extracts and sorts all even numbers from a given array of integers.
Args:
arr (List[int]): The input array of integers.
Ret... |
python_sharegpt_23k | 7,811 | Write a Python function to multiply two given positive integers without using the multiplication operator. The function should use a loop to add one of the numbers to itself repeatedly, the number of times specified by the other number. The code should start with:
```python
def multiply_without_operator(a: int, b: int)... | Here is the Python code to solve the given problem in the markdown block:
:
```python
def multiply_without_operator(a: int, b: int) -> int:
"""
Multiplies two positive integers without using the multiplication operator.
Args:
a (int): The first positive integer.
b (int): The second positive... |
python_sharegpt_23k | 7,814 | Write a Python function to divide two integers without using the division operator and with a time complexity of O(log N), where N is the magnitude of the dividend. The function should handle special cases such as division by zero and negative numbers. The code should start with:
```python
def divide_integers(dividend:... | Here is the Python code to solve the given problem in the markdown block:
```python
def divide_integers(dividend: int, divisor: int) -> int:
"""
Divide two integers without using the division operator and with a time complexity of O(log N),
where N is the magnitude of the dividend.
Args:
divide... |
python_sharegpt_23k | 7,821 | Write a Python function to clone a given list and reverse the order of its elements. The function should return the reversed clone of the input list. The code should start with:
```python
from typing import List
def reverse_clone_list(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 reverse_clone_list(arr: List[int]) -> List[int]:
"""
Clones a given list and reverses the order of its elements.
Args:
arr (List[int]): The input list to be cloned and reversed.
R... |
python_sharegpt_23k | 7,823 | Write a Python function to calculate the sum of a list of numbers while ensuring that the input list contains only numeric values. If any non-numeric values are found, raise a `ValueError` with an appropriate error message. The function should start with:
```python
from typing import List
def calculate_sum(numbers: Li... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def calculate_sum(numbers: List[float]) -> float:
"""
Calculate the sum of a list of numbers, ensuring all elements are numeric.
Args:
numbers (List[float]): A list of numbers (integers or f... |
python_sharegpt_23k | 7,825 | Write a Python function to debug and modify the given loop to ensure it terminates before 10 iterations. The function should print the value of `i` along with the message "Something is wrong!" on each iteration. The loop should terminate when `i` reaches a specific condition that you define.
The code should start with... | Here is the Python code to solve the given problem in the markdown block:
```python
def debug_loop():
"""
Debugs and modifies the given loop to ensure it terminates before 10 iterations.
The loop prints the value of 'i' along with the message "Something is wrong!" on each iteration.
The loop terminates ... |
python_sharegpt_23k | 7,826 | Write a Python function to find the number of occurrences of a given time value in an array and determine the positions of each occurrence. The function should not use any built-in functions or libraries for time/date manipulation.
The code should start with:
```python
from typing import List, Dict, Union
def find_ti... | Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List, Dict, Union
def find_time_occurrences(array: List[str]) -> Dict[str, Dict[str, Union[int, List[int]]]]:
"""
Find the number of occurrences of each time value in the given array and determine the positio... |
python_sharegpt_23k | 7,827 | Write a Python function to determine if a given number is both a prime number and a palindrome. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A palindrome is a number that reads the same backward as forward.
The code should start with:
```python
def is_prime_p... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_prime_palindrome(num: int) -> bool:
"""
Determine if a given number is both a prime number and a palindrome.
Args:
num (int): The number to check.
Returns:
bool: True if the number is both prime ... |
python_sharegpt_23k | 7,829 | Write a Python function to calculate the power of two numbers (base^exp) using only basic arithmetic operations (addition, subtraction, multiplication, and division) and without using any built-in exponentiation or logarithmic functions. The function should handle both positive and negative exponents.
The code should ... | Here is the Python code to solve the given problem in the markdown block:
```python
def power(base: float, exp: int) -> float:
"""
Calculate the power of two numbers (base^exp) using only basic arithmetic operations.
Args:
base (float): The base number.
exp (int): The exponent.
Returns... |
python_sharegpt_23k | 7,836 | Write a Python function to find the greatest number from a given array that can contain both positive and negative integers. The function should return the greatest number in the array.
The code should start with:
```python
from typing import List
def find_greatest_number(arr: List[int]) -> int:
```
| Here is the Python code to solve the given problem in the markdown block:
```python
from typing import List
def find_greatest_number(arr: List[int]) -> int:
"""
Find the greatest number from a given array of integers.
Args:
arr (List[int]): The array of integers from which to find the greatest num... |
python_sharegpt_23k | 7,838 | Write a Python function to detect if a given string is a palindrome, considering the following constraints:
- The algorithm must have a time complexity of O(n), where n is the length of the input string.
- The algorithm must not use any built-in string manipulation functions (e.g., `reverse()`, `join()`, etc.).
- The a... | Here is the Python code to solve the given problem in the markdown block:
```python
def is_palindrome(s: str) -> bool:
"""
Determines if a given string is a palindrome, ignoring non-alphanumeric characters and case.
Args:
s (str): The input string to check for palindrome properties.
Returns:
... |
python_sharegpt_23k | 7,846 | Write a Python function to detect if there is at least one negative number in a given array of numbers without using any comparison operators (e.g., <, >, <=, >=, ==). The function should use only mathematical operations to determine if a number is negative.
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 detect_negative_number(numbers: List[int]) -> bool:
"""
Detects if there is at least one negative number in the given array of numbers
without using any comparison operators.
Args:
n... |
python_sharegpt_23k | 7,851 | Write a Python function to count the number of alphabets in a given string, ignoring any punctuation marks, special characters, and spaces. The function should treat uppercase and lowercase letters as separate entities and only consider the English alphabet. The code should start with:
```python
def count_alphabets(inp... | Here is the Python code to solve the given problem in the markdown block:
```python
def count_alphabets(input_string: str) -> int:
"""
Counts the number of alphabets in a given string, ignoring punctuation, special characters, and spaces.
Uppercase and lowercase letters are treated as separate entities.
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.