Skip to content
🤔prompts chat🧠
🔍
question:def longestEvenSubarray(arr): Returns the length of the longest contiguous subarray that contains only even numbers. Args: arr (List[int]): An array of integers. Returns: int: The length of the longest contiguous subarray with only even numbers. Examples: >>> longestEvenSubarray([1, 2, 4, 6, 1, 2]) 3 >>> longestEvenSubarray([1, 1, 1, 2, 4, 6, 8, 1, 1, 1]) 4 >>> longestEvenSubarray([2, 2, 2, 2]) 4 >>> longestEvenSubarray([1, 3, 5, 7, 9]) 0 >>> longestEvenSubarray([1, 2, 4, 6, 8, 10, 3, 6, 4, 2, 0]) 5 >>> longestEvenSubarray([]) 0 >>> longestEvenSubarray([1]) 0 >>> longestEvenSubarray([4]) 1

answer:def longestEvenSubarray(arr): Returns the length of the longest contiguous subarray that contains only even numbers. max_length = 0 current_length = 0 for num in arr: if num % 2 == 0: current_length += 1 max_length = max(max_length, current_length) else: current_length = 0 return max_length

question:from typing import List def power_set(s: List[int]) -> List[List[int]]: Generates the power set (set of all subsets) of a given set of integers. :param s: Set of integers. :return: A list of lists, where each inner list is a subset of the input set. >>> power_set([1, 2, 3]) [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] >>> power_set([1, 2]) [[], [1], [2], [1, 2]] def test_power_set_empty(): assert power_set([]) == [[]] def test_power_set_single_element(): assert power_set([1]) == [[], [1]] def test_power_set_two_elements(): assert power_set([1, 2]) == [[], [1], [2], [1, 2]] def test_power_set_three_elements(): assert power_set([1, 2, 3]) == [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] def test_power_set_unordered_input(): assert power_set([3, 1, 2]) == [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] def test_power_set_four_elements(): assert power_set([1, 2, 3, 4]) == [ [], [1], [2], [3], [4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4] ]

answer:from itertools import chain, combinations def power_set(s): Generates the power set (set of all subsets) of a given set of integers. :param s: Set of integers. :return: A list of lists, where each inner list is a subset of the input set. s = sorted(s) power_set = list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1))) power_set = [list(subset) for subset in power_set] # Sort by length and then lexicographically power_set.sort(key=lambda x: (len(x), x)) return power_set

question:[Completion Task in Python] def find_duplicates(arr: list) -> list: Returns a list of duplicate elements from the input list arr, with duplicates reported only once in the order of their first appearance. >>> find_duplicates([1, 2, 3, 4, 5]) == [] >>> find_duplicates([1, 2, 3, 1, 5, 2]) == [1, 2] >>> find_duplicates([10, 20, 10, 20, 10, 20]) == [10, 20] >>> find_duplicates([]) == []

answer:def find_duplicates(arr): Returns a list of duplicate elements from the input list arr, with duplicates reported only once in the order of their first appearance. seen = set() duplicates = set() result = [] for num in arr: if num in seen: if num not in duplicates: duplicates.add(num) result.append(num) else: seen.add(num) return result

question:def countDistinctInRange(nums: List[int], start: int, end: int) -> int: Returns the count of distinct integers within the specified inclusive range. >>> countDistinctInRange([1, 2, 3, 4, 4, 5, 6, 7], 3, 6) == 4 >>> countDistinctInRange([1, 2, 3, 4, 4, 5, 6, 7], 8, 10) == 0 >>> countDistinctInRange([1, 2, 3, 4, 4, 5, 6, 7], 1, 7) == 7 >>> countDistinctInRange([1, 2, 3, 3, 3, 3, 3], 3, 3) == 1 >>> countDistinctInRange([1, 2, 3, 4, 4, 5, 6, 7], 4, 4) == 1 >>> countDistinctInRange([-3, -2, -1, 0, 1, 2, 3], -2, 1) == 4 >>> countDistinctInRange([], 3, 6) == 0

answer:def countDistinctInRange(nums, start, end): Returns the count of distinct integers within the specified inclusive range. Args: nums (list of int): List of integers. start (int): Start of the range (inclusive). end (int): End of the range (inclusive). Returns: int: Count of distinct integers within the specified range. range_set = {num for num in nums if start <= num <= end} return len(range_set)

Released under the chat License.

has loaded