Appearance
question:class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def closest_value(root: TreeNode, target: float) -> int: Given a binary search tree (BST) and a target value, determine the value in the BST that is closest to the target. Example 1: >>> root = TreeNode(4, TreeNode(2, TreeNode(1), TreeNode(3)), TreeNode(5)) >>> target = 3.714286 >>> closest_value(root, target) 4 Example 2: >>> root = TreeNode(1) >>> target = 4.428571 >>> closest_value(root, target) 1
answer:class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def closest_value(root, target): closest = root.val current = root while current: if abs(current.val - target) < abs(closest - target): closest = current.val if target < current.val: current = current.left else: current = current.right return closest
question:def shortest_odd_sum_subsequence_length(n: int, sequence: List[int]) -> int: Find the length of the shortest subsequence such that the sum of its elements is an odd number. >>> shortest_odd_sum_subsequence_length(4, [2, 4, 6, 8]) -1 >>> shortest_odd_sum_subsequence_length(5, [1, 2, 3, 4, 5]) 1 >>> shortest_odd_sum_subsequence_length(3, [2, 3, 6]) 1 pass from typing import List def test_all_even(): assert shortest_odd_sum_subsequence_length(4, [2, 4, 6, 8]) == -1 def test_mixed_numbers(): assert shortest_odd_sum_subsequence_length(5, [1, 2, 3, 4, 5]) == 1 def test_single_odd(): assert shortest_odd_sum_subsequence_length(1, [3]) == 1 def test_single_even(): assert shortest_odd_sum_subsequence_length(1, [2]) == -1 def test_large_case_all_even(): assert shortest_odd_sum_subsequence_length(10**6, [2] * 10**6) == -1 def test_large_case_mixed(): sequence = [2] * (10**6 - 1) + [3] assert shortest_odd_sum_subsequence_length(10**6, sequence) == 1 def test_empty_case(): assert shortest_odd_sum_subsequence_length(0, []) == -1
answer:def shortest_odd_sum_subsequence_length(n, sequence): # Check if there is any odd number in the sequence for num in sequence: if num % 2 != 0: return 1 # If no odd number is found, return -1 return -1
question:def max_beauty(t: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Calculate the maximum beauty of any subsegment of the given sequence in each test case. Args: t (int): The number of test cases. test_cases (List[Tuple[int, List[int]]]): A list of tuples where each tuple contains a single integer n (length of the sequence) and a list of n integers representing the sequence. Returns: List[int]: A list of integers where each integer is the maximum beauty of any subsegment of the corresponding test case. Examples: >>> max_beauty(3, [(6, [8, 2, 2, 8, 3, 5]), (5, [1, 2, 2, 3, 4]), (4, [4, 4, 4, 4])]) [18, 10, 4]
answer:def max_beauty(t, test_cases): results = [] for n, b in test_cases: max_beauty = 0 for i in range(n): unique_elements = set() current_beauty = 0 for j in range(i, n): if b[j] not in unique_elements: unique_elements.add(b[j]) current_beauty += b[j] max_beauty = max(max_beauty, current_beauty) results.append(max_beauty) return results
question:def is_perfect_number(n: int) -> bool: Determines if a number n is a perfect number. >>> is_perfect_number(6) True >>> is_perfect_number(28) True >>> is_perfect_number(12) False >>> is_perfect_number(1) False
answer:def is_perfect_number(n): Determines if a number n is a perfect number. if n < 2: return False sum_divisors = 1 for i in range(2, int(n**0.5) + 1): if n % i == 0: sum_divisors += i if i != n // i: sum_divisors += n // i return sum_divisors == n