Skip to content
🤔prompts chat🧠
🔍
question:def is_visible(grid, start, target): Determine if the target point is visible from the start point on the given grid. Parameters: grid (list of list of int): 2D grid representing the environment with obstacles (1) and empty spaces (0). start (tuple of int): Starting point in the grid. target (tuple of int): Target point in the grid. Returns: bool: True if the target is visible from the start, False otherwise. Examples: >>> grid = [ >>> [0, 1, 0, 0], >>> [0, 0, 1, 0], >>> [0, 0, 0, 0], >>> [1, 0, 0, 0] >>> ] >>> start = (0, 0) >>> target = (2, 2) >>> is_visible(grid, start, target) False pass def test_visibility_clear_path(): grid = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] start = (0, 0) target = (2, 2) assert is_visible(grid, start, target) == True def test_visibility_blocked_path(): grid = [ [0, 1, 0], [0, 1, 0], [0, 0, 0] ] start = (0, 0) target = (2, 2) assert is_visible(grid, start, target) == False def test_visibility_same_point(): grid = [ [0, 1, 0], [0, 0, 1], [1, 0, 0] ] start = (1, 1) target = (1, 1) assert is_visible(grid, start, target) == True def test_visibility_no_obstacles(): grid = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ] start = (0, 0) target = (3, 3) assert is_visible(grid, start, target) == True def test_visibility_obstacle_in_path(): grid = [ [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0] ] start = (0, 0) target = (3, 3) assert is_visible(grid, start, target) == False def test_visibility_alternate_path(): grid = [ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0] ] start = (0, 3) target = (3, 0) assert is_visible(grid, start, target) == False

answer:def is_visible(grid, start, target): Determine if the target point is visible from the start point on the given grid. Parameters: grid (list of list of int): 2D grid representing the environment with obstacles (1) and empty spaces (0). start (tuple of int): Starting point in the grid. target (tuple of int): Target point in the grid. Returns: bool: True if the target is visible from the start, False otherwise. def bresenham_line(x1, y1, x2, y2): points = [] dx = abs(x2 - x1) dy = abs(y2 - y1) sx = 1 if x1 < x2 else -1 sy = 1 if y1 < y2 else -1 err = dx - dy while True: points.append((x1, y1)) if x1 == x2 and y1 == y2: break e2 = 2 * err if e2 > -dy: err -= dy x1 += sx if e2 < dx: err += dx y1 += sy return points points_on_line = bresenham_line(start[0], start[1], target[0], target[1]) for (x, y) in points_on_line: if (x, y) != start and (x, y) != target and grid[y][x] == 1: return False return True

question:from typing import List, Tuple def treasure_hunt(moves: List[str], treasure: Tuple[int, int]) -> Tuple[int, int, bool]: Implement a simple text-based adventure game to find a treasure in a 5x5 grid. The player starts at the top-left corner (0,0) and can move 'up', 'down', 'left', or 'right'. The function returns the final coordinates of the player and a boolean indicating whether the player found the treasure. >>> treasure_hunt(["down", "down", "right", "right"], (2, 2)) (2, 2, True) >>> treasure_hunt(["right", "right", "up"], (2, 2)) (0, 2, False)

answer:from typing import List, Tuple def treasure_hunt(moves: List[str], treasure: Tuple[int, int]) -> Tuple[int, int, bool]: player_position = [0, 0] grid_size = 5 move_directions = { "up": (-1, 0), "down": (1, 0), "left": (0, -1), "right": (0, 1) } for move in moves: if move in move_directions: next_position = [ player_position[0] + move_directions[move][0], player_position[1] + move_directions[move][1] ] if 0 <= next_position[0] < grid_size and 0 <= next_position[1] < grid_size: player_position = next_position if tuple(player_position) == treasure: return (player_position[0], player_position[1], True) return (player_position[0], player_position[1], False)

question:def can_be_split_into_unique_substrings(s: str) -> str: Determines if a string can be split into non-empty unique substrings. >>> can_be_split_into_unique_substrings("abcabc") "Да" >>> can_be_split_into_unique_substrings("aaaa") "Нет"

answer:def can_be_split_into_unique_substrings(s): Determines if a string can be split into non-empty unique substrings. :param s: Input string consisting of lowercase Latin letters :return: 'Да' if it's possible to split the string so that each substring is unique, otherwise 'Нет' if len(set(s)) > 1: return "Да" else: return "Нет"

question:from typing import List, Tuple def minimize_travel_cost(N: int, M: int, roads: List[Tuple[int, int, int]]) -> str: Determine a possible configuration of high-speed routes such that the total travel cost between any two cities is minimized. Parameters: - N (int): The number of cities. - M (int): The number of roads. - roads (List[Tuple[int, int, int]]): A list of tuples where each tuple contains three integers u, v, and w, describing a road between cities u and v with a cost of w. Returns: - str: The configuration of high-speed routes in the required format. # Your code here def test_with_example_input(): N = 5 M = 6 roads = [ (1, 2, 10), (1, 3, 20), (2, 3, 5), (2, 4, 15), (3, 5, 25), (4, 5, 10) ] expected = "2n1 4n2 5" assert minimize_travel_cost(N, M, roads) == expected def test_no_roads(): N = 2 M = 0 roads = [] expected = "0" assert minimize_travel_cost(N, M, roads) == expected def test_single_road(): N = 2 M = 1 roads = [ (1, 2, 1) ] expected = "1n1 2" assert minimize_travel_cost(N, M, roads) == expected def test_with_more_cities_and_roads(): N = 4 M = 5 roads = [ (1, 2, 1), (1, 3, 2), (2, 3, 2), (2, 4, 3), (3, 4, 4) ] expected = "3n1 2n1 3n2 4" assert minimize_travel_cost(N, M, roads) == expected

answer:def find_min_high_speed_routes(num_cities, num_roads, roads): from heapq import heappop, heappush from collections import defaultdict def prims_mst(start, adj): mst_edges = [] total_cost = 0 visited = [False] * (num_cities + 1) min_heap = [(0, start, None)] while min_heap: cost, u, parent = heappop(min_heap) if visited[u]: continue visited[u] = True if parent is not None: mst_edges.append((parent, u)) total_cost += cost for v, weight in adj[u]: if not visited[v]: heappush(min_heap, (weight, v, u)) return mst_edges adj_list = defaultdict(list) for u, v, w in roads: adj_list[u].append((v, w)) adj_list[v].append((u, w)) high_speed_routes = prims_mst(1, adj_list) return high_speed_routes def format_output(high_speed_routes): output = [str(len(high_speed_routes))] for u, v in high_speed_routes: output.append(f"{u} {v}") return "n".join(output) def minimize_travel_cost(N, M, roads): high_speed_routes = find_min_high_speed_routes(N, M, roads) result = format_output(high_speed_routes) return result

Released under the chat License.

has loaded