Appearance
question:class Spreadsheet: def __init__(self): self.cells = {} def set_cell(self, cell_name: str, content: str): Set the content (value or formula) of a given cell in the spreadsheet. Args: cell_name (str): The name of the cell (e.g., "A1"). content (str): The content of the cell (e.g., "5" or "A1 + 3"). def get_value(self, cell_name: str): Get the evaluated value of the given cell. Args: cell_name (str): The name of the cell (e.g., "A1"). Returns: float: The evaluated value of the cell. Raises: ValueError: If there's an error in evaluating the cell's formula. >>> sheet = Spreadsheet() >>> sheet.set_cell("A1", "5") >>> sheet.set_cell("B1", "A1 + 3") >>> sheet.get_value("B1") 8.0 >>> sheet.set_cell("A1", "5") >>> sheet.set_cell("B1", "A1 * 2") >>> sheet.get_value("B1") 10.0
answer:class Spreadsheet: def __init__(self): self.cells = {} def set_cell(self, cell_name: str, content: str): self.cells[cell_name] = content def get_value(self, cell_name: str): content = self.cells.get(cell_name, "") if content.isdigit() or (content.replace('.', '', 1).isdigit() if '.' in content else False): return float(content) try: # Replace cell references with their actual values in the formula formula = content for ref in self.cells: if ref in formula: formula = formula.replace(ref, str(self.get_value(ref))) result = eval(formula) return float(result) except Exception as e: raise ValueError(f"Error evaluating cell '{cell_name}': {e}")
question:class CustomLinkedList: class Node: def __init__(self, value=0, next=None): self.value = value self.next = next def __init__(self): Initializes an empty linked list. pass def add_at_head(self, value: int) -> None: Adds a node with the given value at the head of the list. >>> linked_list = CustomLinkedList() >>> linked_list.add_at_head(10) >>> linked_list.head.value == 10 True pass def add_at_tail(self, value: int) -> None: Adds a node with the given value at the tail of the list. >>> linked_list = CustomLinkedList() >>> linked_list.add_at_tail(15) >>> linked_list.head.value == 15 True pass def delete_value(self, value: int) -> bool: Deletes the first node with the given value in the list. Returns True if a node was deleted, False otherwise. >>> linked_list = CustomLinkedList() >>> linked_list.add_at_head(3) >>> linked_list.add_at_head(4) >>> linked_list.add_at_tail(3) >>> linked_list.delete_value(4) True pass def find_middle(self) -> int: Returns the value of the middle node in the linked list. If the list is empty, return -1. If there are two middle nodes, return the second one. >>> linked_list = CustomLinkedList() >>> linked_list.find_middle() -1 >>> linked_list.add_at_head(1) >>> linked_list.find_middle() 1 pass # Test cases def test_add_at_head(): linked_list = CustomLinkedList() linked_list.add_at_head(10) assert linked_list.head.value == 10 linked_list.add_at_head(5) assert linked_list.head.value == 5 def test_add_at_tail(): linked_list = CustomLinkedList() linked_list.add_at_tail(15) assert linked_list.head.value == 15 linked_list.add_at_tail(20) assert linked_list.head.next.value == 20 def test_delete_value(): linked_list = CustomLinkedList() linked_list.add_at_head(3) linked_list.add_at_head(4) linked_list.add_at_tail(3) assert linked_list.delete_value(4) is True assert linked_list.head.value == 3 assert linked_list.delete_value(10) is False def test_find_middle(): linked_list = CustomLinkedList() assert linked_list.find_middle() == -1 linked_list.add_at_head(1) assert linked_list.find_middle() == 1 linked_list.add_at_tail(2) assert linked_list.find_middle() == 2 linked_list.add_at_tail(3) linked_list.add_at_tail(4) assert linked_list.find_middle() == 3 linked_list.delete_value(2) assert linked_list.find_middle() == 3
answer:class CustomLinkedList: class Node: def __init__(self, value=0, next=None): self.value = value self.next = next def __init__(self): self.head = None self.size = 0 def add_at_head(self, value: int) -> None: new_node = self.Node(value, self.head) self.head = new_node self.size += 1 def add_at_tail(self, value: int) -> None: new_node = self.Node(value) if not self.head: self.head = new_node else: current = self.head while current.next: current = current.next current.next = new_node self.size += 1 def delete_value(self, value: int) -> bool: current = self.head previous = None while current: if current.value == value: if previous: previous.next = current.next else: self.head = current.next self.size -= 1 return True previous = current current = current.next return False def find_middle(self) -> int: if not self.head: return -1 slow = self.head fast = self.head while fast and fast.next: slow = slow.next fast = fast.next.next return slow.value
question:from collections import deque def shortest_path(grid: list[list[int]], start: tuple[int, int], end: tuple[int, int]) -> int: Find the shortest path from start to end in a 2D grid using BFS. If no path exists, return -1. >>> grid1 = [ ... [0, 0, 1, 0, 0], ... [0, 0, 0, 0, 1], ... [1, 0, 1, 0, 1], ... [0, 0, 1, 0, 0], ... [0, 1, 0, 0, 0]] >>> start1 = (0, 0) >>> end1 = (4, 4) >>> shortest_path(grid1, start1, end1) 8 >>> grid2 = [ ... [0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], ... [0, 1, 0, 1, 0], ... [0, 0, 0, 1, 0], ... [1, 1, 1, 0, 0]] >>> start2 = (0, 0) >>> end2 = (3, 4) >>> shortest_path(grid2, start2, end2) 7 >>> grid3 = [ ... [0, 1, 1], ... [0, 1, 0], ... [0, 0, 0]] >>> start3 = (0, 0) >>> end3 = (2, 2) >>> shortest_path(grid3, start3, end3) 4 >>> grid4 = [ ... [0, 0, 0], ... [0, 0, 0], ... [0, 0, 0]] >>> start4 = (0, 0) >>> end4 = (2, 2) >>> shortest_path(grid4, start4, end4) 4 >>> grid5 = [ ... [0, 1, 0], ... [1, 1, 0], ... [0, 0, 0]] >>> start5 = (0, 0) >>> end5 = (2, 2) >>> shortest_path(grid5, start5, end5) -1
answer:from collections import deque def shortest_path(grid, start, end): Find the shortest path from start to end in a 2D grid using BFS. If no path exists, return -1. rows, cols = len(grid), len(grid[0]) directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right queue = deque([(start[0], start[1], 0)]) # (row, col, distance) visited = set() visited.add(start) while queue: r, c, dist = queue.popleft() if (r, c) == end: return dist for dr, dc in directions: rr, cc = r + dr, c + dc if 0 <= rr < rows and 0 <= cc < cols and grid[rr][cc] == 0 and (rr, cc) not in visited: queue.append((rr, cc, dist + 1)) visited.add((rr, cc)) return -1
question:from typing import List def sum_excluding_elements(arr: List[int], exclude: List[int]) -> int: Computes the sum of all elements in the input array, excluding the specified elements. Parameters: - arr (List[int]): The main list containing the elements to sum. - exclude (List[int]): The list of elements that should not be included in the sum. Returns: int: The sum of elements in `arr`, excluding those present in `exclude`. >>> sum_excluding_elements([2, 1, 4, 5, 2, 4], [2, 4]) 6 >>> sum_excluding_elements([0, 3, 1, 0, 1], [0, 1]) 3 >>> sum_excluding_elements([5, 8, 13, 3], []) 29 >>> sum_excluding_elements([7, 5, 7, 10], [7]) 15
answer:from typing import List def sum_excluding_elements(arr: List[int], exclude: List[int]) -> int: Returns the sum of all elements in `arr` excluding those present in `exclude`. Parameters: - arr (List[int]): The list of elements to sum. - exclude (List[int]): The list of elements to exclude from the sum. Returns: int: The sum of elements in `arr` excluding those in `exclude`. exclude_set = set(exclude) # Convert exclude list to set for O(1) lookups return sum(x for x in arr if x not in exclude_set)