Appearance
question:from typing import List def determine_sequence(n: int, m: int, grid: List[str]) -> str: Determines the sequence of moves to place the watering system in the target cell. Args: n (int): Number of rows in the farm grid. m (int): Number of columns in the farm grid. grid (List[str]): Description of the farm grid. Returns: str: "YES" followed by the sequence of moves and pushes, or "NO" if not possible. >>> determine_sequence(5, 5, [".....", ".R..X", "..X..", "..W..", "....T"]) "YESnDDRRPUU" >>> determine_sequence(3, 3, ["RXX", ".XW", "..T"]) "NO"
answer:from collections import deque # Directions for moving and pushing the system & corresponding changes in coordinates dirs = [("U", -1, 0), ("D", 1, 0), ("L", 0, -1), ("R", 0, 1)] pdirs = {"U": ("PU", -2, 0), "D": ("PD", 2, 0), "L": ("PL", 0, -2), "R": ("PR", 0, 2)} def bfs(start, is_valid): Standard BFS for shortest path search q = deque([(start[0], start[1], [])]) visited = {start} while q: x, y, path = q.popleft() for d, dx, dy in dirs: nx, ny = x + dx, y + dy if is_valid(nx, ny) and (nx, ny) not in visited: visited.add((nx, ny)) q.append((nx, ny, path + [d])) return visited def is_valid_factory(n, m, grid): Returns a function for valid cell checking def is_valid(x, y): return 0 <= x < n and 0 <= y < m and grid[x][y] != "X" return is_valid def determine_sequence(n, m, grid): # Locate the positions of robot, watering system, and target cell for i in range(n): for j in range(m): if grid[i][j] == "R": robot_start = (i, j) elif grid[i][j] == "W": water_start = (i, j) elif grid[i][j] == "T": target = (i, j) # Check if robot can reach any cell adjacent to the watering system start valid_r = is_valid_factory(n, m, grid) visited = bfs(robot_start, valid_r) water_adjacent_reachable = any((water_start[0] + dx, water_start[1] + dy) in visited for _, dx, dy in dirs) if not water_adjacent_reachable: return "NO" # Initial state for BFS: (robot pos, watering system pos, sequence of moves) initial_state = (robot_start, water_start, []) # BFS to determine sequence q = deque([initial_state]) visited = set([(robot_start, water_start)]) while q: (rx, ry), (wx, wy), seq = q.popleft() # Check if current watering system position is the target if (wx, wy) == target: return "YESn" + ''.join(seq) # Explore all directions for robot movement without pushing for dir, dx, dy in dirs: nrx, nry = rx + dx, ry + dy if valid_r(nrx, nry) and (nrx, nry) != (wx, wy) and ((nrx, nry), (wx, wy)) not in visited: visited.add(((nrx, nry), (wx, wy))) q.append(((nrx, nry), (wx, wy), seq + [dir])) # Explore all directions for pushing the watering system for dir in pdirs: pdir, dx, dy = pdirs[dir] # Robot should be next to water system and can move to push it prx, pry = wx - dirs[["U", "D", "L", "R"].index(dir)][1], wy - dirs[["U", "D", "L", "R"].index(dir)][2] if (rx, ry) == (prx, pry): nwx, nwy = wx + dx//2, wy + dy//2 if valid_r(nwx, nwy) and ((prx, pry), (nwx, nwy)) not in visited: visited.add(((wx, wy), (nwx, nwy))) q.append(((wx, wy), (nwx, nwy), seq + [pdir])) return "NO" def main(n, m, grid): result = determine_sequence(n, m, grid) print(result)
question:def rearrange_string(s: str) -> str: This function takes a string `s` and rearranges it such that all letters come before digits while preserving their original order. :param s: Input string containing letters and digits :return: A string with all letters followed by all digits in their respective original order Examples: >>> rearrange_string("a1b2c3d4") == "abcd1234" >>> rearrange_string("a5b6Z7k8Y9") == "abZkY56789" from solution import rearrange_string def test_rearrange_string_basic(): assert rearrange_string('a1b2c3d4') == 'abcd1234' assert rearrange_string('a5b6Z7k8Y9') == 'abZkY56789' def test_rearrange_string_no_digits(): assert rearrange_string('abcdefg') == 'abcdefg' assert rearrange_string('XYZ') == 'XYZ' def test_rearrange_string_no_letters(): assert rearrange_string('123456') == '123456' assert rearrange_string('09876') == '09876' def test_rearrange_string_mixed_case(): assert rearrange_string('A1a2B3b4') == 'AaBb1234' assert rearrange_string('x7Y8z9') == 'xYz789' def test_rearrange_string_empty_string(): assert rearrange_string('') == '' def test_rearrange_string_single_characters(): assert rearrange_string('a') == 'a' assert rearrange_string('1') == '1' assert rearrange_string('A') == 'A' assert rearrange_string('9') == '9'
answer:def rearrange_string(s): This function takes a string `s` and rearranges it such that all letters come before digits while preserving their original order. :param s: Input string containing letters and digits :return: A string with all letters followed by all digits in their respective original order Example: rearrange_string("a1b2c3d4") -> "abcd1234" rearrange_string("a5b6Z7k8Y9") -> "abZkY56789" letters = [char for char in s if char.isalpha()] digits = [char for char in s if char.isdigit()] return ''.join(letters + digits)
question:def solve(n: int, initial_tree: List[Tuple[int, int, int]], target_tree: List[Tuple[int, int, int]]) -> str: Determine if it is possible to transform the initial binary tree into a target binary tree. Args: n: int - number of nodes in the trees initial_tree: list of tuples describing the initial tree with (value, left_child, right_child) target_tree: list of tuples describing the target tree with (value, left_child, right_child) Returns: A string indicating whether the transformation is possible, and if so, the sequence of operations. >>> solve(6, [(1, 2, 3), (2, 4, -1), (3, -1, 5), (4, -1, -1), (5, -1, 6), (6, -1, -1)], [(1, 3, 2), (3, 5, -1), (2, 6, 4), (5, -1, -1), (6, -1, -1), (4, -1, -1)]) "POSSIBLEn5nSWAP 1nSWAP 2nREMOVE 4nSWAP 3nREMOVE 6n" >>> solve(4, [(1, 2, -1), (2, 3, -1), (3, 4, -1), (4, -1, -1)], [(1, 2, -1), (2, -1, 3), (3, -1, 4), (4, -1, -1)]) "IMPOSSIBLE" >>> solve(3, [(1, 2, 3), (2, -1, -1), (3, -1, -1)], [(1, 2, 3), (2, -1, -1), (3, -1, -1)]) "POSSIBLEn0n"
answer:class TreeNode: def __init__(self, value=-1, left=None, right=None): self.value = value self.left = left self.right = right def build_tree(node_descriptors): nodes = {} for desc in node_descriptors: value, left, right = desc if value not in nodes: nodes[value] = TreeNode(value) if left != -1: if left not in nodes: nodes[left] = TreeNode(left) nodes[value].left = nodes[left] if right != -1: if right not in nodes: nodes[right] = TreeNode(right) nodes[value].right = nodes[right] return nodes[node_descriptors[0][0]] # return the root def trees_are_identical(node1, node2): if not node1 and not node2: return True if not node1 or not node2: return False return (node1.value == node2.value and trees_are_identical(node1.left, node2.left) and trees_are_identical(node1.right, node2.right)) def swap_children(node): node.left, node.right = node.right, node.left def can_transform(init_root, target_root): if trees_are_identical(init_root, target_root): return True, [] operations = [] def transform(node1, node2): if trees_are_identical(node1, node2): return True if node1.value != node2.value: return False # Recursively swap and transform if trees_are_identical(node1.right, node2.left) and trees_are_identical(node1.left, node2.right): operations.append(f"SWAP {node1.value}") swap_children(node1) if node1.left and node2.left and transform(node1.left, node2.left): return transform(node1.right, node2.right) elif node1.left and node2.right and transform(node1.left, node2.right): operations.append(f"SWAP {node1.value}") swap_children(node1) return transform(node1.right, node2.left) return False if transform(init_root, target_root): return True, operations else: return False, [] def solve(n, initial_tree, target_tree): if n == 0: return "POSSIBLEn0n" initial_root = build_tree(initial_tree) target_root = build_tree(target_tree) possible, operations = can_transform(initial_root, target_root) if not possible: return "IMPOSSIBLE" else: return f"POSSIBLEn{len(operations)}n" + "n".join(operations)
question:def unique_aspect_ratios(T: int, test_cases: List[List[int]]) -> List[int]: Returns the number of unique aspect ratios for each test case. Args: T (int): the number of test cases. test_cases (List[List[int]]): the test cases, each represented as a list of integers. Returns: List[int]: a list containing the number of unique aspect ratios for each test case. Example: >>> unique_aspect_ratios(2, [[4, 4, 3, 16, 9, 4, 3, 2, 1], [3, 1, 1, 2, 2, 3, 3]]) [3, 1] >>> unique_aspect_ratios(1, [[5, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7]]) [5]
answer:def gcd(a, b): while b: a, b = b, a % b return a def unique_aspect_ratios(T, test_cases): results = [] for case in test_cases: N = case[0] ratios = set() for i in range(1, len(case), 2): width, height = case[i], case[i + 1] g = gcd(width, height) norm_width, norm_height = width // g, height // g ratios.add((norm_width, norm_height)) results.append(len(ratios)) return results