Appearance
question:def longest_same_parity_subarray(arr: List[int]) -> int: Given an array of integers, determine the length of the longest contiguous subarray such that every element in the subarray is of the same parity (either all even or all odd). >>> longest_same_parity_subarray([1, 3, 5, 4, 6, 8]) == 3 >>> longest_same_parity_subarray([2, 4, 7, 7, 8]) == 2 >>> longest_same_parity_subarray([5, 9, 2, 2, 2, 3, 1]) == 3 >>> longest_same_parity_subarray([1, 3, 5, 7]) == 4 >>> longest_same_parity_subarray([2, 4, 6, 8]) == 4 >>> longest_same_parity_subarray([1]) == 1 >>> longest_same_parity_subarray([2]) == 1 >>> longest_same_parity_subarray([]) == 0 >>> longest_same_parity_subarray([1, 2, 3, 4, 5, 6]) == 1 >>> longest_same_parity_subarray([2, 1, 4, 3, 6, 5]) == 1
answer:from typing import List def longest_same_parity_subarray(arr: List[int]) -> int: if not arr: return 0 max_length = 1 current_length = 1 current_parity = arr[0] % 2 for i in range(1, len(arr)): if (arr[i] % 2) == current_parity: current_length += 1 else: max_length = max(max_length, current_length) current_length = 1 current_parity = arr[i] % 2 # Final check after loop max_length = max(max_length, current_length) return max_length
question:def update_inventory(inventory: dict, new_shipment: dict) -> dict: Updates the inventory dictionary by adding the quantities from new_shipment. If an item in new_shipment is not present in inventory, it is added to the inventory. Args: inventory (dict): The original inventory of items. new_shipment (dict): The new shipment of items to be added to the inventory. Returns: dict: The updated inventory with quantities from new_shipment added. >>> inventory = {"apples": 5, "oranges": 3, "bananas": 2} >>> new_shipment = {"apples": 3, "oranges": 2, "grapes": 4} >>> update_inventory(inventory, new_shipment) {'apples': 8, 'oranges': 5, 'bananas': 2, 'grapes': 4}
answer:def update_inventory(inventory, new_shipment): Updates the inventory dictionary by adding the quantities from new_shipment. If an item in new_shipment is not present in inventory, it is added to the inventory. Args: inventory (dict): The original inventory of items. new_shipment (dict): The new shipment of items to be added to the inventory. Returns: dict: The updated inventory with quantities from new_shipment added. for item, quantity in new_shipment.items(): if item in inventory: inventory[item] += quantity else: inventory[item] = quantity return inventory
question:from typing import List def common_prefix(strings: List[str]) -> str: Returns the longest common prefix shared among all the strings in the list. If there is no common prefix, returns an empty string. >>> common_prefix(["flower", "flow", "flight"]) 'fl' >>> common_prefix(["dog", "racecar", "car"]) '' >>> common_prefix(["interspecies", "interstellar", "interstate"]) 'inters' >>> common_prefix(["throne", "throne"]) 'throne' >>> common_prefix(["hello", "Hello"]) '' >>> common_prefix(["single"]) 'single' >>> common_prefix([]) '' >>> common_prefix(["prefix", "pre", "prepare"]) 'pre' >>> common_prefix(["short", "shorten", "shortest"]) 'short'
answer:def common_prefix(strings): Returns the longest common prefix shared among all the strings in the list. If there is no common prefix, returns an empty string. if not strings: return "" # Sort the list to easily compare the first and last strings strings.sort() first = strings[0] last = strings[-1] i = 0 while i < len(first) and i < len(last) and first[i] == last[i]: i += 1 return first[:i]
question:def smallest_positive_integer_not_in_sum(lst: List[int]) -> int: Calculate the smallest positive integer that is not present in a given list and is not the sum of any subset of integers from the list. >>> smallest_positive_integer_not_in_sum([1, 2, 3, 10]) 7 >>> smallest_positive_integer_not_in_sum([1, 1, 1, 1]) 5 from solution import smallest_positive_integer_not_in_sum def test_case1(): assert smallest_positive_integer_not_in_sum([1, 2, 3, 10]) == 7 def test_case2(): assert smallest_positive_integer_not_in_sum([1, 1, 1, 1]) == 5 def test_case3(): assert smallest_positive_integer_not_in_sum([1, 2, 2, 5, 7]) == 18 def test_case4(): assert smallest_positive_integer_not_in_sum([2, 3, 4, 8]) == 1 # 1 is the smallest integer not in the list def test_case5(): assert smallest_positive_integer_not_in_sum([1, 2, 5, 10]) == 4 def test_case6(): assert smallest_positive_integer_not_in_sum([3, 3, 3, 3]) == 1 def test_case7(): assert smallest_positive_integer_not_in_sum([1, 3, 6, 10]) == 2 def test_case8(): assert smallest_positive_integer_not_in_sum([]) == 1 # Should handle empty list as 1
answer:def smallest_positive_integer_not_in_sum(lst): Returns the smallest positive integer that is not present in the list and not the sum of any subset of integers from the list. lst.sort() smallest_missing = 1 for num in lst: if num > smallest_missing: break smallest_missing += num return smallest_missing