Appearance
question:def is_point_in_triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, xp: int, yp: int) -> str: Determine if a point lies inside, outside, or on the border of a given triangle. Parameters: x1, y1, x2, y2, x3, y3, xp, yp : int The coordinates of the vertices of the triangle (x1, y1), (x2, y2), (x3, y3) and the point (xp, yp). Returns: str "Inside" if the point lies inside the triangle, "Outside" if the point lies outside the triangle, and "Border" if the point lies exactly on the border of the triangle. Examples: >>> is_point_in_triangle(0, 0, 5, 0, 0, 5, 1, 1) 'Inside' >>> is_point_in_triangle(0, 0, 5, 0, 0, 5, 5, 1) 'Outside' >>> is_point_in_triangle(0, 0, 5, 0, 0, 5, 2.5, 0) 'Border'
answer:def orientation(p, q, r): Return the orientation of the triplet (p, q, r). 0 -> p, q and r are collinear 1 -> Clockwise 2 -> Counterclockwise val = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]) if val == 0: return 0 elif val > 0: return 1 else: return 2 def on_segment(p, q, r): Check if point q lies on segment pr if min(p[0], r[0]) <= q[0] <= max(p[0], r[0]) and min(p[1], r[1]) <= q[1] <= max(p[1], r[1]): return True return False def is_point_in_triangle(x1, y1, x2, y2, x3, y3, xp, yp): p1, p2, p3, p = (x1, y1), (x2, y2), (x3, y3), (xp, yp) # Check if the point lies on any of the triangle's edges if orientation(p1, p2, p) == 0 and on_segment(p1, p, p2): return "Border" if orientation(p2, p3, p) == 0 and on_segment(p2, p, p3): return "Border" if orientation(p3, p1, p) == 0 and on_segment(p3, p, p1): return "Border" # Calculate barycentric coordinates denominator = ((y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3)) a = ((y2 - y3) * (xp - x3) + (x3 - x2) * (yp - y3)) / denominator b = ((y3 - y1) * (xp - x3) + (x1 - x3) * (yp - y3)) / denominator c = 1 - a - b if 0 < a < 1 and 0 < b < 1 and 0 < c < 1: return "Inside" else: return "Outside"
question:import heapq from typing import List, Tuple def merge_sorted_arrays(arrays: List[List[int]]) -> List[int]: Merges multiple sorted arrays into a single sorted array. >>> merge_sorted_arrays([[1, 4, 7, 10, 12], [2, 8, 14], [3, 9, 16, 20]]) [1, 2, 3, 4, 7, 8, 9, 10, 12, 14, 16, 20] >>> merge_sorted_arrays([[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2]]) [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]
answer:import heapq from typing import List, Tuple def merge_sorted_arrays(arrays: List[List[int]]) -> List[int]: Merges multiple sorted arrays into a single sorted array. merged_array = [] heap = [] # Initialize the heap with the first element of each array, along with the array index and element index for i, array in enumerate(arrays): if array: heapq.heappush(heap, (array[0], i, 0)) # Extract the smallest elements one by one and add the next element from the same array to the heap while heap: val, array_idx, element_idx = heapq.heappop(heap) merged_array.append(val) next_element_idx = element_idx + 1 if next_element_idx < len(arrays[array_idx]): heapq.heappush(heap, (arrays[array_idx][next_element_idx], array_idx, next_element_idx)) return merged_array
question:def reverse_vowels(s: str) -> str: Takes a string as input and reverses the vowels in the string, while keeping all other characters in their original positions. >>> reverse_vowels("hello") "holle" >>> reverse_vowels("aeiou") "uoiea" >>> reverse_vowels("bcdfg") "bcdfg" >>> reverse_vowels("Leetcode") "Leotcede" >>> reverse_vowels("a") "a" >>> reverse_vowels("b") "b" >>> reverse_vowels("AaEeIiOoUu") "uUoOiIeEaA" >>> reverse_vowels("") ""
answer:def reverse_vowels(s): Returns the string with vowels reversed in their positions. :param s: Input string :type s: str :return: Modified string with reversed vowels :rtype: str vowels = "aeiouAEIOU" s = list(s) i, j = 0, len(s) - 1 while i < j: if s[i] in vowels and s[j] in vowels: s[i], s[j] = s[j], s[i] i += 1 j -= 1 if s[i] not in vowels: i += 1 if s[j] not in vowels: j -= 1 return ''.join(s)
question:from typing import List def unique_paths_with_obstacles(grid: List[List[int]]) -> int: Find the number of different paths from the top-left cell to the bottom-right cell in a grid with obstacles. >>> unique_paths_with_obstacles([ >>> [0, 0, 0], >>> [0, 1, 0], >>> [0, 0, 0] >>> ]) == 2 >>> unique_paths_with_obstacles([ >>> [0, 1], >>> [0, 0] >>> ]) == 1
answer:def unique_paths_with_obstacles(grid): if not grid or not grid[0]: return 0 n, m = len(grid), len(grid[0]) if grid[0][0] == 1 or grid[n-1][m-1] == 1: return 0 dp = [[0] * m for _ in range(n)] dp[0][0] = 1 for i in range(n): for j in range(m): if grid[i][j] == 1: dp[i][j] = 0 else: if i > 0: dp[i][j] += dp[i-1][j] if j > 0: dp[i][j] += dp[i][j-1] return dp[-1][-1]