Skip to content
🤔prompts chat🧠
🔍
question:def solve(grid_data): Given the initial state of the canvas, determine the minimum number of color changes that the painter needs to make to achieve the desired pattern. Input: A list of strings where the first element is the number of test cases (T). For each test case, the next element contains two integers N and M denoting the number of rows and columns of the canvas, and the subsequent N elements are strings representing the grid. Output: A list of integers where each integer represents the minimum number of color changes needed for each test case. Example: >>> solve(["2", "3 3", "RRB", "BRB", "RRB", "2 2", "RR", "BB"]) [4, 2] >>> solve(["1", "2 3", "RBR", "BBR"]) [2] >>> solve(["1", "3 3", "BRB", "BRB", "BRB"]) [3] results = [] def calculate_changes(grid, pattern1, pattern2): changes1 = changes2 = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != pattern1[i % 2][j % 2]: changes1 += 1 if grid[i][j] != pattern2[i % 2][j % 2]: changes2 += 1 return min(changes1, changes2) T = int(grid_data[0]) index = 1 for _ in range(T): N, M = map(int, grid_data[index].split()) grid = grid_data[index + 1 : index + N + 1] index += N + 1 pattern1 = [['R', 'B'], ['B', 'R']] pattern2 = [['B', 'R'], ['R', 'B']] min_changes = calculate_changes(grid, pattern1, pattern2) results.append(min_changes) return results

answer:def min_color_changes(T, test_cases): results = [] def calculate_changes(grid, pattern1, pattern2): changes1 = changes2 = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != pattern1[i % 2][j % 2]: changes1 += 1 if grid[i][j] != pattern2[i % 2][j % 2]: changes2 += 1 return min(changes1, changes2) for case in test_cases: N, M, grid = case pattern1 = [['R', 'B'], ['B', 'R']] pattern2 = [['B', 'R'], ['R', 'B']] min_changes = calculate_changes(grid, pattern1, pattern2) results.append(min_changes) return results # Function to parse input and call the main function def solve(grid_data): T = int(grid_data[0]) test_cases = [] index = 1 for _ in range(T): N, M = map(int, grid_data[index].split()) grid = grid_data[index + 1 : index + N + 1] test_cases.append((N, M, grid)) index += N + 1 results = min_color_changes(T, test_cases) return results

question:from typing import List def longest_subarray_with_limit(arr: List[int], K: int) -> int: Given an integer array `arr`, return the length of the longest contiguous subarray where the difference between the minimum and maximum values in the subarray is less than or equal to a given integer `K`. >>> longest_subarray_with_limit([8, 2, 4, 7], 4) 2 >>> longest_subarray_with_limit([10, 1, 2, 4, 7, 2], 5) 4 >>> longest_subarray_with_limit([4, 2, 2, 2, 4, 4, 2, 2], 0) 3

answer:from collections import deque from typing import List def longest_subarray_with_limit(arr: List[int], K: int) -> int: min_deque = deque() max_deque = deque() left = 0 max_length = 0 for right in range(len(arr)): while min_deque and arr[right] < arr[min_deque[-1]]: min_deque.pop() while max_deque and arr[right] > arr[max_deque[-1]]: max_deque.pop() min_deque.append(right) max_deque.append(right) while arr[max_deque[0]] - arr[min_deque[0]] > K: left += 1 if left > min_deque[0]: min_deque.popleft() if left > max_deque[0]: max_deque.popleft() max_length = max(max_length, right - left + 1) return max_length

question:def count_valid_groups(population): Returns the number of valid groups that can be formed. Each group should consist of exactly two animals from two different species. >>> count_valid_groups([2, 3, 4]) 26 >>> count_valid_groups([1, 1]) 1 >>> count_valid_groups([2, 2]) 4 >>> count_valid_groups([10**9, 10**9, 10**9]) 3000000000000000000 >>> count_valid_groups([1, 10**9]) 1000000000 >>> count_valid_groups([3, 6, 8, 10]) 229 >>> count_valid_groups([1, 2, 3, 4, 5]) 0

answer:def count_valid_groups(population): Returns the number of valid groups that can be formed. Each group should consist of exactly two animals from two different species. total_groups = 0 n = len(population) for i in range(n): for j in range(i + 1, n): total_groups += population[i] * population[j] return total_groups

question:def is_right_angle(x1, y1, x2, y2, x3, y3): Returns "RIGHT" if the points A(x1, y1), B(x2, y2), and C(x3, y3) form a right-angled triangle, otherwise returns "NOT RIGHT". pass def check_datasets(datasets): Given a list of datasets, each containing the coordinates of three points, returns a list of results where each result is either "RIGHT" or "NOT RIGHT". >>> check_datasets([(0, 0, 3, 0, 0, 4), (1, 1, 4, 5, 6, 1), (0, 0, 1, 1, 2, 2)]) ["RIGHT", "NOT RIGHT", "NOT RIGHT"] >>> check_datasets([(0, 0, 1, 0, 0, 1), (2, 3, 5, 7, 8, 3)]) ["RIGHT", "NOT RIGHT"] pass from is_right_angle_check import is_right_angle, check_datasets def test_is_right_angle(): assert is_right_angle(0, 0, 3, 0, 0, 4) == "RIGHT" assert is_right_angle(1, 1, 4, 5, 6, 1) == "NOT RIGHT" assert is_right_angle(0, 0, 1, 1, 2, 2) == "NOT RIGHT" assert is_right_angle(0, 0, 1, 0, 0, 1) == "RIGHT" assert is_right_angle(2, 3, 5, 7, 8, 3) == "NOT RIGHT" def test_check_datasets(): datasets = [ (0, 0, 3, 0, 0, 4), (1, 1, 4, 5, 6, 1), (0, 0, 1, 1, 2, 2) ] assert check_datasets(datasets) == ["RIGHT", "NOT RIGHT", "NOT RIGHT"] datasets = [ (0, 0, 1, 0, 0, 1), (2, 3, 5, 7, 8, 3) ] assert check_datasets(datasets) == ["RIGHT", "NOT RIGHT"]

answer:def is_right_angle(x1, y1, x2, y2, x3, y3): Returns "RIGHT" if the points A(x1, y1), B(x2, y2), and C(x3, y3) form a right-angled triangle, otherwise returns "NOT RIGHT". def dist_sq(xa, ya, xb, yb): return (xb - xa) ** 2 + (yb - ya) ** 2 a2 = dist_sq(x2, y2, x3, y3) b2 = dist_sq(x1, y1, x3, y3) c2 = dist_sq(x1, y1, x2, y2) if a2 + b2 == c2 or a2 + c2 == b2 or b2 + c2 == a2: return "RIGHT" else: return "NOT RIGHT" def check_datasets(datasets): results = [] for dataset in datasets: x1, y1, x2, y2, x3, y3 = dataset result = is_right_angle(x1, y1, x2, y2, x3, y3) results.append(result) return results

Released under the chat License.

has loaded