Appearance
question:class ArrayOperations: def __init__(self, n, a): self.n = n self.a = a self.prefix_sum = self._build_prefix_sum(a) def _build_prefix_sum(self, a): Build the prefix sum array for efficient range queries. prefix_sum = [0] * (self.n + 1) for i in range(1, self.n + 1): prefix_sum[i] = prefix_sum[i - 1] + a[i - 1] return prefix_sum def update(self, x, y): Update the element at index x to y and adjust the prefix sum array. delta = y - self.a[x - 1] self.a[x - 1] = y for i in range(x, self.n + 1): self.prefix_sum[i] += delta def query(self, l, r): Query the sum of the subarray from index l to r (inclusive). return self.prefix_sum[r] - self.prefix_sum[l - 1] def process_operations(n, q, a, operations): Process a list of operations on an array, including updates and range queries. Args: n (int): The length of the array. q (int): Number of operations to perform. a (list of int): The initial elements of the array. operations (list of tuples): The operations to perform. Returns: list of int: The result of each query operation. Example: >>> process_operations(5, 5, [1, 2, 3, 4, 5], [(2, 1, 3), (1, 3, 10), (2, 1, 3), (2, 4, 5), (1, 5, 6)]) [6, 13, 9] array_ops = ArrayOperations(n, a) results = [] for op in operations: if op[0] == 1: x, y = op[1], op[2] array_ops.update(x, y) elif op[0] == 2: l, r = op[1], op[2] results.append(array_ops.query(l, r)) return results # Unit Tests def test_case_1(): n = 5 q = 5 a = [1, 2, 3, 4, 5] operations = [ (2, 1, 3), (1, 3, 10), (2, 1, 3), (2, 4, 5), (1, 5, 6) ] assert process_operations(n, q, a, operations) == [6, 13, 9] def test_case_2(): n = 3 q = 3 a = [1, 6, 9] operations = [ (2, 1, 3), (1, 2, 3), (2, 2, 3) ] assert process_operations(n, q, a, operations) == [16, 12] def test_case_3(): n = 2 q = 2 a = [1000000000, 1000000000] operations = [ (2, 1, 2), (1, 2, 1) ] assert process_operations(n, q, a, operations) == [2000000000] def test_case_4(): n = 4 q = 4 a = [1, 4, 5, 6] operations = [ (2, 1, 2), (1, 1, 3), (1, 2, 2), (2, 1, 2) ] assert process_operations(n, q, a, operations) == [5, 5] def test_case_5(): n = 3 q = 4 a = [5, 8, 7] operations = [ (1, 3, 4), (2, 1, 3), (1, 2, 1), (2, 1, 2) ] assert process_operations(n, q, a, operations) == [17, 6]
answer:class ArrayOperations: def __init__(self, n, a): self.n = n self.a = a self.prefix_sum = self._build_prefix_sum(a) def _build_prefix_sum(self, a): prefix_sum = [0] * (self.n + 1) for i in range(1, self.n + 1): prefix_sum[i] = prefix_sum[i - 1] + a[i - 1] return prefix_sum def update(self, x, y): delta = y - self.a[x - 1] self.a[x - 1] = y for i in range(x, self.n + 1): self.prefix_sum[i] += delta def query(self, l, r): return self.prefix_sum[r] - self.prefix_sum[l - 1] def process_operations(n, q, a, operations): array_ops = ArrayOperations(n, a) results = [] for op in operations: if op[0] == 1: x, y = op[1], op[2] array_ops.update(x, y) elif op[0] == 2: l, r = op[1], op[2] results.append(array_ops.query(l, r)) return results
question:def minimum_stops(n: int, a: int, b: int) -> int: Returns the minimum number of stops required to travel from station a to station b along a looped track. >>> minimum_stops(6, 2, 5) 3 >>> minimum_stops(6, 5, 2) 3 >>> minimum_stops(10, 1, 10) 1
answer:def minimum_stops(n, a, b): Returns the minimum number of stops required to travel from station a to station b along a looped track. if a > b: a, b = b, a # Distance traveling forward from a to b forward_distance = b - a # Distance traveling backward from a to b backward_distance = n - b + a # Return the minimum of the two distances return min(forward_distance, backward_distance)
question:def find_three_sum(arr, target): This function finds a triplet in the array which sums to the target value. Parameters: arr (list): List of integers. target (int): The target sum. Returns: tuple: A tuple of three integers that add up to the target, or None if no such triplet exists. Examples: >>> find_three_sum([12, 3, 4, 1, 6, 9], 24) (12, 3, 9) >>> find_three_sum([1, 2, 3, 4, 5], 10) (2, 3, 5) >>> find_three_sum([-1, 0, 1, 2, -1, -4], 0) (-1, -1, 2) >>> find_three_sum([1, 2, 3], 7) None
answer:def find_three_sum(arr, target): This function finds a triplet in the array which sums to the target value. Parameters: arr (list): List of integers. target (int): The target sum. Returns: tuple: A tuple of three integers that add up to the target, or None if no such triplet exists. n = len(arr) arr.sort() for i in range(n - 2): left, right = i + 1, n - 1 while left < right: current_sum = arr[i] + arr[left] + arr[right] if current_sum == target: return (arr[i], arr[left], arr[right]) elif current_sum < target: left += 1 else: right -= 1 return None
question:def determine_swipe_direction(n: int, points: List[Tuple[int, int]]) -> str: Determines if a given sequence of touch points denotes a clockwise or counter-clockwise circular swipe. :param n: The number of touch points :param points: A list of tuples (x, y) representing the coordinates of the touch points :return: A string, either "Clockwise" or "Counter-Clockwise" >>> determine_swipe_direction(4, [(0, 0), (1, 0), (1, 1), (0, 1)]) 'Counter-Clockwise' >>> determine_swipe_direction(4, [(0, 0), (0, 1), (1, 1), (1, 0)]) 'Clockwise' >>> determine_swipe_direction(3, [(0, 0), (1, 0), (0, 1)]) 'Counter-Clockwise' >>> determine_swipe_direction(3, [(0, 0), (0, 1), (1, 0)]) 'Clockwise' >>> determine_swipe_direction(5, [(0, 0), (2, -1), (3, 1), (2, 3), (0, 2)]) 'Counter-Clockwise' >>> determine_swipe_direction(5, [(0, 0), (0, 2), (2, 3), (3, 1), (2, -1)]) 'Clockwise'
answer:def determine_swipe_direction(n, points): Determines if a given sequence of touch points denotes a clockwise or counter-clockwise circular swipe. :param n: The number of touch points :param points: A list of tuples (x, y) representing the coordinates of the touch points :return: A string, either "Clockwise" or "Counter-Clockwise" total = 0 for i in range(n): x1, y1 = points[i] x2, y2 = points[(i + 1) % n] total += (x2 - x1) * (y2 + y1) return "Clockwise" if total > 0 else "Counter-Clockwise"